mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-18 06:46:00 +00:00
WIP
This commit is contained in:
60
plugins/dbgate-plugin-cassandra/src/backend/Analyser.js
Normal file
60
plugins/dbgate-plugin-cassandra/src/backend/Analyser.js
Normal file
@@ -0,0 +1,60 @@
|
||||
const { DatabaseAnalyser } = global.DBGATE_PACKAGES['dbgate-tools'];
|
||||
const sql = require('./sql');
|
||||
|
||||
class Analyser extends DatabaseAnalyser {
|
||||
constructor(connection, driver) {
|
||||
super(connection, driver);
|
||||
}
|
||||
|
||||
createQuery(resFileName, typeFields, replacements = {}) {
|
||||
let res = sql[resFileName];
|
||||
res = res.replace('#DATABASE#', this.dbhan.database);
|
||||
return super.createQuery(res, typeFields, replacements);
|
||||
}
|
||||
|
||||
async _runAnalysis() {
|
||||
this.feedback({ analysingMessage: 'Loading tables' });
|
||||
const tables = await this.analyserQuery('tables', ['tables']);
|
||||
this.feedback({ analysingMessage: 'Loading columns' });
|
||||
const columns = await this.analyserQuery('columns', ['tables']);
|
||||
// this.feedback({ analysingMessage: 'Loading views' });
|
||||
// const views = await this.analyserQuery('views', ['views']);
|
||||
|
||||
const res = {
|
||||
tables: tables.rows.map((table) => {
|
||||
const tableColumns = columns.rows.filter((col) => col.pureName == table.pureName);
|
||||
const pkColumns = tableColumns.filter((i) => i.kind === 'partition_key' || i.kind === 'clustering');
|
||||
|
||||
return {
|
||||
...table,
|
||||
primaryKeyColumns: pkColumns,
|
||||
columns: tableColumns,
|
||||
primaryKey: pkColumns.length ? { columns: pkColumns } : null,
|
||||
foreignKeys: [],
|
||||
};
|
||||
}),
|
||||
views: [],
|
||||
functions: [],
|
||||
triggers: [],
|
||||
};
|
||||
this.feedback({ analysingMessage: null });
|
||||
return res;
|
||||
}
|
||||
|
||||
async singleObjectAnalysis(dbhan, typeField) {
|
||||
const structure = await this._runAnalysis(dbhan, typeField);
|
||||
const item = structure[typeField]?.find((i) => i.pureName === dbhan.pureName);
|
||||
return item;
|
||||
}
|
||||
|
||||
// async _computeSingleObjectId() {
|
||||
// const { pureName } = this.singleObjectFilter;
|
||||
// const resId = await this.driver.query(
|
||||
// this.dbhan,
|
||||
// `SELECT uuid as id FROM system.tables WHERE database = '${this.dbhan.database}' AND name='${pureName}'`
|
||||
// );
|
||||
// this.singleObjectId = resId.rows[0]?.id;
|
||||
// }
|
||||
}
|
||||
|
||||
module.exports = Analyser;
|
||||
@@ -0,0 +1,84 @@
|
||||
const { createBulkInsertStreamBase } = global.DBGATE_PACKAGES['dbgate-tools'];
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {import('dbgate-types').TableInfo} tableInfo
|
||||
* @param {string} columnName
|
||||
* @returns {{columnName: string, dataType: string} | null}
|
||||
*/
|
||||
function getColumnInfo(tableInfo, columnName) {
|
||||
const column = tableInfo.columns.find((x) => x.columnName == columnName);
|
||||
if (!column) return null;
|
||||
|
||||
return {
|
||||
columnName,
|
||||
dataType: column.dataType,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {string} tableName
|
||||
* @returns {import('dbgate-types').TableInfo | null}
|
||||
*/
|
||||
|
||||
/**
|
||||
* @param {string} tableName
|
||||
* @returns {{ shouldAddUuidPk: true, pkColumnName: string } | { shouldAddUuidPk: false }}
|
||||
*/
|
||||
function getShouldAddUuidPkInfo(tableInfo) {
|
||||
const pkColumnName = tableInfo.primaryKey?.columns[0]?.columnName;
|
||||
if (!pkColumnName) return { shouldAddUuidPk: true, pkColumnName: 'id' };
|
||||
|
||||
const columnInfo = getColumnInfo(tableInfo, pkColumnName);
|
||||
if (!columnInfo || columnInfo.dataType.toLowerCase() !== 'uuid') return { shouldAddUuidPk: false };
|
||||
|
||||
const shouldAddUuidPk = writable.columnNames.every((i) => i !== columnInfo.columnName);
|
||||
if (!shouldAddUuidPk) return { shouldAddUuidPk };
|
||||
|
||||
return { shouldAddUuidPk, pkColumnName };
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {import('dbgate-types').EngineDriver<import('cassandra-driver').Client>} driver
|
||||
* @param {import('stream')} stream
|
||||
* @param {import('dbgate-types').DatabaseHandle<import('cassandra-driver').Client>} dbhan
|
||||
* @param {import('dbgate-types').NamedObjectInfo} name
|
||||
* @param {import('dbgate-types').WriteTableOptions} option
|
||||
*/
|
||||
function createCassandraBulkInsertStream(driver, stream, dbhan, name, options) {
|
||||
const writable = createBulkInsertStreamBase(driver, stream, dbhan, name, options);
|
||||
|
||||
writable.send = async () => {
|
||||
const { shouldAddUuidPk, pkColumnName } = getShouldAddUuidPkInfo(writable.structure);
|
||||
|
||||
const rows = writable.buffer;
|
||||
const fullNameQuoted = writable.fullNameQuoted;
|
||||
writable.buffer = [];
|
||||
|
||||
for (const row of rows) {
|
||||
const dmp = driver.createDumper();
|
||||
dmp.putRaw(`INSERT INTO ${fullNameQuoted} (`);
|
||||
if (shouldAddUuidPk) {
|
||||
dmp.putRaw(driver.dialect.quoteIdentifier(pkColumnName));
|
||||
dmp.putRaw(', ');
|
||||
}
|
||||
dmp.putCollection(',', writable.columnNames, (col) => dmp.putRaw(driver.dialect.quoteIdentifier(col)));
|
||||
dmp.putRaw(')\n VALUES\n');
|
||||
|
||||
dmp.putRaw('(');
|
||||
if (shouldAddUuidPk) {
|
||||
dmp.putRaw('uuid()');
|
||||
dmp.putRaw(', ');
|
||||
}
|
||||
dmp.putCollection(',', writable.columnNames, (col) => dmp.putValue(row[col]));
|
||||
dmp.putRaw(')');
|
||||
await driver.query(dbhan, dmp.s, { discardResult: true });
|
||||
}
|
||||
};
|
||||
|
||||
return writable;
|
||||
}
|
||||
|
||||
module.exports = createCassandraBulkInsertStream;
|
||||
162
plugins/dbgate-plugin-cassandra/src/backend/driver.js
Normal file
162
plugins/dbgate-plugin-cassandra/src/backend/driver.js
Normal file
@@ -0,0 +1,162 @@
|
||||
const _ = require('lodash');
|
||||
const stream = require('stream');
|
||||
const driverBase = require('../frontend/driver');
|
||||
const Analyser = require('./Analyser');
|
||||
const cassandra = require('cassandra-driver');
|
||||
const createCassandraBulkInsertStream = require('./createBulkInsertStream.js');
|
||||
|
||||
function getTypeName(code) {
|
||||
return Object.keys(cassandra.types.dataTypes).find((key) => cassandra.types.dataTypes[key] === code);
|
||||
}
|
||||
|
||||
/** @type {import('dbgate-types').EngineDriver<cassandra.Client>} */
|
||||
const driver = {
|
||||
...driverBase,
|
||||
analyserClass: Analyser,
|
||||
// creating connection
|
||||
async connect({ server, port, user, password, database, localDataCenter, useDatabaseUrl, databaseUrl }) {
|
||||
console.log('#conn', localDataCenter);
|
||||
const client = new cassandra.Client({
|
||||
// port,
|
||||
// user,
|
||||
// password,
|
||||
contactPoints: server.split(','),
|
||||
localDataCenter: localDataCenter ?? this.defaultLocalDataCenter,
|
||||
keyspace: database,
|
||||
});
|
||||
|
||||
client.connect();
|
||||
|
||||
return {
|
||||
client,
|
||||
database,
|
||||
};
|
||||
},
|
||||
|
||||
// called for retrieve data (eg. browse in data grid) and for update database
|
||||
async query(dbhan, query, options) {
|
||||
const offset = options?.range?.offset;
|
||||
if (options?.discardResult) {
|
||||
await dbhan.client.execute(query);
|
||||
return {
|
||||
rows: [],
|
||||
columns: [],
|
||||
};
|
||||
}
|
||||
const result = await dbhan.client.execute(query);
|
||||
if (!result.rows?.[0]) {
|
||||
return {
|
||||
rows: [],
|
||||
columns: [],
|
||||
};
|
||||
}
|
||||
|
||||
const columns = result.columns.map(({ name, type: { code } }) => ({
|
||||
columnName: name,
|
||||
dataType: getTypeName(code),
|
||||
}));
|
||||
|
||||
return {
|
||||
rows: offset ? result.rows.slice(offset) : result.rows,
|
||||
columns,
|
||||
};
|
||||
},
|
||||
// called in query console
|
||||
async stream(dbhan, query, options) {
|
||||
try {
|
||||
if (!query.match(/^\s*SELECT/i)) {
|
||||
await dbhan.client.execute(query);
|
||||
options.done();
|
||||
return;
|
||||
}
|
||||
|
||||
const strm = dbhan.client.stream(query);
|
||||
|
||||
strm.on('readable', () => {
|
||||
let row;
|
||||
while ((row = strm.read())) {
|
||||
options.row(row);
|
||||
}
|
||||
});
|
||||
|
||||
strm.on('end', () => {
|
||||
options.done();
|
||||
});
|
||||
|
||||
strm.on('error', (err) => {
|
||||
options.info({
|
||||
message: err.toString(),
|
||||
time: new Date(),
|
||||
severity: 'error',
|
||||
});
|
||||
options.done();
|
||||
});
|
||||
} catch (err) {
|
||||
const mLine = err.message.match(/\(line (\d+)\,/);
|
||||
let line = undefined;
|
||||
if (mLine) {
|
||||
line = parseInt(mLine[1]) - 1;
|
||||
}
|
||||
|
||||
options.info({
|
||||
message: err.message,
|
||||
time: new Date(),
|
||||
severity: 'error',
|
||||
line,
|
||||
});
|
||||
options.done();
|
||||
}
|
||||
},
|
||||
// called when exporting table or view
|
||||
async readQuery(dbhan, query, structure) {
|
||||
const pass = new stream.PassThrough({
|
||||
objectMode: true,
|
||||
highWaterMark: 100,
|
||||
});
|
||||
|
||||
const strm = dbhan.client.stream(query);
|
||||
|
||||
strm.on('readable', () => {
|
||||
let row;
|
||||
while ((row = strm.read())) {
|
||||
pass.write(row);
|
||||
}
|
||||
});
|
||||
|
||||
strm.on('end', () => {
|
||||
pass.end();
|
||||
});
|
||||
|
||||
strm.on('error', (err) => {
|
||||
pass.info({
|
||||
message: err.toString(),
|
||||
time: new Date(),
|
||||
severity: 'error',
|
||||
});
|
||||
pass.end();
|
||||
});
|
||||
|
||||
return pass;
|
||||
},
|
||||
async writeTable(dbhan, name, options) {
|
||||
return createCassandraBulkInsertStream(this, stream, dbhan, name, options);
|
||||
},
|
||||
// detect server version
|
||||
async getVersion(dbhan) {
|
||||
const result = await dbhan.client.execute('SELECT release_version from system.local');
|
||||
return { version: result.rows[0].release_version };
|
||||
},
|
||||
// list databases on server
|
||||
async listDatabases(dbhan) {
|
||||
const result = await dbhan.client.execute(
|
||||
"SELECT keyspace_name FROM system_schema.keyspaces WHERE keyspace_name >= 'system' ALLOW FILTERING"
|
||||
);
|
||||
return result.rows.map((row) => ({ name: row.keyspace_name }));
|
||||
},
|
||||
|
||||
async close(dbhan) {
|
||||
return dbhan.client.shutdown();
|
||||
},
|
||||
};
|
||||
|
||||
module.exports = driver;
|
||||
6
plugins/dbgate-plugin-cassandra/src/backend/index.js
Normal file
6
plugins/dbgate-plugin-cassandra/src/backend/index.js
Normal file
@@ -0,0 +1,6 @@
|
||||
const driver = require('./driver');
|
||||
|
||||
module.exports = {
|
||||
packageName: 'dbgate-plugin-cassandra',
|
||||
drivers: [driver],
|
||||
};
|
||||
@@ -0,0 +1,9 @@
|
||||
module.exports = `
|
||||
SELECT
|
||||
table_name as "pureName",
|
||||
column_name as "columnName",
|
||||
type as "dataType",
|
||||
kind as "kind"
|
||||
FROM system_schema.columns
|
||||
WHERE keyspace_name = '#DATABASE#'
|
||||
`;
|
||||
9
plugins/dbgate-plugin-cassandra/src/backend/sql/index.js
Normal file
9
plugins/dbgate-plugin-cassandra/src/backend/sql/index.js
Normal file
@@ -0,0 +1,9 @@
|
||||
const columns = require('./columns');
|
||||
const tables = require('./tables');
|
||||
const views = require('./views');
|
||||
|
||||
module.exports = {
|
||||
columns,
|
||||
tables,
|
||||
views,
|
||||
};
|
||||
@@ -0,0 +1,5 @@
|
||||
module.exports = `
|
||||
SELECT table_name as "pureName"
|
||||
FROM system_schema.tables
|
||||
WHERE keyspace_name='#DATABASE#';
|
||||
`;
|
||||
10
plugins/dbgate-plugin-cassandra/src/backend/sql/views.js
Normal file
10
plugins/dbgate-plugin-cassandra/src/backend/sql/views.js
Normal file
@@ -0,0 +1,10 @@
|
||||
module.exports = `
|
||||
select
|
||||
tables.name as "pureName",
|
||||
tables.uuid as "objectId",
|
||||
views.view_definition as "viewDefinition",
|
||||
tables.metadata_modification_time as "contentHash"
|
||||
from information_schema.views
|
||||
inner join system.tables on views.table_name = tables.name and views.table_schema = tables.database
|
||||
where views.table_schema='#DATABASE#' and tables.uuid =OBJECT_ID_CONDITION
|
||||
`;
|
||||
27
plugins/dbgate-plugin-cassandra/src/frontend/Dumper.js
Normal file
27
plugins/dbgate-plugin-cassandra/src/frontend/Dumper.js
Normal file
@@ -0,0 +1,27 @@
|
||||
/**
|
||||
* @type {{ SqlDumper: import('dbgate-types').SqlDumper}}
|
||||
*/
|
||||
const { SqlDumper } = global.DBGATE_PACKAGES['dbgate-tools'];
|
||||
|
||||
class Dumper extends SqlDumper {
|
||||
/**
|
||||
* @param {import('dbgate-types').ColumnInfo} column
|
||||
* @param {string} newName
|
||||
*
|
||||
* @returns {void}
|
||||
*/
|
||||
renameColumn(column, newName) {
|
||||
this.putCmd('^alter ^table %f ^rename %i ^to %i', column, column.columnName, newName);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {import('dbgate-types').ColumnInfo} column
|
||||
*
|
||||
* @returns {void}
|
||||
*/
|
||||
dropColumn(column) {
|
||||
this.putCmd('^alter ^table %f ^drop %i', column, column.columnName);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Dumper;
|
||||
117
plugins/dbgate-plugin-cassandra/src/frontend/driver.js
Normal file
117
plugins/dbgate-plugin-cassandra/src/frontend/driver.js
Normal file
@@ -0,0 +1,117 @@
|
||||
const { driverBase } = global.DBGATE_PACKAGES['dbgate-tools'];
|
||||
const Dumper = require('./Dumper');
|
||||
const { mysqlSplitterOptions } = require('dbgate-query-splitter/lib/options');
|
||||
const _cloneDeepWith = require('lodash/cloneDeepWith');
|
||||
|
||||
/** @type {import('dbgate-types').SqlDialect} */
|
||||
const dialect = {
|
||||
limitSelect: true,
|
||||
rangeSelect: true,
|
||||
rawUuids: true,
|
||||
stringEscapeChar: "'",
|
||||
fallbackDataType: 'varchar',
|
||||
offsetNotSupported: true,
|
||||
allowMultipleValuesInsert: false,
|
||||
createColumn: true,
|
||||
dropColumn: true,
|
||||
changeColumn: true,
|
||||
changeAutoIncrement: true,
|
||||
createIndex: true,
|
||||
dropIndex: true,
|
||||
anonymousPrimaryKey: true,
|
||||
createColumnWithColumnKeyword: true,
|
||||
specificNullabilityImplementation: true,
|
||||
omitForeignKeys: true,
|
||||
omitUniqueConstraints: true,
|
||||
omitIndexes: true,
|
||||
omitTableAliases: true,
|
||||
omitTableBeforeColumn: true,
|
||||
sortingKeys: true,
|
||||
predefinedDataTypes: [
|
||||
'custom',
|
||||
'ascii',
|
||||
'bigint',
|
||||
'blob',
|
||||
'boolean',
|
||||
'counter',
|
||||
'decimal',
|
||||
'double',
|
||||
'float',
|
||||
'int',
|
||||
'text',
|
||||
'timestamp',
|
||||
'uuid',
|
||||
'varchar',
|
||||
'varint',
|
||||
'timeuuid',
|
||||
'inet',
|
||||
'date',
|
||||
'time',
|
||||
'smallint',
|
||||
'tinyint',
|
||||
'duration',
|
||||
'list',
|
||||
'map',
|
||||
'set',
|
||||
'udt',
|
||||
'tuple',
|
||||
],
|
||||
disableAutoIncrement: true,
|
||||
disableNonPrimaryKeyRename: true,
|
||||
defaultNewTableColumns: [
|
||||
{
|
||||
columnName: 'id',
|
||||
dataType: 'uuid',
|
||||
notNull: true,
|
||||
},
|
||||
],
|
||||
columnProperties: {
|
||||
columnComment: true,
|
||||
},
|
||||
|
||||
quoteIdentifier(s) {
|
||||
return `"${s}"`;
|
||||
},
|
||||
};
|
||||
|
||||
/** @type {import('dbgate-types').EngineDriver} */
|
||||
const driver = {
|
||||
...driverBase,
|
||||
supportsTransactions: false,
|
||||
defaultPort: 9042,
|
||||
defaultLocalDataCenter: 'datacenter1',
|
||||
dumperClass: Dumper,
|
||||
dialect,
|
||||
engine: 'cassandra@dbgate-plugin-cassandra',
|
||||
title: 'Cassandra',
|
||||
showConnectionField: (field, values) =>
|
||||
['server', 'port', 'singleDatabase', 'localDataCenter', 'isReadOnly', 'user', 'password'].includes(field),
|
||||
getQuerySplitterOptions: (usage) =>
|
||||
usage == 'editor'
|
||||
? { ...mysqlSplitterOptions, ignoreComments: true, preventSingleLineSplit: true }
|
||||
: mysqlSplitterOptions,
|
||||
adaptTableInfo(table) {
|
||||
if (!table.primaryKey && !table.sortingKey) {
|
||||
return {
|
||||
...table,
|
||||
primaryKey: {
|
||||
columns: [
|
||||
{
|
||||
columnName: 'id',
|
||||
},
|
||||
],
|
||||
},
|
||||
columns: [
|
||||
{
|
||||
columnName: 'id',
|
||||
dataType: 'uuid',
|
||||
},
|
||||
...table.columns,
|
||||
],
|
||||
};
|
||||
}
|
||||
return table;
|
||||
},
|
||||
};
|
||||
|
||||
module.exports = driver;
|
||||
6
plugins/dbgate-plugin-cassandra/src/frontend/index.js
Normal file
6
plugins/dbgate-plugin-cassandra/src/frontend/index.js
Normal file
@@ -0,0 +1,6 @@
|
||||
import driver from './driver';
|
||||
|
||||
export default {
|
||||
packageName: 'dbgate-plugin-cassandra',
|
||||
drivers: [driver],
|
||||
};
|
||||
Reference in New Issue
Block a user