-
-
Notifications
You must be signed in to change notification settings - Fork 285
fix(editor): make statement splitting dollar-quote aware for PostgreSQL (#1559) #1560
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
5a1b244
fix(editor): make statement splitting dollar-quote aware for PostgreS…
datlechin 7d5818b
refactor(editor): share the dollar-quote token-start guard across bot…
datlechin 3403999
Merge branch 'main' into worktree-fix-1559-dollar-quote
datlechin ababc0b
Merge branch 'main' into worktree-fix-1559-dollar-quote
datlechin 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
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
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
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
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
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
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
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
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
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
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,74 @@ | ||
| // | ||
| // SqlDollarQuote.swift | ||
| // TablePro | ||
| // | ||
|
|
||
| import Foundation | ||
|
|
||
| enum SqlDollarQuote { | ||
| enum Opener { | ||
| case opener(length: Int, tag: String) | ||
| case notOpener | ||
| case needsMoreData | ||
| } | ||
|
|
||
| static let dollar: unichar = 0x24 | ||
|
|
||
| static func isIdentifierStart(_ ch: unichar) -> Bool { | ||
| (ch >= 0x41 && ch <= 0x5A) || (ch >= 0x61 && ch <= 0x7A) || ch == 0x5F | ||
| } | ||
|
|
||
| static func isIdentifierPart(_ ch: unichar) -> Bool { | ||
| isIdentifierStart(ch) || (ch >= 0x30 && ch <= 0x39) | ||
| } | ||
|
|
||
| /// Whether a `$` following this character is part of the preceding identifier, | ||
| /// per PostgreSQL's rule that a dollar quote must be separated from a | ||
| /// preceding identifier by whitespace (so `a$$b` is one identifier, not an | ||
| /// opener). | ||
| static func isIdentifierContinuation(_ ch: unichar) -> Bool { | ||
| isIdentifierPart(ch) || ch == dollar | ||
| } | ||
|
|
||
| /// Resolves a `$` at `pos` to a dollar-quote opener, a positional parameter | ||
| /// like `$1`, or a non-tag dollar. A `$` glued to a preceding identifier is | ||
| /// not an opener. Returns `needsMoreData` when the buffer ends mid-tag; a | ||
| /// whole-string caller treats that as `notOpener`. | ||
| static func scanOpener(at pos: Int, in buffer: NSString, bufLen: Int) -> Opener { | ||
| if pos > 0, isIdentifierContinuation(buffer.character(at: pos - 1)) { | ||
| return .notOpener | ||
| } | ||
| var p = pos + 1 | ||
| while p < bufLen { | ||
| let ch = buffer.character(at: p) | ||
| if ch == dollar { | ||
| let tagLen = p - pos - 1 | ||
| if tagLen == 0 { | ||
| return .opener(length: 2, tag: "") | ||
| } | ||
| if !isIdentifierStart(buffer.character(at: pos + 1)) { | ||
| return .notOpener | ||
| } | ||
| let tag = buffer.substring(with: NSRange(location: pos + 1, length: tagLen)) | ||
| return .opener(length: tagLen + 2, tag: tag) | ||
| } | ||
| if !isIdentifierPart(ch) { | ||
| return .notOpener | ||
| } | ||
| p += 1 | ||
| } | ||
| return .needsMoreData | ||
| } | ||
|
|
||
| /// Whether the closing delimiter for `tag` starts at `pos`. The tag match is | ||
| /// exact and case-sensitive, per PostgreSQL. | ||
| static func matchesClose(at pos: Int, tag: String, in buffer: NSString, bufLen: Int) -> Bool { | ||
| let closeLen = (tag as NSString).length + 2 | ||
| guard pos + closeLen <= bufLen else { return false } | ||
| if buffer.character(at: pos) != dollar { return false } | ||
| if buffer.character(at: pos + closeLen - 1) != dollar { return false } | ||
| if tag.isEmpty { return true } | ||
| let tagRange = NSRange(location: pos + 1, length: (tag as NSString).length) | ||
| return buffer.substring(with: tagRange) == tag | ||
| } | ||
| } |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For PostgreSQL, this now makes
DO $$ BEGIN DROP TABLE t; END $$;count as a single statement, butclassifyTierstill only checks top-level prefixes and returns.safeforDO. The AI/MCPexecute_querypaths rely on this multi-statement guard plus the tier check before running with pre-cleared capabilities, so a destructive operation hidden inside a dollar-quoted DO block can bypass the dedicated confirmation tool and safe-mode write/destructive classification. Please either classifyDOblocks conservatively for PostgreSQL or keep a separate non-UI guard for these executable blocks.Useful? React with 👍 / 👎.