SQL parser how-to guides and runnable demos¶
Use this page when you know the result your application needs. Choose a task to understand the design and production considerations, or open the Java or .NET project to run working source code immediately.
All of these examples analyze SQL inside your application. They do not require a database connection unless your own integration adds one.
Find the right guide¶
| What do you want to do? | Learn how it works | Run the source code |
|---|---|---|
| Validate SQL syntax offline before execution | Validate SQL without a database | Java · .NET |
| Pretty-print or normalize SQL | Format SQL | Java · .NET |
| List the tables and columns used by a query | Extract table and column references | Java · .NET |
| Enforce a policy, modify the AST, and regenerate SQL | Modify a SQL AST and regenerate SQL safely | Java · .NET |
| Export the SQL parse tree as XML | Export SQL as an XML parse tree | Java · .NET |
| Convert Oracle or SQL Server proprietary joins to ANSI joins | Modernize proprietary join syntax | Java · .NET |
| Trace each output column back to its source | Column-level data lineage | Java · .NET |
Choose by application¶
| If you are building... | Start here | What the integration can produce |
|---|---|---|
| A SQL editor, IDE extension, or CI quality gate | Offline syntax validation | Dialect-aware diagnostics before SQL is deployed |
| A customer-facing query tool or AI SQL workflow | Validation, then AST policy and rewriting | An explicit allow, reject, or validated rewritten statement before execution |
| A data catalog, SQL inventory, or access-review service | Table and column extraction | Structured object references instead of text matches |
| A governance or impact-analysis platform | Column-level data lineage | Source-to-target relationships across SQL transformations |
| A migration or automated refactoring tool | AST rewriting, join conversion, and dialect guides | SQL transformed with awareness of statement and expression structure |
| A readable query log, review bot, or generated-SQL workflow | SQL formatting | Consistent SQL that is easier to review and compare |
| An XML-based rule engine or language-neutral integration | SQL-to-XML export | A structured parse-tree representation for another component |
Transform and govern SQL¶
Modify a SQL AST and regenerate SQL safely¶
Put a structured policy layer between incoming SQL and your database. You can
allow only one SELECT, remove sensitive output columns, add a tenant filter
without changing AND/OR precedence, regenerate the statement, and validate
the result again before execution.
This pattern fits multi-tenant applications, report builders, AI-generated SQL, schema migration tools, query fixers, and other products that must inspect or change SQL received from a less-trusted boundary.
Format SQL¶
Turn compact or generated SQL into readable output. You can choose keyword and identifier casing, control indentation and comma placement, process multi-statement scripts, and account for formatter differences between Java and .NET.
Modernize proprietary join syntax¶
Convert legacy comma joins, Oracle (+) outer joins, and SQL Server proprietary
join syntax into explicit ANSI JOIN ... ON clauses. Use this when migrating a
database, modernizing stored SQL, remediating a query library, or producing SQL
for a target platform that does not accept the original join notation.
The Java join converter and .NET join converter include runnable examples.
Validate and understand SQL¶
Validate SQL without a database¶
Check SQL in a desktop tool, CI pipeline, IDE, API, ETL service, or application before it reaches an execution or deployment path. Select the correct database dialect to catch vendor-specific syntax errors and return useful line and column information to the caller.
Run the Java syntax checker or .NET syntax checker with a built-in sample first, then pass your own SQL file and dialect.
Extract table and column references¶
Discover which database objects a query reads or writes. This supports dependency discovery, access review, catalog enrichment, impact analysis, query routing, and database documentation.
GSP uses the parsed structure to distinguish real object references from text inside comments and string literals and to follow references through nested queries. Start with the Java table and column demos or the .NET project.
Trace column-level data lineage¶
Follow target columns through expressions, joins, aliases, subqueries, CTEs, and stored procedures to their source columns. Use lineage for governance, impact analysis, migration validation, and data-product documentation.
Export SQL as an XML parse tree¶
Export a structured representation of SQL when another component consumes XML instead of Java or .NET objects. The output can support parser diagnostics, parse-tree visualization, test fixtures, language-neutral integrations, and external rule engines.
Run toXml.java
or the .NET visitor project
and inspect the generated XML before adapting its visitor to your integration.
Work with database dialects and production concerns¶
- Parse Oracle PL/SQL — packages, procedures, triggers, and
EXECUTE IMMEDIATEdynamic SQL. - Handle SQL Server T-SQL —
GObatches, blocks,TRY...CATCH, stored procedures, and temporary tables. - Work with PostgreSQL extensions — dollar-quoted
functions,
RETURNING, CTEs, andCOPY. - Improve parser performance — parser reuse, script splitting, parallel parsing, and measurement.
- Handle parse errors — syntax error details and batch validation strategies.
- Configure identifier rules — case sensitivity and identifier normalization.
Start with a runnable project¶
- Choose Java or .NET and open the source link for your task.
- Clone the repository and run the built-in input before changing the code.
- Set the parser dialect explicitly for the database that produced the SQL.
- Replace the sample with a small query from your application and verify the parsed structure or regenerated output.
- Add tests for the vendor syntax and edge cases your product must support.
- Treat parse failures and transformations you cannot verify as explicit outcomes; do not send uncertain SQL to the next stage silently.
If you have not installed GSP yet, begin with Quick Start. Use the AST node reference when you need to extend a demo beyond its initial task.