Merge pull request #981 from dbgate/feature/triggers-sqlite

Feature/triggers sqlite
This commit is contained in:
Jan Prochazka
2025-01-07 13:23:36 +01:00
committed by GitHub
20 changed files with 146 additions and 71 deletions

View File

@@ -37,7 +37,7 @@
},
"dependencies": {
"bson": "^6.8.0",
"dbgate-query-splitter": "^4.11.2",
"dbgate-query-splitter": "^4.11.3",
"dbgate-tools": "^6.0.0-alpha.1",
"is-promise": "^4.0.0",
"lodash": "^4.17.21",

View File

@@ -37,7 +37,7 @@
},
"dependencies": {
"async-lock": "^1.2.6",
"dbgate-query-splitter": "^4.11.2",
"dbgate-query-splitter": "^4.11.3",
"dbgate-tools": "^6.0.0-alpha.1",
"lodash": "^4.17.21",
"tedious": "^18.2.0"

View File

@@ -37,7 +37,7 @@
},
"dependencies": {
"antares-mysql-dumper": "^0.0.1",
"dbgate-query-splitter": "^4.11.2",
"dbgate-query-splitter": "^4.11.3",
"dbgate-tools": "^6.0.0-alpha.1",
"lodash": "^4.17.21",
"mysql2": "^3.11.3"

View File

@@ -35,7 +35,7 @@
"webpack-cli": "^5.1.4"
},
"dependencies": {
"dbgate-query-splitter": "^4.11.2",
"dbgate-query-splitter": "^4.11.3",
"dbgate-tools": "^6.0.0-alpha.1",
"lodash": "^4.17.21"
},

View File

@@ -37,7 +37,7 @@
"dependencies": {
"wkx": "^0.5.0",
"pg-copy-streams": "^6.0.6",
"dbgate-query-splitter": "^4.11.2",
"dbgate-query-splitter": "^4.11.3",
"dbgate-tools": "^6.0.0-alpha.1",
"lodash": "^4.17.21",
"pg": "^8.11.5"

View File

@@ -34,7 +34,7 @@
"webpack-cli": "^5.1.4"
},
"dependencies": {
"dbgate-query-splitter": "^4.11.2",
"dbgate-query-splitter": "^4.11.3",
"dbgate-tools": "^6.0.0-alpha.1",
"lodash": "^4.17.21",
"async": "^3.2.3",

View File

@@ -37,7 +37,7 @@
"dependencies": {
"dbgate-tools": "^6.0.0-alpha.1",
"lodash": "^4.17.21",
"dbgate-query-splitter": "^4.11.2"
"dbgate-query-splitter": "^4.11.3"
},
"optionalDependencies": {
"better-sqlite3": "9.6.0"

View File

@@ -1,19 +1,6 @@
const _ = require('lodash');
const { DatabaseAnalyser } = global.DBGATE_PACKAGES['dbgate-tools'];
const indexcolsQuery = `
SELECT
m.name as tableName,
il.name as constraintName,
il."unique" as isUnique,
ii.name as columnName,
il.origin
FROM sqlite_schema AS m,
pragma_index_list(m.name) AS il,
pragma_index_info(il.name) AS ii
WHERE m.type='table' AND il.origin <> 'pk'
ORDER BY ii.seqno, il.name
`;
const sql = require('./sql');
class Analyser extends DatabaseAnalyser {
constructor(dbhan, driver, version) {
@@ -26,8 +13,8 @@ class Analyser extends DatabaseAnalyser {
}
async _getFastSnapshot() {
const objects = await this.driver.query(this.dbhan, "select * from sqlite_master where type='table' or type='view'");
const indexcols = await this.driver.query(this.dbhan, indexcolsQuery);
const objects = await this.driver.query(this.dbhan, sql.objects);
const indexcols = await this.driver.query(this.dbhan, sql.indexcols);
return {
tables: objects.rows
@@ -53,10 +40,7 @@ class Analyser extends DatabaseAnalyser {
}
async _runAnalysis() {
const objects = await this.analyserQuery(
"select * from sqlite_master where (type='table' or type='view') and name =OBJECT_ID_CONDITION",
['tables', 'views']
);
const objects = await this.analyserQuery(sql.objectsConditioned, ['tables', 'views']);
const tables = objects.rows.filter((x) => x.type == 'table');
const views = objects.rows.filter((x) => x.type == 'view');
// console.log('TABLES', tables);
@@ -79,7 +63,7 @@ class Analyser extends DatabaseAnalyser {
createSql: x.sql,
}));
const indexcols = await this.driver.query(this.dbhan, indexcolsQuery);
const indexcols = await this.driver.query(this.dbhan, sql.indexcols);
for (const tableName of this.getRequestedObjectPureNames(
'tables',
@@ -165,9 +149,12 @@ class Analyser extends DatabaseAnalyser {
}));
}
const triggers = await this.driver.query(this.dbhan, sql.triggers);
return {
tables: tableList,
views: viewList,
triggers: triggers.rows,
};
}
}

View File

@@ -0,0 +1,11 @@
const objects = require('./objects.js');
const objectsConditioned = require('./objectsConditioned.js');
const indexcols = require('./indexcols.js');
const triggers = require('./triggers.js');
module.exports = {
objects,
objectsConditioned,
indexcols,
triggers,
};

View File

@@ -0,0 +1,14 @@
module.exports = `
SELECT
m.name as tableName,
il.name as constraintName,
il."unique" as isUnique,
ii.name as columnName,
il.origin
FROM sqlite_schema AS m,
pragma_index_list(m.name) AS il,
pragma_index_info(il.name) AS ii
WHERE m.type='table' AND il.origin <> 'pk'
ORDER BY ii.seqno, il.name
`;

View File

@@ -0,0 +1,3 @@
module.exports = `
select * from sqlite_master where (type='table' or type='view')
`;

View File

@@ -0,0 +1,3 @@
module.exports = `
select * from sqlite_master where (type='table' or type='view') and name =OBJECT_ID_CONDITION
`;

View File

@@ -0,0 +1,23 @@
module.exports = `
SELECT
rowid AS objectId,
name AS pureName,
CASE
WHEN sql LIKE '% AFTER %' THEN 'AFTER'
WHEN sql LIKE '% BEFORE %' THEN 'BEFORE'
WHEN sql LIKE '% INSTEAD OF %' THEN 'INSTEAD OF'
ELSE 'UNKNOWN'
END AS triggerTiming,
CASE
WHEN sql LIKE '% INSERT %' THEN 'INSERT'
WHEN sql LIKE '% UPDATE %' THEN 'UPDATE'
WHEN sql LIKE '% DELETE %' THEN 'DELETE'
ELSE NULL
END AS eventType,
tbl_name AS tableName,
sql AS createSql
FROM
sqlite_master
WHERE
type = 'trigger';
`;