Class PgDiffUtils

java.lang.Object
org.pgcodekeeper.core.database.pg.utils.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
    • 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
    • getDefaultObjectName

      public static String getDefaultObjectName(String tableName, String columnName, String suffix)
      Create a name for an implicitly created index, sequence, constraint, extended statistics, etc.

      The result is "tableName_columnName_label", but the table and column names are truncated if needed to keep the total length under 63 bytes. The suffix is never truncated.

      When truncation is needed, the longer name (table or column) is shortened first. This ensures the result stays within PostgreSQL's 63 bytes limit.

      Note: This implementation assumes the database encoding is UTF-8.

      Parameters:
      tableName - the table name (name1)
      columnName - the column name (name2), can be null
      suffix - the suffix (like "pkey", "not_null"), can be null
      Returns:
      the generated name with proper truncation
      See Also:
    • isSystemSchema

      public static boolean isSystemSchema(String schema)
      Checks if a schema is a PostgreSQL system schema.
      Parameters:
      schema - the schema name to check
      Returns:
      true if the schema is 'pg_catalog' or 'information_schema', false otherwise