From 0b5846644641eef334b7a30a766dd14de89ff6f7 Mon Sep 17 00:00:00 2001 From: Faizan Hemotra <77664620+fzhem@users.noreply.github.com> Date: Thu, 28 Nov 2024 10:20:33 +0000 Subject: [PATCH] mssql QoL changes and a major bug fix (#1399) * Make database optional * Use database specific information_schema This fixes an issue with sidebar where users can't see database tables/views * Return empty list when schema isn't accessible This resolves 'sqltools.getChildrenForTreeItem' failed error when clicking on an inaccessible schema. --- packages/driver.mssql/connection.schema.json | 2 -- packages/driver.mssql/src/ls/driver.ts | 10 ++++++++- packages/driver.mssql/src/ls/queries.ts | 22 ++++++++++---------- 3 files changed, 20 insertions(+), 14 deletions(-) diff --git a/packages/driver.mssql/connection.schema.json b/packages/driver.mssql/connection.schema.json index b1c82495..f3dd34f5 100644 --- a/packages/driver.mssql/connection.schema.json +++ b/packages/driver.mssql/connection.schema.json @@ -121,7 +121,6 @@ "required": [ "server", "port", - "database", "username" ] }, @@ -149,7 +148,6 @@ }, "required": [ "socketPath", - "database", "username" ] }, diff --git a/packages/driver.mssql/src/ls/driver.ts b/packages/driver.mssql/src/ls/driver.ts index 29d07ee0..16b233b2 100644 --- a/packages/driver.mssql/src/ls/driver.ts +++ b/packages/driver.mssql/src/ls/driver.ts @@ -157,7 +157,15 @@ export default class MSSQL extends AbstractDriver private async getChildrenForGroup({ parent, item }: Arg0) { switch (item.childType) { case ContextValue.SCHEMA: - return this.queryResults(this.queries.fetchSchemas(parent as NSDatabase.IDatabase)); + try { + const result = await this.queryResults( + this.queries.fetchSchemas(parent as NSDatabase.IDatabase) + ); + return result; + } catch (error) { + this.close(); + return []; + } case ContextValue.TABLE: return this.queryResults(this.queries.fetchTables(parent as NSDatabase.ISchema)); case ContextValue.VIEW: diff --git a/packages/driver.mssql/src/ls/queries.ts b/packages/driver.mssql/src/ls/queries.ts index 1013d082..626b1477 100644 --- a/packages/driver.mssql/src/ls/queries.ts +++ b/packages/driver.mssql/src/ls/queries.ts @@ -34,20 +34,20 @@ SELECT (CASE WHEN LOWER(TC.CONSTRAINT_TYPE) = 'primary key' THEN 1 ELSE 0 END) as "isPk", (CASE WHEN LOWER(TC.CONSTRAINT_TYPE) = 'foreign key' THEN 1 ELSE 0 END) as "isFk" FROM - INFORMATION_SCHEMA.COLUMNS C - LEFT JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS KCU ON ( + ${p => p.database ? `${escapeTableName({ database: p.database, schema: "INFORMATION_SCHEMA", label: "COLUMNS" })}` : 'INFORMATION_SCHEMA.COLUMNS'} C + LEFT JOIN ${p => p.database ? `${escapeTableName({ database: p.database, schema: "INFORMATION_SCHEMA", label: "KEY_COLUMN_USAGE" })}` : 'INFORMATION_SCHEMA.KEY_COLUMN_USAGE'} AS KCU ON ( C.TABLE_CATALOG = KCU.TABLE_CATALOG AND C.TABLE_NAME = KCU.TABLE_NAME AND C.TABLE_SCHEMA = KCU.TABLE_SCHEMA AND C.TABLE_CATALOG = KCU.TABLE_CATALOG AND C.COLUMN_NAME = KCU.COLUMN_NAME ) - LEFT JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS TC ON ( + LEFT JOIN ${p => p.database ? `${escapeTableName({ database: p.database, schema: "INFORMATION_SCHEMA", label: "TABLE_CONSTRAINTS" })}` : 'INFORMATION_SCHEMA.TABLE_CONSTRAINTS'} AS TC ON ( TC.CONSTRAINT_NAME = KCU.CONSTRAINT_NAME AND TC.TABLE_SCHEMA = KCU.TABLE_SCHEMA AND TC.TABLE_CATALOG = KCU.TABLE_CATALOG ) - JOIN INFORMATION_SCHEMA.TABLES AS T ON C.TABLE_NAME = T.TABLE_NAME + JOIN ${p => p.database ? `${escapeTableName({ database: p.database, schema: "INFORMATION_SCHEMA", label: "TABLES" })}` : 'INFORMATION_SCHEMA.TABLES'} AS T ON C.TABLE_NAME = T.TABLE_NAME AND C.TABLE_SCHEMA = T.TABLE_SCHEMA AND C.TABLE_CATALOG = T.TABLE_CATALOG WHERE @@ -79,7 +79,7 @@ SELECT T.TABLE_SCHEMA AS "schema", T.TABLE_CATALOG AS "database", CONVERT(BIT, CASE WHEN T.TABLE_TYPE = 'BASE TABLE' THEN 0 ELSE 1 END) AS "isView" -FROM INFORMATION_SCHEMA.TABLES AS T +FROM ${p => p.database ? `${escapeTableName({ database: p.database, schema: "INFORMATION_SCHEMA", label: "TABLES" })}` : 'INFORMATION_SCHEMA.TABLES'} AS T WHERE T.TABLE_SCHEMA = '${p => p.schema}' AND T.TABLE_CATALOG = '${p => p.database}' @@ -98,7 +98,7 @@ SELECT '${ContextValue.SCHEMA}' as "type", 'group-by-ref-type' as "iconId", catalog_name as "database" -FROM information_schema.schemata +FROM ${p => p.database ? `${escapeTableName({ database: p.database, schema: "information_schema", label: "schemata" })}` : 'information_schema.schemata'} WHERE LOWER(schema_name) NOT IN ('information_schema', 'sys', 'guest') AND LOWER(schema_name) NOT LIKE 'db\\_%' ESCAPE '\\' @@ -123,7 +123,7 @@ SELECT (CASE WHEN T.TABLE_TYPE = 'BASE TABLE' THEN 0 ELSE 1 END) AS "isView", (CASE WHEN T.TABLE_TYPE = 'BASE TABLE' THEN 'table' ELSE 'view' END) AS description, ('[' + T.TABLE_CATALOG + '].[' + T.TABLE_SCHEMA + '].[' + T.TABLE_NAME + ']') as detail -FROM INFORMATION_SCHEMA.TABLES AS T +FROM ${p => p.database ? `${escapeTableName({ database: p.database, schema: "INFORMATION_SCHEMA", label: "TABLES" })}` : 'INFORMATION_SCHEMA.TABLES'} AS T WHERE LOWER(T.TABLE_SCHEMA) NOT IN ('information_schema', 'sys', 'guest') AND LOWER(T.TABLE_SCHEMA) NOT LIKE 'db\\_%' ESCAPE '\\' @@ -152,20 +152,20 @@ SELECT (CASE WHEN LOWER(TC.CONSTRAINT_TYPE) = 'primary key' THEN 1 ELSE 0 END) as "isPk", (CASE WHEN LOWER(TC.CONSTRAINT_TYPE) = 'foreign key' THEN 1 ELSE 0 END) as "isFk" FROM - INFORMATION_SCHEMA.COLUMNS C - LEFT JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS KCU ON ( + ${p => p.tables[0].database ? `${escapeTableName({ database: p.tables[0].database, schema: "INFORMATION_SCHEMA", label: "COLUMNS" })}` : 'INFORMATION_SCHEMA.COLUMNS'} C + LEFT JOIN ${p => p.tables[0].database ? `${escapeTableName({ database: p.tables[0].database, schema: "INFORMATION_SCHEMA", label: "KEY_COLUMN_USAGE" })}` : 'INFORMATION_SCHEMA.KEY_COLUMN_USAGE'} AS KCU ON ( C.TABLE_CATALOG = KCU.TABLE_CATALOG AND C.TABLE_NAME = KCU.TABLE_NAME AND C.TABLE_SCHEMA = KCU.TABLE_SCHEMA AND C.TABLE_CATALOG = KCU.TABLE_CATALOG AND C.COLUMN_NAME = KCU.COLUMN_NAME ) - LEFT JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS TC ON ( + LEFT JOIN ${p => p.tables[0].database ? `${escapeTableName({ database: p.tables[0].database, schema: "INFORMATION_SCHEMA", label: "TABLE_CONSTRAINTS" })}` : 'INFORMATION_SCHEMA.TABLE_CONSTRAINTS'} AS TC ON ( TC.CONSTRAINT_NAME = KCU.CONSTRAINT_NAME AND TC.TABLE_SCHEMA = KCU.TABLE_SCHEMA AND TC.TABLE_CATALOG = KCU.TABLE_CATALOG ) - JOIN INFORMATION_SCHEMA.TABLES AS T ON C.TABLE_NAME = T.TABLE_NAME + JOIN ${p => p.tables[0].database ? `${escapeTableName({ database: p.tables[0].database, schema: "INFORMATION_SCHEMA", label: "TABLES" })}` : 'INFORMATION_SCHEMA.TABLES'} AS T ON C.TABLE_NAME = T.TABLE_NAME AND C.TABLE_SCHEMA = T.TABLE_SCHEMA AND C.TABLE_CATALOG = T.TABLE_CATALOG WHERE LOWER(C.TABLE_SCHEMA) NOT IN ('information_schema', 'sys', 'guest')