public class Parser
extends java.lang.Object
Modifier and Type | Class and Description |
---|---|
private static class |
Parser.SqlParseState |
Modifier and Type | Field and Description |
---|---|
private static int[] |
NO_BINDS |
private static char[] |
QUOTE_OR_ALPHABETIC_MARKER |
private static char[] |
QUOTE_OR_ALPHABETIC_MARKER_OR_PARENTHESIS |
private static char[] |
SINGLE_QUOTE |
Constructor and Description |
---|
Parser() |
Modifier and Type | Method and Description |
---|---|
private static boolean |
addReturning(java.lang.StringBuilder nativeSql,
SqlCommandType currentCommandType,
java.lang.String[] returningColumnNames,
boolean isReturningPresent) |
static boolean |
charTerminatesIdentifier(char c) |
private static void |
checkParsePosition(int i,
int len,
int i0,
char[] sql,
java.lang.String message) |
static int |
digitAt(java.lang.String s,
int pos)
Converts digit at position
pos in string s to integer or throws. |
private static int |
escapeFunction(char[] sql,
int i,
java.lang.StringBuilder newsql,
boolean stdStrings) |
private static int |
escapeFunctionArguments(java.lang.StringBuilder newsql,
java.lang.String functionName,
char[] sql,
int i,
boolean stdStrings)
Generate sql for escaped functions.
|
private static int |
findOpenBrace(char[] sql,
int i) |
static boolean |
isDigitAt(java.lang.String s,
int pos)
Returns true if a given string
s has digit at position pos . |
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,
EscapeSyntaxCallMode escapeSyntaxCallMode)
Converts JDBC-specific callable statement escapes
{ [? =] call <some_function> [(?,
[?,..])] } into the PostgreSQL format which is select <some_function> (?, [?, ...]) as
result or select * from <some_function> (?, [?, ...]) as result (7.3) |
static boolean |
parseAsKeyword(char[] query,
int offset)
Parse string to check presence of AS keyword regardless of case.
|
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 long |
parseLong(java.lang.String s,
int beginIndex,
int endIndex)
Faster version of
Long.parseLong(String) when parsing a substring is required |
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.
|
private static int |
parseSql(char[] sql,
int i,
java.lang.StringBuilder newsql,
boolean stopOnComma,
boolean stdStrings)
parse the given sql from index i, appending it to the given buffer until we hit an unmatched
right parentheses or end of string.
|
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.
|
private static SqlCommandType |
parseWithCommandType(char[] aChars,
int i,
int keywordStart,
int wordLength) |
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 sql,
boolean replaceProcessingEnabled,
boolean standardConformingStrings)
Filter the SQL string of Java SQL Escape clauses.
|
private static boolean |
subArraysEqual(char[] arr,
int offA,
int offB,
int len)
Compares two sub-arrays of the given character array for equalness.
|
private static int[] |
toIntArray(java.util.List<java.lang.Integer> list)
Converts
List<Integer> to int[] . |
private static final int[] NO_BINDS
private static final char[] QUOTE_OR_ALPHABETIC_MARKER
private static final char[] QUOTE_OR_ALPHABETIC_MARKER_OR_PARENTHESIS
private static final char[] SINGLE_QUOTE
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
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 namesjava.sql.SQLException
- if unable to add returning clause (invalid column names)private static SqlCommandType parseWithCommandType(char[] aChars, int i, int keywordStart, int wordLength)
private static boolean addReturning(java.lang.StringBuilder nativeSql, SqlCommandType currentCommandType, java.lang.String[] returningColumnNames, boolean isReturningPresent) throws java.sql.SQLException
java.sql.SQLException
private static int[] toIntArray(java.util.List<java.lang.Integer> list)
List<Integer>
to int[]
. Empty and null
lists are converted to
empty array.list
- input listpublic 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.
query
- queryoffset
- start offsetstandardConformingStrings
- standard conforming stringspublic 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.
query
- queryoffset
- start offsetpublic static int parseDollarQuotes(char[] query, int offset)
$
) at the given offset starts a dollar-quoted string and
return the offset of the ending dollar character.query
- queryoffset
- start offsetpublic static int parseLineComment(char[] query, int offset)
-
character at offset
starts a --
style line comment,
and return the position of the first \r
or \n
character.query
- queryoffset
- start offset\r
or \n
characterpublic static int parseBlockComment(char[] query, int offset)
/
character at offset
starts a block comment, and return the
position of the last /
character.query
- queryoffset
- start offset/
characterpublic static boolean parseDeleteKeyword(char[] query, int offset)
query
- char[] of the query statementoffset
- position of query to start checkingpublic static boolean parseInsertKeyword(char[] query, int offset)
query
- char[] of the query statementoffset
- position of query to start checkingpublic static boolean parseMoveKeyword(char[] query, int offset)
query
- char[] of the query statementoffset
- position of query to start checkingpublic static boolean parseReturningKeyword(char[] query, int offset)
query
- char[] of the query statementoffset
- position of query to start checkingpublic static boolean parseSelectKeyword(char[] query, int offset)
query
- char[] of the query statementoffset
- position of query to start checkingpublic static boolean parseUpdateKeyword(char[] query, int offset)
query
- char[] of the query statementoffset
- position of query to start checkingpublic static boolean parseValuesKeyword(char[] query, int offset)
query
- char[] of the query statementoffset
- position of query to start checkingpublic static long parseLong(java.lang.String s, int beginIndex, int endIndex)
Long.parseLong(String)
when parsing a substring is requireds
- string to parsebeginIndex
- begin indexendIndex
- end indexpublic static boolean parseWithKeyword(char[] query, int offset)
query
- char[] of the query statementoffset
- position of query to start checkingpublic static boolean parseAsKeyword(char[] query, int offset)
query
- char[] of the query statementoffset
- position of query to start checkingpublic static boolean isDigitAt(java.lang.String s, int pos)
s
has digit at position pos
.s
- input stringpos
- position (0-based)public static int digitAt(java.lang.String s, int pos)
pos
in string s
to integer or throws.s
- input stringpos
- position (0-based)java.lang.NumberFormatException
- if character at position pos is not an integerpublic static boolean isSpace(char c)
c
- characterpublic static boolean isOperatorChar(char c)
c
- characterpublic static boolean isIdentifierStartChar(char c)
c
- the character to checkpublic static boolean isIdentifierContChar(char c)
c
- the character to checkpublic static boolean charTerminatesIdentifier(char c)
c
- characterpublic static boolean isDollarQuoteStartChar(char c)
c
- the character to checkpublic static boolean isDollarQuoteContChar(char c)
c
- the character to checkprivate static boolean subArraysEqual(char[] arr, int offA, int offB, int len)
arr
- a char arrayoffA
- first sub-array start offsetoffB
- second sub-array start offsetlen
- length of the sub arrays to comparepublic static JdbcCallParseInfo modifyJdbcCall(java.lang.String jdbcSql, boolean stdStrings, int serverVersion, int protocolVersion, EscapeSyntaxCallMode escapeSyntaxCallMode) throws java.sql.SQLException
{ [? =] call <some_function> [(?,
[?,..])] }
into the PostgreSQL format which is select <some_function> (?, [?, ...]) as
result
or select * from <some_function> (?, [?, ...]) as result
(7.3)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 statementjava.sql.SQLException
- if given SQL is malformedpublic static java.lang.String replaceProcessing(java.lang.String 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'".
sql
- the original query textreplaceProcessingEnabled
- whether replace_processing_enabled is onstandardConformingStrings
- whether standard_conforming_strings is onjava.sql.SQLException
- if given SQL is wrongprivate static int parseSql(char[] sql, int i, java.lang.StringBuilder newsql, boolean stopOnComma, boolean stdStrings) throws java.sql.SQLException
sql
- the original query texti
- starting position for replacingnewsql
- where to write the replaced outputstopOnComma
- should we stop after hitting the first comma in sql text?stdStrings
- whether standard_conforming_strings is onjava.sql.SQLException
- if given SQL is wrongprivate static int findOpenBrace(char[] sql, int i)
private static void checkParsePosition(int i, int len, int i0, char[] sql, java.lang.String message) throws PSQLException
PSQLException
private static int escapeFunction(char[] sql, int i, java.lang.StringBuilder newsql, boolean stdStrings) throws java.sql.SQLException
java.sql.SQLException
private static int escapeFunctionArguments(java.lang.StringBuilder newsql, java.lang.String functionName, char[] sql, int i, boolean stdStrings) throws java.sql.SQLException
newsql
- destination StringBuilderfunctionName
- the escaped function namesql
- input SQL text (containing arguments of a function call with possible JDBC escapes)i
- position in the input SQLstdStrings
- whether standard_conforming_strings is onjava.sql.SQLException
- if something goes wrong