Package org.postgresql.core
Class Parser
- java.lang.Object
-
- org.postgresql.core.Parser
-
public class Parser extends java.lang.Object
Basic query parser infrastructure. Note: This class should not be considered as pgjdbc public API.- Author:
- Michael Paesold (mpaesold@gmx.at), Christopher Deckers (chrriis@gmail.com)
-
-
Constructor Summary
Constructors Constructor Description Parser()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static boolean
charTerminatesIdentifier(char c)
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)
static JdbcCallParseInfo
modifyJdbcCall(java.lang.String jdbcSql, boolean stdStrings, int serverVersion, int protocolVersion)
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 int
parseBlockComment(char[] query, int offset)
Test if the / character at offset starts a block comment, and return the position of the last / character.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 java.util.List<NativeQuery>
parseJdbcSql(java.lang.String query, boolean standardConformingStrings, boolean withParameters, boolean splitStatements, boolean isBatchedReWriteConfigured, java.lang.String... returningColumnNames)
Parses JDBC query into PostgreSQL's native format.static int
parseLineComment(char[] query, int offset)
Test if the - character at offset starts a -- style line comment, and return the position of the first \r or \n character.static 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 java.lang.String
replaceProcessing(java.lang.String p_sql, boolean replaceProcessingEnabled, boolean standardConformingStrings)
Filter the SQL string of Java SQL Escape clauses.
-
-
-
Method Detail
-
parseJdbcSql
public static java.util.List<NativeQuery> parseJdbcSql(java.lang.String query, boolean standardConformingStrings, boolean withParameters, boolean splitStatements, boolean isBatchedReWriteConfigured, java.lang.String... returningColumnNames) throws java.sql.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 enabledreturningColumnNames
- for simple insert, update, delete add returning with given column names- Returns:
- list of native queries
- Throws:
java.sql.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 at offset 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 at offset 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
-
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
-
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
-
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
-
isSpace
public static boolean isSpace(char c)
- Parameters:
c
- character- Returns:
- true if the character is a whitespace character as defined in the backend's parser
-
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.- Parameters:
c
- the character to check- Returns:
- true if valid as first character of an identifier; false if not
-
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(java.lang.String jdbcSql, boolean stdStrings, int serverVersion, int protocolVersion) throws java.sql.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 version- Returns:
- SQL in appropriate for given server format
- Throws:
java.sql.SQLException
- if given SQL is malformed
-
replaceProcessing
public static java.lang.String replaceProcessing(java.lang.String p_sql, boolean replaceProcessingEnabled, boolean standardConformingStrings) throws java.sql.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:
p_sql
- the original query textreplaceProcessingEnabled
- whether replace_processing_enabled is onstandardConformingStrings
- whether standard_conforming_strings is on- Returns:
- PostgreSQL-compatible SQL
- Throws:
java.sql.SQLException
-
-