Add ExecSQL support to libsq #502
Open
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.
ExecSQL Fix - Branch Summary
Branch:
fix/execsql-supportThis branch contains a fundamental correctness fix for how
sq sqlexecutes DDL/DML statements.Quick Start
Run All Tests
See What Changed
The Problem
The CLI was using
QuerySQL()(which callsdb.QueryContext()) for ALL SQL statements:Why This Was Wrong
Per Go's database/sql documentation:
QueryContextis for queries that return rows (SELECT, SHOW, etc.)ExecContextis for statements that don't return rows (CREATE, INSERT, UPDATE, DELETE, DROP, etc.)Consequences
The Solution
Files Changed
1. New
ExecSQL()FunctionLocation:
libsq/libsq.go:125-1622. SQL Type Detection
Location:
cli/cmd_sql.go:119-1603. Proper Routing
Location:
cli/cmd_sql.go:162-192Evidence: This Was a Bug for ALL Databases
Test Results
Unit Tests (37 test cases)
Semantic Correctness Tests
CLI Demonstration
$ ./test-execsql-fix.sh === Testing SQLite === ✓ CREATE TABLE returned affected rows message ✓ INSERT correctly reported 3 rows affected ✓ SELECT confirms 3 rows exist ✓ UPDATE correctly reported 2 rows affected ✓ DELETE correctly reported 1 row affected ✓ SELECT confirms 2 rows remain after DELETE ✓ DROP TABLE returned affected rows message ✓ All SQLite tests passed!Comparison Table
Commits
1.
ccaa2d1- Core Fix2.
ee136a9- Unit Tests3. Documentation
For Reviewers
Key Questions This Fix Answers
Q: Was this really a bug if MySQL/Postgres worked?
A: Yes. Just because lenient drivers accept incorrect usage doesn't make it correct. The code violated Go's database/sql contract.
Q: How do we prove it was wrong?
A: Three ways:
Q: Could this break anything?
A: No. For queries (SELECT), behavior is unchanged. For statements (INSERT, etc.), output now shows correct affected counts instead of 0.
Q: Why extract this from the ClickHouse PR?
A: To show this is a fundamental correctness fix, not a "ClickHouse workaround". This fix benefits all databases.
Testing Checklist
go test ./cli -run TestIsQueryStatement -vgo test ./libsq -run TestQueryVsExec -v./test-execsql-fix.shReferences
Go Documentation
Related Issues
Summary
This fix: