diff --git a/packages/api/src/controllers/databaseConnections.js b/packages/api/src/controllers/databaseConnections.js index 5b8c5b1f..02576a5a 100644 --- a/packages/api/src/controllers/databaseConnections.js +++ b/packages/api/src/controllers/databaseConnections.js @@ -17,6 +17,13 @@ module.exports = { existing.structure = structure; socket.emitChanged(`database-structure-changed-${conid}-${database}`); }, + handle_version(conid, database, { version }) { + const existing = this.opened.find(x => x.conid == conid && x.database == database); + if (!existing) return; + existing.serverVersion = version; + socket.emitChanged(`database-server-version-changed-${conid}-${database}`); + }, + handle_error(conid, database, props) { const { error } = props; console.log(`Error in database connection ${conid}, database ${database}: ${error}`); @@ -51,6 +58,7 @@ module.exports = { database, subprocess, structure: lastClosed ? lastClosed.structure : DatabaseAnalyser.createEmptyStructure(), + serverVersion: lastClosed ? lastClosed.serverVersion : null, connection, status: { name: 'pending' }, }; @@ -175,6 +183,12 @@ module.exports = { // }; }, + serverVersion_meta: 'get', + async serverVersion({ conid, database }) { + const opened = await this.ensureOpened(conid, database); + return opened.serverVersion; + }, + sqlPreview_meta: 'post', async sqlPreview({ conid, database, objects, options }) { // wait for structure diff --git a/packages/api/src/controllers/serverConnections.js b/packages/api/src/controllers/serverConnections.js index 527992b8..288ff371 100644 --- a/packages/api/src/controllers/serverConnections.js +++ b/packages/api/src/controllers/serverConnections.js @@ -17,6 +17,12 @@ module.exports = { existing.databases = databases; socket.emitChanged(`database-list-changed-${conid}`); }, + handle_version(conid, { version }) { + const existing = this.opened.find(x => x.conid == conid); + if (!existing) return; + existing.version = version; + socket.emitChanged(`server-version-changed-${conid}`); + }, handle_status(conid, { status }) { const existing = this.opened.find(x => x.conid == conid); if (!existing) return; @@ -85,6 +91,12 @@ module.exports = { return opened.databases; }, + version_meta: 'get', + async version({ conid }) { + const opened = await this.ensureOpened(conid); + return opened.version; + }, + serverStatus_meta: 'get', async serverStatus() { return { diff --git a/packages/api/src/proc/databaseConnectionProcess.js b/packages/api/src/proc/databaseConnectionProcess.js index 77f1a815..6a072467 100644 --- a/packages/api/src/proc/databaseConnectionProcess.js +++ b/packages/api/src/proc/databaseConnectionProcess.js @@ -56,6 +56,12 @@ function setStatusName(name) { setStatus({ name }); } +async function readVersion() { + const driver = requireEngineDriver(storedConnection); + const version = await driver.getVersion(systemConnection); + process.send({ msgtype: 'version', version }); +} + async function handleConnect({ connection, structure }) { storedConnection = connection; lastPing = new Date().getTime(); @@ -63,6 +69,7 @@ async function handleConnect({ connection, structure }) { if (!structure) setStatusName('pending'); const driver = requireEngineDriver(storedConnection); systemConnection = await checkedAsyncCall(connectUtility(driver, storedConnection)); + readVersion(); if (structure) { analysedStructure = structure; handleIncrementalRefresh(); diff --git a/packages/api/src/proc/serverConnectionProcess.js b/packages/api/src/proc/serverConnectionProcess.js index 0ff79d1a..4adbe3fa 100644 --- a/packages/api/src/proc/serverConnectionProcess.js +++ b/packages/api/src/proc/serverConnectionProcess.js @@ -31,6 +31,12 @@ async function handleRefresh() { } } +async function readVersion() { + const driver = requireEngineDriver(storedConnection); + const version = await driver.getVersion(systemConnection); + process.send({ msgtype: 'version', version }); +} + function setStatus(status) { const statusString = stableStringify(status); if (lastStatus != statusString) { @@ -51,6 +57,7 @@ async function handleConnect(connection) { const driver = requireEngineDriver(storedConnection); try { systemConnection = await connectUtility(driver, storedConnection); + readVersion(); handleRefresh(); setInterval(handleRefresh, 30 * 1000); } catch (err) { diff --git a/packages/types/index.d.ts b/packages/types/index.d.ts index 6134439f..d1898b83 100644 --- a/packages/types/index.d.ts +++ b/packages/types/index.d.ts @@ -4,6 +4,7 @@ export interface OpenedDatabaseConnection { conid: string; database: string; structure: DatabaseInfo; + serverVersion?: any; subprocess: ChildProcess; disconnected?: boolean; status?: { diff --git a/packages/web/src/datagrid/TableDataGrid.svelte b/packages/web/src/datagrid/TableDataGrid.svelte index 5d9b6238..6025b5f9 100644 --- a/packages/web/src/datagrid/TableDataGrid.svelte +++ b/packages/web/src/datagrid/TableDataGrid.svelte @@ -14,7 +14,12 @@ import { extensions } from '../stores'; import stableStringify from 'json-stable-stringify'; - import { useConnectionInfo, useDatabaseInfo } from '../utility/metadataLoaders'; + import { + useConnectionInfo, + useDatabaseInfo, + useDatabaseServerVersion, + useServerVersion, + } from '../utility/metadataLoaders'; import DataGrid from './DataGrid.svelte'; import ReferenceHeader from './ReferenceHeader.svelte'; @@ -38,6 +43,9 @@ $: connection = useConnectionInfo({ conid }); $: dbinfo = useDatabaseInfo({ conid, database }); + // $: serverVersion = useDatabaseServerVersion({ conid, database }); + + // $: console.log('serverVersion', $serverVersion); let myLoadedTime = 0; diff --git a/packages/web/src/utility/metadataLoaders.ts b/packages/web/src/utility/metadataLoaders.ts index 0ed32ae7..1a642ce4 100644 --- a/packages/web/src/utility/metadataLoaders.ts +++ b/packages/web/src/utility/metadataLoaders.ts @@ -76,6 +76,18 @@ const databaseListLoader = ({ conid }) => ({ reloadTrigger: `database-list-changed-${conid}`, }); +const serverVersionLoader = ({ conid }) => ({ + url: 'server-connections/version', + params: { conid }, + reloadTrigger: `server-version-changed-${conid}`, +}); + +const databaseServerVersionLoader = ({ conid, database }) => ({ + url: 'database-connections/server-version', + params: { conid, database }, + reloadTrigger: `database-server-version-changed-${conid}-${database}`, +}); + const archiveFoldersLoader = () => ({ url: 'archive/folders', params: {}, @@ -318,6 +330,21 @@ export function useDatabaseList(args) { return useCore(databaseListLoader, args); } +export function getServerVersion(args) { + return getCore(serverVersionLoader, args); +} +export function useServerVersion(args) { + return useCore(serverVersionLoader, args); +} + +export function getDatabaseServerVersion(args) { + return getCore(databaseServerVersionLoader, args); +} +export function useDatabaseServerVersion(args) { + return useCore(databaseServerVersionLoader, args); +} + + export function getServerStatus() { return getCore(serverStatusLoader, {}); } diff --git a/plugins/dbgate-plugin-mssql/src/backend/driver.js b/plugins/dbgate-plugin-mssql/src/backend/driver.js index 8be12fe7..2b615142 100644 --- a/plugins/dbgate-plugin-mssql/src/backend/driver.js +++ b/plugins/dbgate-plugin-mssql/src/backend/driver.js @@ -80,7 +80,17 @@ const driver = { }, async getVersion(pool) { const { version } = (await this.query(pool, 'SELECT @@VERSION AS version')).rows[0]; - return { version }; + + const { productVersion } = ( + await this.query(pool, "SELECT SERVERPROPERTY ('productversion') as productVersion") + ).rows[0]; + let productVersionNumber = 0; + if (productVersion) { + const splitted = productVersion.split('.'); + const number = parseInt(splitted[0]) || 0; + productVersionNumber = number; + } + return { version, productVersion, productVersionNumber }; }, async listDatabases(pool) { const { rows } = await this.query(pool, 'SELECT name FROM sys.databases order by name');