Skip to content

Defer prepared statement long data writes - #1810

Merged
methane merged 1 commit into
go-sql-driver:masterfrom
methane:refactor-longdata
Sep 6, 2026
Merged

Defer prepared statement long data writes#1810
methane merged 1 commit into
go-sql-driver:masterfrom
methane:refactor-longdata

Conversation

@methane

@methane methane commented Sep 6, 2026

Copy link
Copy Markdown
Member

Description

This pull request refactors the handling of "long data" parameters in prepared statement execution to ensure all arguments are validated before any data is sent to the MySQL server. This prevents partial writes and improves error handling. The changes also add comprehensive tests to verify correct behavior in various edge cases.

Prepared Statement Execution Improvements:

  • Refactored writeExecutePacket to collect long data parameters first, validate all arguments and packet size, and only write to the server after validation passes. This prevents partial long data writes if an argument is invalid or the packet is too large. [1] [2] [3] [4] [5]
  • Added a reset method to mysqlStmt to reset the statement on the server if a long data write fails, ensuring the server state remains consistent.

Testing Enhancements:

  • Added several tests in packets_test.go to verify:
    • No writes occur if argument validation fails.
    • No writes occur if the packet size is too large.
    • Long data parameters are sent before the execute command, and in the correct order.
    • The statement is reset if a long data write fails, and the connection is closed if reset fails.
  • Improved the mockConn test helper to support write error injection and tracking. [1] [2]

These changes make prepared statement execution more robust and the codebase better tested.

Checklist

  • Code compiles correctly
  • Created tests which fail without the change (if possible)
  • All tests passing
  • Extended the README / documentation, if necessary
  • Added myself / the copyright holder to the AUTHORS file

@coderabbitai

coderabbitai Bot commented Sep 6, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Team

Run ID: dc4d304e-6e63-4bb4-a01f-c963b9eeac2d

📥 Commits

Reviewing files that changed from the base of the PR and between 7b50e17 and 9424329.

📒 Files selected for processing (2)
  • packets.go
  • packets_test.go

Included review availability: Your plan provides up to 10 included reviews per hour; 9 remain after this review.


Walkthrough

writeExecutePacket now defers large parameters, validates arguments and packet size before writing, sends long data before execution, and resets the statement after long-data write failures.

Changes

Statement execution

Layer / File(s) Summary
Deferred long-data collection
packets.go
Large []byte and string arguments are collected in stmtLongData values instead of being sent during argument processing.
Validated long-data execution
packets.go
The execute packet size is validated before writes. Deferred long data is sent before COM_STMT_EXECUTE. Write failures trigger COM_STMT_RESET, with connection closure if reset fails.
Execution validation and recovery tests
packets_test.go
Tests cover validation before writes, packet-size checks, command ordering, configurable write errors, and statement reset handling.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Merge Risk: ⚪ Minimal · up to 94243

Prepared-statement long data is now validated before transmission and recovers protocol state after write failures. The covered behavior introduces no identified merge-blocking risk.

Sequence Diagram(s)

sequenceDiagram
  participant writeExecutePacket
  participant mysqlStmt
  participant Connection
  writeExecutePacket->>mysqlStmt: Validate arguments and packet size
  writeExecutePacket->>Connection: Send COM_STMT_SEND_LONG_DATA
  Connection-->>writeExecutePacket: Return write result
  writeExecutePacket->>Connection: Send COM_STMT_EXECUTE
  writeExecutePacket->>mysqlStmt: Reset after long-data write error
  mysqlStmt->>Connection: Send COM_STMT_RESET
Loading
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 6 functions across 2 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the primary change: deferring prepared statement long data writes.
Description check ✅ Passed The description directly explains the long data handling refactor, reset behavior, error handling, and related tests.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
  • Fix all pre-merge checks with AI
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coveralls

Copy link
Copy Markdown

Coverage Status

coverage: 84.672% (+0.2%) from 84.503% — methane:refactor-longdata into go-sql-driver:master

@methane
methane merged commit 6a57eb1 into go-sql-driver:master Sep 6, 2026
27 checks passed
@methane
methane deleted the refactor-longdata branch September 6, 2026 12:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants