Fix: allow multiple SQL statements in sqlmanager plugin
Fix Kate SQL Plugin PostgreSQL DDL Support and Multiple Statement Execution
Description
This PR fixes critical issues in the Kate SQL plugin's query execution engine that prevented proper PostgreSQL support and multiple statement execution.
Problem
The current SQL plugin has two major limitations:
-
PostgreSQL DDL Statement Failures: The plugin attempts to use prepared statements for ALL queries, including DDL statements (CREATE, ALTER, DROP) which PostgreSQL does not support in prepared statements, causing errors like:
ERROR: syntax error at or near "CREATE" PREPARE qpsqlpstmt_2 AS CREATE TABLE voters; -
Single Statement Limitation: The plugin can only execute one SQL statement per execution, making it impractical for running migration scripts or multiple related commands.
Solution
1. Smart Query Type Detection
- Added DDL detection for statements starting with: CREATE, DROP, ALTER, TRUNCATE, COMMENT ON, GRANT, REVOKE, BEGIN, COMMIT, ROLLBACK
- PostgreSQL-specific handling for additional statements: VACUUM, REINDEX, ANALYZE
- Direct execution for non-preparable statements while maintaining prepared statement benefits for compatible queries
2. Multiple Statement Support
-
Advanced SQL parsing that properly handles:
- Semicolons within string literals
- Single-line (--) and multi-line (/* */) comments
- Parentheses in CREATE TABLE statements
- Statement boundary detection that respects SQL syntax context
3. Enhanced Error Handling
- Better error messages that include the problematic statement
- Graceful fallback from prepared to direct execution when preparation fails
- Proper result tracking across multiple statements
Key Changes
sqlmanager.cpp
-
Rewrote
runQuerymethod with comprehensive SQL parsing - Added query classification logic to distinguish between DDL and DML
- Implemented context-aware statement splitting
- Enhanced PostgreSQL-specific optimizations
Testing
The fix has been validated with:
-
✅ PostgreSQL: CREATE TABLE, ALTER TABLE, DROP TABLE commands -
✅ SQLite: Multiple statements in single execution -
✅ Mixed scripts: DDL and DML statements in the same file -
✅ Complex SQL: Statements with strings, comments, and parentheses
Backward Compatibility
- Fully backward compatible with existing functionality
- Maintains prepared statement benefits for compatible queries
- No changes to public API or configuration interfaces
Benefits
- PostgreSQL DDL Support: Users can now execute schema changes in PostgreSQL
- Batch Execution: Multiple SQL statements can be executed in a single operation
- Better User Experience: No more "Preparing the query failed" errors for valid SQL
- Database Portability: Consistent behavior across different database systems
Example Usage
Before (would fail):
CREATE TABLE users (id SERIAL PRIMARY KEY, name VARCHAR(100));
INSERT INTO users (name) VALUES ('John Doe');
SELECT * FROM users;
After (executes successfully): All three statements execute in sequence within a single operation.
Related Issues
Fixes the long-standing limitation where users had to execute SQL statements one at a time and could not use DDL commands with PostgreSQL databases.
Checklist
-
Code follows Kate's coding conventions -
PR description clearly describes problem and solution -
Changes have been tested with multiple database systems -
No regression in existing functionality -
Documentation updated if applicable
This fix significantly enhances the Kate SQL plugin's usability for database development and administration tasks, making it a more robust tool for working with multiple database systems.