-
Notifications
You must be signed in to change notification settings - Fork 690
Fix backslash escape handling in SQL string literals with performance optimization #2335
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Copilot
wants to merge
11
commits into
develop
Choose a base branch
from
copilot/fix-ascii-escapes
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
8b5606e
Initial plan
Copilot 323e526
Fix ASCII backslash escaping in SQL string literals
Copilot 4d68d1f
Revert unintended package.json changes
Copilot 03e5382
Optimize regex patterns by pre-compiling them
Copilot 927df05
Revert unintended yarn.lock registry URL changes
Copilot 3e12c42
Add tests for edge case: escaped backslash followed by escaped quote
Copilot 20eedaf
Revert yarn.lock to avoid unintended changes
Copilot 0c8c2ac
Fix backslash replacement to use String.fromCharCode for proper escaping
Copilot edcea84
Add comprehensive quote and backslash escaping tests
Copilot 8235c49
Add double quote and parameterized query tests
Copilot 472f3a8
merge2
mathiasrw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mathiasrw marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,195 @@ | ||
| if (typeof exports === 'object') { | ||
| var assert = require('assert'); | ||
| var alasql = require('..'); | ||
| } | ||
|
|
||
| describe('Test 489 - ASCII with backslash character', function () { | ||
| it('A) SELECT ASCII with backslash should return 92', function () { | ||
| var res = alasql("VALUE OF SELECT ASCII('\\\\')"); | ||
| assert.equal(res, 92); | ||
| }); | ||
|
|
||
| it('B) SELECT ASCII with backslash in column should return 92', function () { | ||
| var res = alasql("SELECT ASCII('\\\\')"); | ||
| assert.equal(res[0]["ASCII('\\')"], 92); | ||
| }); | ||
|
|
||
| it('C) Verify backslash character works in string literals', function () { | ||
| var res = alasql("VALUE OF SELECT '\\\\'"); | ||
| assert.equal(res, '\\'); | ||
| }); | ||
|
|
||
| it('D) Escaped backslash followed by escaped quote (edge case)', function () { | ||
| // In SQL: '\\'' means backslash followed by quote | ||
| // \\ = one backslash, '' = one quote (SQL quote doubling) | ||
| var res = alasql("VALUE OF SELECT '\\\\'''"); | ||
| assert.equal(res.length, 2); | ||
| assert.equal(res.charCodeAt(0), 92); // backslash | ||
| assert.equal(res.charCodeAt(1), 39); // quote | ||
| assert.equal(res, "\\'"); | ||
| }); | ||
|
|
||
| it('E) Multiple backslashes followed by quote', function () { | ||
| // In SQL: '\\\\'' means two backslashes followed by quote | ||
| // \\\\ = two backslashes, '' = one quote | ||
| var res = alasql("VALUE OF SELECT '\\\\\\\\'''"); | ||
| assert.equal(res.length, 3); | ||
| assert.equal(res.charCodeAt(0), 92); // backslash | ||
| assert.equal(res.charCodeAt(1), 92); // backslash | ||
| assert.equal(res.charCodeAt(2), 39); // quote | ||
| assert.equal(res, "\\\\'"); | ||
mathiasrw marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }); | ||
|
|
||
| it('F) Comprehensive quote and backslash escaping tests', function () { | ||
| // Building from fundamentals to complex combinations | ||
| // Testing single quotes, double quotes, and backslashes | ||
|
|
||
| // Basic single quote in double quotes | ||
| assert.equal(alasql('VALUE OF SELECT "\'"'), "'"); | ||
|
|
||
| // SQL quote doubling - '' becomes ' | ||
| assert.equal(alasql("VALUE OF SELECT ''''"), "'"); | ||
|
|
||
| // Double quote inside single quotes | ||
| assert.equal(alasql('VALUE OF SELECT \'""\''), '""'); | ||
|
|
||
| // Backslash escaping - \\ becomes \ | ||
| assert.equal(alasql("VALUE OF SELECT '\\\\'"), '\\'); | ||
|
|
||
| // Two backslashes - \\\\ becomes \\ | ||
| assert.equal(alasql("VALUE OF SELECT '\\\\\\\\'"), '\\\\'); | ||
|
|
||
| // Three backslashes - \\\\\\ becomes \\\ | ||
| assert.equal(alasql("VALUE OF SELECT '\\\\\\\\\\\\'"), '\\\\\\'); | ||
|
|
||
| // Four backslashes - \\\\\\\\ becomes \\\\ | ||
| assert.equal(alasql("VALUE OF SELECT '\\\\\\\\\\\\\\\\'"), '\\\\\\\\'); | ||
|
|
||
| // Five backslashes - \\\\\\\\\\ becomes \\\\\ | ||
| assert.equal(alasql("VALUE OF SELECT '\\\\\\\\\\\\\\\\\\\\'"), '\\\\\\\\\\'); | ||
|
|
||
| // Backslash followed by doubled quote - \\'' becomes \' | ||
| assert.equal(alasql("VALUE OF SELECT '\\\\'''"), "\\'"); | ||
|
|
||
| // Two backslashes followed by doubled quote - \\\\'' becomes \\' | ||
| assert.equal(alasql("VALUE OF SELECT '\\\\\\\\'''"), "\\\\'"); | ||
|
|
||
| // Three backslashes followed by doubled quote - \\\\\\'' becomes \\\' | ||
| assert.equal(alasql("VALUE OF SELECT '\\\\\\\\\\\\'''"), "\\\\\\'"); | ||
|
|
||
| // Four backslashes followed by doubled quote - \\\\\\\\'' becomes \\\\' | ||
| assert.equal(alasql("VALUE OF SELECT '\\\\\\\\\\\\\\\\'''"), "\\\\\\\\'"); | ||
|
|
||
| // Five backslashes followed by doubled quote - \\\\\\\\\\'' becomes \\\\\' | ||
| assert.equal(alasql("VALUE OF SELECT '\\\\\\\\\\\\\\\\\\\\'''"), "\\\\\\\\\\'"); | ||
|
|
||
| // Doubled quote with text - ''a'' becomes 'a' | ||
| assert.equal(alasql("VALUE OF SELECT '''a'''"), "'a'"); | ||
|
|
||
| // Text with doubled quote in middle - a''b becomes a'b | ||
| assert.equal(alasql("VALUE OF SELECT 'a''b'"), "a'b"); | ||
|
|
||
| // Backslash before doubled quote in text - a\\'' becomes a\' | ||
| assert.equal(alasql("VALUE OF SELECT 'a\\\\'''"), "a\\'"); | ||
|
|
||
| // Multiple doubled quotes - '''' becomes ' | ||
| assert.equal(alasql("VALUE OF SELECT ''''"), "'"); | ||
|
|
||
| // Two sets of doubled quotes - '''''' becomes '' | ||
| assert.equal(alasql("VALUE OF SELECT ''''''"), "''"); | ||
|
|
||
| // Three sets of doubled quotes - '''''''' becomes ''' | ||
| assert.equal(alasql("VALUE OF SELECT ''''''''"), "'''"); | ||
|
|
||
| // Double quotes with backslash inside | ||
| assert.equal(alasql('VALUE OF SELECT "\\\\"'), '\\'); | ||
|
|
||
| // Complex: backslash, quote, backslash, quote in one string - \\''\\' | ||
| assert.equal(alasql("VALUE OF SELECT '\\\\''\\\\'''"), "\\'\\'"); | ||
|
|
||
| // Very complex: multiple backslashes and quotes | ||
| var result = alasql("VALUE OF SELECT '\\\\\\\\''\\\\'''"); | ||
| assert.equal(result, "\\\\'\\'"); | ||
| }); | ||
|
|
||
| it('G) Double quote escaping tests', function () { | ||
| // Note: In SQL, double quotes are typically for identifiers, not string literals | ||
| // AlaSQL allows both single and double quotes for strings, but only single quote | ||
| // doubling works as an escape sequence. Double quotes inside double-quoted strings | ||
| // are treated as literal characters, not escape sequences. | ||
|
|
||
| // Double-quoted string with literal double quotes (no escaping) | ||
| assert.equal(alasql('VALUE OF SELECT """"'), '""'); | ||
|
|
||
| // Double-quoted string (alternative syntax, no escaping needed) | ||
| assert.equal(alasql('VALUE OF SELECT """"'), '""'); | ||
|
|
||
| // Double quote inside single quotes (no escaping, just literal characters) | ||
| assert.equal(alasql('VALUE OF SELECT \'""\''), '""'); | ||
|
|
||
| // Single double quote in double-quoted string | ||
| assert.equal(alasql('VALUE OF SELECT "x"'), 'x'); | ||
|
|
||
| // Multiple double quotes are literal in double-quoted strings | ||
| assert.equal(alasql('VALUE OF SELECT "a""b"'), 'a""b'); | ||
| }); | ||
|
|
||
| it('H) Parameterized queries with escaped data', function () { | ||
| // Test how the parser handles escaped characters when data is passed in | ||
|
|
||
| // Single quote in parameter | ||
| var res1 = alasql('VALUE OF SELECT ?', ["'"]); | ||
| assert.equal(res1, "'"); | ||
|
|
||
| // Double single quotes in parameter | ||
| var res2 = alasql('VALUE OF SELECT ?', ["''"]); | ||
| assert.equal(res2, "''"); | ||
|
|
||
| // Backslash in parameter | ||
| var res3 = alasql('VALUE OF SELECT ?', ['\\']); | ||
| assert.equal(res3, '\\'); | ||
|
|
||
| // Multiple backslashes in parameter | ||
| var res4 = alasql('VALUE OF SELECT ?', ['\\\\']); | ||
| assert.equal(res4, '\\\\'); | ||
|
|
||
| // Backslash followed by quote in parameter | ||
| var res5 = alasql('VALUE OF SELECT ?', ["\\'"]); | ||
| assert.equal(res5, "\\'"); | ||
|
|
||
| // Double quote in parameter | ||
| var res6 = alasql('VALUE OF SELECT ?', ['"']); | ||
| assert.equal(res6, '"'); | ||
|
|
||
| // Double quotes in parameter | ||
| var res7 = alasql('VALUE OF SELECT ?', ['""']); | ||
| assert.equal(res7, '""'); | ||
|
|
||
| // Complex: backslash, quote, backslash in parameter | ||
| var res8 = alasql('VALUE OF SELECT ?', ["\\'\\"]); | ||
| assert.equal(res8, "\\'\\"); | ||
|
|
||
| // Test round-trip: insert and select with escaped characters | ||
| alasql('CREATE TABLE test_escape (val STRING)'); | ||
|
|
||
| // Insert single quote | ||
| alasql('INSERT INTO test_escape VALUES (?)', ["'"]); | ||
| var result1 = alasql('SELECT val FROM test_escape'); | ||
| assert.equal(result1[0].val, "'"); | ||
|
|
||
| // Clear and insert backslash | ||
| alasql('DELETE FROM test_escape'); | ||
| alasql('INSERT INTO test_escape VALUES (?)', ['\\']); | ||
| var result2 = alasql('SELECT val FROM test_escape'); | ||
| assert.equal(result2[0].val, '\\'); | ||
|
|
||
| // Clear and insert backslash + quote | ||
| alasql('DELETE FROM test_escape'); | ||
| alasql('INSERT INTO test_escape VALUES (?)', ["\\'"]); | ||
| var result3 = alasql('SELECT val FROM test_escape'); | ||
| assert.equal(result3[0].val, "\\'"); | ||
|
|
||
| // Clean up | ||
| alasql('DROP TABLE test_escape'); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.