Skip to content

SQL Statement Nodes

SQL Statement nodes represent the top-level operations in SQL commands. Each statement type has its own dedicated AST node class that contains the specific components and clauses relevant to that operation.

Overview

Statement nodes are the root nodes of parsed SQL commands. They inherit from TSqlStatement and provide methods to access their constituent parts like clauses, expressions, and table references.

Available Statement Types

Data Query Language (DQL)

  • SELECT Statement - TSelectSqlStatement
  • Query data from tables
  • Supports complex joins, subqueries, aggregations
  • Most commonly used statement type

Data Manipulation Language (DML)

  • INSERT Statement - TInsertSqlStatement
  • Insert new data into tables
  • Supports VALUES clauses and SELECT-based inserts

  • UPDATE Statement - TUpdateSqlStatement

  • Modify existing data in tables
  • Includes SET clauses and WHERE conditions

  • DELETE Statement - TDeleteSqlStatement

  • Remove data from tables
  • Supports conditional deletion with WHERE clauses

Data Definition Language (DDL)

  • CREATE Statement - TCreateSqlStatement
  • Create database objects (tables, views, indexes, etc.)
  • Database-specific variations supported

  • ALTER Statement - TAlterSqlStatement

  • Modify existing database objects
  • Add/drop columns, constraints, etc.

  • DROP Statement - TDropSqlStatement

  • Remove database objects
  • Cascade options supported

Common Properties

All statement nodes share these characteristics:

Property Description Access Method
sqlstatementtype Type of SQL statement getSqlStatementType()
startToken First token in statement getStartToken()
endToken Last token in statement getEndToken()
sourceString Original SQL text toString()

Usage Patterns

Identifying Statement Types

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
TSqlStatement stmt = parser.sqlstatements.get(0);
switch (stmt.getSqlStatementType()) {
    case sstselect:
        TSelectSqlStatement select = (TSelectSqlStatement) stmt;
        // Process SELECT statement
        break;
    case sstinsert:
        TInsertSqlStatement insert = (TInsertSqlStatement) stmt;
        // Process INSERT statement
        break;
    // ... other cases
}

Extracting Tables from Any Statement

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
public Set<String> getReferencedTables(TSqlStatement stmt) {
    final Set<String> tables = new HashSet<>();

    stmt.acceptChildren(new IElementVisitor() {
        public void visitElement(TParseTreeNode node) {
            if (node instanceof TTable) {
                TTable table = (TTable) node;
                tables.add(table.getTableName());
            }
        }
    });

    return tables;
}

Database-Specific Considerations

Some statements have database-specific variations:

  • SQL Server: MERGE statements, OUTPUT clauses
  • Oracle: MERGE statements, hierarchical queries with CONNECT BY
  • PostgreSQL: UPSERT with ON CONFLICT
  • MySQL: REPLACE statements, ON DUPLICATE KEY UPDATE

See Also