Class PgParserUtils
Provides helper functions for working with PostgreSQL syntax during ANTLR-based parsing
-
Method Summary
Modifier and TypeMethodDescriptionstatic voidcheckToClean(long cleaningInterval) Checks if parser caches need cleaning based on last usage time.static voidClears the PostgreSQL parser cache.static PrivilegesParsercreatePrivilegesParser(String aclArrayAsString) Creates a parser for PostgreSQL privilege strings.static SQLParsercreateSqlParser(InputStream is, String charset, String parsedObjectName, List<Object> errors) Creates a PostgreSQL SQL parser from input stream.static SQLParsercreateSqlParser(String sql, String parsedObjectName, List<Object> errors) Creates a PostgreSQL SQL parser from string input.static SQLParsercreateSqlParser(String sql, String parsedObjectName, List<Object> errors, org.antlr.v4.runtime.Token start) Creates a PostgreSQL SQL parser from string input with position offset.static StringgetTokenText(int type, org.antlr.v4.runtime.Token token) static booleanisSpecialChar(int type, int previous) static StringnormalizeWhitespaceUnquoted(org.antlr.v4.runtime.ParserRuleContext ctx, org.antlr.v4.runtime.CommonTokenStream stream) static QNameParser<org.antlr.v4.runtime.ParserRuleContext>parsePgOperator(String schemaQualifiedName) Parses a PostgreSQL operator name into its components.static QNameParser<org.antlr.v4.runtime.ParserRuleContext>parseQName(String schemaQualifiedName) Parses a PostgreSQL qualified name into its components.static voidparseSqlStream(InputStreamProvider inputStream, String parsedObjectName, DiffSettings diffSettings, int monitoringLevel, IPgContextProcessor listener, Queue<AntlrTask<?>> antlrTasks) Parses PostgreSQL SQL stream asynchronously.static voidremoveIntoStatements(org.antlr.v4.runtime.Parser parser) Removes INTO statements from SQL tokens that aren't part of PL/pgSQL INTO clauses.static QNameParserWrapperwrapParsedPgOperator(String fullName) Creates a wrapper for parsing PostgreSQL operator names.static QNameParserWrapperwrapParsedQName(String fullName) Creates a wrapper for parsing PostgreSQL qualified names.
-
Method Details
-
createPrivilegesParser
Creates a parser for PostgreSQL privilege strings.- Parameters:
aclArrayAsString- privilege string to parse- Returns:
- configured PrivilegesParser instance
-
createSqlParser
Creates a PostgreSQL SQL parser from string input.- Parameters:
sql- SQL string to parseparsedObjectName- name of the object being parsed (for error reporting)errors- list to collect parsing errors- Returns:
- configured SQLParser instance
-
createSqlParser
public static SQLParser createSqlParser(String sql, String parsedObjectName, List<Object> errors, org.antlr.v4.runtime.Token start) Creates a PostgreSQL SQL parser from string input with position offset.- Parameters:
sql- SQL string to parseparsedObjectName- name of the object being parsed (for error reporting)errors- list to collect parsing errorsstart- token providing position offset information- Returns:
- configured SQLParser instance
-
createSqlParser
public static SQLParser createSqlParser(InputStream is, String charset, String parsedObjectName, List<Object> errors) throws IOException Creates a PostgreSQL SQL parser from input stream.- Parameters:
is- input stream containing SQLcharset- character encoding of the streamparsedObjectName- name of the object being parsed (for error reporting)errors- list to collect parsing errors- Returns:
- configured SQLParser instance
- Throws:
IOException- if there's an error reading the stream
-
parseSqlStream
public static void parseSqlStream(InputStreamProvider inputStream, String parsedObjectName, DiffSettings diffSettings, int monitoringLevel, IPgContextProcessor listener, Queue<AntlrTask<?>> antlrTasks) Parses PostgreSQL SQL stream asynchronously.- Parameters:
inputStream- provider of the input streamparsedObjectName- name of the object being parseddiffSettings- unified context object containing settings, monitor, and error accumulatormonitoringLevel- level of parse tree monitoringlistener- processor for the parsed contentantlrTasks- queue for parser tasks
-
checkToClean
public static void checkToClean(long cleaningInterval) Checks if parser caches need cleaning based on last usage time.- Parameters:
cleaningInterval- time interval in milliseconds after which cache should be cleaned
-
cleanCachePgParser
public static void cleanCachePgParser()Clears the PostgreSQL parser cache. -
isSpecialChar
public static boolean isSpecialChar(int type, int previous) -
normalizeWhitespaceUnquoted
public static String normalizeWhitespaceUnquoted(org.antlr.v4.runtime.ParserRuleContext ctx, org.antlr.v4.runtime.CommonTokenStream stream) -
getTokenText
-
removeIntoStatements
public static void removeIntoStatements(org.antlr.v4.runtime.Parser parser) Removes INTO statements from SQL tokens that aren't part of PL/pgSQL INTO clauses. Handles special cases for INSERT INTO and IMPORT FOREIGN SCHEMA INTO.Because INTO is sometimes used in the main SQL grammar, we have to be careful not to take any such usage of INTO as a PL/pgSQL INTO clause. There are currently three such cases:
1. SELECT ... INTO. We don't care, we just override that with the PL/pgSQL definition.
2. INSERT INTO. This is relatively easy to recognize since the words must appear adjacently; but we can't assume INSERT starts the command, because it can appear in CREATE RULE or WITH. Unfortunately, INSERT is *not* fully reserved, so that means there is a chance of a false match, but it's not very likely.
3. IMPORT FOREIGN SCHEMA ... INTO. This is not allowed in CREATE RULE or WITH, so we just check for IMPORT as the command's first token. (If IMPORT FOREIGN SCHEMA returned data someone might wish to capture with an INTO-variables clause, we'd have to work much harder here.)
See pl_gram.y
-
parseQName
public static QNameParser<org.antlr.v4.runtime.ParserRuleContext> parseQName(String schemaQualifiedName) Parses a PostgreSQL qualified name into its components.- Parameters:
schemaQualifiedName- the qualified name string to parse- Returns:
- QNameParser instance containing parsed components
-
parsePgOperator
public static QNameParser<org.antlr.v4.runtime.ParserRuleContext> parsePgOperator(String schemaQualifiedName) Parses a PostgreSQL operator name into its components.- Parameters:
schemaQualifiedName- the operator name string to parse- Returns:
- QNameParser instance containing parsed components
-
wrapParsedQName
Creates a wrapper for parsing PostgreSQL qualified names.- Parameters:
fullName- the qualified name string to parse (e.g. "schema.table")- Returns:
- wrapper containing parsed name components
-
wrapParsedPgOperator
Creates a wrapper for parsing PostgreSQL operator names.- Parameters:
fullName- the operator name string to parse- Returns:
- wrapper containing parsed name components
-