A simple command-line tool to validate SQL queries without executing them.
This tool checks if your SQL queries are written correctly. It's like a spell-checker for SQL code. It doesn't run the queries or connect to any database - it just checks if they follow proper SQL rules.
- Python 3.6 or higher
- No external libraries needed (uses only built-in Python modules)
sql_validator/
├── main.py # Start the program here
├── cli.py # User interface
├── lexer.py # Breaks SQL into pieces (tokens)
├── parser.py # Checks if pieces are in correct order
├── validator.py # Final validation checks
├── error_handler.py # Manages errors
├── output_formatter.py # Formats results
├── config.py # Settings
├── app.py # UI based app
├── test_queries.sql # Sample queries to test
├── queries.sql # Sample queries to test
├── implementation_guide.txt
├── test/
├── test_engine.py
├── test_lexer.py
├── conftest.py
├── test_parser.py
├── test_validator.py
└── README.md # This file
Open your terminal or command prompt and run the following command to download the project:
git clone https://github.com/revathii-nair/sql-validator.gitcd sql-validatorpython main.pyOnce you run the program, you'll see a menu with 5 options:
This lets you type a SQL query directly.
Example:
Enter your choice: 1
Enter your SQL query:
SELECT name FROM users;
The program will tell you if it's valid or show errors.
This validates all queries in a file.
Example:
Enter your choice: 2
Enter the path to SQL file: test_queries.sql
The program will check each query in the file.
Change how the validator works:
- SQL Dialect: Choose ANSI (default) or MSSQL
- Output Format: Choose text, json, or custom
- Strict Mode: Enable/disable extra checks
Shows information about supported SQL commands and usage tips.
Quit the program.
The validator supports these SQL commands:
- SELECT: Get data from tables
- INSERT: Add new data
- UPDATE: Change existing data
- DELETE: Remove data
- CREATE TABLE: Make new tables
- DROP TABLE: Delete tables
- ALTER TABLE: Change table structure
-- Simple SELECT
SELECT * FROM users;
-- SELECT with WHERE clause
SELECT name, age FROM users WHERE age > 18;
-- INSERT with all columns
INSERT INTO users (name, age) VALUES ('John', 25);
-- UPDATE with condition
UPDATE users SET age = 26 WHERE name = 'John';
-- DELETE with condition
DELETE FROM users WHERE age < 18;
-- CREATE TABLE
CREATE TABLE students (id INT, name VARCHAR);
-- DROP TABLE
DROP TABLE students;
-- ALTER TABLE
ALTER TABLE users ADD email VARCHAR;-- Missing semicolon
SELECT * FROM users
-- Missing FROM keyword
SELECT name WHERE age > 18;
-- Missing table name
SELECT * FROM;
-- Wrong column count in INSERT
INSERT INTO users (name) VALUES ('John', 25);============================================================
SQL QUERY VALIDATION RESULT
============================================================
Query: SELECT * FROM users;
Result: SUCCESS
Message: Query validated successfully
============================================================
============================================================
SQL QUERY VALIDATION RESULT
============================================================
Query: SELECT * FROM
Result: FAILED
--- ERRORS ---
Error ID: SYNTAX_001
Type: Syntax
Message: Expected IDENTIFIER but got KEYWORD at line 1, column 15
Line: 1, Column: 15
============================================================
Easy to read, good for viewing on screen.
Structured format, good for processing by other programs.
Example:
{
"query": "SELECT * FROM users;",
"success": true,
"errors": [],
"warnings": []
}After validation, the program asks:
Save output to file? (yes/no):
Type yes and enter a filename to save results.
A test file test_queries.sql is included with sample queries.
To test:
- Run the program:
python main.py - Choose option 2 (Validate from file)
- Enter:
test_queries.sql - See results for all queries
- Lexer - Breaks your SQL into small pieces (like words in a sentence)
- Parser - Checks if pieces are in the right order (like grammar check)
- Validator - Applies SQL rules to make sure everything makes sense
Think of it like checking an essay:
- Lexer = Breaking into words
- Parser = Checking grammar
- Validator = Making sure it makes sense
- Does NOT execute queries
- Does NOT connect to databases
- Does NOT check if tables/columns actually exist
- Only validates syntax and structure