2021-07-17 07:11:03 +00:00
|
|
|
import { Knex, knex } from 'knex';
|
2022-03-10 06:59:48 +00:00
|
|
|
import {
|
|
|
|
|
cacheConnection,
|
|
|
|
|
getCachedConnection,
|
|
|
|
|
ConnectionTestResult,
|
|
|
|
|
QueryService,
|
|
|
|
|
QueryResult,
|
2022-07-11 11:13:07 +00:00
|
|
|
QueryError,
|
2022-03-10 06:59:48 +00:00
|
|
|
} from '@tooljet-plugins/common';
|
|
|
|
|
import { SourceOptions, QueryOptions } from './types';
|
2021-07-15 09:26:12 +00:00
|
|
|
|
|
|
|
|
export default class MysqlQueryService implements QueryService {
|
2022-01-17 07:08:17 +00:00
|
|
|
private static _instance: MysqlQueryService;
|
|
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
|
if (MysqlQueryService._instance) {
|
|
|
|
|
return MysqlQueryService._instance;
|
|
|
|
|
}
|
2022-02-25 06:53:45 +00:00
|
|
|
|
2022-01-17 07:08:17 +00:00
|
|
|
MysqlQueryService._instance = this;
|
|
|
|
|
return MysqlQueryService._instance;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-21 13:48:28 +00:00
|
|
|
async run(
|
2022-01-24 13:59:21 +00:00
|
|
|
sourceOptions: SourceOptions,
|
|
|
|
|
queryOptions: QueryOptions,
|
2021-09-21 13:48:28 +00:00
|
|
|
dataSourceId: string,
|
|
|
|
|
dataSourceUpdatedAt: string
|
|
|
|
|
): Promise<QueryResult> {
|
2021-08-20 13:56:47 +00:00
|
|
|
let result = {
|
2021-09-21 13:48:28 +00:00
|
|
|
rows: [],
|
2021-08-20 13:56:47 +00:00
|
|
|
};
|
|
|
|
|
let query = '';
|
|
|
|
|
|
2021-09-21 13:48:28 +00:00
|
|
|
if (queryOptions.mode === 'gui') {
|
|
|
|
|
if (queryOptions.operation === 'bulk_update_pkey') {
|
2021-08-20 13:56:47 +00:00
|
|
|
query = await this.buildBulkUpdateQuery(queryOptions);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
query = queryOptions.query;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-03 05:06:56 +00:00
|
|
|
const knexInstance = await this.getConnection(sourceOptions, {}, true, dataSourceId, dataSourceUpdatedAt);
|
2021-07-15 09:26:12 +00:00
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
result = await knexInstance.raw(query);
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.log(err);
|
2022-07-11 11:13:07 +00:00
|
|
|
throw new QueryError('Query could not be completed', err.message, {});
|
2021-07-15 09:26:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
status: 'ok',
|
2021-09-21 13:48:28 +00:00
|
|
|
data: result[0],
|
|
|
|
|
};
|
2021-07-15 09:26:12 +00:00
|
|
|
}
|
2021-07-17 07:59:28 +00:00
|
|
|
|
2022-01-24 13:59:21 +00:00
|
|
|
async testConnection(sourceOptions: SourceOptions): Promise<ConnectionTestResult> {
|
2021-08-03 05:06:56 +00:00
|
|
|
const knexInstance = await this.getConnection(sourceOptions, {}, false);
|
2023-02-20 20:48:14 +00:00
|
|
|
await knexInstance.raw('select @@version;');
|
|
|
|
|
knexInstance.destroy();
|
2021-07-18 07:11:44 +00:00
|
|
|
|
|
|
|
|
return {
|
2021-09-21 13:48:28 +00:00
|
|
|
status: 'ok',
|
|
|
|
|
};
|
2021-07-18 07:11:44 +00:00
|
|
|
}
|
|
|
|
|
|
2022-01-24 13:59:21 +00:00
|
|
|
async buildConnection(sourceOptions: SourceOptions) {
|
2023-08-02 10:19:31 +00:00
|
|
|
// either use socket_path or host/port + ssl
|
|
|
|
|
const props = sourceOptions.socket_path
|
|
|
|
|
? { socketPath: sourceOptions.socket_path }
|
|
|
|
|
: {
|
|
|
|
|
host: sourceOptions.host,
|
|
|
|
|
port: +sourceOptions.port,
|
|
|
|
|
ssl: sourceOptions.ssl_enabled ?? false, // Disabling by default for backward compatibility
|
|
|
|
|
};
|
2021-07-17 07:59:28 +00:00
|
|
|
const config: Knex.Config = {
|
2023-08-10 15:29:04 +00:00
|
|
|
client: 'mysql2',
|
2021-07-17 07:59:28 +00:00
|
|
|
connection: {
|
2023-08-02 10:19:31 +00:00
|
|
|
...props,
|
2021-09-21 13:48:28 +00:00
|
|
|
user: sourceOptions.username,
|
|
|
|
|
password: sourceOptions.password,
|
|
|
|
|
database: sourceOptions.database,
|
2021-09-14 13:07:41 +00:00
|
|
|
multipleStatements: true,
|
2023-08-31 15:59:04 +00:00
|
|
|
...(sourceOptions.ssl_enabled && { ssl: { rejectUnauthorized: false } }),
|
2021-09-21 13:48:28 +00:00
|
|
|
},
|
2021-07-17 07:59:28 +00:00
|
|
|
};
|
2021-08-03 05:06:56 +00:00
|
|
|
|
2021-07-17 07:59:28 +00:00
|
|
|
return knex(config);
|
|
|
|
|
}
|
2021-08-03 05:06:56 +00:00
|
|
|
|
2021-09-21 13:48:28 +00:00
|
|
|
async getConnection(
|
2022-01-24 13:59:21 +00:00
|
|
|
sourceOptions: SourceOptions,
|
2021-09-21 13:48:28 +00:00
|
|
|
options: any,
|
|
|
|
|
checkCache: boolean,
|
|
|
|
|
dataSourceId?: string,
|
|
|
|
|
dataSourceUpdatedAt?: string
|
|
|
|
|
): Promise<any> {
|
|
|
|
|
if (checkCache) {
|
2021-08-03 05:06:56 +00:00
|
|
|
let connection = await getCachedConnection(dataSourceId, dataSourceUpdatedAt);
|
|
|
|
|
|
2021-09-21 13:48:28 +00:00
|
|
|
if (connection) {
|
2021-08-03 05:06:56 +00:00
|
|
|
return connection;
|
|
|
|
|
} else {
|
|
|
|
|
connection = await this.buildConnection(sourceOptions);
|
2022-01-17 07:08:17 +00:00
|
|
|
dataSourceId && cacheConnection(dataSourceId, connection);
|
2021-08-03 05:06:56 +00:00
|
|
|
return connection;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
return await this.buildConnection(sourceOptions);
|
|
|
|
|
}
|
2021-08-20 13:56:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async buildBulkUpdateQuery(queryOptions: any): Promise<string> {
|
|
|
|
|
let queryText = '';
|
|
|
|
|
|
|
|
|
|
const tableName = queryOptions['table'];
|
|
|
|
|
const primaryKey = queryOptions['primary_key_column'];
|
|
|
|
|
const records = queryOptions['records'];
|
|
|
|
|
|
2021-09-21 13:48:28 +00:00
|
|
|
for (const record of records) {
|
2022-03-10 06:59:48 +00:00
|
|
|
const primaryKeyValue = typeof record[primaryKey] === 'string' ? `'${record[primaryKey]}'` : record[primaryKey];
|
2022-02-25 06:53:45 +00:00
|
|
|
|
2021-08-20 13:56:47 +00:00
|
|
|
queryText = `${queryText} UPDATE ${tableName} SET`;
|
|
|
|
|
|
2021-09-21 13:48:28 +00:00
|
|
|
for (const key of Object.keys(record)) {
|
|
|
|
|
if (key !== primaryKey) {
|
2021-08-20 14:26:43 +00:00
|
|
|
queryText = ` ${queryText} ${key} = '${record[key]}',`;
|
2021-08-20 13:56:47 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-20 14:26:43 +00:00
|
|
|
queryText = queryText.slice(0, -1);
|
2022-02-25 06:53:45 +00:00
|
|
|
queryText = `${queryText} WHERE ${primaryKey} = ${primaryKeyValue};`;
|
2021-08-20 13:56:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return queryText.trim();
|
2021-08-03 05:06:56 +00:00
|
|
|
}
|
2021-07-15 09:26:12 +00:00
|
|
|
}
|