db handle

This commit is contained in:
Jan Prochazka
2024-09-19 18:53:12 +02:00
parent 31dfc1dc28
commit dadde225f1
2 changed files with 18 additions and 16 deletions

View File

@@ -57,7 +57,7 @@ class Analyser extends DatabaseAnalyser {
createQuery(resFileName, typeFields, replacements = {}) { createQuery(resFileName, typeFields, replacements = {}) {
const query = super.createQuery(sql[resFileName], typeFields, replacements); const query = super.createQuery(sql[resFileName], typeFields, replacements);
const dbname = this.pool.__dbgate_database_name__; const dbname = this.pool.database;
const schemaCondition = isCompositeDbName(dbname) const schemaCondition = isCompositeDbName(dbname)
? `= '${splitCompositeDbName(dbname).schema}' ` ? `= '${splitCompositeDbName(dbname).schema}' `
: ' IS NOT NULL '; : ' IS NOT NULL ';

View File

@@ -90,24 +90,26 @@ const drivers = driverBases.map(driverBase => ({
await this.query(client, 'SET SESSION CHARACTERISTICS AS TRANSACTION READ ONLY'); await this.query(client, 'SET SESSION CHARACTERISTICS AS TRANSACTION READ ONLY');
} }
// client.__dbgate_database_name__ = database; return {
return client; client,
database,
};
}, },
async close(pool) { async close(handle) {
return pool.end(); return handle.client.end();
}, },
async query(client, sql) { async query(handle, sql) {
if (sql == null) { if (sql == null) {
return { return {
rows: [], rows: [],
columns: [], columns: [],
}; };
} }
const res = await client.query({ text: sql, rowMode: 'array' }); const res = await handle.client.query({ text: sql, rowMode: 'array' });
const columns = extractPostgresColumns(res); const columns = extractPostgresColumns(res);
return { rows: (res.rows || []).map(row => zipDataRow(row, columns)), columns }; return { rows: (res.rows || []).map(row => zipDataRow(row, columns)), columns };
}, },
stream(client, sql, options) { stream(handle, sql, options) {
const query = new pg.Query({ const query = new pg.Query({
text: sql, text: sql,
rowMode: 'array', rowMode: 'array',
@@ -166,10 +168,10 @@ const drivers = driverBases.map(driverBase => ({
options.done(); options.done();
}); });
client.query(query); handle.client.query(query);
}, },
async getVersion(client) { async getVersion(handle) {
const { rows } = await this.query(client, 'SELECT version()'); const { rows } = await this.query(handle, 'SELECT version()');
const { version } = rows[0]; const { version } = rows[0];
const isCockroach = version.toLowerCase().includes('cockroachdb'); const isCockroach = version.toLowerCase().includes('cockroachdb');
@@ -252,8 +254,8 @@ const drivers = driverBases.map(driverBase => ({
// @ts-ignore // @ts-ignore
return createBulkInsertStreamBase(this, stream, pool, name, options); return createBulkInsertStreamBase(this, stream, pool, name, options);
}, },
async listDatabases(client) { async listDatabases(handle) {
const { rows } = await this.query(client, 'SELECT datname AS name FROM pg_database WHERE datistemplate = false'); const { rows } = await this.query(handle, 'SELECT datname AS name FROM pg_database WHERE datistemplate = false');
return rows; return rows;
}, },
@@ -270,12 +272,12 @@ const drivers = driverBases.map(driverBase => ({
]; ];
}, },
async listSchemas(pool) { async listSchemas(handle) {
const schemaRows = await this.query( const schemaRows = await this.query(
pool, handle,
'select oid as "object_id", nspname as "schema_name" from pg_catalog.pg_namespace' 'select oid as "object_id", nspname as "schema_name" from pg_catalog.pg_namespace'
); );
const defaultSchemaRows = await this.query(pool, 'SHOW SEARCH_PATH;'); const defaultSchemaRows = await this.query(handle, 'SHOW SEARCH_PATH;');
const searchPath = defaultSchemaRows.rows[0]?.search_path?.replace('"$user",', '')?.trim(); const searchPath = defaultSchemaRows.rows[0]?.search_path?.replace('"$user",', '')?.trim();
const schemas = schemaRows.rows.map(x => ({ const schemas = schemaRows.rows.map(x => ({