Class PgParserUtils

java.lang.Object
org.pgcodekeeper.core.database.pg.parser.PgParserUtils

public final class PgParserUtils extends Object
Utility methods for PostgreSQL parsing with ANTLR.

Provides helper functions for working with PostgreSQL syntax during ANTLR-based parsing

  • Method Details

    • createPrivilegesParser

      public static PrivilegesParser createPrivilegesParser(String aclArrayAsString)
      Creates a parser for PostgreSQL privilege strings.
      Parameters:
      aclArrayAsString - privilege string to parse
      Returns:
      configured PrivilegesParser instance
    • createSqlParser

      public static SQLParser createSqlParser(String sql, String parsedObjectName, List<Object> errors)
      Creates a PostgreSQL SQL parser from string input.
      Parameters:
      sql - SQL string to parse
      parsedObjectName - 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 parse
      parsedObjectName - name of the object being parsed (for error reporting)
      errors - list to collect parsing errors
      start - 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 SQL
      charset - character encoding of the stream
      parsedObjectName - 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 stream
      parsedObjectName - name of the object being parsed
      diffSettings - unified context object containing settings, monitor, and error accumulator
      monitoringLevel - level of parse tree monitoring
      listener - processor for the parsed content
      antlrTasks - 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

      public static String getTokenText(int type, org.antlr.v4.runtime.Token token)
    • 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

      public static QNameParserWrapper wrapParsedQName(String fullName)
      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

      public static QNameParserWrapper wrapParsedPgOperator(String fullName)
      Creates a wrapper for parsing PostgreSQL operator names.
      Parameters:
      fullName - the operator name string to parse
      Returns:
      wrapper containing parsed name components