Repository navigation
perf(parser): code-unit scanner in DestructiveSqlDetector.stripCommentsAndStrings (#1511) - #1603
Merged
Merged
Conversation
stripCommentsAndStrings compiled a RegExp and copied the rest of the script (sql.substring(i)) at every $, including $1 / $2 parameters, and indexed the text one-character string at a time. The loop now reads code units, with _skipLineComment, _skipBlockComment, _skipDollarQuote and _skipStringLiteral returning the next offset, and appends the untouched text in runs. The WHERE and SET regexes are static. The output is the same; a test compares it with the previous implementation on hand-written and random scripts. Closes #1511
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
`stripCommentsAndStrings` (runs before every destructive-statement check) compiled `RegExp(r'^$([a-zA-Z0-9_]*)$')` and copied the remainder of the script with `sql.substring(i)` at every `$` (so at every `$1`, `$2` parameter), and read the text one one-character string at a time.
Behaviour is unchanged on purpose, including the quirks (a backslash escapes inside single quotes, an unterminated comment / string / dollar quote runs to the end, the line break after a `--` comment is kept). This detector guards destructive statements, so the new tests compare against the previous implementation:
Tests (not run locally; CI will)
`test/core/database/destructive_sql_strip_test.dart` holds the old implementation as the reference and checks hand-written scripts (unterminated forms, `$1`, `$tag$`, surrogate pairs), 3000 seeded random SQL-like strings, and a 20,000-line script.
Closes #1511