sqlite table analyser

This commit is contained in:
Jan Prochazka
2021-05-05 20:04:49 +02:00
parent 28e19402f3
commit e739aed80d
5 changed files with 57 additions and 16 deletions

View File

@@ -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
);

View File

@@ -5,7 +5,7 @@ const Dumper = require('./Dumper');
const dialect = {
limitSelect: true,
rangeSelect: true,
offsetFetchRangeSyntax: true,
offsetFetchRangeSyntax: false,
stringEscapeChar: "'",
fallbackDataType: 'nvarchar(max)',
quoteIdentifier(s) {