-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Enhance interpolateParams to correctly handle placeholders #1732
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
base: master
Are you sure you want to change the base?
Conversation
""" WalkthroughA new state-machine-based function was introduced to accurately find parameter placeholders in SQL queries, ignoring those inside comments, strings, and backticks. The parameter interpolation logic was updated to use this function and refactored to handle argument escaping with a simplified flag. Corresponding tests were updated to verify correct handling of placeholders in various SQL contexts including comments and backticks. Changes
Sequence Diagram(s)sequenceDiagram
participant Client
participant mysqlConn
Client->>mysqlConn: interpolateParams(query, args)
mysqlConn->>mysqlConn: parse query with state machine to find real placeholders
mysqlConn->>mysqlConn: interpolate arguments only at valid placeholders
mysqlConn-->>Client: return interpolated query or error
Possibly related PRs
Suggested reviewers
Warning Review ran into problems🔥 ProblemsCheck-run timed out after 90 seconds. Some checks/pipelines were still in progress when the timeout was reached. Consider increasing the reviews.tools.github-checks.timeout_ms value in your CodeRabbit configuration to allow more time for checks to complete. 📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (4)
🧰 Additional context used🧠 Learnings (4)📓 Common learnings
utils_test.go (2)
connection.go (3)
utils.go (1)
🧬 Code Graph Analysis (1)connection_test.go (1)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (20)
🔇 Additional comments (14)
✨ Finishing Touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
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.
Actionable comments posted: 1
🧹 Nitpick comments (3)
connection.go (2)
248-271
: Consider using idiomatic Go constant definitions.The byte constants use runtime conversions and non-standard naming. Consider defining them as proper constants:
-var ( - QUOTE_BYTE = byte('\'') - DBL_QUOTE_BYTE = byte('"') - BACKSLASH_BYTE = byte('\\') - QUESTION_MARK_BYTE = byte('?') - SLASH_BYTE = byte('/') - STAR_BYTE = byte('*') - HASH_BYTE = byte('#') - MINUS_BYTE = byte('-') - LINE_FEED_BYTE = byte('\n') - RADICAL_BYTE = byte('`') -) +const ( + quoteByte = '\'' + dblQuoteByte = '"' + backslashByte = '\\' + questionMarkByte = '?' + slashByte = '/' + starByte = '*' + hashByte = '#' + minusByte = '-' + lineFeedByte = '\n' + backtickByte = '`' +)
272-274
: Simplify slice initialization.The slice initialization can be simplified for better readability:
-paramPositions := make([]int, 0) +var paramPositions []intconnection_test.go (1)
206-214
: Consider adding edge case test coverage.While the current test cases are comprehensive, consider adding tests for additional edge cases:
// Escaped quotes in strings {"SELECT 'don\\'t', ?", []driver.Value{int64(42)}, "SELECT 'don\\'t', 42", false}, // Double-quoted strings {"SELECT \"?\", ?", []driver.Value{int64(42)}, "SELECT \"?\", 42", false}, // Nested quotes {"SELECT \"It's a string\", ?", []driver.Value{int64(42)}, "SELECT \"It's a string\", 42", false}, // Comment without newline at end {"SELECT ? -- comment", []driver.Value{int64(42)}, "SELECT 42 -- comment", false},
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
connection.go
(5 hunks)connection_test.go
(1 hunks)
🧰 Additional context used
🧠 Learnings (2)
📓 Common learnings
Learnt from: shogo82148
PR: go-sql-driver/mysql#1576
File: CHANGELOG.md:1-6
Timestamp: 2024-07-26T20:55:56.049Z
Learning: PRs #1562 and #1570 in the go-sql-driver/mysql repository collectively address the same underlying issue related to fixing a race condition when a context is canceled, as documented in the CHANGELOG.md for version 1.8.1.
connection.go (1)
Learnt from: shogo82148
PR: go-sql-driver/mysql#1576
File: CHANGELOG.md:1-6
Timestamp: 2024-07-26T20:55:56.049Z
Learning: PRs #1562 and #1570 in the go-sql-driver/mysql repository collectively address the same underlying issue related to fixing a race condition when a context is canceled, as documented in the CHANGELOG.md for version 1.8.1.
🧬 Code Graph Analysis (1)
connection_test.go (1)
dsn.go (1)
Config
(37-84)
⏰ Context from checks skipped due to timeout of 90000ms (20)
- GitHub Check: test (windows-latest, 1.24, mariadb-10.11)
- GitHub Check: test (windows-latest, 1.24, mariadb-10.6)
- GitHub Check: test (windows-latest, 1.24, mariadb-11.2)
- GitHub Check: test (ubuntu-latest, 1.24, mariadb-10.11)
- GitHub Check: test (macos-latest, 1.24, mariadb-10.11)
- GitHub Check: test (macos-latest, 1.24, mariadb-10.6)
- GitHub Check: test (ubuntu-latest, 1.24, mariadb-10.5)
- GitHub Check: test (windows-latest, 1.24, 9.0)
- GitHub Check: test (macos-latest, 1.24, mariadb-11.2)
- GitHub Check: test (ubuntu-latest, 1.24, mariadb-10.6)
- GitHub Check: test (macos-latest, 1.24, 9.0)
- GitHub Check: test (ubuntu-latest, 1.24, mariadb-11.1)
- GitHub Check: test (macos-latest, 1.24, 8.0)
- GitHub Check: test (ubuntu-latest, 1.24, 5.7)
- GitHub Check: test (ubuntu-latest, 1.24, mariadb-11.4)
- GitHub Check: test (ubuntu-latest, 1.24, 9.0)
- GitHub Check: test (ubuntu-latest, 1.24, mariadb-11.2)
- GitHub Check: test (ubuntu-latest, 1.24, 8.4)
- GitHub Check: lint
- GitHub Check: Analyze (go)
🔇 Additional comments (5)
connection.go (4)
248-341
: Excellent state machine implementation for SQL parsing.The
findParamPositions
function implements a comprehensive state machine that correctly handles various SQL contexts including comments (--
,#
,/* */
), string literals, backticks, and escape sequences. The logic properly tracks state transitions and accurately identifies real parameter placeholders while ignoring those inside comments or strings.The handling of the
noBackslashEscapes
flag is particularly well done, respecting MySQL's SQL mode settings.
344-348
: Improved parameter counting logic.The change from simple character counting to using
findParamPositions
is a significant improvement. The function now correctly validates that the number of real parameters matches the number of arguments before attempting interpolation.
400-404
: Simplified and consistent escaping logic.The refactored escaping logic using the
noBackslashEscapes
flag is cleaner and more consistent. The conditional logic is now easier to understand and maintain across different data types (JSON, bytes, strings).Also applies to: 411-415, 420-424
362-435
: Correct implementation of position-based parameter replacement.The new interpolation loop correctly processes parameters based on their actual positions in the query rather than simple sequential replacement. The
lastIdx
tracking ensures that query segments are properly appended between parameter substitutions.connection_test.go (1)
190-236
: Comprehensive test coverage for enhanced parameter interpolation.The
TestInterpolateParamsWithComments
test provides excellent coverage of the new parameter parsing logic. It correctly tests:
- Single-line comments with
--
and#
- Multi-line comments with
/* */
- String literals with single quotes
- Backtick identifiers
- Complex queries with multiple comment types
- Error cases where parameter count doesn't match argument count
The test case on line 216 correctly expects
driver.ErrSkip
because there are 4 real placeholders but only 3 arguments provided.
Please run BenchmarkInterpolation and write the results. Is this algorithm identical to the MySQL or MariaDB? And are there examples of this algorithm implemented in other database drivers? |
…s with comments, strings, and backticks. * Add `findParamPositions` to identify real parameter positions * Update and expand related tests.
force push implementation with performance improvement. current implementation :
initial proposal:
new proposal:
There is only one loop now, and escaping methods for parameters have been improved. about other driver using that :
|
Enhance client side statement to correctly handle placeholders in queries with comments, strings, and backticks.
Add findParamPositions to identify real parameter positions
Update and expand related tests.