diff --git a/packages/tools/src/DatabaseAnalyser.ts b/packages/tools/src/DatabaseAnalyser.ts index 0c60a782d..953356763 100644 --- a/packages/tools/src/DatabaseAnalyser.ts +++ b/packages/tools/src/DatabaseAnalyser.ts @@ -71,6 +71,17 @@ export class DatabaseAnalyser { // } } + getRequestedObjectPureNames(objectTypeField, allPureNames) { + if (this.singleObjectFilter) { + const { typeField, pureName } = this.singleObjectFilter; + if (typeField == objectTypeField) return [pureName]; + } + if (this.modifications) { + return this.modifications.filter(x => x.objectTypeField == objectTypeField).map(x => x.newName.pureName); + } + return allPureNames; + } + // findObjectById(id) { // return this.structure.tables.find((x) => x.objectId == id); // } diff --git a/packages/web/src/elements/ConstraintLabel.svelte b/packages/web/src/elements/ConstraintLabel.svelte index cedec2ce9..6de9617a4 100644 --- a/packages/web/src/elements/ConstraintLabel.svelte +++ b/packages/web/src/elements/ConstraintLabel.svelte @@ -13,5 +13,5 @@ - {constraintName} + {constraintName || '(without name)'} diff --git a/plugins/dbgate-plugin-mysql/src/backend/Analyser.js b/plugins/dbgate-plugin-mysql/src/backend/Analyser.js index 36159e076..7a8c99a6d 100644 --- a/plugins/dbgate-plugin-mysql/src/backend/Analyser.js +++ b/plugins/dbgate-plugin-mysql/src/backend/Analyser.js @@ -66,14 +66,7 @@ class Analyser extends DatabaseAnalyser { } getRequestedViewNames(allViewNames) { - if (this.singleObjectFilter) { - const { typeField, pureName } = this.singleObjectFilter; - if (typeField == 'views') return [pureName]; - } - if (this.modifications) { - return this.modifications.filter(x => x.objectTypeField == 'views').map(x => x.newName.pureName); - } - return allViewNames; + return this.getRequestedObjectPureNames('views', allViewNames); } async getViewTexts(allViewNames) { @@ -82,7 +75,7 @@ class Analyser extends DatabaseAnalyser { try { const resp = await this.driver.query(this.pool, `SHOW CREATE VIEW \`${viewName}\``); res[viewName] = resp.rows[0]['Create View']; - } catch(err) { + } catch (err) { console.log('ERROR', err); res[viewName] = `${err}`; } diff --git a/plugins/dbgate-plugin-sqlite/src/backend/Analyser.js b/plugins/dbgate-plugin-sqlite/src/backend/Analyser.js index abf301823..65ffe60fd 100644 --- a/plugins/dbgate-plugin-sqlite/src/backend/Analyser.js +++ b/plugins/dbgate-plugin-sqlite/src/backend/Analyser.js @@ -1,3 +1,4 @@ +const _ = require('lodash'); const { DatabaseAnalyser } = require('dbgate-tools'); class Analyser extends DatabaseAnalyser { @@ -7,14 +8,50 @@ class Analyser extends DatabaseAnalyser { async _runAnalysis() { const tables = await this.driver.query(this.pool, "select * from sqlite_master where type='table'"); - console.log('tables', tables); + console.log('TABLES', tables); + + const tableSqls = _.zipObject( + tables.rows.map((x) => x.name), + tables.rows.map((x) => x.sql) + ); + + const tableList = tables.rows.map((x) => ({ + pureName: x.name, + objectId: x.name, + })); + + for (const tableName of this.getRequestedObjectPureNames( + 'tables', + tables.rows.map((x) => x.name) + )) { + const info = await this.driver.query(this.pool, `pragma table_info('${tableName}')`); + const tableObj = tableList.find((x) => x.pureName == tableName); + tableObj.columns = info.rows.map((col) => ({ + columnName: col.name, + dataType: col.type, + notNull: !!col.notnull, + defaultValue: col.dflt_value == null ? undefined : col.dflt_value, + autoIncrement: tableSqls[tableName].toLowerCase().includes('autoincrement') && !!col.pk, + })); + + const pkColumns = info.rows + .filter((x) => x.pk) + .map((col) => ({ + columnName: col.name, + })); + + if (pkColumns.length > 0) { + tableObj.primaryKey = { + columns: pkColumns, + }; + } + + // console.log(info); + } const res = this.mergeAnalyseResult( { - tables: tables.rows.map((x) => ({ - pureName: x.name, - objectId: x.name, - })), + tables: tableList, }, (x) => x.pureName ); diff --git a/plugins/dbgate-plugin-sqlite/src/frontend/driver.js b/plugins/dbgate-plugin-sqlite/src/frontend/driver.js index 7eeb2a07a..8bfb95bfb 100644 --- a/plugins/dbgate-plugin-sqlite/src/frontend/driver.js +++ b/plugins/dbgate-plugin-sqlite/src/frontend/driver.js @@ -5,7 +5,7 @@ const Dumper = require('./Dumper'); const dialect = { limitSelect: true, rangeSelect: true, - offsetFetchRangeSyntax: true, + offsetFetchRangeSyntax: false, stringEscapeChar: "'", fallbackDataType: 'nvarchar(max)', quoteIdentifier(s) {