Package org.pgcodekeeper.core
Class PgDiffUtils
java.lang.Object
org.pgcodekeeper.core.PgDiffUtils
Utility class for handling quoting and unquoting of identifiers and literals in PostgreSQL.
- Author:
- fordfrog
-
Method Summary
Modifier and TypeMethodDescriptionstatic voidappendSqlWrappedInDo(StringBuilder sbResult, StringBuilder sbSQL, String expectedErrCode) Wraps SQL in DO block with error handling.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 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 StringQuotes string with single quotes for SQL.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
-
quoteString
Quotes string with single quotes for SQL.- Parameters:
s- the string to quote- Returns:
- quoted string
-
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
-
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 tosbSQL- the SQL to wrapexpectedErrCode- the expected error code to handle
-