fix(sqlserver): support TLS fallback, TOP syntax, and schema browsing

This commit is contained in:
t8y2 2026-04-30 14:36:31 +08:00
parent 5490ed007e
commit 62ae308914
3 changed files with 25 additions and 6 deletions

View File

@ -8,6 +8,13 @@ use super::{ColumnInfo, DatabaseInfo, ForeignKeyInfo, IndexInfo, QueryResult, Ta
pub type SqlServerClient = Client<Compat<TcpStream>>;
pub async fn connect(host: &str, port: u16, user: &str, pass: &str, database: Option<&str>) -> Result<SqlServerClient, String> {
match try_connect(host, port, user, pass, database, true).await {
Ok(client) => Ok(client),
Err(_) => try_connect(host, port, user, pass, database, false).await,
}
}
async fn try_connect(host: &str, port: u16, user: &str, pass: &str, database: Option<&str>, use_encryption: bool) -> Result<SqlServerClient, String> {
let mut config = Config::new();
config.host(host);
config.port(port);
@ -16,6 +23,9 @@ pub async fn connect(host: &str, port: u16, user: &str, pass: &str, database: Op
config.database(db);
}
config.trust_cert();
if !use_encryption {
config.encryption(tiberius::EncryptionLevel::NotSupported);
}
let tcp = TcpStream::connect(config.get_addr())
.await

View File

@ -319,7 +319,7 @@ function quoteIdent(tab: ActiveTab, name: string): string {
function qualifiedTableName(tab: NonNullable<typeof activeTab.value>): string {
const config = connectionStore.getConfig(tab.connectionId);
if (!tab.tableMeta) return "";
if ((config?.db_type === "postgres" || config?.db_type === "oracle") && tab.tableMeta.schema) {
if ((config?.db_type === "postgres" || config?.db_type === "oracle" || config?.db_type === "sqlserver") && tab.tableMeta.schema) {
return `${quoteIdent(tab, tab.tableMeta.schema)}.${quoteIdent(tab, tab.tableMeta.tableName)}`;
}
return quoteIdent(tab, tab.tableMeta.tableName);
@ -345,6 +345,10 @@ function buildTableSql(
return `SELECT * FROM ${qualifiedTableName(tab)}${order}${offset} FETCH FIRST ${limit} ROWS ONLY`;
}
if (config?.db_type === "sqlserver") {
return `SELECT TOP ${limit} * FROM ${qualifiedTableName(tab)}${order}`;
}
const offset = options.offset ? ` OFFSET ${options.offset}` : "";
return `SELECT * FROM ${qualifiedTableName(tab)}${order} LIMIT ${limit}${offset};`;
}

View File

@ -107,7 +107,7 @@ async function toggle() {
queryStore.updateSql(tab, node.label);
} else if (node.type === "database" && node.connectionId && node.database) {
const config = connectionStore.getConfig(node.connectionId);
if (config?.db_type === "postgres") {
if (config?.db_type === "postgres" || config?.db_type === "sqlserver") {
await connectionStore.loadSchemas(node.connectionId, node.database);
} else {
await connectionStore.loadTables(node.connectionId, node.database);
@ -144,7 +144,7 @@ async function openData() {
if (!(node.type === "table" || node.type === "view") || !node.connectionId || !node.database) return;
await connectionStore.ensureConnected(node.connectionId);
const config = connectionStore.getConfig(node.connectionId);
const qualifiedName = (config?.db_type === "postgres" || config?.db_type === "oracle") && node.schema
const qualifiedName = (config?.db_type === "postgres" || config?.db_type === "oracle" || config?.db_type === "sqlserver") && node.schema
? `${quoteIdent(node.schema)}.${quoteIdent(node.label)}`
: quoteIdent(node.label);
const tabId = queryStore.createTab(node.connectionId, node.database, node.label, "data");
@ -153,9 +153,14 @@ async function openData() {
const columns = await api.getColumns(node.connectionId, node.database, querySchema, node.label);
const pks = columns.filter((c) => c.is_primary_key).map((c) => c.name);
const order = pks.length ? ` ORDER BY ${pks.map((pk) => `${quoteIdent(pk)} ASC`).join(", ")}` : "";
const sql = config?.db_type === "oracle"
? `SELECT * FROM ${qualifiedName}${order} FETCH FIRST 100 ROWS ONLY`
: `SELECT * FROM ${qualifiedName}${order} LIMIT 100;`;
let sql: string;
if (config?.db_type === "oracle") {
sql = `SELECT * FROM ${qualifiedName}${order} FETCH FIRST 100 ROWS ONLY`;
} else if (config?.db_type === "sqlserver") {
sql = `SELECT TOP 100 * FROM ${qualifiedName}${order}`;
} else {
sql = `SELECT * FROM ${qualifiedName}${order} LIMIT 100;`;
}
queryStore.updateSql(tabId, sql);
queryStore.setTableMeta(tabId, {
schema: node.schema,