Class Parser
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic boolean
charTerminatesIdentifier
(char c) static int
Converts digit at positionpos
in strings
to integer or throws.static boolean
isArrayWhiteSpace
(char c) Identifies white space characters which the backend uses to determine if aString
value needs to be quoted in array representation.static boolean
Returns true if a given strings
has digit at positionpos
.static boolean
isDollarQuoteContChar
(char c) Checks if a character is valid as the second or later character of a dollar quoting tag.static boolean
isDollarQuoteStartChar
(char c) Checks if a character is valid as the start of a dollar quoting tag.static boolean
isIdentifierContChar
(char c) Checks if a character is valid as the second or later character of an identifier.static boolean
isIdentifierStartChar
(char c) Checks if a character is valid as the start of an identifier.static boolean
isOperatorChar
(char c) static boolean
isSpace
(char c) Identifies characters which the backend scanner considers to be whitespace.static JdbcCallParseInfo
modifyJdbcCall
(String jdbcSql, boolean stdStrings, int serverVersion, int protocolVersion, EscapeSyntaxCallMode escapeSyntaxCallMode) Converts JDBC-specific callable statement escapes{ [? =] call <some_function> [(?, [?,..])] }
into the PostgreSQL format which isselect <some_function> (?, [?, ...]) as result
orselect * from <some_function> (?, [?, ...]) as result
(7.3)static boolean
parseAlterKeyword
(char[] query, int offset) Parse string to check presence of CREATE keyword regardless of case.static boolean
parseAsKeyword
(char[] query, int offset) Parse string to check presence of AS keyword regardless of case.static boolean
parseAtomicKeyword
(char[] query, int offset) Parse string to check presence of ATOMIC keyword regardless of case.static boolean
parseBeginKeyword
(char[] query, int offset) Parse string to check presence of BEGIN keyword regardless of case.static int
parseBlockComment
(char[] query, int offset) Test if the/
character atoffset
starts a block comment, and return the position of the last/
character.static boolean
parseCreateKeyword
(char[] query, int offset) Parse string to check presence of CREATE keyword regardless of case.static boolean
parseDeleteKeyword
(char[] query, int offset) Parse string to check presence of DELETE keyword regardless of case.static int
parseDollarQuotes
(char[] query, int offset) Test if the dollar character ($
) at the given offset starts a dollar-quoted string and return the offset of the ending dollar character.static int
parseDoubleQuotes
(char[] query, int offset) Find the end of the double-quoted string starting at the given offset.static boolean
parseInsertKeyword
(char[] query, int offset) Parse string to check presence of INSERT keyword regardless of case.static List<NativeQuery>
parseJdbcSql
(String query, boolean standardConformingStrings, boolean withParameters, boolean splitStatements, boolean isBatchedReWriteConfigured, boolean quoteReturningIdentifiers, String... returningColumnNames) Parses JDBC query into PostgreSQL's native format.static int
parseLineComment
(char[] query, int offset) Test if the-
character atoffset
starts a--
style line comment, and return the position of the first\r
or\n
character.static long
Faster version ofLong.parseLong(String)
when parsing a substring is requiredstatic boolean
parseMoveKeyword
(char[] query, int offset) Parse string to check presence of MOVE keyword regardless of case.static boolean
parseReturningKeyword
(char[] query, int offset) Parse string to check presence of RETURNING keyword regardless of case.static boolean
parseSelectKeyword
(char[] query, int offset) Parse string to check presence of SELECT keyword regardless of case.static int
parseSingleQuotes
(char[] query, int offset, boolean standardConformingStrings) Find the end of the single-quoted string starting at the given offset.static boolean
parseUpdateKeyword
(char[] query, int offset) Parse string to check presence of UPDATE keyword regardless of case.static boolean
parseValuesKeyword
(char[] query, int offset) Parse string to check presence of VALUES keyword regardless of case.static boolean
parseWithKeyword
(char[] query, int offset) Parse string to check presence of WITH keyword regardless of case.static String
replaceProcessing
(String sql, boolean replaceProcessingEnabled, boolean standardConformingStrings) Filter the SQL string of Java SQL Escape clauses.
-
Constructor Details
-
Parser
public Parser()
-
-
Method Details
-
parseJdbcSql
public static List<NativeQuery> parseJdbcSql(String query, boolean standardConformingStrings, boolean withParameters, boolean splitStatements, boolean isBatchedReWriteConfigured, boolean quoteReturningIdentifiers, String... returningColumnNames) throws SQLException Parses JDBC query into PostgreSQL's native format. Several queries might be given if separated by semicolon.- Parameters:
query
- jdbc query to parsestandardConformingStrings
- whether to allow backslashes to be used as escape characters in single quote literalswithParameters
- whether to replace ?, ? with $1, $2, etcsplitStatements
- whether to split statements by semicolonisBatchedReWriteConfigured
- whether re-write optimization is enabledquoteReturningIdentifiers
- whether to quote identifiers returned using returning clausereturningColumnNames
- for simple insert, update, delete add returning with given column names- Returns:
- list of native queries
- Throws:
SQLException
- if unable to add returning clause (invalid column names)
-
parseSingleQuotes
public static int parseSingleQuotes(char[] query, int offset, boolean standardConformingStrings) Find the end of the single-quoted string starting at the given offset.Note: for
'single '' quote in string'
, this method currently returns the offset of first'
character after the initial one. The caller must call the method a second time for the second part of the quoted string.- Parameters:
query
- queryoffset
- start offsetstandardConformingStrings
- standard conforming strings- Returns:
- position of the end of the single-quoted string
-
parseDoubleQuotes
public static int parseDoubleQuotes(char[] query, int offset) Find the end of the double-quoted string starting at the given offset.Note: for
"double "" quote in string"
, this method currently returns the offset of first"
character after the initial one. The caller must call the method a second time for the second part of the quoted string.- Parameters:
query
- queryoffset
- start offset- Returns:
- position of the end of the double-quoted string
-
parseDollarQuotes
public static int parseDollarQuotes(char[] query, int offset) Test if the dollar character ($
) at the given offset starts a dollar-quoted string and return the offset of the ending dollar character.- Parameters:
query
- queryoffset
- start offset- Returns:
- offset of the ending dollar character
-
parseLineComment
public static int parseLineComment(char[] query, int offset) Test if the-
character atoffset
starts a--
style line comment, and return the position of the first\r
or\n
character.- Parameters:
query
- queryoffset
- start offset- Returns:
- position of the first
\r
or\n
character
-
parseBlockComment
public static int parseBlockComment(char[] query, int offset) Test if the/
character atoffset
starts a block comment, and return the position of the last/
character.- Parameters:
query
- queryoffset
- start offset- Returns:
- position of the last
/
character
-
parseDeleteKeyword
public static boolean parseDeleteKeyword(char[] query, int offset) Parse string to check presence of DELETE keyword regardless of case. The initial character is assumed to have been matched.- Parameters:
query
- char[] of the query statementoffset
- position of query to start checking- Returns:
- boolean indicates presence of word
-
parseInsertKeyword
public static boolean parseInsertKeyword(char[] query, int offset) Parse string to check presence of INSERT keyword regardless of case.- Parameters:
query
- char[] of the query statementoffset
- position of query to start checking- Returns:
- boolean indicates presence of word
-
parseBeginKeyword
public static boolean parseBeginKeyword(char[] query, int offset) Parse string to check presence of BEGIN keyword regardless of case.- Parameters:
query
- char[] of the query statementoffset
- position of query to start checking- Returns:
- boolean indicates presence of word
-
parseAtomicKeyword
public static boolean parseAtomicKeyword(char[] query, int offset) Parse string to check presence of ATOMIC keyword regardless of case.- Parameters:
query
- char[] of the query statementoffset
- position of query to start checking- Returns:
- boolean indicates presence of word
-
parseMoveKeyword
public static boolean parseMoveKeyword(char[] query, int offset) Parse string to check presence of MOVE keyword regardless of case.- Parameters:
query
- char[] of the query statementoffset
- position of query to start checking- Returns:
- boolean indicates presence of word
-
parseReturningKeyword
public static boolean parseReturningKeyword(char[] query, int offset) Parse string to check presence of RETURNING keyword regardless of case.- Parameters:
query
- char[] of the query statementoffset
- position of query to start checking- Returns:
- boolean indicates presence of word
-
parseSelectKeyword
public static boolean parseSelectKeyword(char[] query, int offset) Parse string to check presence of SELECT keyword regardless of case.- Parameters:
query
- char[] of the query statementoffset
- position of query to start checking- Returns:
- boolean indicates presence of word
-
parseAlterKeyword
public static boolean parseAlterKeyword(char[] query, int offset) Parse string to check presence of CREATE keyword regardless of case.- Parameters:
query
- char[] of the query statementoffset
- position of query to start checking- Returns:
- boolean indicates presence of word
-
parseCreateKeyword
public static boolean parseCreateKeyword(char[] query, int offset) Parse string to check presence of CREATE keyword regardless of case.- Parameters:
query
- char[] of the query statementoffset
- position of query to start checking- Returns:
- boolean indicates presence of word
-
parseUpdateKeyword
public static boolean parseUpdateKeyword(char[] query, int offset) Parse string to check presence of UPDATE keyword regardless of case.- Parameters:
query
- char[] of the query statementoffset
- position of query to start checking- Returns:
- boolean indicates presence of word
-
parseValuesKeyword
public static boolean parseValuesKeyword(char[] query, int offset) Parse string to check presence of VALUES keyword regardless of case.- Parameters:
query
- char[] of the query statementoffset
- position of query to start checking- Returns:
- boolean indicates presence of word
-
parseLong
Faster version ofLong.parseLong(String)
when parsing a substring is required- Parameters:
s
- string to parsebeginIndex
- begin indexendIndex
- end index- Returns:
- long value
-
parseWithKeyword
public static boolean parseWithKeyword(char[] query, int offset) Parse string to check presence of WITH keyword regardless of case.- Parameters:
query
- char[] of the query statementoffset
- position of query to start checking- Returns:
- boolean indicates presence of word
-
parseAsKeyword
public static boolean parseAsKeyword(char[] query, int offset) Parse string to check presence of AS keyword regardless of case.- Parameters:
query
- char[] of the query statementoffset
- position of query to start checking- Returns:
- boolean indicates presence of word
-
isDigitAt
Returns true if a given strings
has digit at positionpos
.- Parameters:
s
- input stringpos
- position (0-based)- Returns:
- true if input string s has digit at position pos
-
digitAt
Converts digit at positionpos
in strings
to integer or throws.- Parameters:
s
- input stringpos
- position (0-based)- Returns:
- integer value of a digit at position pos
- Throws:
NumberFormatException
- if character at position pos is not an integer
-
isSpace
public static boolean isSpace(char c) Identifies characters which the backend scanner considers to be whitespace.https://github.com/postgres/postgres/blob/17bb62501787c56e0518e61db13a523d47afd724/src/backend/parser/scan.l#L194-L198
- Parameters:
c
- character- Returns:
- true if the character is a whitespace character as defined in the backend's parser
-
isArrayWhiteSpace
public static boolean isArrayWhiteSpace(char c) Identifies white space characters which the backend uses to determine if aString
value needs to be quoted in array representation.https://github.com/postgres/postgres/blob/f2c587067a8eb9cf1c8f009262381a6576ba3dd0/src/backend/utils/adt/arrayfuncs.c#L421-L438
- Parameters:
c
- Character to examine.- Returns:
- Indication if the character is a whitespace which back end will escape.
-
isOperatorChar
public static boolean isOperatorChar(char c) - Parameters:
c
- character- Returns:
- true if the given character is a valid character for an operator in the backend's parser
-
isIdentifierStartChar
public static boolean isIdentifierStartChar(char c) Checks if a character is valid as the start of an identifier. PostgreSQL 9.4 allows column names like _, ‿, ⁀, ⁔, ︳, ︴, ﹍, ﹎, ﹏, _, so it is assumed isJavaIdentifierPart is good enough for PostgreSQL.- Parameters:
c
- the character to check- Returns:
- true if valid as first character of an identifier; false if not
- See Also:
-
isIdentifierContChar
public static boolean isIdentifierContChar(char c) Checks if a character is valid as the second or later character of an identifier.- Parameters:
c
- the character to check- Returns:
- true if valid as second or later character of an identifier; false if not
-
charTerminatesIdentifier
public static boolean charTerminatesIdentifier(char c) - Parameters:
c
- character- Returns:
- true if the character terminates an identifier
-
isDollarQuoteStartChar
public static boolean isDollarQuoteStartChar(char c) Checks if a character is valid as the start of a dollar quoting tag.- Parameters:
c
- the character to check- Returns:
- true if valid as first character of a dollar quoting tag; false if not
-
isDollarQuoteContChar
public static boolean isDollarQuoteContChar(char c) Checks if a character is valid as the second or later character of a dollar quoting tag.- Parameters:
c
- the character to check- Returns:
- true if valid as second or later character of a dollar quoting tag; false if not
-
modifyJdbcCall
public static JdbcCallParseInfo modifyJdbcCall(String jdbcSql, boolean stdStrings, int serverVersion, int protocolVersion, EscapeSyntaxCallMode escapeSyntaxCallMode) throws SQLException Converts JDBC-specific callable statement escapes{ [? =] call <some_function> [(?, [?,..])] }
into the PostgreSQL format which isselect <some_function> (?, [?, ...]) as result
orselect * from <some_function> (?, [?, ...]) as result
(7.3)- Parameters:
jdbcSql
- sql text with JDBC escapesstdStrings
- if backslash in single quotes should be regular character or escape oneserverVersion
- server versionprotocolVersion
- protocol versionescapeSyntaxCallMode
- mode specifying whether JDBC escape call syntax is transformed into a CALL/SELECT statement- Returns:
- SQL in appropriate for given server format
- Throws:
SQLException
- if given SQL is malformed
-
replaceProcessing
public static String replaceProcessing(String sql, boolean replaceProcessingEnabled, boolean standardConformingStrings) throws SQLException Filter the SQL string of Java SQL Escape clauses.Currently implemented Escape clauses are those mentioned in 11.3 in the specification. Basically we look through the sql string for {d xxx}, {t xxx}, {ts xxx}, {oj xxx} or {fn xxx} in non-string sql code. When we find them, we just strip the escape part leaving only the xxx part. So, something like "select * from x where d={d '2001-10-09'}" would return "select * from x where d= '2001-10-09'".
- Parameters:
sql
- the original query textreplaceProcessingEnabled
- whether replace_processing_enabled is onstandardConformingStrings
- whether standard_conforming_strings is on- Returns:
- PostgreSQL-compatible SQL
- Throws:
SQLException
- if given SQL is wrong
-