Skip to content

Fix: Support PRAGMA statements and finalize results before commit (#199) - #200

Open
givemehat wants to merge 1 commit into
cs50:mainfrom
givemehat:fix/pragma-statements-199
Open

givemehat wants to merge 1 commit into
cs50:mainfrom
givemehat:fix/pragma-statements-199

Conversation

@givemehat

Copy link
Copy Markdown

Summary

Resolves #199 by adding support for SQLite PRAGMA statements in cs50.SQL.

Problem & Root Cause

  1. Command Recognition: In sqlparse, PRAGMA is tokenized as an Identifier (Token.Name) rather than a standard DDL/DML keyword (Token.Keyword). Consequently, full_statement evaluated to an empty string, and command was parsed as None.
  2. Transaction & Commit Conflict: When executing in autocommit mode (self._autocommit = True), cs50.SQL wrapped the query in BEGIN ... COMMIT. Certain PRAGMA directives (such as PRAGMA journal_mode = WAL) cannot be executed within a transaction. Furthermore, for PRAGMA queries that return rows (such as PRAGMA journal_mode or PRAGMA table_info(...)), calling COMMIT while statement result rows remain unconsumed raises sqlite3.OperationalError: cannot commit transaction - SQL statements in progress.
  3. Return Format: Because command != "SELECT", any rows returned by querying PRAGMAs were discarded, returning True instead of the result set.

Solution

  1. Statement Parsing: Inspect the first non-comment, non-whitespace token to reliably identify PRAGMA statements case-insensitively, and add "PRAGMA" to commands.
  2. Autocommit Boundary: Bypass transaction wrapping (BEGIN/COMMIT) for PRAGMA statements, allowing configuration PRAGMAs to execute cleanly.
  3. Result Consumption: Fetch and coerce results before committing when statements yield rows, and return the formatted list of row dictionaries for SELECT or any PRAGMA returning rows (result.returns_rows).
  4. Unit Tests: Added test_pragma covering row-returning PRAGMAs (PRAGMA journal_mode, PRAGMA foreign_keys, PRAGMA table_info), configuration PRAGMAs (PRAGMA foreign_keys = ON), and commented statements. All 23 SQLite unit tests pass.

Closes #199.

Resolves cs50#199 by:
- Recognizing PRAGMA statements during command parsing (sqlparse parses PRAGMA as an Identifier rather than a generic SQL keyword).
- Adding PRAGMA to supported commands.
- Bypassing autocommit transaction wrapping for PRAGMA statements (some PRAGMAs cannot be executed inside transactions, e.g., WAL mode).
- Consuming statement results before committing when results return rows, preventing 'cannot commit transaction - SQL statements in progress'.
- Returning formatted list-of-dicts result rows for querying PRAGMAs (e.g., journal_mode, table_info, foreign_keys) and True for configuration PRAGMAs.
- Adding comprehensive test_pragma coverage in SQLiteTests.

Signed-off-by: Rajnish Singh <rajnisihsingh@gmail.com>
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.

PRAGMA statements fail with "cannot commit transaction - SQL statements in progress"

1 participant