ToolJet/plugins/packages/mssql/lib/index.ts

554 lines
18 KiB
TypeScript
Raw Normal View History

import { Knex, knex } from 'knex';
import {
ConnectionTestResult,
QueryError,
QueryResult,
QueryService,
2025-05-27 11:23:22 +00:00
cacheConnectionWithConfiguration,
generateSourceOptionsHash,
getCachedConnection,
User,
App,
} from '@tooljet-plugins/common';
import { SourceOptions, QueryOptions } from './types';
import { isEmpty } from '@tooljet-plugins/common';
import { Client } from 'ssh2';
import net from 'net';
interface SSHTunnel {
client: Client;
server: net.Server;
localPort: number;
}
2025-02-25 06:52:50 +00:00
const recognizedBooleans = {
true: true,
false: false,
};
function interpretValue(value: string): string | boolean | number {
return recognizedBooleans[value.toLowerCase()] ?? (!isNaN(Number.parseInt(value)) ? Number.parseInt(value) : value);
}
function createSSHTunnel(sourceOptions: SourceOptions): Promise<SSHTunnel> {
return new Promise((resolve, reject) => {
const sshClient = new Client();
sshClient.on('ready', () => {
const server = net.createServer(socket => {
sshClient.forwardOut(
socket.remoteAddress || '127.0.0.1',
socket.remotePort || 0,
sourceOptions.host,
Number(sourceOptions.port),
(err, stream) => {
if (err) {
socket.destroy();
return;
}
socket.pipe(stream);
stream.pipe(socket);
}
);
});
server.on('error', err => {
sshClient.end();
reject(err);
});
server.listen(0, '127.0.0.1', () => {
const { port } = server.address() as net.AddressInfo;
resolve({ client: sshClient, server, localPort: port });
});
});
sshClient.on('error', reject);
sshClient.connect({
host: sourceOptions.ssh_host,
port: sourceOptions.ssh_port || 22,
username: sourceOptions.ssh_username,
readyTimeout: 20000,
keepaliveInterval: 10000,
...(sourceOptions.ssh_auth_type === 'password'
? { password: sourceOptions.ssh_password }
: {
privateKey: sourceOptions.ssh_private_key,
...(sourceOptions.ssh_passphrase
? { passphrase: sourceOptions.ssh_passphrase }
: {}),
}),
});
});
}
2021-07-17 07:11:03 +00:00
export default class MssqlQueryService implements QueryService {
Move plugins to root (#1728) * feat: move plugins to root * modify tsconfig * add .gitignore * delete old plugins file * add parcel * docker compose volume mount * add gcs * add typescript to plugins folder * gcs to ts * add dynamodb * add elastic search * add firestore * add gsheets * add graphql * add mongodb * mssql * add mysql * add postgresql * add redis * add s3 * add slack * add stripe * remove plugin related packages from pkgjson * add lib folder * add gitignore * remove typescript generated files * remove generated file * remove generated files * add twilio * add dist to docker compose cache binding * add dist prefix * cleanup - 1 * delete dist * rename to index.ts + add jest config * add it.todo in tests * test fixes * test file changes * fix type checks * add @tooljet/plugins to server package json * esm vs commonjs bug, reduce got to 11.8.2 from 12.0.0 * docker file npm package version fix * add typesense * cleaup - 2 * add sendgrid * add lerna build and clean script for all packages + tsconfig * cleanup -3 * add plugins build step * add missing plugins build step in npm run build * add mssql, mysql & postgres as singleton classes * add db connection to cache only if datasourceId is available * client: add data source schema/manifest files * add query operations files * logic for wrapping form with schema * add script to create index file * add @tooljet/plugins to frontend folder * cleanup 1 -frontend * cleanup - 2 // frontend // data queries * add client and index to gitignore * update gitignore * fix lint & test * update ci * fix unit, e2e * cleanup -3 * fix test * fix tests * fix indent * try npm ci * fix tests * fix typo * fix * rename file for server entry * heroku fix * add main and types entry points in pkg json * move common to root * cleanup - 4: remove redundant $ sign prefix * cleanup - 4: remove redundant $ sign prefix * update options in-sync before DOM is painted * change type cloud to cloud storage * update readme * update ci.yml * update ci yml * add pkg-lock.json * rename index.ts to server.ts * update lock files * add server package.lock * remove unused import * revert commit: add minio * add root dep * import server.ts * remove plugins build step * add npm shrinkwrap * update version - plugins * add new version - 0.0.8 * upgrade version * move to symlinked package * add lock file * feat: add icon inside package * add plugin creation docs * Remove seed * move icons to plugins folder * install pg dep * add react to packages * add seed cmd * revert change * add plugins build in lint, e2e, unit * e2e, lint use npm ci * update dockerfile for plugins * try combining release with web * limit memory on release * try executing seed script post transpile * try executing seed from server directory * update seed execution * add minio * add correct type * add minio to pkg json * remove old file * fix provider key * add python installable + npm ^7.2.0 (#1752) * add python installable + npm ^7.2.0 * add py to prod file * pin npm version to 7.20.0 * pin npm version to 7.20.0 * split into multi stage build and remove python for buildx * copy plugins from buider stage * update dependencies * add freetds dependency * update server dockerfile * update client dockerfile * update dev dockerfile and compose file * fix entrypoint * fix server dev dockerfile * update docker-compose * remove npm install on root dir on docker build * fix heroku script * make lerna prod dependency to enable prod builds * remove redundant env setup Co-authored-by: Akshay Sasidharan <akshaysasidharan93@gmail.com> Co-authored-by: navaneeth <navaneethpk@outlook.com>
2022-01-17 07:08:17 +00:00
private static _instance: MssqlQueryService;
private STATEMENT_TIMEOUT;
Move plugins to root (#1728) * feat: move plugins to root * modify tsconfig * add .gitignore * delete old plugins file * add parcel * docker compose volume mount * add gcs * add typescript to plugins folder * gcs to ts * add dynamodb * add elastic search * add firestore * add gsheets * add graphql * add mongodb * mssql * add mysql * add postgresql * add redis * add s3 * add slack * add stripe * remove plugin related packages from pkgjson * add lib folder * add gitignore * remove typescript generated files * remove generated file * remove generated files * add twilio * add dist to docker compose cache binding * add dist prefix * cleanup - 1 * delete dist * rename to index.ts + add jest config * add it.todo in tests * test fixes * test file changes * fix type checks * add @tooljet/plugins to server package json * esm vs commonjs bug, reduce got to 11.8.2 from 12.0.0 * docker file npm package version fix * add typesense * cleaup - 2 * add sendgrid * add lerna build and clean script for all packages + tsconfig * cleanup -3 * add plugins build step * add missing plugins build step in npm run build * add mssql, mysql & postgres as singleton classes * add db connection to cache only if datasourceId is available * client: add data source schema/manifest files * add query operations files * logic for wrapping form with schema * add script to create index file * add @tooljet/plugins to frontend folder * cleanup 1 -frontend * cleanup - 2 // frontend // data queries * add client and index to gitignore * update gitignore * fix lint & test * update ci * fix unit, e2e * cleanup -3 * fix test * fix tests * fix indent * try npm ci * fix tests * fix typo * fix * rename file for server entry * heroku fix * add main and types entry points in pkg json * move common to root * cleanup - 4: remove redundant $ sign prefix * cleanup - 4: remove redundant $ sign prefix * update options in-sync before DOM is painted * change type cloud to cloud storage * update readme * update ci.yml * update ci yml * add pkg-lock.json * rename index.ts to server.ts * update lock files * add server package.lock * remove unused import * revert commit: add minio * add root dep * import server.ts * remove plugins build step * add npm shrinkwrap * update version - plugins * add new version - 0.0.8 * upgrade version * move to symlinked package * add lock file * feat: add icon inside package * add plugin creation docs * Remove seed * move icons to plugins folder * install pg dep * add react to packages * add seed cmd * revert change * add plugins build in lint, e2e, unit * e2e, lint use npm ci * update dockerfile for plugins * try combining release with web * limit memory on release * try executing seed script post transpile * try executing seed from server directory * update seed execution * add minio * add correct type * add minio to pkg json * remove old file * fix provider key * add python installable + npm ^7.2.0 (#1752) * add python installable + npm ^7.2.0 * add py to prod file * pin npm version to 7.20.0 * pin npm version to 7.20.0 * split into multi stage build and remove python for buildx * copy plugins from buider stage * update dependencies * add freetds dependency * update server dockerfile * update client dockerfile * update dev dockerfile and compose file * fix entrypoint * fix server dev dockerfile * update docker-compose * remove npm install on root dir on docker build * fix heroku script * make lerna prod dependency to enable prod builds * remove redundant env setup Co-authored-by: Akshay Sasidharan <akshaysasidharan93@gmail.com> Co-authored-by: navaneeth <navaneethpk@outlook.com>
2022-01-17 07:08:17 +00:00
constructor() {
this.STATEMENT_TIMEOUT =
process.env?.PLUGINS_SQL_DB_STATEMENT_TIMEOUT && !isNaN(Number(process.env?.PLUGINS_SQL_DB_STATEMENT_TIMEOUT))
? Number(process.env.PLUGINS_SQL_DB_STATEMENT_TIMEOUT)
: 120000;
2025-02-25 06:52:50 +00:00
if (!MssqlQueryService._instance) {
MssqlQueryService._instance = this;
process.on('uncaughtException', (err: any) => {
if (err?.code === 'ESOCKET') return;
throw err;
});
Move plugins to root (#1728) * feat: move plugins to root * modify tsconfig * add .gitignore * delete old plugins file * add parcel * docker compose volume mount * add gcs * add typescript to plugins folder * gcs to ts * add dynamodb * add elastic search * add firestore * add gsheets * add graphql * add mongodb * mssql * add mysql * add postgresql * add redis * add s3 * add slack * add stripe * remove plugin related packages from pkgjson * add lib folder * add gitignore * remove typescript generated files * remove generated file * remove generated files * add twilio * add dist to docker compose cache binding * add dist prefix * cleanup - 1 * delete dist * rename to index.ts + add jest config * add it.todo in tests * test fixes * test file changes * fix type checks * add @tooljet/plugins to server package json * esm vs commonjs bug, reduce got to 11.8.2 from 12.0.0 * docker file npm package version fix * add typesense * cleaup - 2 * add sendgrid * add lerna build and clean script for all packages + tsconfig * cleanup -3 * add plugins build step * add missing plugins build step in npm run build * add mssql, mysql & postgres as singleton classes * add db connection to cache only if datasourceId is available * client: add data source schema/manifest files * add query operations files * logic for wrapping form with schema * add script to create index file * add @tooljet/plugins to frontend folder * cleanup 1 -frontend * cleanup - 2 // frontend // data queries * add client and index to gitignore * update gitignore * fix lint & test * update ci * fix unit, e2e * cleanup -3 * fix test * fix tests * fix indent * try npm ci * fix tests * fix typo * fix * rename file for server entry * heroku fix * add main and types entry points in pkg json * move common to root * cleanup - 4: remove redundant $ sign prefix * cleanup - 4: remove redundant $ sign prefix * update options in-sync before DOM is painted * change type cloud to cloud storage * update readme * update ci.yml * update ci yml * add pkg-lock.json * rename index.ts to server.ts * update lock files * add server package.lock * remove unused import * revert commit: add minio * add root dep * import server.ts * remove plugins build step * add npm shrinkwrap * update version - plugins * add new version - 0.0.8 * upgrade version * move to symlinked package * add lock file * feat: add icon inside package * add plugin creation docs * Remove seed * move icons to plugins folder * install pg dep * add react to packages * add seed cmd * revert change * add plugins build in lint, e2e, unit * e2e, lint use npm ci * update dockerfile for plugins * try combining release with web * limit memory on release * try executing seed script post transpile * try executing seed from server directory * update seed execution * add minio * add correct type * add minio to pkg json * remove old file * fix provider key * add python installable + npm ^7.2.0 (#1752) * add python installable + npm ^7.2.0 * add py to prod file * pin npm version to 7.20.0 * pin npm version to 7.20.0 * split into multi stage build and remove python for buildx * copy plugins from buider stage * update dependencies * add freetds dependency * update server dockerfile * update client dockerfile * update dev dockerfile and compose file * fix entrypoint * fix server dev dockerfile * update docker-compose * remove npm install on root dir on docker build * fix heroku script * make lerna prod dependency to enable prod builds * remove redundant env setup Co-authored-by: Akshay Sasidharan <akshaysasidharan93@gmail.com> Co-authored-by: navaneeth <navaneethpk@outlook.com>
2022-01-17 07:08:17 +00:00
}
return MssqlQueryService._instance;
}
2025-02-25 06:52:50 +00:00
sanitizeOptions(options: string[][]) {
const _connectionOptions = (options || [])
.filter((o) => o.every((e) => !!e))
.map(([key, value]) => [key, interpretValue(value)]);
2025-02-25 06:52:50 +00:00
return Object.fromEntries(_connectionOptions);
}
async run(
sourceOptions: SourceOptions,
queryOptions: QueryOptions,
dataSourceId: string,
dataSourceUpdatedAt: string
): Promise<QueryResult> {
let knexInstance: Knex | undefined;
let checkCache: boolean;
// Dynamic connection parameters
if (sourceOptions.allow_dynamic_connection_parameters) {
if (sourceOptions.connection_type === 'manual') {
sourceOptions.host = queryOptions['host'] ? queryOptions['host'] : sourceOptions.host;
sourceOptions.database = queryOptions['database'] ? queryOptions['database'] : sourceOptions.database;
} else if (sourceOptions.connection_type === 'string') {
if (queryOptions['host']) sourceOptions.host = queryOptions['host'];
if (queryOptions['database']) sourceOptions.database = queryOptions['database'];
}
}
checkCache = sourceOptions.allow_dynamic_connection_parameters ? false : true;
try {
knexInstance = await this.getConnection(sourceOptions, {}, checkCache, dataSourceId, dataSourceUpdatedAt);
switch (queryOptions.mode) {
case 'sql':
return await this.handleRawQuery(knexInstance, queryOptions);
case 'gui':
return await this.handleGuiQuery(knexInstance, queryOptions);
default:
throw new Error("Invalid query mode. Must be either 'sql' or 'gui'.");
}
} catch (err) {
const errorMessage = err instanceof Error ? err?.message : 'An unknown error occurred';
const errorDetails: any = {};
if (err && err instanceof Error) {
const msSqlError = err as any;
const { code, severity, state, number, lineNumber, serverName, class: errorClass } = msSqlError;
errorDetails.code = code || null;
errorDetails.severity = severity || null;
errorDetails.state = state || null;
errorDetails.number = number || null;
errorDetails.lineNumber = lineNumber || null;
errorDetails.serverName = serverName || null;
errorDetails.class = errorClass || null;
}
throw new QueryError('Query could not be completed', errorMessage, errorDetails);
}
}
private async handleGuiQuery(knexInstance: Knex, queryOptions: QueryOptions): Promise<any> {
if (queryOptions.operation !== 'bulk_update_pkey') {
return { rows: [] };
}
const query = this.buildBulkUpdateQuery(queryOptions);
return await this.executeQuery(knexInstance, query);
}
private async handleRawQuery(knexInstance: Knex, queryOptions: QueryOptions): Promise<QueryResult> {
const { query, query_params } = queryOptions;
const queryParams = query_params || [];
const sanitizedQueryParams: Record<string, any> = Object.fromEntries(queryParams.filter(([key]) => !isEmpty(key)));
const result = await this.executeQuery(knexInstance, query, sanitizedQueryParams);
return { status: 'ok', data: result };
}
private async executeQuery(knexInstance: Knex, query: string, sanitizedQueryParams: Record<string, any> = {}) {
if (isEmpty(query)) throw new Error('Query is empty');
const result = await knexInstance.raw(query, sanitizedQueryParams).timeout(this.STATEMENT_TIMEOUT);
return result;
}
async testConnection(sourceOptions: SourceOptions): Promise<ConnectionTestResult> {
let knexInstance;
try {
knexInstance = await this.getConnection(sourceOptions, {}, false);
await knexInstance.raw('select @@version;').timeout(this.STATEMENT_TIMEOUT);
try { await knexInstance.destroy(); } catch (_) {};
return {
status: 'ok',
};
} catch (err: any) {
let message = 'Connection test failed';
let details: any = {};
2021-07-18 06:58:05 +00:00
if (err?.code || err?.number || err?.serverName) {
message = err.message;
details = {
code: err.code ?? null,
severity: err.severity ?? null,
state: err.state ?? null,
number: err.number ?? null,
lineNumber: err.lineNumber ?? null,
serverName: err.serverName ?? null,
class: err.class ?? null,
};
}
else if (err?.code === 'ESOCKET' || err?.code === 'ECONNREFUSED' || err?.code === 'ETIMEDOUT') {
message = `Network error: ${err.message}`;
}
else if (err?.code === 'ELOGIN') {
message = `Authentication failed: ${err.message}`;
}
else if (err?.message?.includes('SSH')) {
message = `SSH connection failed: ${err.message}`;
}
else if (err?.name === 'KnexTimeoutError') {
message = 'Database connection timeout. Please check host/port/firewall';
}
else if (err?.message) {
message = err.message;
}
throw new QueryError('Connection test failed', message, details);
} finally {
if (knexInstance) {
try { await knexInstance.destroy(); } catch (_) {}
}
}
2021-07-18 06:58:05 +00:00
}
private parseConnectionString(connectionString: string): Partial<SourceOptions> {
const parsed: Partial<SourceOptions> = {};
if (!connectionString) return parsed;
const trimmed = connectionString.trim();
const withoutScheme = /^sqlserver:\/\//i.test(trimmed)
? trimmed.replace(/^sqlserver:\/\//i, '')
: trimmed;
const looksLikeHybrid = withoutScheme.includes(';') &&
!/^[a-z ]+=/i.test(withoutScheme.split(';')[0]);
if (looksLikeHybrid) {
const firstSemi = withoutScheme.indexOf(';');
const hostSegment = withoutScheme.slice(0, firstSemi);
const rest = withoutScheme.slice(firstSemi + 1);
const hostMatch = hostSegment.match(/^([^:\\,]+)(?::(\d+))?(?:\\([^,]*))?(?:,(\d+))?/);
if (hostMatch) {
if (hostMatch[1]) parsed.host = hostMatch[1].trim();
if (hostMatch[2]) parsed.port = (parseInt(hostMatch[2], 10));
if (hostMatch[3]) parsed.instanceName = hostMatch[3].trim();
if (hostMatch[4]) parsed.port = (parseInt(hostMatch[4], 10));
}
rest.split(';').forEach(pair => {
if (!pair.includes('=')) return;
const [key, ...valueParts] = pair.split('=');
const value = valueParts.join('=').trim();
const lowerKey = key.trim().toLowerCase();
if (lowerKey === 'database' || lowerKey === 'initial catalog') {
parsed.database = value;
} else if (lowerKey === 'user id' || lowerKey === 'uid' || lowerKey === 'user') {
parsed.username = value;
} else if (lowerKey === 'password' || lowerKey === 'pwd') {
parsed.password = value;
} else if (lowerKey === 'encrypt') {
parsed.azure = ['true', '1', 'yes'].includes(value.toLowerCase()) as any;
} else if (lowerKey === 'port') {
parsed.port = (parseInt(value, 10));
} else if (lowerKey === 'instance' || lowerKey === 'instance name') {
parsed.instanceName = value;
}
});
}
return parsed;
}
async buildConnection(sourceOptions: SourceOptions): Promise<Knex> {
2026-03-26 12:08:09 +00:00
const finalOptions: SourceOptions = sourceOptions;
// SSL config
const shouldUseSSL = finalOptions.ssl_enabled === true;
let sslObject: any = null;
if (shouldUseSSL) {
if (finalOptions.ssl_certificate === 'ca_certificate') {
sslObject = {
rejectUnauthorized: true,
ca: finalOptions.ca_cert,
key: finalOptions.client_key,
cert: finalOptions.client_cert,
};
} else if (finalOptions.ssl_certificate === 'self_signed') {
sslObject = {
rejectUnauthorized: false,
ca: finalOptions.root_cert,
key: finalOptions.client_key,
cert: finalOptions.client_cert,
};
} else {
sslObject = { rejectUnauthorized: false };
}
}
let tunnel: SSHTunnel | null = null;
let host: string;
let port: number;
if (sourceOptions.ssh_enabled=='enabled') {
tunnel = await createSSHTunnel(sourceOptions);
host = '127.0.0.1';
port = tunnel.localPort;
} else {
host = finalOptions.host;
port = +finalOptions.port;
}
// Service Principal (Azure AD) authentication
const isServicePrincipal = finalOptions.auth_type === 'service_principal';
2021-07-17 07:11:03 +00:00
const config: Knex.Config = {
client: 'mssql',
connection: {
...(isServicePrincipal
? {
// Knex mssql dialect builds the tedious auth block from these FLAT fields.
// It ignores any nested 'authentication' object entirely.
server: host,
type: 'azure-active-directory-service-principal-secret',
tenantId: finalOptions.sp_tenant_id,
clientId: finalOptions.sp_client_id,
clientSecret: finalOptions.sp_client_secret,
}
: {
host: host,
user: finalOptions.username,
password: finalOptions.password,
}),
database: finalOptions.database,
port: port,
options: {
encrypt: isServicePrincipal ? true : ((finalOptions.azure ?? false) || shouldUseSSL),
instanceName: finalOptions.instanceName,
trustServerCertificate: !isServicePrincipal && shouldUseSSL && finalOptions.ssl_certificate === 'none',
requestTimeout: this.STATEMENT_TIMEOUT,
...(shouldUseSSL && !isServicePrincipal ? { cryptoCredentialsDetails: sslObject } : {}),
...(finalOptions.connection_options && this.sanitizeOptions(finalOptions.connection_options)),
},
},
pool: {
min: 0,
afterCreate: (conn: any, done: (err: Error | null, conn: any) => void) => {
conn.on('error', (_err: Error) => {});
done(null, conn);
},
},
2021-07-17 07:11:03 +00:00
};
2021-08-03 05:07:35 +00:00
const knexInstance = knex(config);
if (tunnel) {
const originalDestroy = knexInstance.destroy.bind(knexInstance);
Object.defineProperty(knexInstance, 'destroy', {
value: async function () {
try {
await originalDestroy();
} finally {
tunnel.server.close();
tunnel.client.end();
}
},
});
}
return knexInstance;
2021-07-17 07:11:03 +00:00
}
2021-08-03 05:07:35 +00:00
async getConnection(
sourceOptions: SourceOptions,
options: any,
checkCache: boolean,
dataSourceId?: string,
dataSourceUpdatedAt?: string
): Promise<Knex> {
if (checkCache) {
2025-05-27 11:23:22 +00:00
const optionsHash = generateSourceOptionsHash(sourceOptions);
const enhancedCacheKey = `${dataSourceId}_${optionsHash}`;
let connection = await getCachedConnection(enhancedCacheKey, dataSourceUpdatedAt);
2021-08-03 05:07:35 +00:00
if (connection) {
2021-08-03 05:07:35 +00:00
return connection;
} else {
connection = await this.buildConnection(sourceOptions);
2025-05-27 11:23:22 +00:00
cacheConnectionWithConfiguration(dataSourceId, enhancedCacheKey, connection);
2021-08-03 05:07:35 +00:00
return connection;
}
} else {
return await this.buildConnection(sourceOptions);
}
}
buildBulkUpdateQuery(queryOptions: QueryOptions): string {
let queryText = '';
const { table, primary_key_column, records } = queryOptions;
for (const record of records) {
const primaryKeyValue =
typeof record[primary_key_column] === 'string' ? `'${record[primary_key_column]}'` : record[primary_key_column];
queryText = `${queryText} UPDATE ${table} SET`;
for (const key of Object.keys(record)) {
if (key !== primary_key_column) {
queryText = ` ${queryText} ${key} = '${record[key]}',`;
}
}
queryText = queryText.slice(0, -1);
queryText = `${queryText} WHERE ${primary_key_column} = ${primaryKeyValue};`;
}
return queryText.trim();
}
async listTables(
sourceOptions: SourceOptions,
queryOptions?: { search?: string; page?: number; limit?: number }
): Promise<QueryResult> {
let knexInstance;
try {
knexInstance = await this.buildConnection(sourceOptions);
const search = queryOptions?.search || '';
const searchPattern = `%${search}%`;
const db = sourceOptions.database;
if (queryOptions?.limit) {
const limit = queryOptions.limit;
const page = queryOptions.page || 1;
const offset = (page - 1) * limit;
const [dataResult, countResult] = await Promise.all([
knexInstance
.raw(
`SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_CATALOG = ? AND TABLE_NAME LIKE ? ORDER BY TABLE_NAME OFFSET ? ROWS FETCH NEXT ? ROWS ONLY`,
[db, searchPattern, offset, limit]
)
.timeout(this.STATEMENT_TIMEOUT),
knexInstance
.raw(
`SELECT COUNT(*) AS total FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_CATALOG = ? AND TABLE_NAME LIKE ?`,
[db, searchPattern]
)
.timeout(this.STATEMENT_TIMEOUT),
]);
const rows = dataResult.map((row: any) => ({ label: row.TABLE_NAME, value: row.TABLE_NAME }));
const totalCount = parseInt(countResult?.[0]?.total ?? '0', 10);
return { status: 'ok', data: { rows, totalCount } };
}
const result = await knexInstance
.raw(
`SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_CATALOG = ? AND TABLE_NAME LIKE ? ORDER BY TABLE_NAME`,
[db, searchPattern]
)
.timeout(this.STATEMENT_TIMEOUT);
const tables = result.map((row: any) => ({
label: row.TABLE_NAME,
value: row.TABLE_NAME,
}));
return { status: 'ok', data: tables };
} catch (err) {
const errorMessage = err instanceof Error ? err?.message : 'An unknown error occurred';
throw new QueryError('Could not fetch tables', errorMessage, {});
} finally {
if (knexInstance) {
await knexInstance.destroy();
}
}
}
async invokeMethod(
methodName: string,
context: { user?: User; app?: App },
sourceOptions: SourceOptions,
args?: any
): Promise<any> {
try {
if (methodName === 'getTables') {
const isPaginated = !!args?.limit;
const result = await this.listTables(sourceOptions, {
search: args?.search,
page: args?.page,
limit: args?.limit,
});
const payload = (result as any)?.data ?? [];
if (isPaginated) {
const rows = (payload as any)?.rows ?? [];
const totalCount = (payload as any)?.totalCount ?? 0;
return { items: rows, totalCount };
}
return { status: 'ok', data: Array.isArray(payload) ? payload : [] };
}
throw new QueryError(
'Method not found',
`Method ${methodName} is not supported for MSSQL plugin`,
{
availableMethods: ['getTables'],
}
);
} catch (err) {
if (err instanceof QueryError) {
throw err;
}
const errorMessage = err instanceof Error ? err?.message : 'An unknown error occurred';
const errorDetails: any = {};
if (err && err instanceof Error) {
const msSqlError = err as any;
const { code, severity, state, number, lineNumber, serverName, class: errorClass } = msSqlError;
errorDetails.code = code || null;
errorDetails.severity = severity || null;
errorDetails.state = state || null;
errorDetails.number = number || null;
errorDetails.lineNumber = lineNumber || null;
errorDetails.serverName = serverName || null;
errorDetails.class = errorClass || null;
}
throw new QueryError('Method invocation failed', errorMessage, errorDetails);
}
}
2021-07-17 07:11:03 +00:00
}