Skip to content

Commit 0c2cc81

Browse files
Merge branch 'main' into fix/cte-params
2 parents b8f3adb + e15e335 commit 0c2cc81

File tree

3 files changed

+19
-28
lines changed

3 files changed

+19
-28
lines changed

src/defines.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,11 @@ export type ExecutionType = 'LISTING' | 'MODIFICATION' | 'INFORMATION' | 'ANON_B
7878

7979
export interface ParamTypes {
8080
positional?: boolean;
81-
numbered?: Array<'?' | ':' | '$'>;
82-
named?: Array<':' | '@' | '$'>;
83-
quoted?: Array<':' | '@' | '$'>;
81+
numbered?: ('?' | ':' | '$')[];
82+
named?: (':' | '@' | '$')[];
83+
quoted?: (':' | '@' | '$')[];
8484
// regex for identifying that it is a param
85-
custom?: Array<string>;
85+
custom?: string[];
8686
}
8787

8888
export interface IdentifyOptions {

src/index.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { parse, EXECUTION_TYPES, defaultParamTypesFor } from './parser';
2-
import { DIALECTS, ParamTypes } from './defines';
2+
import { DIALECTS } from './defines';
33
import type { ExecutionType, IdentifyOptions, IdentifyResult, StatementType } from './defines';
44

55
export type {
@@ -21,14 +21,8 @@ export function identify(query: string, options: IdentifyOptions = {}): Identify
2121
throw new Error(`Unknown dialect. Allowed values: ${DIALECTS.join(', ')}`);
2222
}
2323

24-
let paramTypes: ParamTypes;
25-
2624
// Default parameter types for each dialect
27-
if (options.paramTypes) {
28-
paramTypes = options.paramTypes;
29-
} else {
30-
paramTypes = defaultParamTypesFor(dialect);
31-
}
25+
const paramTypes = options.paramTypes || defaultParamTypesFor(dialect);
3226

3327
const result = parse(query, isStrict, dialect, options.identifyTables, paramTypes);
3428
const sort = dialect === 'psql' && !options.paramTypes;

src/tokenizer.ts

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -269,11 +269,11 @@ function getCustomParam(state: State, paramTypes: ParamTypes): string | null | u
269269
}
270270

271271
function scanParameter(state: State, dialect: Dialect, paramTypes: ParamTypes): Token {
272-
const curCh: any = state.input[state.start];
272+
const curCh = state.input[state.start];
273273
const nextChar = peek(state);
274274
let matched = false;
275275

276-
if (paramTypes.numbered && paramTypes.numbered.length && paramTypes.numbered.includes(curCh)) {
276+
if (paramTypes.numbered?.length && paramTypes.numbered.some((type) => type === curCh)) {
277277
const endIndex = state.input
278278
.slice(state.start + 1)
279279
.split('')
@@ -293,19 +293,14 @@ function scanParameter(state: State, dialect: Dialect, paramTypes: ParamTypes):
293293
}
294294
}
295295

296-
if (!matched && paramTypes.named && paramTypes.named.length && paramTypes.named.includes(curCh)) {
296+
if (!matched && paramTypes.named?.length && paramTypes.named.some((type) => type === curCh)) {
297297
if (!isQuotedIdentifier(nextChar, dialect)) {
298298
while (isAlphaNumeric(peek(state))) read(state);
299299
matched = true;
300300
}
301301
}
302302

303-
if (
304-
!matched &&
305-
paramTypes.quoted &&
306-
paramTypes.quoted.length &&
307-
paramTypes.quoted.includes(curCh)
308-
) {
303+
if (!matched && paramTypes.quoted?.length && paramTypes.quoted.some((type) => type === curCh)) {
309304
if (isQuotedIdentifier(nextChar, dialect)) {
310305
const quoteChar = read(state) as string;
311306
// end when we reach the end quote
@@ -462,32 +457,34 @@ function isString(ch: Char, dialect: Dialect): boolean {
462457
return stringStart.includes(ch);
463458
}
464459

465-
function isCustomParam(state: State, paramTypes: ParamTypes): boolean | undefined {
466-
return paramTypes?.custom?.some((regex) => {
460+
function isCustomParam(state: State, customParamType: NonNullable<ParamTypes['custom']>): boolean {
461+
return customParamType.some((regex) => {
467462
const reg = new RegExp(`^(?:${regex})`, 'uy');
468463
return reg.test(state.input.slice(state.start));
469464
});
470465
}
471466

472467
function isParameter(ch: Char, state: State, paramTypes: ParamTypes): boolean {
473-
const curCh: any = ch;
468+
if (!ch) {
469+
return false;
470+
}
474471
const nextChar = peek(state);
475472
if (paramTypes.positional && ch === '?') return true;
476473

477-
if (paramTypes.numbered && paramTypes.numbered.length && paramTypes.numbered.includes(curCh)) {
474+
if (paramTypes.numbered?.length && paramTypes.numbered.some((type) => ch === type)) {
478475
if (nextChar !== null && !isNaN(Number(nextChar))) {
479476
return true;
480477
}
481478
}
482479

483480
if (
484-
(paramTypes.named && paramTypes.named.length && paramTypes.named.includes(curCh)) ||
485-
(paramTypes.quoted && paramTypes.quoted.length && paramTypes.quoted.includes(curCh))
481+
(paramTypes.named?.length && paramTypes.named.some((type) => type === ch)) ||
482+
(paramTypes.quoted?.length && paramTypes.quoted.some((type) => type === ch))
486483
) {
487484
return true;
488485
}
489486

490-
if (paramTypes.custom && paramTypes.custom.length && isCustomParam(state, paramTypes)) {
487+
if (paramTypes.custom?.length && isCustomParam(state, paramTypes.custom)) {
491488
return true;
492489
}
493490

0 commit comments

Comments
 (0)