more sql verbs handled

This commit is contained in:
Rolands 2024-07-18 13:17:31 +03:00
parent b875c0597b
commit c9052a75af
2 changed files with 36 additions and 2 deletions

View file

@ -13,7 +13,10 @@ const table_extract_regex = '(?<tables>[\\w\\s\\.,_]+?)'
const Verb2TableRegex = {
SELECT: RegExp('FROM' + table_extract_regex + '(?:WHERE|ON|USING|;|$)', 'mi'),
INSERT: RegExp('INTO' + table_extract_regex + '(?:\\(|VALUES|DEFAULT)', 'i'),
UPDATE: RegExp('UPDATE\\s+(?:OR\\s+(?:ABORT|FAIL|IGNORE|REPLACE|ROLLBACK))?' + table_extract_regex + 'SET', 'i')
UPDATE: RegExp('UPDATE\\s+(?:OR\\s+(?:ABORT|FAIL|IGNORE|REPLACE|ROLLBACK))?' + table_extract_regex + 'SET', 'i'),
DROP: RegExp('TABLE(?:\\s+IF EXISTS)?\\s+' + table_extract_regex + '(?:$|;)', 'mi'),
CREATE: RegExp('TABLE(?:\\s+IF NOT EXISTS)?\\s+' + table_extract_regex + '(?:AS|\\(|;|$)', 'mi'),
ALTER: RegExp('TABLE(?:\\s+IF NOT EXISTS)?\\s+' + table_extract_regex + '(?:RENAME|ADD|DROP)', 'i'),
}
//query helper functions
@ -57,5 +60,6 @@ export function ParseQueryTables(q: string): string[] {
//always returns uppercase verb if found
export function ParseQueryVerb(q: string): string | null {
return q.match(/^(\s+)?(?<verb>SELECT|INSERT|DROP|UPDATE|CREATE)/mi)?.groups?.verb?.toUpperCase() || null;
return q.match(/^(\s+)?(?<verb>\w+)/mi)?.groups?.verb?.toUpperCase() || null;
// return q.match(/^(\s+)?(?<verb>SELECT|INSERT|DROP|UPDATE|CREATE|ALTER)/mi)?.groups?.verb?.toUpperCase() || null;
}

View file

@ -141,6 +141,36 @@ if (test_cases.includes('table_parsing') || all) {
str = 'UPDATE OR ABORT Users SET name = "bob";';
test_obj('complex UPDATE', ParseQueryTables(str), ['Users']);
// https://www.sqlite.org/lang_altertable.html
str = 'ALTER TABLE Users ADD n INT;';
test_obj('ALTER', ParseQueryTables(str), ['Users']);
// https://www.sqlite.org/lang_createtable.html
str = 'CREATE TABLE Users;';
test_obj('CREATE', ParseQueryTables(str), ['Users']);
str = 'CREATE TEMP TABLE Users;';
test_obj('CREATE temp', ParseQueryTables(str), ['Users']);
str = 'CREATE TABLE IF NOT EXISTS Users;';
test_obj('CREATE', ParseQueryTables(str), ['Users']);
str = 'CREATE TABLE Users AS (...);';
test_obj('CREATE as', ParseQueryTables(str), ['Users']);
str = 'CREATE TABLE Users (name VARCHAR(50));';
test_obj('CREATE with fields', ParseQueryTables(str), ['Users']);
// https://www.sqlite.org/lang_droptable.html
str = 'DROP TABLE Users;';
test_obj('DROP', ParseQueryTables(str), ['Users']);
str = 'DROP TABLE Users';
test_obj('DROP no ending ;', ParseQueryTables(str), ['Users']);
str = 'DROP TABLE IF EXISTS Users;';
test_obj('DROP if exists', ParseQueryTables(str), ['Users']);
}
if (test_cases.includes('verb_parsing') || all) {