Skip to content

perf(parser): code-unit scanner in DestructiveSqlDetector.stripCommentsAndStrings (#1511) - #1603

Merged
ZhuchkaTriplesix merged 1 commit into
devfrom
issue/1511-destructive-sql-detector
Oct 15, 2026
Merged

ZhuchkaTriplesix merged 1 commit into
devfrom
issue/1511-destructive-sql-detector

Conversation

@ZhuchkaTriplesix

Copy link
Copy Markdown
Member

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.

  • The loop reads code units and delegates to `_skipLineComment`, `_skipBlockComment`, `_skipDollarQuote` and `_skipStringLiteral`, each returning the next offset.
  • Dollar tags are scanned by hand; the closing tag is found with `indexOf` from the end of the opening tag, no substring of the remainder.
  • Text between tokens is appended in runs.
  • The `\bWHERE\b` and `\bSET\b` regexes used per statement are static now.

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

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
@github-actions github-actions Bot added parser Theme parser epic label: parser performance Theme parser epic label: performance core Core library logic and services P2 Medium priority / Parity & Refactoring labels Oct 15, 2026
@github-actions github-actions Bot added this to the 0.5.0 milestone Oct 15, 2026
@ZhuchkaTriplesix
ZhuchkaTriplesix merged commit 3acfe51 into dev Oct 15, 2026
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core Core library logic and services P2 Medium priority / Parity & Refactoring parser Theme parser epic label: parser performance Theme parser epic label: performance

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant