Skip to content

Commit d5a03d5

Browse files
author
Laszlo Radics
committed
fix: execute (file) function do not start new dedicated connection when run in one, add support for /* */ comment
1 parent e0fce7a commit d5a03d5

10 files changed

+66
-41
lines changed

lib/pgDb.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export declare class PgDb extends QueryAble {
5555
transactionCommit(): Promise<PgDb>;
5656
transactionRollback(): Promise<PgDb>;
5757
isTransactionActive(): boolean;
58-
execute(fileName: any, statementTransformerFunction?: (string) => string): Promise<void>;
58+
execute(fileName: string, statementTransformerFunction?: (string) => string): Promise<void>;
5959
private listeners;
6060
listen(channel: string, callback: (notification: Notification) => void): Promise<void>;
6161
unlisten(channel: string, callback?: (Notification) => void): Promise<void>;

lib/pgDb.js

+23-15
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/pgDb.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/queryAble.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export declare class QueryAble {
4444
constructor();
4545
setLogger(logger: PgDbLogger): void;
4646
getLogger(useConsoleAsDefault?: boolean): any;
47-
run(sql: string): Promise<any[]>;
47+
run(sql: string, params?: any[] | {}, options?: SqlQueryOptions): Promise<any[]>;
4848
query(sql: string, params?: any[] | {}, options?: SqlQueryOptions): Promise<any[]>;
4949
protected internalQuery(options: {
5050
connection;

lib/queryAble.js

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/queryAble.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/resources/tricky.sql

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
--insert into "groups" (name) values ('joe');
2-
select * from __SCHEMA__."groups" where name like 'joe--'; ; ; -- select * from "groups" where name like 'joe';
2+
select * from __SCHEMA__."groups" where name like 'joe --''"/*-- '; ; ; -- select * from "groups" where name like 'joe';
33
select * from __SCHEMA__."groups" where name like $$joe2'"-- ;
44
-- not a comment ;
55
$$;
6+
7+
-- unbalanced quote '" $$
8+
9+
/*
10+
multi line comment '" $$
11+
*/
12+
13+
select * from __SCHEMA__."groups" where name like 'joe--';

src/pgDb.ts

+23-16
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {ConnectionOptions} from './connectionOptions';
1212
import * as EventEmitter from 'events';
1313

1414
const CONNECTION_URL_REGEXP = /^postgres:\/\/(?:([^:]+)(?::([^@]*))?@)?([^\/:]+)?(?::([^\/]+))?\/(.*)$/;
15-
const SQL_TOKENIZER_REGEXP = /''|'|""|"|;|\$|--|(.+?)/g;
15+
const SQL_TOKENIZER_REGEXP = /''|'|""|"|;|\$|--|\/\*|\*\/|(.+?)/g;
1616
const SQL_$_ESCAPE_REGEXP = /\$[^$]*\$/g;
1717

1818
/** looks like we only get back those that we have access to */
@@ -389,9 +389,10 @@ export class PgDb extends QueryAble {
389389
return this.connection != null;
390390
}
391391

392-
async execute(fileName, statementTransformerFunction?: (string) => string): Promise<void> {
392+
async execute(fileName: string, statementTransformerFunction?: (string) => string): Promise<void> {
393393
let isTransactionInPlace = this.isTransactionActive();
394-
let pgdb = await this.dedicatedConnectionBegin();
394+
// statements must be run in a dedicated connection
395+
let pgdb = isTransactionInPlace ? this : await this.dedicatedConnectionBegin();
395396

396397
/** run statements one after the other */
397398
let runStatementList = (statementList) => {
@@ -423,25 +424,25 @@ export class PgDb extends QueryAble {
423424
let lineCounter = 0;
424425
let promise = new Promise<void>((resolve, reject) => {
425426
let statementList = [];
426-
let tmp = '', m;
427+
let tmp = '', t: RegExpExecArray;
427428
let consumer;
428-
let inQuotedString;
429+
let inQuotedString:string;
429430
let rl = readline.createInterface({
430431
input: fs.createReadStream(fileName),
431432
terminal: false
432433
}).on('line', (line) => {
433434
lineCounter++;
434435
try {
435436
//console.log('Line: ' + line);
436-
while (m = SQL_TOKENIZER_REGEXP.exec(line)) {
437-
if (m[0] == '"' || m[0] == "'") {
437+
while (t = SQL_TOKENIZER_REGEXP.exec(line)) {
438+
if (!inQuotedString && (t[0] == '"' || t[0] == "'") || inQuotedString == '"' || inQuotedString == "'") {
438439
if (!inQuotedString) {
439-
inQuotedString = m[0];
440-
} else if (inQuotedString == m[0]) {
440+
inQuotedString = t[0];
441+
} else if (inQuotedString == t[0]) {
441442
inQuotedString = null;
442443
}
443-
tmp += m[0];
444-
} else if (m[0] == '$' && (!inQuotedString || inQuotedString[0] == '$')) {
444+
tmp += t[0];
445+
} else if (!inQuotedString && t[0] == '$' || inQuotedString && inQuotedString[0] == '$') {
445446
if (!inQuotedString) {
446447
let s = line.slice(SQL_TOKENIZER_REGEXP.lastIndex - 1);
447448
let token = s.match(SQL_$_ESCAPE_REGEXP);
@@ -452,12 +453,20 @@ export class PgDb extends QueryAble {
452453
SQL_TOKENIZER_REGEXP.lastIndex += inQuotedString.length - 1;
453454
tmp += inQuotedString;
454455
} else {
455-
tmp += m[0];
456+
tmp += t[0];
456457
if (tmp.endsWith(inQuotedString)) {
457458
inQuotedString = null;
458459
}
459460
}
460-
} else if (!inQuotedString && m[0] == ';') {
461+
} else if (!inQuotedString && t[0] == '/*' || inQuotedString == '/*') {
462+
if (!inQuotedString) {
463+
inQuotedString = t[0];
464+
} else if (t[0] == '*/') {
465+
inQuotedString = null;
466+
}
467+
} else if (!inQuotedString && t[0] == '--') {
468+
line = '';
469+
} else if (!inQuotedString && t[0] == ';') {
461470
//console.log('push ' + tmp);
462471
if (tmp.trim() != '') {
463472
statementList.push(tmp);
@@ -472,10 +481,8 @@ export class PgDb extends QueryAble {
472481
}
473482
}
474483
tmp = '';
475-
} else if (!inQuotedString && m[0].substring(0, 2) == '--') {
476-
line = '';
477484
} else {
478-
tmp += m[0];
485+
tmp += t[0];
479486
}
480487
}
481488
if (tmp && line) {

src/queryAble.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,9 @@ export class QueryAble {
6565
return this.logger || this.schema && this.schema.logger || this.db.logger || (useConsoleAsDefault ? console : defaultLogger);
6666
}
6767

68-
async run(sql: string): Promise<any[]> {
69-
return this.query(sql);
68+
/** alias to {@link query} */
69+
async run(sql: string, params?: any[] | {}, options?: SqlQueryOptions): Promise<any[]> {
70+
return this.query(sql, params, options);
7071
}
7172

7273
/**

src/test/pgDbSpec.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,8 @@ describe("pgdb", () => {
420420
await table.query(`INSERT INTO ${table} (name, created, createdtz) values ('A2', '${d}'::timestamptz, '${d}'::timestamptz)`);
421421
res = await table.findOne({name: 'A2'});
422422

423-
expect(res.created).toEqual(new Date('2000-01-01 00:00:00'));
423+
// this expectation is depend on machine timezone
424+
// expect(res.created).toEqual(new Date('2000-01-01 00:00:00'));
424425
expect(res.createdtz).toEqual(new Date('2000-01-01 00:00:00'));
425426

426427
res = await table.query(`SELECT * FROM ${table} WHERE name='A2' AND created='${d}'::timestamptz`);

0 commit comments

Comments
 (0)