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
2 changes: 1 addition & 1 deletion packages/core/src/utils/sql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const UPDATE_RE = new RegExp(
);
const DELETE_RE = new RegExp(`^\\s*(?<operation>DELETE)\\s+FROM\\s+(?<table>${TABLE_NAME})`, 'i');

const SELECT_RE = /^\s*\(?\s*(?<operation>SELECT)\b/i;
const SELECT_RE = /^\s*(?:\(\s*)?(?<operation>SELECT)\b/i;

const PRAGMA_RE = /^\s*(?<operation>PRAGMA)\s+(?<command>\S+)/i;

Expand Down
13 changes: 13 additions & 0 deletions packages/core/test/lib/utils/sql.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ describe('getSqlQuerySummary', () => {
['select count(*) from orders', 'select orders'],
['SELECT DISTINCT email FROM subscribers WHERE active = ?', 'SELECT subscribers'],
[' SELECT * FROM users', 'SELECT users'],
['(SELECT * FROM users)', 'SELECT users'],
[' \t( \nselect * from users)', 'select users'],
])('%j => %j', (input, expected) => {
expect(getSqlQuerySummary(input)).toBe(expected);
});
Expand Down Expand Up @@ -165,6 +167,17 @@ describe('getSqlQuerySummary', () => {
it('handles leading whitespace in fallback', () => {
expect(getSqlQuerySummary(' VACUUM')).toBe('VACUUM');
});

it('handles long leading whitespace without excessive backtracking', () => {
const query = `${' '.repeat(100_000)}VACUUM`;
const start = performance.now();

const summary = getSqlQuerySummary(query);
const duration = performance.now() - start;

expect(summary).toBe('VACUUM');
expect(duration).toBeLessThan(1_000);
});
});

describe('set operations', () => {
Expand Down
Loading