diff --git a/src-tauri/src/db/sqlserver.rs b/src-tauri/src/db/sqlserver.rs index 6f11841eb..501fc7ff1 100644 --- a/src-tauri/src/db/sqlserver.rs +++ b/src-tauri/src/db/sqlserver.rs @@ -8,6 +8,13 @@ use super::{ColumnInfo, DatabaseInfo, ForeignKeyInfo, IndexInfo, QueryResult, Ta pub type SqlServerClient = Client>; pub async fn connect(host: &str, port: u16, user: &str, pass: &str, database: Option<&str>) -> Result { + 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 { 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 diff --git a/src/App.vue b/src/App.vue index 3eac20f1f..2f7cf8e95 100644 --- a/src/App.vue +++ b/src/App.vue @@ -319,7 +319,7 @@ function quoteIdent(tab: ActiveTab, name: string): string { function qualifiedTableName(tab: NonNullable): 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};`; } diff --git a/src/components/sidebar/TreeItem.vue b/src/components/sidebar/TreeItem.vue index 26771efc1..1c3224b19 100644 --- a/src/components/sidebar/TreeItem.vue +++ b/src/components/sidebar/TreeItem.vue @@ -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,