2020-02-02 19:28:27 +00:00
|
|
|
const PostgreAnalyser = require('./PostgreAnalyser');
|
|
|
|
const PostgreDumper = require('./PostgreDumper');
|
2020-01-04 20:59:53 +00:00
|
|
|
|
2020-02-03 18:52:02 +00:00
|
|
|
/** @type {import('@dbgate/types').SqlDialect} */
|
2020-02-02 18:27:25 +00:00
|
|
|
const dialect = {
|
|
|
|
rangeSelect: true,
|
2020-03-12 11:46:07 +00:00
|
|
|
stringEscapeChar: "\\",
|
2020-02-02 18:27:25 +00:00
|
|
|
quoteIdentifier(s) {
|
|
|
|
return '"' + s + '"';
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2020-02-03 18:52:02 +00:00
|
|
|
/** @type {import('@dbgate/types').EngineDriver} */
|
2020-02-02 19:28:27 +00:00
|
|
|
const driver = {
|
2020-02-03 19:34:38 +00:00
|
|
|
async connect(nativeModules, { server, port, user, password, database }) {
|
|
|
|
const client = new nativeModules.pg.Client({ host: server, port, user, password, database: database || 'postgres' });
|
2020-01-04 20:59:53 +00:00
|
|
|
await client.connect();
|
2020-02-03 19:34:38 +00:00
|
|
|
client._nativeModules = nativeModules;
|
2020-01-04 20:59:53 +00:00
|
|
|
return client;
|
|
|
|
},
|
|
|
|
async query(client, sql) {
|
|
|
|
const res = await client.query(sql);
|
2020-02-02 19:28:27 +00:00
|
|
|
return { rows: res.rows, columns: res.fields };
|
2020-01-04 20:59:53 +00:00
|
|
|
},
|
|
|
|
async getVersion(client) {
|
2020-01-25 16:26:51 +00:00
|
|
|
const { rows } = await this.query(client, 'SELECT version()');
|
2020-01-04 20:59:53 +00:00
|
|
|
const { version } = rows[0];
|
|
|
|
return { version };
|
|
|
|
},
|
2020-02-02 19:28:27 +00:00
|
|
|
async analyseFull(pool) {
|
|
|
|
const analyser = new PostgreAnalyser(pool, this);
|
|
|
|
await analyser.runAnalysis();
|
|
|
|
return analyser.result;
|
|
|
|
},
|
|
|
|
createDumper() {
|
|
|
|
return new PostgreDumper(this);
|
|
|
|
},
|
2020-01-04 20:59:53 +00:00
|
|
|
async listDatabases(client) {
|
2020-01-25 16:26:51 +00:00
|
|
|
const { rows } = await this.query(client, 'SELECT datname AS name FROM pg_database WHERE datistemplate = false');
|
|
|
|
return rows;
|
2020-01-04 20:59:53 +00:00
|
|
|
},
|
2020-02-02 18:27:25 +00:00
|
|
|
dialect,
|
2020-01-04 20:59:53 +00:00
|
|
|
};
|
2020-02-02 19:28:27 +00:00
|
|
|
|
|
|
|
module.exports = driver;
|