mirror of
https://github.com/dbgate/dbgate
synced 2024-11-07 20:26:23 +00:00
sqlite table analyser
This commit is contained in:
parent
28e19402f3
commit
e739aed80d
@ -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);
|
||||
// }
|
||||
|
@ -13,5 +13,5 @@
|
||||
|
||||
<span class="nowrap">
|
||||
<FontIcon icon={getConstraintIcon(constraintType)} />
|
||||
{constraintName}
|
||||
{constraintName || '(without name)'}
|
||||
</span>
|
||||
|
@ -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}`;
|
||||
}
|
||||
|
@ -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
|
||||
);
|
||||
|
@ -5,7 +5,7 @@ const Dumper = require('./Dumper');
|
||||
const dialect = {
|
||||
limitSelect: true,
|
||||
rangeSelect: true,
|
||||
offsetFetchRangeSyntax: true,
|
||||
offsetFetchRangeSyntax: false,
|
||||
stringEscapeChar: "'",
|
||||
fallbackDataType: 'nvarchar(max)',
|
||||
quoteIdentifier(s) {
|
||||
|
Loading…
Reference in New Issue
Block a user