feat: add listSchemas to duckdb

This commit is contained in:
Nybkox
2025-04-08 18:07:09 +02:00
parent e7c42f3623
commit 0ece8c7dec
4 changed files with 51 additions and 8 deletions

View File

@@ -231,6 +231,7 @@ function mapConstraintRowToForeignKeyInfo(duckDbConstraintData) {
constraintType: 'foreignKey',
columns: columns,
refTableName: duckDbConstraintData.referenced_table,
refSchemaName: duckDbConstraintData.schema_name,
};
if (duckDbConstraintData.constraint_name != null) {
@@ -363,6 +364,31 @@ function mapIndexRowToIndexInfo(duckDbIndexRow) {
return /** @type {import("dbgate-types").IndexInfo} */ (indexInfo);
}
/**
* @typedef {object} DuckDbSchemaRow
* @property {string} oid
* @property {string} database_name
* @property {string} database_oid
* @property {string} schema_name
* @property {string | null} comment
* @property {{ [key: string]: string } | null} tags
* @property {boolean} internal
* @property {string | null} sql
*/
/**
* @param {DuckDbSchemaRow} duckDbSchemaRow
* @returns {import("dbgate-types").SchemaInfo}
*/
function mapSchemaRowToSchemaInfo(duckDbSchemaRow) {
const schemaInfo = {
schemaName: duckDbSchemaRow.schema_name,
objectId: duckDbSchemaRow.oid,
};
return /** @type {import("dbgate-types").SchemaInfo} */ (schemaInfo);
}
module.exports = {
mapRawTableToTableInfo,
mapRawColumnToColumnInfo,
@@ -371,4 +397,5 @@ module.exports = {
mapConstraintRowToUniqueInfo,
mapViewRowToViewInfo,
mapIndexRowToIndexInfo,
mapSchemaRowToSchemaInfo,
};

View File

@@ -5,6 +5,8 @@ const Analyser = require('./Analyser');
const driverBase = require('../frontend/driver');
const { getLogger, extractErrorLogData, createBulkInsertStreamBase } = require('dbgate-tools');
const { getColumnsInfo, normalizeRow } = require('./helpers');
const sql = require('./sql');
const { mapSchemaRowToSchemaInfo } = require('./Analyser.helpers');
const logger = getLogger('sqliteDriver');
@@ -111,11 +113,13 @@ const driver = {
options.done();
}
},
async script(dbhan, sql) {
const dmp1 = driver.createDumper();
dmp1.beginTransaction();
async script(dbhan, sql, { useTransaction } = { useTransaction: false }) {
if (useTransaction) {
const dmp1 = driver.createDumper();
dmp1.beginTransaction();
await dbhan.client.run(dmp1.s);
await dbhan.client.run(dmp1.s);
}
const statements = await dbhan.client.extractStatements(sql);
const count = statements.count;
@@ -125,10 +129,12 @@ const driver = {
await stmt.run();
}
const dmp2 = driver.createDumper();
dmp2.commitTransaction();
if (useTransaction) {
const dmp2 = driver.createDumper();
dmp2.commitTransaction();
await dbhan.client.run(dmp2.s);
await dbhan.client.run(dmp2.s);
}
},
async readQuery(dbhan, sql, structure) {
@@ -168,13 +174,20 @@ const driver = {
},
async getVersion(dbhan) {
const { rows } = await this.query(dbhan, 'SELECT version() AS version;');
const { version } = rows[0];
const { version } = rows?.[0];
return {
version,
versionText: `DuchDB ${version}`,
};
},
async listSchemas(dbhan) {
const schemasResult = await this.query(dbhan, sql.schemas);
const schemas = schemasResult.rows?.map(mapSchemaRowToSchemaInfo);
return schemas ?? null;
},
};
module.exports = driver;

View File

@@ -5,6 +5,7 @@ const primaryKeys = require('./primaryKeys.js');
const indexes = require('./indexes.js');
const uniques = require('./uniques.js');
const views = require('./views.js');
const schemas = require('./schemas.js');
module.exports = {
tables,
@@ -14,4 +15,5 @@ module.exports = {
indexes,
uniques,
views,
schemas,
};

View File

@@ -0,0 +1 @@
module.exports = `SELECT * FROM duckdb_schemas() WHERE internal = false`;