Class PgDiffUtils

java.lang.Object
org.pgcodekeeper.core.PgDiffUtils

public final class PgDiffUtils extends Object
Utility class for handling quoting and unquoting of identifiers and literals in PostgreSQL.
Author:
fordfrog
  • Method Details

    • isValidId

      public static boolean isValidId(String id, boolean allowKeywords, boolean allowCaps)
      Checks if string is a valid PostgreSQL identifier.
      Parameters:
      id - the identifier to check
      allowKeywords - whether to allow reserved keywords
      allowCaps - whether to allow uppercase letters
      Returns:
      true if valid identifier, false otherwise
    • isValidIdChar

      public static boolean isValidIdChar(char c)
      Checks if character is valid for PostgreSQL identifiers.
      Parameters:
      c - the character to check
      Returns:
      true if valid identifier character, false otherwise
    • isValidIdChar

      public static boolean isValidIdChar(char c, boolean allowCaps, boolean allowDigits)
      Checks if character is valid for PostgreSQL identifiers.
      Parameters:
      c - the character to check
      allowCaps - whether to allow uppercase letters
      allowDigits - whether to allow digits (not in first position)
      Returns:
      true if valid identifier character, false otherwise
    • getQuotedName

      public static String getQuotedName(String name)
      If name contains only lower case characters and digits and is not keyword, it is returned not quoted, otherwise the string is returned quoted.
      Parameters:
      name - name
      Returns:
      quoted string if needed, otherwise not quoted string
    • quoteName

      public static String quoteName(String name)
      Quotes PostgreSQL identifier with double quotes.
      Parameters:
      name - the name to quote
      Returns:
      quoted identifier
    • quoteString

      public static String quoteString(String s)
      Quotes string with single quotes for SQL.
      Parameters:
      s - the string to quote
      Returns:
      quoted string
    • quoteStringDollar

      public static String quoteStringDollar(String contents)
      Quotes string using dollar-quoting ($$) syntax.

      Function equivalent to appendStringLiteralDQ in pgdump's dumputils.c

      Parameters:
      contents - the string to quote
      Returns:
      dollar-quoted string
    • unquoteQuotedName

      public static String unquoteQuotedName(String name)
      Unquotes a double-quoted PostgreSQL identifier.
      Parameters:
      name - the quoted name
      Returns:
      unquoted identifier
    • unquoteQuotedString

      public static String unquoteQuotedString(String s, int start)
      Unquotes a single-quoted SQL string starting from specified position.
      Parameters:
      s - the quoted string to unquote
      start - the starting position of the quoted content
      Returns:
      unquoted string
    • startsWithId

      public static boolean startsWithId(String text, String id, int offset)
      Checks if text starts with identifier at specified offset.
      Parameters:
      text - the text to check
      id - the identifier to look for
      offset - the position to start checking
      Returns:
      true if text starts with identifier at offset, false otherwise
    • isValidLanguage

      public static boolean isValidLanguage(String language)
      Checks if language is valid for PostgreSQL (PLPGSQL or SQL).
      Parameters:
      language - the language to check
      Returns:
      true if language is valid, false otherwise
    • appendSqlWrappedInDo

      public static void appendSqlWrappedInDo(StringBuilder sbResult, StringBuilder sbSQL, String expectedErrCode)
      Wraps SQL in DO block with error handling.
      Parameters:
      sbResult - the StringBuilder to append to
      sbSQL - the SQL to wrap
      expectedErrCode - the expected error code to handle