From 0293766bad9f1eab455899de2a41bb5760cd1abc Mon Sep 17 00:00:00 2001 From: Jan Prochazka Date: Thu, 8 Sep 2022 14:23:13 +0200 Subject: [PATCH] connecting via socket for mysql and postgres #358 --- packages/types/engines.d.ts | 2 ++ .../settings/ConnectionDriverFields.svelte | 13 +++++-- packages/web/src/tabs/ConnectionTab.svelte | 1 + .../src/backend/drivers.js | 27 ++++++++++++--- .../src/frontend/drivers.js | 10 ++++-- .../src/backend/drivers.js | 34 +++++++++++++++++-- .../src/frontend/drivers.js | 11 +++++- 7 files changed, 84 insertions(+), 14 deletions(-) diff --git a/packages/types/engines.d.ts b/packages/types/engines.d.ts index 5ee6f2f9..cc15d56d 100644 --- a/packages/types/engines.d.ts +++ b/packages/types/engines.d.ts @@ -72,6 +72,8 @@ export interface EngineDriver { beforeConnectionSave?: (values: any) => any; databaseUrlPlaceholder?: string; defaultAuthTypeName?: string; + defaultSocketPath?: string; + authTypeLabel?: string; importExportArgs?: any[]; connect({ server, port, user, password, database }): Promise; close(pool): Promise; diff --git a/packages/web/src/settings/ConnectionDriverFields.svelte b/packages/web/src/settings/ConnectionDriverFields.svelte index 997b78c7..9f931091 100644 --- a/packages/web/src/settings/ConnectionDriverFields.svelte +++ b/packages/web/src/settings/ConnectionDriverFields.svelte @@ -77,7 +77,7 @@ {#if $authTypes && driver?.showConnectionField('authType', $values)} {/if} @@ -119,6 +119,15 @@ {/if} {/if} +{#if driver?.showConnectionField('socketPath', $values)} + +{/if} + {#if showUser && showPassword}
{#if showUser} diff --git a/packages/web/src/tabs/ConnectionTab.svelte b/packages/web/src/tabs/ConnectionTab.svelte index aac50f8f..8473f17a 100644 --- a/packages/web/src/tabs/ConnectionTab.svelte +++ b/packages/web/src/tabs/ConnectionTab.svelte @@ -81,6 +81,7 @@ 'password', 'defaultDatabase', 'singleDatabase', + 'socketPath', ]; const visibleProps = allProps.filter(x => driver?.showConnectionField(x, $values)); const omitProps = _.difference(allProps, visibleProps); diff --git a/plugins/dbgate-plugin-mysql/src/backend/drivers.js b/plugins/dbgate-plugin-mysql/src/backend/drivers.js index 5387f0ad..5c5bbbbd 100644 --- a/plugins/dbgate-plugin-mysql/src/backend/drivers.js +++ b/plugins/dbgate-plugin-mysql/src/backend/drivers.js @@ -29,10 +29,11 @@ const drivers = driverBases.map(driverBase => ({ ...driverBase, analyserClass: Analyser, - async connect({ server, port, user, password, database, ssl, isReadOnly, forceRowsAsObjects }) { - const connection = mysql2.createConnection({ - host: server, - port, + async connect({ server, port, user, password, database, ssl, isReadOnly, forceRowsAsObjects, socketPath, authType }) { + const options = { + host: authType == 'socket' ? null : server, + port: authType == 'socket' ? null : port, + socketPath: authType == 'socket' ? socketPath || driverBase.defaultSocketPath : null, user, password, database, @@ -43,7 +44,9 @@ const drivers = driverBases.map(driverBase => ({ dateStrings: true, // TODO: test following options // multipleStatements: true, - }); + }; + + const connection = mysql2.createConnection(options); connection._database_name = database; if (isReadOnly) { await this.query(connection, 'SET SESSION TRANSACTION READ ONLY'); @@ -182,6 +185,20 @@ const drivers = driverBases.map(driverBase => ({ }); return res; }, + getAuthTypes() { + return [ + { + title: 'Host and port', + name: 'hostPort', + disabledFields: ['socketPath'], + }, + { + title: 'Socket', + name: 'socket', + disabledFields: ['server', 'port'], + }, + ]; + }, })); module.exports = drivers; diff --git a/plugins/dbgate-plugin-mysql/src/frontend/drivers.js b/plugins/dbgate-plugin-mysql/src/frontend/drivers.js index 4793ea9a..b9a840fe 100644 --- a/plugins/dbgate-plugin-mysql/src/frontend/drivers.js +++ b/plugins/dbgate-plugin-mysql/src/frontend/drivers.js @@ -102,21 +102,25 @@ const dialect = { const mysqlDriverBase = { ...driverBase, showConnectionField: (field, values) => - ['server', 'port', 'user', 'password', 'defaultDatabase', 'singleDatabase', 'isReadOnly'].includes(field), + ['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: () => mysqlSplitterOptions, readOnlySessions: true, supportsDatabaseDump: true, + authTypeLabel: 'Connection mode', + defaultAuthTypeName: 'hostPort', + defaultSocketPath: '/var/run/mysqld/mysqld.sock', 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 ;', + 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' }, ]; diff --git a/plugins/dbgate-plugin-postgres/src/backend/drivers.js b/plugins/dbgate-plugin-postgres/src/backend/drivers.js index 03a9f8de..46d11f02 100644 --- a/plugins/dbgate-plugin-postgres/src/backend/drivers.js +++ b/plugins/dbgate-plugin-postgres/src/backend/drivers.js @@ -31,7 +31,20 @@ const drivers = driverBases.map(driverBase => ({ ...driverBase, analyserClass: Analyser, - async connect({ engine, server, port, user, password, database, databaseUrl, useDatabaseUrl, ssl, isReadOnly }) { + async connect({ + engine, + server, + port, + user, + password, + database, + databaseUrl, + useDatabaseUrl, + ssl, + isReadOnly, + authType, + socketPath, + }) { let options = null; if (engine == 'redshift@dbgate-plugin-postgres') { @@ -56,8 +69,8 @@ const drivers = driverBases.map(driverBase => ({ connectionString: databaseUrl, } : { - host: server, - port, + host: authType == 'socket' ? socketPath || driverBase.defaultSocketPath : server, + port: authType == 'socket' ? null : port, user, password, database: database || 'postgres', @@ -65,6 +78,8 @@ const drivers = driverBases.map(driverBase => ({ }; } + console.log('OPTIONS', options); + const client = new pg.Client(options); await client.connect(); @@ -233,6 +248,19 @@ const drivers = driverBases.map(driverBase => ({ const { rows } = await this.query(client, 'SELECT datname AS name FROM pg_database WHERE datistemplate = false'); return rows; }, + + getAuthTypes() { + return [ + { + title: 'Host and port', + name: 'hostPort', + }, + { + title: 'Socket', + name: 'socket', + }, + ]; + }, })); module.exports = drivers; diff --git a/plugins/dbgate-plugin-postgres/src/frontend/drivers.js b/plugins/dbgate-plugin-postgres/src/frontend/drivers.js index 5d60186f..601a67aa 100644 --- a/plugins/dbgate-plugin-postgres/src/frontend/drivers.js +++ b/plugins/dbgate-plugin-postgres/src/frontend/drivers.js @@ -115,7 +115,12 @@ const postgresDriverBase = { if (values.useDatabaseUrl) { return ['databaseUrl', 'isReadOnly'].includes(field); } - return ['server', 'port', 'user', 'password', 'defaultDatabase', 'singleDatabase', 'isReadOnly'].includes(field); + + return ( + ['authType', 'user', 'password', 'defaultDatabase', 'singleDatabase', 'isReadOnly'].includes(field) || + (values.authType == 'socket' && ['socketPath'].includes(field)) || + (values.authType != 'socket' && ['server', 'port'].includes(field)) + ); }, beforeConnectionSave: connection => { @@ -159,6 +164,10 @@ $$ LANGUAGE plpgsql;`, }, ]; }, + + authTypeLabel: 'Connection mode', + defaultAuthTypeName: 'hostPort', + defaultSocketPath: '/var/run/postgresql', }; /** @type {import('dbgate-types').EngineDriver} */