Skip to content

SQL Parser Use Cases

A SQL parser turns SQL text into a structured syntax tree you can query programmatically — and that single capability powers a surprising range of products: data lineage tools, impact analysis, CI validation gates, SQL editors, security auditors, formatters, and migration assessments. This page shows the seven use cases General SQL Parser (GSP) is most often embedded for, each with working Java code or a link to the full guide.

GSP parses 30+ SQL dialects (Oracle, SQL Server, MySQL, PostgreSQL, Snowflake, BigQuery, Teradata, Hive, and more) with one consistent API, including full stored-procedure languages like PL/SQL and T-SQL — see the SQL syntax support reference.

Column-Level Data Lineage

Data lineage answers "where did this column's data come from?" — the backbone of data catalogs, governance platforms, and regulatory reporting (BCBS 239, GDPR). GSP's built-in DataFlowAnalyzer traces dataflow at column granularity through views, INSERT ... SELECT, CTEs, MERGE statements, and stored procedures:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
import gudusoft.gsqlparser.EDbVendor;
import gudusoft.gsqlparser.dlineage.DataFlowAnalyzer;

DataFlowAnalyzer analyzer = new DataFlowAnalyzer(
    "CREATE VIEW v_sales AS " +
    "SELECT o.order_id, c.name AS customer " +
    "FROM orders o JOIN customers c ON o.cust_id = c.id",
    EDbVendor.dbvoracle, /* simpleOutput */ false);

String lineageXml = analyzer.generateDataFlow();
// XML model: v_sales.customer <- customers.name, v_sales.order_id <- orders.order_id

The output is a machine-readable model of source-to-target column relationships, including join conditions as indirect dependencies. GSP's lineage accuracy is validated against real-world codebases — see the data lineage validation reports.

Impact Analysis

Impact analysis is lineage read in the other direction: "if I change or drop this column, what breaks downstream?" Build the same dataflow model over your full SQL codebase (views, ETL scripts, stored procedures), then walk the graph forward from the changed column to find every affected view, report, and job — before the change ships instead of after. The DataFlowAnalyzer accepts a File[] of scripts, so whole-repository analysis is a single call. The recursive statement traversal that feeds it is shown in Parse Oracle PL/SQL.

SQL Validation in CI Pipelines and Editors

Because GSP validates syntax offline — no database connection, no execution — it slots directly into CI gates and editor plugins. One bad statement doesn't stop the rest of the script from being checked, and every error carries line, column, and the offending token:

1
2
3
4
5
6
7
8
9
TGSqlParser parser = new TGSqlParser(EDbVendor.dbvmssql);
parser.sqltext = changedSqlFileContent;

if (parser.parse() != 0) {
    for (TSyntaxError e : parser.getSyntaxErrors()) {
        System.out.printf("%s:%d:%d: near '%s'%n", fileName, e.lineNo, e.columnNo, e.tokentext);
    }
    System.exit(1); // fail the build
}

The full per-statement reporting pattern — including validating scripts where some statements fail — is in the error handling guide. Try it interactively in the SQL Syntax Checker.

Table and Column Extraction

Nearly every metadata tool starts with "which tables and columns does this query touch?" Every parsed statement exposes its referenced tables, with linked columns per table:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
TGSqlParser parser = new TGSqlParser(EDbVendor.dbvoracle);
parser.sqltext = "SELECT e.name FROM employees e JOIN departments d ON e.dept_id = d.id";

if (parser.parse() == 0) {
    TCustomSqlStatement stmt = parser.getSqlstatements().get(0);
    for (int i = 0; i < stmt.tables.size(); i++) {
        System.out.println(stmt.tables.getTable(i).getFullName());
        // employees, departments
    }
}

For SELECT-list, WHERE, and JOIN details, see Basic SQL Parsing; for statements nested inside procedures, combine this with the recursive walk in the PL/SQL guide.

SQL Formatting and Standardization

Enforce one SQL style across a team — in a pre-commit hook, an editor action, or a code review bot. GSP's formatter pretty-prints any statement it can parse, across all supported dialects:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
import gudusoft.gsqlparser.pp.para.GFmtOpt;
import gudusoft.gsqlparser.pp.para.GFmtOptFactory;
import gudusoft.gsqlparser.pp.stmtformatter.FormatterFactory;

TGSqlParser parser = new TGSqlParser(EDbVendor.dbvoracle);
parser.sqltext = "select order_id,total from orders where status='open' order by total desc";

if (parser.parse() == 0) {
    GFmtOpt option = GFmtOptFactory.newInstance();
    System.out.println(FormatterFactory.pp(parser, option));
}
1
2
3
4
5
SELECT   order_id,
         total
FROM     orders
WHERE    status = 'open'
ORDER BY total DESC

Because formatting works on the parse tree — not regex — it never corrupts strings, comments, or vendor-specific syntax.

SQL Security Auditing

Security and compliance tooling needs to know exactly which objects each query reads or writes: flagging access to PII tables, detecting DELETE/UPDATE without a WHERE clause, or spotting dynamic SQL that could hide injection. The AST makes these checks precise:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
if (parser.parse() == 0) {
    for (int i = 0; i < parser.getSqlstatements().size(); i++) {
        TCustomSqlStatement stmt = parser.getSqlstatements().get(i);

        // Rule 1: statement touches a sensitive table?
        for (int t = 0; t < stmt.tables.size(); t++) {
            if (sensitiveTables.contains(stmt.tables.getTable(t).getFullName().toLowerCase())) {
                report("sensitive-table-access", stmt);
            }
        }
        // Rule 2: UPDATE/DELETE without WHERE?
        if ((stmt.sqlstatementtype == ESqlStatementType.sstupdate
             || stmt.sqlstatementtype == ESqlStatementType.sstdelete)
            && stmt.getWhereClause() == null) {
            report("unbounded-write", stmt);
        }
    }
}

For stored procedures, recurse into procedure bodies and flag EXECUTE IMMEDIATE / dynamic SQL boundaries — see dynamic SQL handling.

Migration Assessment

Before migrating a codebase between databases (Oracle to PostgreSQL, Teradata to Snowflake, ...), you need an inventory: how many statements, which vendor-specific constructs, which stored procedures, and where. Parse the entire legacy codebase with the source dialect and classify every statement by sqlstatementtype, flagging constructs that need manual attention (CONNECT BY, temp tables, dialect-specific functions). The result is an effort estimate grounded in the actual code rather than file counts. The splitting and parallel-parsing patterns keep this fast even on codebases with hundreds of thousands of statements.

Try It Live

Ready to build? Start with the Quick Start — it covers both Java and C# in side-by-side tabs — then work through the tutorials.