From e21c6d4872dabd4812af873a7c856d090c745879 Mon Sep 17 00:00:00 2001 From: Jan Prochazka Date: Thu, 12 Sep 2024 13:49:10 +0200 Subject: [PATCH] clickhouse - view support, incremental structure update --- .../src/backend/Analyser.js | 21 +++++++++++++++++++ .../src/backend/sql/index.js | 4 ++++ .../src/backend/sql/tableModifications.js | 5 +++++ .../src/backend/sql/tables.js | 2 +- .../src/backend/sql/views.js | 10 +++++++++ 5 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 plugins/dbgate-plugin-clickhouse/src/backend/sql/tableModifications.js create mode 100644 plugins/dbgate-plugin-clickhouse/src/backend/sql/views.js diff --git a/plugins/dbgate-plugin-clickhouse/src/backend/Analyser.js b/plugins/dbgate-plugin-clickhouse/src/backend/Analyser.js index 0a072e49..3a734fc0 100644 --- a/plugins/dbgate-plugin-clickhouse/src/backend/Analyser.js +++ b/plugins/dbgate-plugin-clickhouse/src/backend/Analyser.js @@ -33,6 +33,8 @@ class Analyser extends DatabaseAnalyser { const tables = await this.analyserQuery('tables', ['tables']); this.feedback({ analysingMessage: 'Loading columns' }); const columns = await this.analyserQuery('columns', ['tables', 'views']); + this.feedback({ analysingMessage: 'Loading views' }); + const views = await this.analyserQuery('views', ['views']); const res = { tables: tables.rows.map((table) => ({ @@ -53,10 +55,29 @@ class Analyser extends DatabaseAnalyser { : null, foreignKeys: [], })), + views: views.rows.map((view) => ({ + ...view, + columns: columns.rows + .filter((col) => col.pureName == view.pureName) + .map((col) => ({ + ...col, + ...extractDataType(col.dataType), + })), + createSql: `CREATE VIEW "${view.pureName}"\nAS\n${view.viewDefinition}`, + })), }; this.feedback({ analysingMessage: null }); return res; } + + async _getFastSnapshot() { + const tableModificationsQueryData = await this.analyserQuery('tableModifications'); + + return { + tables: tableModificationsQueryData.rows.filter((x) => x.tableEngine != 'View'), + views: tableModificationsQueryData.rows.filter((x) => x.tableEngine == 'View'), + }; + } } module.exports = Analyser; diff --git a/plugins/dbgate-plugin-clickhouse/src/backend/sql/index.js b/plugins/dbgate-plugin-clickhouse/src/backend/sql/index.js index 1d0b3f63..6465f821 100644 --- a/plugins/dbgate-plugin-clickhouse/src/backend/sql/index.js +++ b/plugins/dbgate-plugin-clickhouse/src/backend/sql/index.js @@ -1,7 +1,11 @@ const columns = require('./columns'); const tables = require('./tables'); +const views = require('./views'); +const tableModifications = require('./tableModifications'); module.exports = { columns, tables, + views, + tableModifications, }; diff --git a/plugins/dbgate-plugin-clickhouse/src/backend/sql/tableModifications.js b/plugins/dbgate-plugin-clickhouse/src/backend/sql/tableModifications.js new file mode 100644 index 00000000..15ec1916 --- /dev/null +++ b/plugins/dbgate-plugin-clickhouse/src/backend/sql/tableModifications.js @@ -0,0 +1,5 @@ +module.exports = ` +select metadata_modification_time as "contentHash", uuid as "objectId", engine as "tableEngine" +from system.tables +where database='#DATABASE#'; +`; diff --git a/plugins/dbgate-plugin-clickhouse/src/backend/sql/tables.js b/plugins/dbgate-plugin-clickhouse/src/backend/sql/tables.js index be79c3a5..a9960f5e 100644 --- a/plugins/dbgate-plugin-clickhouse/src/backend/sql/tables.js +++ b/plugins/dbgate-plugin-clickhouse/src/backend/sql/tables.js @@ -2,5 +2,5 @@ module.exports = ` select name as "pureName", metadata_modification_time as "contentHash", total_rows as "tableRowCount", uuid as "objectId", comment as "objectComment", engine as "tableEngine", primary_key as "primaryKeyColumns", sorting_key as "sortingKeyColumns" from system.tables -where database='#DATABASE#' and uuid =OBJECT_ID_CONDITION; +where database='#DATABASE#' and uuid =OBJECT_ID_CONDITION and engine != 'View'; `; diff --git a/plugins/dbgate-plugin-clickhouse/src/backend/sql/views.js b/plugins/dbgate-plugin-clickhouse/src/backend/sql/views.js new file mode 100644 index 00000000..c773517a --- /dev/null +++ b/plugins/dbgate-plugin-clickhouse/src/backend/sql/views.js @@ -0,0 +1,10 @@ +module.exports = ` +select + tables.name as "pureName", + tables.uuid as "objectId", + views.view_definition as "viewDefinition", + tables.metadata_modification_time as "contentHash" +from information_schema.views +inner join system.tables on views.table_name = tables.name and views.table_schema = tables.database +where views.table_schema='#DATABASE#' and tables.uuid =OBJECT_ID_CONDITION +`;