From 7ad195077794f121191ed733296a48ff572fa7c2 Mon Sep 17 00:00:00 2001 From: Jan Prochazka Date: Wed, 11 Sep 2024 14:01:11 +0200 Subject: [PATCH] getTableFormOptions moved to dialect --- packages/tools/src/driverBase.ts | 4 - packages/types/dialect.d.ts | 5 + packages/types/engines.d.ts | 5 - .../web/src/tableeditor/TableEditor.svelte | 2 +- .../src/frontend/driver.js | 40 +++---- .../src/frontend/drivers.js | 106 ++++++++++-------- 6 files changed, 83 insertions(+), 79 deletions(-) diff --git a/packages/tools/src/driverBase.ts b/packages/tools/src/driverBase.ts index 3c9c8d8f..e67433fc 100644 --- a/packages/tools/src/driverBase.ts +++ b/packages/tools/src/driverBase.ts @@ -177,8 +177,4 @@ export const driverBase = { createSaveChangeSetScript(changeSet, dbinfo, defaultCreator) { return defaultCreator(changeSet, dbinfo); }, - - getTableFormOptions() { - return null; - }, }; diff --git a/packages/types/dialect.d.ts b/packages/types/dialect.d.ts index 6a28f736..9a6460d3 100644 --- a/packages/types/dialect.d.ts +++ b/packages/types/dialect.d.ts @@ -44,4 +44,9 @@ export interface SqlDialect { // create sql-tree expression createColumnViewExpression(columnName: string, dataType: string, source: { alias: string }, alias?: string): any; + + getTableFormOptions(intent: 'newTableForm' | 'editTableForm' | 'sqlCreateTable' | 'sqlAlterTable'): { + name: string; + sqlFormatString: string; + }[]; } diff --git a/packages/types/engines.d.ts b/packages/types/engines.d.ts index e3166bd0..477bb784 100644 --- a/packages/types/engines.d.ts +++ b/packages/types/engines.d.ts @@ -155,11 +155,6 @@ export interface EngineDriver extends FilterBehaviourProvider { collectionNameLabel?: string; newCollectionFormParams?: any[]; - getTableFormOptions(intent: 'newTableForm' | 'editTableForm' | 'sqlCreateTable' | 'sqlAlterTable'): { - name: string; - sqlFormatString: string; - }[]; - supportedCreateDatabase?: boolean; showConnectionField?: ( field: string, diff --git a/packages/web/src/tableeditor/TableEditor.svelte b/packages/web/src/tableeditor/TableEditor.svelte index aa8e6cb1..63b9d32f 100644 --- a/packages/web/src/tableeditor/TableEditor.svelte +++ b/packages/web/src/tableeditor/TableEditor.svelte @@ -156,7 +156,7 @@ invalidateCommands(); } - $: tableFormOptions = driver?.getTableFormOptions(tableInfo?.objectId ? 'editTableForm' : 'newTableForm'); + $: tableFormOptions = driver?.dialect?.getTableFormOptions?.(tableInfo?.objectId ? 'editTableForm' : 'newTableForm');
diff --git a/plugins/dbgate-plugin-clickhouse/src/frontend/driver.js b/plugins/dbgate-plugin-clickhouse/src/frontend/driver.js index baff6fa2..f7607b3b 100644 --- a/plugins/dbgate-plugin-clickhouse/src/frontend/driver.js +++ b/plugins/dbgate-plugin-clickhouse/src/frontend/driver.js @@ -72,6 +72,26 @@ const dialect = { quoteIdentifier(s) { return `"${s}"`; }, + + getTableFormOptions(intent) { + const isNewTable = intent == 'newTableForm'; + return [ + { + type: isNewTable ? 'dropdowntext' : 'text', + options: clickhouseEngines, + label: 'Engine', + name: 'tableEngine', + sqlFormatString: '^engine = %s', + disabled: !isNewTable, + }, + { + type: 'text', + label: 'Comment', + name: 'objectComment', + sqlFormatString: '^comment %v', + }, + ]; + }, }; /** @type {import('dbgate-types').EngineDriver} */ @@ -117,26 +137,6 @@ const driver = { } return res; }, - - getTableFormOptions(intent) { - const isNewTable = intent == 'newTableForm'; - return [ - { - type: isNewTable ? 'dropdowntext' : 'text', - options: clickhouseEngines, - label: 'Engine', - name: 'tableEngine', - sqlFormatString: '^engine = %s', - disabled: !isNewTable, - }, - { - type: 'text', - label: 'Comment', - name: 'objectComment', - sqlFormatString: '^comment %v', - }, - ]; - }, }; module.exports = driver; diff --git a/plugins/dbgate-plugin-mysql/src/frontend/drivers.js b/plugins/dbgate-plugin-mysql/src/frontend/drivers.js index 8b49c945..1131c2cc 100644 --- a/plugins/dbgate-plugin-mysql/src/frontend/drivers.js +++ b/plugins/dbgate-plugin-mysql/src/frontend/drivers.js @@ -99,39 +99,6 @@ const dialect = { }; } }, -}; - -const mysqlDriverBase = { - ...driverBase, - showConnectionField: (field, values) => - ['authType', 'user', 'password', 'defaultDatabase', 'singleDatabase', 'isReadOnly'].includes(field) || - (values.authType == 'socket' && ['socketPath'].includes(field)) || - (values.authType != 'socket' && ['server', 'port'].includes(field)), - dumperClass: Dumper, - dialect, - defaultPort: 3306, - getQuerySplitterOptions: usage => - usage == 'editor' - ? { ...mysqlSplitterOptions, ignoreComments: true, preventSingleLineSplit: true } - : mysqlSplitterOptions, - - readOnlySessions: true, - supportsDatabaseDump: true, - authTypeLabel: 'Connection mode', - defaultAuthTypeName: 'hostPort', - defaultSocketPath: '/var/run/mysqld/mysqld.sock', - supportsTransactions: true, - - getNewObjectTemplates() { - return [ - { label: 'New view', sql: 'CREATE VIEW myview\nAS\nSELECT * FROM table1' }, - { - label: 'New procedure', - sql: 'DELIMITER //\n\nCREATE PROCEDURE myproc (IN arg1 INT)\nBEGIN\n SELECT * FROM table1;\nEND\n\nDELIMITER ;', - }, - { label: 'New function', sql: 'CREATE FUNCTION myfunc (arg1 INT)\nRETURNS INT DETERMINISTIC\nRETURN 1' }, - ]; - }, getSupportedEngines() { return []; @@ -158,15 +125,8 @@ const mysqlDriverBase = { }, }; -/** @type {import('dbgate-types').EngineDriver} */ -const mysqlDriver = { - ...mysqlDriverBase, - engine: 'mysql@dbgate-plugin-mysql', - title: 'MySQL', - __analyserInternals: { - quoteDefaultValues: true, - }, - +const mysqlDialect = { + ...dialect, getSupportedEngines() { const mysqlEngines = [ 'InnoDB', // Default and most commonly used engine with ACID transaction support and referential integrity. @@ -189,14 +149,51 @@ const mysqlDriver = { }, }; -/** @type {import('dbgate-types').EngineDriver} */ -const mariaDriver = { - ...mysqlDriverBase, - engine: 'mariadb@dbgate-plugin-mysql', - title: 'MariaDB', - __analyserInternals: { - quoteDefaultValues: false, +const mysqlDriverBase = { + ...driverBase, + showConnectionField: (field, values) => + ['authType', 'user', 'password', 'defaultDatabase', 'singleDatabase', 'isReadOnly'].includes(field) || + (values.authType == 'socket' && ['socketPath'].includes(field)) || + (values.authType != 'socket' && ['server', 'port'].includes(field)), + dumperClass: Dumper, + defaultPort: 3306, + getQuerySplitterOptions: usage => + usage == 'editor' + ? { ...mysqlSplitterOptions, ignoreComments: true, preventSingleLineSplit: true } + : mysqlSplitterOptions, + + readOnlySessions: true, + supportsDatabaseDump: true, + authTypeLabel: 'Connection mode', + defaultAuthTypeName: 'hostPort', + defaultSocketPath: '/var/run/mysqld/mysqld.sock', + supportsTransactions: true, + + getNewObjectTemplates() { + return [ + { label: 'New view', sql: 'CREATE VIEW myview\nAS\nSELECT * FROM table1' }, + { + label: 'New procedure', + sql: 'DELIMITER //\n\nCREATE PROCEDURE myproc (IN arg1 INT)\nBEGIN\n SELECT * FROM table1;\nEND\n\nDELIMITER ;', + }, + { label: 'New function', sql: 'CREATE FUNCTION myfunc (arg1 INT)\nRETURNS INT DETERMINISTIC\nRETURN 1' }, + ]; }, +}; + +/** @type {import('dbgate-types').EngineDriver} */ +const mysqlDriver = { + ...mysqlDriverBase, + dialect: mysqlDialect, + engine: 'mysql@dbgate-plugin-mysql', + title: 'MySQL', + __analyserInternals: { + quoteDefaultValues: true, + }, +}; + +const mariaDbDialect = { + ...dialect, getSupportedEngines() { const mariaDBEngines = [ 'InnoDB', // Main transactional engine, similar to MySQL, supports ACID transactions and referential integrity. @@ -224,4 +221,15 @@ const mariaDriver = { }, }; +/** @type {import('dbgate-types').EngineDriver} */ +const mariaDriver = { + ...mysqlDriverBase, + dialect: mariaDbDialect, + engine: 'mariadb@dbgate-plugin-mysql', + title: 'MariaDB', + __analyserInternals: { + quoteDefaultValues: false, + }, +}; + module.exports = [mysqlDriver, mariaDriver];