Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/defines.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ export interface IdentifyResult {
executionType: ExecutionType;
parameters: string[];
tables: string[];
parameterMacros: Record<string, string>;
}

export interface Statement {
Expand All @@ -124,6 +125,7 @@ export interface Statement {
parameters: string[];
tables: string[];
isCte?: boolean;
parameterMacros: Record<string, string>;
}

export interface ConcreteStatement extends Statement {
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export function identify(query: string, options: IdentifyOptions = {}): Identify
// we want to sort the postgres params: $1 $2 $3, regardless of the order they appear
parameters: sort ? statement.parameters.sort() : statement.parameters,
tables: statement.tables || [],
parameterMacros: statement.parameterMacros || {},
};
return result;
});
Expand Down
26 changes: 26 additions & 0 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,24 @@ function createInitialStatement(): Statement {
end: 0,
parameters: [],
tables: [],
parameterMacros: {},
};
}

const INLINE_MACRO_RE = /^--\s*%([^%]+)%\s*=\s*(.+)$/;
const BLOCK_MACRO_RE = /^\/\*\s*%([^%]+)%\s*=\s*(.*?)\s*\*\/$/s;

function parseMacroFromComment(token: Token): [string, string] | null {
if (token.type === 'comment-inline') {
const match = INLINE_MACRO_RE.exec(token.value.trim());
if (match) return [match[1].trim(), match[2].trim()];
} else if (token.type === 'comment-block') {
const match = BLOCK_MACRO_RE.exec(token.value.trim());
if (match) return [match[1].trim(), match[2].trim()];
}
return null;
}

