public class PostgreSqlParser extends AbstractSqlParser
This parser handles PostgreSQL-specific SQL syntax including:
Design Notes:
AbstractSqlParserTLexerPostgresql, TParserPostgresqlSqlUsage Example:
// Get PostgreSQL parser from factory
SqlParser parser = SqlParserFactory.get(EDbVendor.dbvpostgresql);
// Build context
ParserContext context = new ParserContext.Builder(EDbVendor.dbvpostgresql)
.sqlText("SELECT * FROM employees WHERE dept_id = 10")
.build();
// Parse
SqlParseResult result = parser.parse(context);
// Access statements
TStatementList statements = result.getSqlStatements();
SqlParser,
AbstractSqlParser,
TLexerPostgresql,
TParserPostgresqlAbstractSqlParser.PreparedSqlReader| Modifier and Type | Field and Description |
|---|---|
TLexerPostgresql |
flexer
The PostgreSQL lexer used for tokenization (public for TGSqlParser.getFlexer())
|
defaultDelimiterStr, delimiterChar, frameStack, globalContext, globalFrame, lexer, parserContext, sourcetokenlist, sqlcmds, sqlEnv, sqlstatements, syntaxErrors, vendor| Constructor and Description |
|---|
PostgreSqlParser()
Construct PostgreSQL SQL parser.
|
| Modifier and Type | Method and Description |
|---|---|
protected void |
extractVendorRawStatements(SqlParseResult.Builder builder)
Call PostgreSQL-specific raw statement extraction logic.
|
protected TCustomLexer |
getLexer(ParserContext context)
Return the PostgreSQL lexer instance.
|
protected TCustomParser |
getParser(ParserContext context,
TSourceTokenList tokens)
Return the PostgreSQL SQL parser instance with updated token list.
|
protected void |
onRawStatementComplete(ParserContext context,
TCustomSqlStatement statement,
TCustomParser mainParser,
TCustomParser secondaryParser,
TStatementList statementList,
boolean isLastStatement,
SqlParseResult.Builder builder)
Override onRawStatementComplete to add PostgreSQL-specific processing.
|
protected TStatementList |
performParsing(ParserContext context,
TCustomParser parser,
TCustomParser secondaryParser,
TSourceTokenList tokens,
TStatementList rawStatements)
Perform full parsing of statements with syntax checking.
|
protected void |
setupVendorParsersForExtraction()
Setup PostgreSQL parser for raw statement extraction.
|
protected void |
tokenizeVendorSql()
Call PostgreSQL-specific tokenization logic.
|
String |
toString() |
afterStatementParsed, attemptErrorRecovery, copyErrorsFromStatement, doAfterTokenize, doExtractRawStatements, extractRawStatements, getanewsourcetoken, getDefaultDelimiterStr, getDelimiterChar, getErrorCount, getrawsqlstatements, getSecondaryParser, getSyntaxErrors, getVendor, handleStatementParsingException, initializeGlobalContext, isDollarFunctionDelimiter, onRawStatementCompleteVendorSpecific, parse, performInterpreter, performSemanticAnalysis, performTokenization, prepareSqlReader, processTokensBeforeParse, processTokensInTokenTable, setTokenHandle, tokenize, towinlinebreakpublic TLexerPostgresql flexer
public PostgreSqlParser()
Configures the parser for PostgreSQL database with default delimiter: semicolon (;)
Following the original TGSqlParser pattern, the lexer and parser are created once in the constructor and reused for all parsing operations.
protected TCustomLexer getLexer(ParserContext context)
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.
getLexer in class AbstractSqlParsercontext - parser context (not used, lexer already created)protected TCustomParser getParser(ParserContext context, TSourceTokenList tokens)
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.
getParser in class AbstractSqlParsercontext - parser context (not used, parser already created)tokens - source token list to parseprotected void tokenizeVendorSql()
Delegates to dopostgresqltexttotokenlist which handles PostgreSQL's specific keyword recognition, dollar quotes, and token generation.
tokenizeVendorSql in class AbstractSqlParserprotected void setupVendorParsersForExtraction()
PostgreSQL uses a single parser, so we inject sqlcmds and update the token list for the main parser only.
setupVendorParsersForExtraction in class AbstractSqlParserprotected void extractVendorRawStatements(SqlParseResult.Builder builder)
Delegates to dopostgresqlgetrawsqlstatements which handles PostgreSQL's statement delimiters (semicolon for SQL, $$ for PL/pgSQL functions).
extractVendorRawStatements in class AbstractSqlParserbuilder - the result builder to populate with raw statementsprotected TStatementList performParsing(ParserContext context, TCustomParser parser, TCustomParser secondaryParser, TSourceTokenList tokens, TStatementList rawStatements)
This method orchestrates the parsing of all statements.
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).
performParsing in class AbstractSqlParsercontext - parser contextparser - main SQL parser (TParserPostgresql)secondaryParser - not used for PostgreSQLtokens - source token listrawStatements - raw statements already extracted (never null)protected void onRawStatementComplete(ParserContext context, TCustomSqlStatement statement, TCustomParser mainParser, TCustomParser secondaryParser, TStatementList statementList, boolean isLastStatement, SqlParseResult.Builder builder)
This method handles special processing for stored procedures/functions whose body is written in non-SQL languages (e.g., PL/Python, PL/Perl, PL/R).
For such routines, the tokens between dollar-quote delimiters ($$, $function$, etc.) are marked as non-SQL content to prevent parsing errors.
onRawStatementComplete in class AbstractSqlParsercontext - parser contextstatement - the completed statementmainParser - main SQL parsersecondaryParser - secondary parser (not used for PostgreSQL)statementList - list to add the statement toisLastStatement - whether this is the last statementbuilder - result builder for populating parse resultspublic String toString()
toString in class AbstractSqlParser