Skip to content

Add client-side SQL syntax pre-validation before execution#62

Open
muk2 wants to merge 1 commit intomainfrom
feature/issue-55-sql-prevalidation
Open

Add client-side SQL syntax pre-validation before execution#62
muk2 wants to merge 1 commit intomainfrom
feature/issue-55-sql-prevalidation

Conversation

@muk2
Copy link
Owner

@muk2 muk2 commented Feb 21, 2026

Summary

  • Adds lightweight client-side SQL syntax validation using the sqlparser crate (already a dependency) that runs before sending queries to PostgreSQL
  • Catches obvious syntax errors (typos like SELEC, missing clauses, etc.) instantly without a database round-trip
  • Skips pre-validation for PostgreSQL-specific commands that sqlparser doesn't support (SHOW, SET, COPY, VACUUM, DO, etc.) — these are sent directly to PostgreSQL
  • Extracts line/column information from sqlparser error messages and displays them using the existing structured error UI
  • Fails open: if sqlparser rejects a query that might be valid PostgreSQL-specific syntax, a hint is shown suggesting the parser may not support it

What Changed

  • src/ui/app.rs: Added prevalidate_sql() method and parse_line_col_from_sqlparser_error() helper. Integrated pre-validation into the execute_query() flow — if parsing fails, the error is shown immediately without contacting the database.

Test Plan

  • Write an obviously invalid query (e.g., SELEC * FROM users) and press F5 — should see an instant syntax error without hitting the database
  • Write a valid query (e.g., SELECT * FROM users LIMIT 10) — should execute normally against the database
  • Write a PostgreSQL-specific command (e.g., SHOW server_version) — should skip pre-validation and execute on the server
  • Write a query with a \ meta-command prefix — should skip pre-validation
  • Verify the error display shows line/column information when available
  • Run cargo test — all 230 tests pass

Closes #55

🤖 Generated with Claude Code

Uses the sqlparser crate (already a dependency) to parse SQL locally
before sending to PostgreSQL. This catches obvious syntax errors
instantly without a database round-trip.

- Pre-validates on F5/Ctrl+Enter using PostgreSqlDialect
- Skips validation for PostgreSQL-specific commands (SHOW, SET, COPY, etc.)
- Extracts line/column from sqlparser error messages
- Displays errors using existing StructuredError UI
- Fails open: if sqlparser can't parse valid PostgreSQL syntax,
  the query is still sent to the server

Closes #55

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.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.

Add client-side SQL syntax pre-validation before execution

1 participant