Class OracleSqlParser

Object
gudusoft.gsqlparser.parser.AbstractSqlParser
gudusoft.gsqlparser.parser.OracleSqlParser
All Implemented Interfaces:
SqlParser

public class OracleSqlParser extends AbstractSqlParser
Oracle database SQL parser implementation.

This parser handles Oracle-specific SQL syntax including:

  • PL/SQL blocks (procedures, functions, packages, triggers)
  • SQL*Plus commands (spool, set, show, etc.)
  • Oracle-specific DML/DDL (MERGE, flashback, etc.)
  • Oracle analytical functions and extensions
  • Special token handling (INNER, NOT DEFERRABLE, etc.)

Implementation Status: PHASE 3 - IN PROGRESS

  • Completed: Oracle classes (TLexerOracle, TParserOracleSql, TParserOraclePLSql) are now PUBLIC
  • Current: Skeleton implementation delegates to legacy TGSqlParser
  • Next: Extract vendor-specific logic from TGSqlParser into this class
  • Goal: Fully self-contained Oracle parser using AbstractSqlParser template

Design Notes:

Usage Example:

 // Get Oracle parser from factory
 SqlParser parser = SqlParserFactory.get(EDbVendor.dbvoracle);

 // Build context
 ParserContext context = new ParserContext.Builder(EDbVendor.dbvoracle)
     .sqlText("SELECT * FROM emp WHERE deptno = 10")
     .build();

 // Parse
 SqlParseResult result = parser.parse(context);

 // Access statements
 TStatementList statements = result.getSqlStatements();
 

Phase 3 Extraction Roadmap:

  1. ✅ DONE: Make TLexerOracle, TParserOracleSql, TParserOraclePLSql public
  2. ⏳ TODO: Extract tokenization logic (~367 lines from TGSqlParser.dooraclesqltexttotokenlist())
  3. ⏳ TODO: Extract raw statement logic (~200 lines from TGSqlParser.dooraclegetrawsqlstatements())
  4. ⏳ TODO: Extract parsing orchestration (SQL vs PL/SQL parser selection)
  5. ⏳ TODO: Extract helper methods (getanewsourcetoken, getprevsolidtoken, etc.)
  6. ⏳ TODO: Extend AbstractSqlParser and use template method pattern fully
  7. ⏳ TODO: Remove all delegation to TGSqlParser

Key Methods to Extract from TGSqlParser:

  • dooraclesqltexttotokenlist() - Oracle tokenization with SQL*Plus command detection
  • dooraclegetrawsqlstatements() - Oracle raw statement boundaries (handles PL/SQL blocks)
  • getanewsourcetoken() - Token iterator from lexer
  • getprevsolidtoken() - Navigate token list backwards
  • IsValidPlaceForDivToSqlplusCmd() - Slash vs divide operator disambiguation
  • countLines() - Multi-line token handling
  • spaceAtTheEndOfReturnToken() - SQL*Plus command validation
