mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-05-03 01:23:57 +00:00
sqlite table analyser
This commit is contained in:
@@ -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) {
|
// findObjectById(id) {
|
||||||
// return this.structure.tables.find((x) => x.objectId == id);
|
// return this.structure.tables.find((x) => x.objectId == id);
|
||||||
// }
|
// }
|
||||||
|
|||||||
@@ -13,5 +13,5 @@
|
|||||||
|
|
||||||
<span class="nowrap">
|
<span class="nowrap">
|
||||||
<FontIcon icon={getConstraintIcon(constraintType)} />
|
<FontIcon icon={getConstraintIcon(constraintType)} />
|
||||||
{constraintName}
|
{constraintName || '(without name)'}
|
||||||
</span>
|
</span>
|
||||||
|
|||||||
@@ -66,14 +66,7 @@ class Analyser extends DatabaseAnalyser {
|
|||||||
}
|
}
|
||||||
|
|
||||||
getRequestedViewNames(allViewNames) {
|
getRequestedViewNames(allViewNames) {
|
||||||
if (this.singleObjectFilter) {
|
return this.getRequestedObjectPureNames('views', allViewNames);
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async getViewTexts(allViewNames) {
|
async getViewTexts(allViewNames) {
|
||||||
@@ -82,7 +75,7 @@ class Analyser extends DatabaseAnalyser {
|
|||||||
try {
|
try {
|
||||||
const resp = await this.driver.query(this.pool, `SHOW CREATE VIEW \`${viewName}\``);
|
const resp = await this.driver.query(this.pool, `SHOW CREATE VIEW \`${viewName}\``);
|
||||||
res[viewName] = resp.rows[0]['Create View'];
|
res[viewName] = resp.rows[0]['Create View'];
|
||||||
} catch(err) {
|
} catch (err) {
|
||||||
console.log('ERROR', err);
|
console.log('ERROR', err);
|
||||||
res[viewName] = `${err}`;
|
res[viewName] = `${err}`;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
const _ = require('lodash');
|
||||||
const { DatabaseAnalyser } = require('dbgate-tools');
|
const { DatabaseAnalyser } = require('dbgate-tools');
|
||||||
|
|
||||||
class Analyser extends DatabaseAnalyser {
|
class Analyser extends DatabaseAnalyser {
|
||||||
@@ -7,14 +8,50 @@ class Analyser extends DatabaseAnalyser {
|
|||||||
|
|
||||||
async _runAnalysis() {
|
async _runAnalysis() {
|
||||||
const tables = await this.driver.query(this.pool, "select * from sqlite_master where type='table'");
|
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(
|
const res = this.mergeAnalyseResult(
|
||||||
{
|
{
|
||||||
tables: tables.rows.map((x) => ({
|
tables: tableList,
|
||||||
pureName: x.name,
|
|
||||||
objectId: x.name,
|
|
||||||
})),
|
|
||||||
},
|
},
|
||||||
(x) => x.pureName
|
(x) => x.pureName
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ const Dumper = require('./Dumper');
|
|||||||
const dialect = {
|
const dialect = {
|
||||||
limitSelect: true,
|
limitSelect: true,
|
||||||
rangeSelect: true,
|
rangeSelect: true,
|
||||||
offsetFetchRangeSyntax: true,
|
offsetFetchRangeSyntax: false,
|
||||||
stringEscapeChar: "'",
|
stringEscapeChar: "'",
|
||||||
fallbackDataType: 'nvarchar(max)',
|
fallbackDataType: 'nvarchar(max)',
|
||||||
quoteIdentifier(s) {
|
quoteIdentifier(s) {
|
||||||
|
|||||||
Reference in New Issue
Block a user