connecting via socket for mysql and postgres #358

This commit is contained in:
Jan Prochazka 2022-09-08 14:23:13 +02:00
parent 5eda39cb62
commit 0293766bad
7 changed files with 84 additions and 14 deletions

View File

@ -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<any>;
close(pool): Promise<any>;

View File

@ -77,7 +77,7 @@
{#if $authTypes && driver?.showConnectionField('authType', $values)}
<FormSelectField
label="Authentication"
label={driver?.authTypeLabel ?? 'Authentication'}
name="authType"
isNative
disabled={isConnected}
@ -106,7 +106,7 @@
name="port"
disabled={isConnected || disabledFields.includes('port')}
templateProps={{ noMargin: true }}
placeholder={driver && driver.defaultPort}
placeholder={driver?.defaultPort}
/>
</div>
{/if}
@ -119,6 +119,15 @@
{/if}
{/if}
{#if driver?.showConnectionField('socketPath', $values)}
<FormTextField
label="Socket path"
name="socketPath"
disabled={isConnected || disabledFields.includes('socketPath')}
placeholder={driver?.defaultSocketPath}
/>
{/if}
{#if showUser && showPassword}
<div class="row">
{#if showUser}

View File

@ -81,6 +81,7 @@
'password',
'defaultDatabase',
'singleDatabase',
'socketPath',
];
const visibleProps = allProps.filter(x => driver?.showConnectionField(x, $values));
const omitProps = _.difference(allProps, visibleProps);

View File

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

View File

@ -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' },
];

View File

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

View File

@ -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} */