Skip to content
Merged
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ yarn.lock
lib/
node_modules/
webpack/
mise.toml
11 changes: 10 additions & 1 deletion src/defines.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,19 @@ export type StatementType =
| 'ALTER_FUNCTION'
| 'ALTER_INDEX'
| 'ALTER_PROCEDURE'
| 'BEGIN_TRANSACTION'
| 'COMMIT'
| 'ROLLBACK'
| 'ANON_BLOCK'
| 'UNKNOWN';

export type ExecutionType = 'LISTING' | 'MODIFICATION' | 'INFORMATION' | 'ANON_BLOCK' | 'UNKNOWN';
export type ExecutionType =
| 'LISTING'
| 'MODIFICATION'
| 'INFORMATION'
| 'ANON_BLOCK'
| 'TRANSACTION'
| 'UNKNOWN';

export interface ParamTypes {
positional?: boolean;
Expand Down
81 changes: 81 additions & 0 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ export const EXECUTION_TYPES: Record<StatementType, ExecutionType> = {
ALTER_FUNCTION: 'MODIFICATION',
ALTER_INDEX: 'MODIFICATION',
ALTER_PROCEDURE: 'MODIFICATION',
BEGIN_TRANSACTION: 'TRANSACTION',
COMMIT: 'TRANSACTION',
ROLLBACK: 'TRANSACTION',
UNKNOWN: 'UNKNOWN',
ANON_BLOCK: 'ANON_BLOCK',
};
Expand Down Expand Up @@ -342,7 +345,16 @@ function createStatementParserByToken(
if (['bigquery', 'oracle'].includes(options.dialect) && nextToken.value !== 'TRANSACTION') {
return createBlockStatementParser(options);
}
return createBeginTransactionStatementParser(options);
case 'START':
if (nextToken.value === 'TRANSACTION') {
return createBeginTransactionStatementParser(options);
}
break;
case 'COMMIT':
return createCommitStatementParser(options);
case 'ROLLBACK':
return createRollbackStatementParser(options);
case 'DECLARE':
if (options.dialect === 'oracle') {
return createBlockStatementParser(options);
Expand Down Expand Up @@ -709,6 +721,75 @@ function createShowStatementParser(options: ParseOptions) {
return stateMachineStatementParser(statement, steps, options);
}

function createBeginTransactionStatementParser(options: ParseOptions) {
const statement = createInitialStatement();

const steps: Step[] = [
{
preCanGoToNext: () => false,
validation: {
acceptTokens: [
{ type: 'keyword', value: 'BEGIN' },
{ type: 'keyword', value: 'START' },
],
},
add: (token) => {
statement.type = 'BEGIN_TRANSACTION';
if (statement.start < 0) {
statement.start = token.start;
}
},
postCanGoToNext: () => true,
},
];

return stateMachineStatementParser(statement, steps, options);
}

function createCommitStatementParser(options: ParseOptions) {
const statement = createInitialStatement();

const steps: Step[] = [
{
preCanGoToNext: () => false,
validation: {
acceptTokens: [{ type: 'keyword', value: 'COMMIT' }],
},
add: (token) => {
statement.type = 'COMMIT';
if (statement.start < 0) {
statement.start = token.start;
}
},
postCanGoToNext: () => true,
},
];

return stateMachineStatementParser(statement, steps, options);
}

function createRollbackStatementParser(options: ParseOptions) {
const statement = createInitialStatement();

const steps: Step[] = [
{
preCanGoToNext: () => false,
validation: {
acceptTokens: [{ type: 'keyword', value: 'ROLLBACK' }],
},
add: (token) => {
statement.type = 'ROLLBACK';
if (statement.start < 0) {
statement.start = token.start;
}
},
postCanGoToNext: () => true,
},
];

return stateMachineStatementParser(statement, steps, options);
}

function createUnknownStatementParser(options: ParseOptions) {
const statement = createInitialStatement();

Expand Down
9 changes: 9 additions & 0 deletions src/tokenizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ const KEYWORDS = [
'AS',
'MATERIALIZED',
'BEGIN',
'START',
'COMMIT',
'ROLLBACK',
'TRANSACTION',
'TRAN',
'WORK',
'DEFERRED',
'IMMEDIATE',
'EXCLUSIVE',
'DECLARE',
'CASE',
'LOOP',
Expand Down
16 changes: 8 additions & 8 deletions test/identifier/multiple-statement.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -394,8 +394,8 @@ describe('identifier', () => {
start: 0,
end: 17,
text: statements[0],
type: 'UNKNOWN',
executionType: 'UNKNOWN',
type: 'BEGIN_TRANSACTION',
executionType: 'TRANSACTION',
parameters: [],
tables: [],
},
Expand All @@ -412,8 +412,8 @@ describe('identifier', () => {
start: 29,
end: 35,
text: statements[2],
type: 'UNKNOWN',
executionType: 'UNKNOWN',
type: 'COMMIT',
executionType: 'TRANSACTION',
parameters: [],
tables: [],
},
Expand All @@ -432,8 +432,8 @@ describe('identifier', () => {
start: 0,
end: 17 + offset,
text: statements[0],
type: 'UNKNOWN',
executionType: 'UNKNOWN',
type: 'BEGIN_TRANSACTION',
executionType: 'TRANSACTION',
parameters: [],
tables: [],
},
Expand All @@ -450,8 +450,8 @@ describe('identifier', () => {
start: 29 + offset,
end: 35 + offset,
text: statements[2],
type: 'UNKNOWN',
executionType: 'UNKNOWN',
type: 'COMMIT',
executionType: 'TRANSACTION',
parameters: [],
tables: [],
},
Expand Down
Loading