Skip to content

Commit

Permalink
feat: implement a better query splitter for SQL queries, fixes #926
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabio286 committed Jan 31, 2025
1 parent e3b3035 commit 96ae09f
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 64 deletions.
1 change: 1 addition & 0 deletions src/common/interfaces/antares.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export interface ClientParams {
| { databasePath: string; readonly: boolean };
poolSize?: number;
logger?: () => void;
querySplitter?: (sql: string, clieng?: string) => string[];
}

/**
Expand Down
86 changes: 86 additions & 0 deletions src/common/libs/querySplitter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { ClientCode } from 'common/interfaces/antares';

export const querySplitter =(sql: string, dbType: ClientCode): string[] => {
const queries: string[] = [];
let currentQuery = '';
let insideBlock = false;
let insideString = false;
let stringDelimiter: string | null = null;
let insideDollarTag = false;
let dollarTagDelimiter: string | null = null;

// Regex patterns for BEGIN-END blocks, dollar tags in PostgreSQL, and semicolons
const beginRegex = /\bBEGIN\b/i;
const endRegex = /\bEND\b;/i;
const dollarTagRegex = /\$(\w+)?\$/; // Matches $tag$ or $$

// Split on semicolons, keeping semicolons attached to the lines
const lines = sql.split(/(?<=;)/);

for (let line of lines) {
line = line.trim();

if (!line) continue;

for (let i = 0; i < line.length; i++) {
const char = line[i];

// Handle string boundaries
if ((char === '\'' || char === '"') && (!insideString || char === stringDelimiter)) {
if (!insideString) {
insideString = true;
stringDelimiter = char;
}
else {
insideString = false;
stringDelimiter = null;
}
}

currentQuery += char;

if (dbType === 'pg') {
// Handle dollar-quoted blocks in PostgreSQL
if (!insideString && line.slice(i).match(dollarTagRegex)) {
const match = line.slice(i).match(dollarTagRegex);
if (match) {
const tag = match[0];
if (!insideDollarTag) {
insideDollarTag = true;
dollarTagDelimiter = tag;
currentQuery += tag;
i += tag.length - 1;
}
else if (dollarTagDelimiter === tag) {
insideDollarTag = false;
dollarTagDelimiter = null;
currentQuery += tag;
i += tag.length - 1;
}
}
}
}

// Check BEGIN-END blocks
if (!insideString && !insideDollarTag) {
if (beginRegex.test(line))
insideBlock = true;

if (insideBlock && endRegex.test(line))
insideBlock = false;
}
}

// Append the query if we encounter a semicolon outside a BEGIN-END block, outside a string, and outside dollar tags
if (!insideBlock && !insideString && !insideDollarTag && /;\s*$/.test(line)) {
queries.push(currentQuery.trim());
currentQuery = '';
}
}

// Add any remaining query
if (currentQuery.trim())
queries.push(currentQuery.trim());

return queries;
};
24 changes: 4 additions & 20 deletions src/main/libs/clients/BaseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,9 @@ import * as antares from 'common/interfaces/antares';
import mysql from 'mysql2/promise';
import * as pg from 'pg';
import SSH2Promise = require('@fabio286/ssh2-promise');
import { querySplitter } from 'common/libs/querySplitter';

export type LoggerLevel = 'query' | 'error'

const ipcLogger = ({ content, cUid, level }: {content: string; cUid: string; level: LoggerLevel}) => {
if (level === 'error') {
if (process.type !== undefined) {
const mainWindow = require('electron').webContents.fromId(1);
mainWindow.send('non-blocking-exception', { cUid, message: content, date: new Date() });
}
if (process.env.NODE_ENV === 'development' && process.type === 'browser') console.log(content);
}
else if (level === 'query') {
// Remove comments, newlines and multiple spaces
const escapedSql = content.replace(/(\/\*(.|[\r\n])*?\*\/)|(--(.*|[\r\n]))/gm, '').replace(/\s\s+/g, ' ');
if (process.type !== undefined) {
const mainWindow = require('electron').webContents.fromId(1);
mainWindow.send('query-log', { cUid, sql: escapedSql, date: new Date() });
}
if (process.env.NODE_ENV === 'development' && process.type === 'browser') console.log(escapedSql);
}
};
import { ipcLogger, LoggerLevel } from '../misc/ipcLogger';

/**
* As Simple As Possible Query Builder Core
Expand All @@ -34,6 +16,7 @@ export abstract class BaseClient {
protected _poolSize: number;
protected _ssh?: SSH2Promise;
protected _logger: (args: {content: string; cUid: string; level: LoggerLevel}) => void;
protected _querySplitter: (sql: string, client: antares.ClientCode) => string[];
protected _queryDefaults: antares.QueryBuilderObject;
protected _query: antares.QueryBuilderObject;

Expand All @@ -43,6 +26,7 @@ export abstract class BaseClient {
this._params = args.params;
this._poolSize = args.poolSize || undefined;
this._logger = args.logger || ipcLogger;
this._querySplitter = args.querySplitter || querySplitter;

this._queryDefaults = {
schema: '',
Expand Down
20 changes: 9 additions & 11 deletions src/main/libs/clients/FirebirdSQLClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,10 +245,10 @@ export class FirebirdSQLClient extends BaseClient {
name: db.name,
size: schemaSize,
tables: remappedTables,
functions: [],
functions: [] as null[],
procedures: remappedProcedures,
triggers: remappedTriggers,
schedulers: []
schedulers: [] as null[]
};
});
}
Expand Down Expand Up @@ -337,7 +337,7 @@ export class FirebirdSQLClient extends BaseClient {

return {
name: field.FIELD_NAME.trim(),
key: null,
key: null as null,
type: fieldType,
schema: schema,
table: table,
Expand All @@ -346,14 +346,14 @@ export class FirebirdSQLClient extends BaseClient {
datePrecision: field.FIELD_NAME.trim() === 'TIMESTAMP' ? 4 : null,
charLength: ![...NUMBER, ...FLOAT].includes(fieldType) ? field.FIELD_LENGTH : null,
nullable: !field.NOT_NULL,
unsigned: null,
zerofill: null,
unsigned: null as null,
zerofill: null as null,
order: field.FIELD_POSITION+1,
default: defaultValue,
charset: field.CHARSET,
collation: null,
collation: null as null,
autoIncrement: false,
onUpdate: null,
onUpdate: null as null,
comment: field.DESCRIPTION?.trim()
};
});
Expand Down Expand Up @@ -457,7 +457,7 @@ export class FirebirdSQLClient extends BaseClient {
table: table,
field: field.FKCOLUMN_NAME.trim(),
position: field.KEY_SEQ,
constraintPosition: null,
constraintPosition: null as null,
constraintName: field.FK_NAME.trim(),
refSchema: schema,
refTable: field.PKTABLE_NAME.trim(),
Expand Down Expand Up @@ -1041,9 +1041,7 @@ export class FirebirdSQLClient extends BaseClient {
const resultsArr = [];
let paramsArr = [];
const queries = args.split
? sql.split(/((?:[^;'"]*(?:"(?:\\.|[^"])*"|'(?:\\.|[^'])*')[^;'"]*)+)|;/gm)
.filter(Boolean)
.map(q => q.trim())
? this._querySplitter(sql, 'firebird')
: [sql];

let connection: firebird.Database | firebird.Transaction;
Expand Down
4 changes: 1 addition & 3 deletions src/main/libs/clients/MySQLClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1755,9 +1755,7 @@ export class MySQLClient extends BaseClient {
const resultsArr: antares.QueryResult[] = [];
let paramsArr = [];
const queries = args.split
? sql.split(/((?:[^;'"]*(?:"(?:\\.|[^"])*"|'(?:\\.|[^'])*')[^;'"]*)+)|;/gm)
.filter(Boolean)
.map(q => q.trim())
? this._querySplitter(sql, 'mysql')
: [sql];

const connection = await this.getConnection(args);
Expand Down
30 changes: 14 additions & 16 deletions src/main/libs/clients/PostgreSQLClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ export class PostgreSQLClient extends BaseClient {
procedures: remappedProcedures,
triggers: remappedTriggers,
triggerFunctions: remappedTriggerFunctions,
schedulers: []
schedulers: [] as null[]
};
}
else {
Expand Down Expand Up @@ -532,7 +532,7 @@ export class PostgreSQLClient extends BaseClient {

return {
name: field.column_name,
key: null,
key: null as null,
type: type.toUpperCase(),
isArray,
schema: field.table_schema,
Expand All @@ -542,14 +542,14 @@ export class PostgreSQLClient extends BaseClient {
datePrecision: field.datetime_precision,
charLength: field.character_maximum_length,
nullable: field.is_nullable.includes('YES'),
unsigned: null,
zerofill: null,
unsigned: null as null,
zerofill: null as null,
order: field.ordinal_position,
default: field.column_default,
charset: field.character_set_name,
collation: field.collation_name,
autoIncrement: false,
onUpdate: null,
onUpdate: null as null,
comment: field.column_comment
};
});
Expand Down Expand Up @@ -1252,9 +1252,9 @@ export class PostgreSQLClient extends BaseClient {
return results.rows.map(async row => {
if (!row.pg_get_functiondef) {
return {
definer: null,
definer: null as null,
sql: '',
parameters: [],
parameters: [] as null[],
name: routine,
comment: '',
security: 'DEFINER',
Expand Down Expand Up @@ -1303,8 +1303,8 @@ export class PostgreSQLClient extends BaseClient {
name: routine,
comment: '',
security: row.pg_get_functiondef.includes('SECURITY DEFINER') ? 'DEFINER' : 'INVOKER',
deterministic: null,
dataAccess: null,
deterministic: null as null,
dataAccess: null as null,
language: row.pg_get_functiondef.match(/(?<=LANGUAGE )(.*)(?<=[\S+\n\r\s])/gm)[0]
};
})[0];
Expand Down Expand Up @@ -1368,9 +1368,9 @@ export class PostgreSQLClient extends BaseClient {
return results.rows.map(async row => {
if (!row.pg_get_functiondef) {
return {
definer: null,
definer: null as null,
sql: '',
parameters: [],
parameters: [] as null[],
name: func,
comment: '',
security: 'DEFINER',
Expand Down Expand Up @@ -1418,8 +1418,8 @@ export class PostgreSQLClient extends BaseClient {
name: func,
comment: '',
security: row.pg_get_functiondef.includes('SECURITY DEFINER') ? 'DEFINER' : 'INVOKER',
deterministic: null,
dataAccess: null,
deterministic: null as null,
dataAccess: null as null,
language: row.pg_get_functiondef.match(/(?<=LANGUAGE )(.*)(?<=[\S+\n\r\s])/gm)[0],
returns: row.pg_get_functiondef.match(/(?<=RETURNS )(.*)(?<=[\S+\n\r\s])/gm)[0].replace('SETOF ', '').toUpperCase()
};
Expand Down Expand Up @@ -1665,9 +1665,7 @@ export class PostgreSQLClient extends BaseClient {
const resultsArr: antares.QueryResult[] = [];
let paramsArr = [];
const queries = args.split
? sql.split(/(?!\B'[^']*);(?![^']*'\B)/gm)
.filter(Boolean)
.map(q => q.trim())
? this._querySplitter(sql, 'pg')
: [sql];

let connection: pg.Client | pg.PoolClient;
Expand Down
26 changes: 12 additions & 14 deletions src/main/libs/clients/SQLiteClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,10 @@ export class SQLiteClient extends BaseClient {
name: db.name,
size: schemaSize,
tables: remappedTables,
functions: [],
procedures: [],
functions: [] as null[],
procedures: [] as null[],
triggers: remappedTriggers,
schedulers: []
schedulers: [] as null[]
};
}
else {
Expand Down Expand Up @@ -166,22 +166,22 @@ export class SQLiteClient extends BaseClient {

return {
name: field.name,
key: null,
key: null as null,
type: type.trim(),
schema: schema,
table: table,
numLength: [...NUMBER, ...FLOAT].includes(type) ? length : null,
datePrecision: null,
datePrecision: null as null,
charLength: ![...NUMBER, ...FLOAT].includes(type) ? length : null,
nullable: !field.notnull,
unsigned: null,
zerofill: null,
unsigned: null as null,
zerofill: null as null,
order: typeof field.cid === 'string' ? +field.cid + 1 : field.cid + 1,
default: field.dflt_value,
charset: null,
collation: null,
charset: null as null,
collation: null as null,
autoIncrement: false,
onUpdate: null,
onUpdate: null as null,
comment: ''
};
});
Expand Down Expand Up @@ -267,7 +267,7 @@ export class SQLiteClient extends BaseClient {
table: table,
field: field.from,
position: field.id + 1,
constraintPosition: null,
constraintPosition: null as null,
constraintName: field.id,
refSchema: schema,
refTable: field.table,
Expand Down Expand Up @@ -629,9 +629,7 @@ export class SQLiteClient extends BaseClient {
const resultsArr = [];
let paramsArr = [];
const queries = args.split
? sql.split(/((?:[^;'"]*(?:"(?:\\.|[^"])*"|'(?:\\.|[^'])*')[^;'"]*)+)|;/gm)
.filter(Boolean)
.map(q => q.trim())
? this._querySplitter(sql, 'sqlite')
: [sql];

let connection: sqlite.Database;
Expand Down
20 changes: 20 additions & 0 deletions src/main/libs/misc/ipcLogger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export type LoggerLevel = 'query' | 'error'

export const ipcLogger = ({ content, cUid, level }: {content: string; cUid: string; level: LoggerLevel}) => {
if (level === 'error') {
if (process.type !== undefined) {
const mainWindow = require('electron').webContents.fromId(1);
mainWindow.send('non-blocking-exception', { cUid, message: content, date: new Date() });
}
if (process.env.NODE_ENV === 'development' && process.type === 'browser') console.log(content);
}
else if (level === 'query') {
// Remove comments, newlines and multiple spaces
const escapedSql = content.replace(/(\/\*(.|[\r\n])*?\*\/)|(--(.*|[\r\n]))/gm, '').replace(/\s\s+/g, ' ');
if (process.type !== undefined) {
const mainWindow = require('electron').webContents.fromId(1);
mainWindow.send('query-log', { cUid, sql: escapedSql, date: new Date() });
}
if (process.env.NODE_ENV === 'development' && process.type === 'browser') console.log(escapedSql);
}
};

0 comments on commit 96ae09f

Please sign in to comment.