Class PgDiffUtils
java.lang.Object
org.pgcodekeeper.core.database.pg.utils.PgDiffUtils
Utility class for handling quoting and unquoting of identifiers and literals in PostgreSQL.
- Author:
- fordfrog
-
Method Summary
Modifier and TypeMethodDescriptionstatic StringgetDefaultObjectName(String tableName, String columnName, String suffix) Create a name for an implicitly created index, sequence, constraint, extended statistics, etc.static StringgetQuotedName(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.static booleanisSystemSchema(String schema) Checks if a schema is a PostgreSQL system schema.static booleanChecks if string is a valid PostgreSQL identifier.static booleanisValidIdChar(char c) Checks if character is valid for PostgreSQL identifiers.static booleanisValidIdChar(char c, boolean allowCaps, boolean allowDigits) Checks if character is valid for PostgreSQL identifiers.static booleanisValidLanguage(String language) Checks if language is valid for PostgreSQL (PLPGSQL or SQL).static StringQuotes PostgreSQL identifier with double quotes.static StringquoteStringDollar(String contents) Quotes string using dollar-quoting ($$) syntax.static booleanstartsWithId(String text, String id, int offset) Checks if text starts with identifier at specified offset.static StringunquoteQuotedName(String name) Unquotes a double-quoted PostgreSQL identifier.static StringunquoteQuotedString(String s, int start) Unquotes a single-quoted SQL string starting from specified position.
-
Method Details
-
isValidId
Checks if string is a valid PostgreSQL identifier.- Parameters:
id- the identifier to checkallowKeywords- whether to allow reserved keywordsallowCaps- 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 checkallowCaps- whether to allow uppercase lettersallowDigits- whether to allow digits (not in first position)- Returns:
- true if valid identifier character, false otherwise
-
getQuotedName
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
Quotes PostgreSQL identifier with double quotes.- Parameters:
name- the name to quote- Returns:
- quoted identifier
-
quoteStringDollar
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
Unquotes a double-quoted PostgreSQL identifier.- Parameters:
name- the quoted name- Returns:
- unquoted identifier
-
unquoteQuotedString
Unquotes a single-quoted SQL string starting from specified position.- Parameters:
s- the quoted string to unquotestart- the starting position of the quoted content- Returns:
- unquoted string
-
startsWithId
Checks if text starts with identifier at specified offset.- Parameters:
text- the text to checkid- the identifier to look foroffset- the position to start checking- Returns:
- true if text starts with identifier at offset, false otherwise
-
isValidLanguage
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
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 nullsuffix- the suffix (like "pkey", "not_null"), can be null- Returns:
- the generated name with proper truncation
- See Also:
-
isSystemSchema
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
-