function nextNonWhitespaceToken(state: State, dialect: Dialect): Token {
let token: Token;
do {
Expand Down Expand Up @@ -161,6 +176,7 @@ export function parse(

let prevState: State = topLevelState;
let statementParser: StatementParser | null = null;
let pendingMacros: Record<string, string> = {};
const cteState: {
isCte: boolean;
asSeen: boolean;
Expand Down Expand Up @@ -189,6 +205,12 @@ export function parse(
if (!cteState.isCte && ignoreOutsideBlankTokens.includes(token.type)) {
topLevelStatement.tokens.push(token);
prevState = tokenState;
if (token.type === 'comment-inline' || token.type === 'comment-block') {
const macro = parseMacroFromComment(token);
if (macro) pendingMacros[macro[0]] = macro[1];
} else if (token.type === 'semicolon') {
pendingMacros = {};
}
} else if (
!cteState.isCte &&
token.type === 'keyword' &&
Expand All @@ -211,7 +233,9 @@ export function parse(
executionType: 'UNKNOWN',
parameters: [],
tables: [],
parameterMacros: { ...pendingMacros },
});
pendingMacros = {};
cteState.isCte = false;
cteState.asSeen = false;
cteState.statementEnd = false;
Expand Down Expand Up @@ -253,6 +277,8 @@ export function parse(
dialect,
identifyTables,
});
statementParser.getStatement().parameterMacros = { ...pendingMacros };
pendingMacros = {};
if (cteState.isCte) {
statementParser.getStatement().start = cteState.state.start;
statementParser.getStatement().isCte = true;
Expand Down
4 changes: 4 additions & 0 deletions test/identifier/inner-statements.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ describe('identifier', () => {
executionType: 'MODIFICATION',
parameters: [],
tables: [],
parameterMacros: {},
},
];

Expand All @@ -36,6 +37,7 @@ describe('identifier', () => {
executionType: 'MODIFICATION',
parameters: [],
tables: [],
parameterMacros: {},
},
];

Expand All @@ -57,6 +59,7 @@ describe('identifier', () => {
executionType: 'MODIFICATION',
parameters: [],
tables: [],
parameterMacros: {},
},
];

Expand All @@ -79,6 +82,7 @@ describe('identifier', () => {
executionType: 'MODIFICATION',
parameters: [],
tables: [],
parameterMacros: {},
},
];

Expand Down
27 changes: 27 additions & 0 deletions test/identifier/multiple-statement.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ describe('identifier', () => {
executionType: 'MODIFICATION',
parameters: [],
tables: [],
parameterMacros: {},
},
{
end: 76,
Expand All @@ -26,6 +27,7 @@ describe('identifier', () => {
executionType: 'LISTING',
parameters: [],
tables: [],
parameterMacros: {},
},
];

Expand All @@ -47,6 +49,7 @@ describe('identifier', () => {
executionType: 'MODIFICATION',
parameters: [],
tables: [],
parameterMacros: {},
},
{
start: 74,
Expand All @@ -56,6 +59,7 @@ describe('identifier', () => {
executionType: 'LISTING',
parameters: [],
tables: [],
parameterMacros: {},
},
];

Expand All @@ -80,6 +84,7 @@ describe('identifier', () => {
executionType: 'LISTING',
parameters: [],
tables: [],
parameterMacros: {},
},
{
start: 35,
Expand All @@ -89,6 +94,7 @@ describe('identifier', () => {
executionType: 'LISTING',
parameters: [],
tables: [],
parameterMacros: {},
},
];

Expand All @@ -112,6 +118,7 @@ describe('identifier', () => {
executionType: 'LISTING',
parameters: [],
tables: [],
parameterMacros: {},
},
{
start: 20,
Expand All @@ -121,6 +128,7 @@ describe('identifier', () => {
executionType: 'LISTING',
parameters: [],
tables: [],
parameterMacros: {},
},
{
start: 50,
Expand All @@ -130,6 +138,7 @@ describe('identifier', () => {
executionType: 'LISTING',
parameters: [],
tables: [],
parameterMacros: {},
},
];

Expand Down Expand Up @@ -171,6 +180,7 @@ describe('identifier', () => {
executionType: 'ANON_BLOCK',
parameters: [],
tables: [],
parameterMacros: {},
start: 11,
text: 'DECLARE\n PK_NAME VARCHAR(200);\n\n BEGIN\n EXECUTE IMMEDIATE (\'CREATE SEQUENCE "untitled_table8_seq"\');\n\n SELECT\n cols.column_name INTO PK_NAME\n FROM\n all_constraints cons,\n all_cons_columns cols\n WHERE\n cons.constraint_type = \'P\'\n AND cons.constraint_name = cols.constraint_name\n AND cons.owner = cols.owner\n AND cols.table_name = \'untitled_table8\';\n\n execute immediate (\n \'create or replace trigger "untitled_table8_autoinc_trg" BEFORE INSERT on "untitled_table8" for each row declare checking number := 1; begin if (:new."\' || PK_NAME || \'" is null) then while checking >= 1 loop select "untitled_table8_seq".nextval into :new."\' || PK_NAME || \'" from dual; select count("\' || PK_NAME || \'") into checking from "untitled_table8" where "\' || PK_NAME || \'" = :new."\' || PK_NAME || \'"; end loop; end if; end;\'\n );\n\n END;',
type: 'ANON_BLOCK',
Expand Down Expand Up @@ -219,6 +229,7 @@ describe('identifier', () => {
executionType: 'MODIFICATION',
parameters: [],
tables: [],
parameterMacros: {},
start: 11,
text: 'create table\n "untitled_table8" (\n "id" integer not null primary key,\n "created_at" varchar(255) not null\n );',
type: 'CREATE_TABLE',
Expand All @@ -228,6 +239,7 @@ describe('identifier', () => {
executionType: 'ANON_BLOCK',
parameters: [],
tables: [],
parameterMacros: {},
start: 180,
text: 'DECLARE\n PK_NAME VARCHAR(200);\n\n BEGIN\n EXECUTE IMMEDIATE (\'CREATE SEQUENCE "untitled_table8_seq"\');\n\n SELECT\n cols.column_name INTO PK_NAME\n FROM\n all_constraints cons,\n all_cons_columns cols\n WHERE\n cons.constraint_type = \'P\'\n AND cons.constraint_name = cols.constraint_name\n AND cons.owner = cols.owner\n AND cols.table_name = \'untitled_table8\';\n\n execute immediate (\n \'create or replace trigger "untitled_table8_autoinc_trg" BEFORE INSERT on "untitled_table8" for each row declare checking number := 1; begin if (:new."\' || PK_NAME || \'" is null) then while checking >= 1 loop select "untitled_table8_seq".nextval into :new."\' || PK_NAME || \'" from dual; select count("\' || PK_NAME || \'") into checking from "untitled_table8" where "\' || PK_NAME || \'" = :new."\' || PK_NAME || \'"; end loop; end if; end;\'\n );\n\n END;',
type: 'ANON_BLOCK',
Expand Down Expand Up @@ -261,6 +273,7 @@ describe('identifier', () => {
executionType: 'MODIFICATION',
parameters: [],
tables: [],
parameterMacros: {},
},
{
start: 79,
Expand All @@ -270,6 +283,7 @@ describe('identifier', () => {
executionType: 'LISTING',
parameters: [],
tables: [],
parameterMacros: {},
},
{
start: 250,
Expand All @@ -279,6 +293,7 @@ describe('identifier', () => {
executionType: 'LISTING',
parameters: [],
tables: [],
parameterMacros: {},
},
];

Expand All @@ -302,6 +317,7 @@ describe('identifier', () => {
executionType: 'UNKNOWN',
parameters: [],
tables: [],
parameterMacros: {},
},
{
start: 54,
Expand All @@ -311,6 +327,7 @@ describe('identifier', () => {
executionType: 'LISTING',
parameters: [],
tables: [],
parameterMacros: {},
},
];

Expand All @@ -335,6 +352,7 @@ describe('identifier', () => {
executionType: 'UNKNOWN',
parameters: [],
tables: [],
parameterMacros: {},
},
{
start: 6,
Expand All @@ -344,6 +362,7 @@ describe('identifier', () => {
executionType: 'LISTING',
parameters: [],
tables: [],
parameterMacros: {},
},
];

Expand All @@ -367,6 +386,7 @@ describe('identifier', () => {
executionType: 'UNKNOWN',
parameters: [],
tables: [],
parameterMacros: {},
},
{
start: 24,
Expand All @@ -376,6 +396,7 @@ describe('identifier', () => {
executionType: 'LISTING',
parameters: [],
tables: [],
parameterMacros: {},
},
];

Expand All @@ -398,6 +419,7 @@ describe('identifier', () => {
executionType: 'TRANSACTION',
parameters: [],
tables: [],
parameterMacros: {},
},
{
start: 19,
Expand All @@ -407,6 +429,7 @@ describe('identifier', () => {
executionType: 'LISTING',
parameters: [],
tables: [],
parameterMacros: {},
},
{
start: 29,
Expand All @@ -416,6 +439,7 @@ describe('identifier', () => {
executionType: 'TRANSACTION',
parameters: [],
tables: [],
parameterMacros: {},
},
];
expect(actual).to.eql(expected);
Expand All @@ -436,6 +460,7 @@ describe('identifier', () => {
executionType: 'TRANSACTION',
parameters: [],
tables: [],
parameterMacros: {},
},
{
start: 19 + offset,
Expand All @@ -445,6 +470,7 @@ describe('identifier', () => {
executionType: 'LISTING',
parameters: [],
tables: [],
parameterMacros: {},
},
{
start: 29 + offset,
Expand All @@ -454,6 +480,7 @@ describe('identifier', () => {
executionType: 'TRANSACTION',
parameters: [],
tables: [],
parameterMacros: {},
},
];
expect(actual).to.eql(expected);
Expand Down
Loading
Loading