Since:
3.2.0.0
See Also:
  • Field Details

  • Constructor Details

    • OracleSqlParser

      public OracleSqlParser()
      Construct Oracle SQL parser.

      Configures the parser for Oracle database with default delimiters:

      • SQL statements: semicolon (;)
      • PL/SQL blocks: forward slash (/)

      Following the original TGSqlParser pattern, the lexer and parsers are created once in the constructor and reused for all parsing operations. This avoids unnecessary object allocation overhead since the parser is not thread-safe and designed for single-use per instance.

  • Method Details

    • getLexer

      protected TCustomLexer getLexer(ParserContext context)
      Return the Oracle lexer instance.

      The lexer is created once in the constructor and reused for all parsing operations. This method simply returns the existing instance, matching the original TGSqlParser pattern where the lexer is created once and reset before each use.

      Specified by:
      getLexer in class AbstractSqlParser
      Parameters:
      context - parser context (not used, lexer already created)
      Returns:
      the Oracle lexer instance created in constructor
    • getParser

      protected TCustomParser getParser(ParserContext context, TSourceTokenList tokens)
      Return the Oracle SQL parser instance with updated token list.

      The parser is created once in the constructor and reused for all parsing operations. This method updates the token list and returns the existing instance, matching the original TGSqlParser pattern.

      Specified by:
      getParser in class AbstractSqlParser
      Parameters:
      context - parser context (not used, parser already created)
      tokens - source token list to parse
      Returns:
      the Oracle SQL parser instance created in constructor
    • getSecondaryParser

      Return the Oracle PL/SQL parser instance with updated token list.

      Oracle needs a secondary parser (TParserOraclePLSql) for PL/SQL blocks (procedures, functions, packages, triggers, anonymous blocks).

      The parser is created once in the constructor and reused for all parsing operations. This method updates the token list and returns the existing instance, matching the original TGSqlParser pattern.

      Overrides:
      getSecondaryParser in class AbstractSqlParser
      Parameters:
      context - parser context (not used, parser already created)
      tokens - source token list to parse
      Returns:
      the Oracle PL/SQL parser instance created in constructor
    • tokenizeVendorSql

      protected void tokenizeVendorSql()
      Call Oracle-specific tokenization logic.

      Delegates to dooraclesqltexttotokenlist which handles Oracle's specific keyword recognition, SQL*Plus commands, forward slash disambiguation, and token generation.

      Specified by:
      tokenizeVendorSql in class AbstractSqlParser
    • setupVendorParsersForExtraction

      Setup Oracle parsers for raw statement extraction.

      Oracle uses dual parsers (SQL + PL/SQL), so we inject sqlcmds and update token lists for both parsers.

      Specified by:
      setupVendorParsersForExtraction in class AbstractSqlParser
    • extractVendorRawStatements

      Call Oracle-specific raw statement extraction logic.

      Delegates to dooraclegetrawsqlstatements which handles Oracle's statement delimiters (semicolon and forward slash).

      Specified by:
      extractVendorRawStatements in class AbstractSqlParser
      Parameters:
      builder - the result builder to populate with raw statements
    • performParsing

      protected TStatementList performParsing(ParserContext context, TCustomParser parser, TCustomParser secondaryParser, TSourceTokenList tokens, TStatementList rawStatements)
      Perform full parsing of statements with syntax checking.

      This method orchestrates the parsing of all statements by:

      • Using the raw statements passed from AbstractSqlParser.parse()
      • Initializing SQL and PL/SQL parsers
      • Creating global context and frame stack
      • Looping through each raw statement
      • Calling parsestatement() on each to build AST
      • Handling error recovery for CREATE TABLE/INDEX
      • Collecting syntax errors

      Important: This method does NOT extract raw statements - they are passed in as a parameter already extracted by AbstractSqlParser.extractRawStatements(gudusoft.gsqlparser.parser.ParserContext, gudusoft.gsqlparser.TSourceTokenList, gudusoft.gsqlparser.TCustomLexer, long). This eliminates duplicate extraction that was occurring in the old design.

      Extracted from: TGSqlParser.doparse() lines 16903-17026

      Specified by:
      performParsing in class AbstractSqlParser
      Parameters:
      context - parser context
      parser - main SQL parser (TParserOracleSql)
      secondaryParser - PL/SQL parser (TParserOraclePLSql)
      tokens - source token list
      rawStatements - raw statements already extracted (never null)
      Returns:
      list of fully parsed statements with AST built
    • afterStatementParsed

      Override to provide Oracle-specific post-processing after statement parsing.

      For Oracle, we check if the statement is PL/SQL and recursively find syntax errors in nested PL/SQL statements.

      Overrides:
      afterStatementParsed in class AbstractSqlParser
      Parameters:
      stmt - the statement that was just parsed
    • performSemanticAnalysis

      protected void performSemanticAnalysis(ParserContext context, TStatementList statements)
      Perform Oracle-specific semantic analysis using TSQLResolver.

      This includes:

      • Column-to-table resolution
      • Dataflow analysis
      • Reference resolution
      • Scope resolution
      Overrides:
      performSemanticAnalysis in class AbstractSqlParser
      Parameters:
      context - the parser context
      statements - the parsed statements
    • performInterpreter

      protected void performInterpreter(ParserContext context, TStatementList statements)
      Perform Oracle-specific AST interpretation/evaluation using TASTEvaluator.

      This executes simple SQL statements and evaluates expressions for static analysis and constant folding.

      Overrides:
      performInterpreter in class AbstractSqlParser
      Parameters:
      context - the parser context
      statements - the parsed statements
    • toString

      public String toString()
      Copy syntax errors from a statement to our error list. Extracted from TGSqlParser.copyerrormsg().
      Overrides:
      toString in class AbstractSqlParser