Package net.sourceforge.jtds.jdbc
Class SQLParser
java.lang.Object
net.sourceforge.jtds.jdbc.SQLParser
Process JDBC escape strings and parameter markers in the SQL string.
This code recognizes the following escapes:
- Date {d 'yyyy-mm-dd'}
- Time {t 'hh:mm:ss'}
- Timestamp {ts 'yyyy-mm-dd hh:mm:ss.nnn'}
- ESCAPE {escape 'x'}
- Function {fn xxxx([arg,arg...])} NB The concat(arg, arg) operator is converted to (arg + arg)
- OuterJoin {oj .....}
- Call {?=call proc [arg, arg...]} or {call proc [arg, arg...]}
- This code is designed to be as efficient as possible and as result the validation done here is limited.
- Author:
- Mike Hutchinson, Holger Rehn
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static class
Serialized version of a parsed SQL query (the value stored in the cache for a parsed SQL). -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final SimpleLRUCache<SQLCacheKey,
SQLParser.CachedSQLQuery> a LRU cache for the last 500 parsed SQL statementsprivate final JtdsConnection
Connection object for server specific parsing.private static HashMap
Map of jdbc to server data types for convertprivate int
Current position in output buffer.private static final byte[]
Syntax mask for date escape.private static HashMap
Map of jdbc to sybase function names.private static boolean[]
Lookup table to test if character is part of an identifier.private final char[]
Input buffer with SQL statement.private String
First SQL keyword or identifier in statement.private final int
Length of input buffer.private static HashMap
Map of jdbc to sql server function names.private char[]
Output buffer to contain parsed SQL.private final ArrayList
Parameter list to be populated ornull
if no parameters are expected.private String
Procedure name in call escape.private int
Current position in input buffer.private final String
Original SQL stringprivate String
First table name in from clauseprivate char
Current expected terminator character.private static final byte[]
Syntax mask for time escape.(package private) static final byte[]
Syntax mask for timestamp escape. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivate
SQLParser
(String sqlIn, ArrayList paramList, JtdsConnection connection) Constructs a new parser object to process the supplied SQL. -
Method Summary
Modifier and TypeMethodDescriptionprivate final void
append
(char character) private final void
append
(char[] chars) private void
Processes the JDBC {call procedure [(?,?,?)]} type escape.private String
Copies over possible SQL keyword eg 'SELECT'private void
copyLiteral
(String txt) Inserts a String literal in the output buffer.private void
Builds a new parameter item.private String
Copies an embedded parameter name to the output buffer.private String
Copies an embedded stored procedure identifier over to the output buffer.private void
Copies over an embedded string literal unchanged.private void
Copies over white space.private void
escape()
Processes the JDBC escape sequences.private void
Processes the JDBC escape {fn function()}.private boolean
getDateTimeField
(byte[] mask) Utility routine to validate date and time escapes.private String
Extracts the first table name following the keyword FROM.private static boolean
isIdentifier
(int ch) Determines if character could be part of an SQL identifier.private void
Processes the JDBC escape {escape 'X'}.private void
mustbe
(char c, boolean copy) Checks that the next character is as expected.private void
Processes the JDBC escape {oj left outer join etc}.(package private) String[]
parse
(boolean extractTable) Parses the SQL statement processing JDBC escapes and parameter markers.(package private) static String[]
parse
(String sql, ArrayList paramList, JtdsConnection connection, boolean extractTable) Parse the SQL statement processing JDBC escapes and parameter markers.private void
Skips multi-line commentsprivate void
Skips single-line comments.private void
-
Field Details
-
_Cache
a LRU cache for the last 500 parsed SQL statements -
sql
Original SQL string -
in
private final char[] inInput buffer with SQL statement. -
s
private int sCurrent position in input buffer. -
len
private final int lenLength of input buffer. -
out
private char[] outOutput buffer to contain parsed SQL. -
d
private int dCurrent position in output buffer. -
params
Parameter list to be populated ornull
if no parameters are expected. -
terminator
private char terminatorCurrent expected terminator character. -
procName
Procedure name in call escape. -
keyWord
First SQL keyword or identifier in statement. -
tableName
First table name in from clause -
connection
Connection object for server specific parsing. -
identifierChar
private static boolean[] identifierCharLookup table to test if character is part of an identifier. -
timeMask
private static final byte[] timeMaskSyntax mask for time escape. -
dateMask
private static final byte[] dateMaskSyntax mask for date escape. -
timestampMask
static final byte[] timestampMaskSyntax mask for timestamp escape. -
fnMap
Map of jdbc to sybase function names. -
msFnMap
Map of jdbc to sql server function names. -
cvMap
Map of jdbc to server data types for convert
-
-
Constructor Details
-
SQLParser
Constructs a new parser object to process the supplied SQL.- Parameters:
sqlIn
- the SQL statement to parseparamList
- the parameter list array to populate ornull
if no parameters are expectedconnection
- the parent Connection object
-
-
Method Details
-
parse
static String[] parse(String sql, ArrayList paramList, JtdsConnection connection, boolean extractTable) throws SQLException Parse the SQL statement processing JDBC escapes and parameter markers.
- Parameters:
extractTable
-true
to return the first table name in the FROM clause of a SELECT- Returns:
- the processed SQL statement, any procedure name, the first SQL keyword
and (optionally) the first table name as elements 0, 1, 2 and 3 of the
returned
String[]
. - Throws:
SQLException
- if a parse error occurs
-
isIdentifier
private static boolean isIdentifier(int ch) Determines if character could be part of an SQL identifier. Characters > 127 are assumed to be unicode letters in other languages than english which is reasonable in this application.- Parameters:
ch
- the character to test.- Returns:
boolean
true if ch in A-Z a-z 0-9 @ $ # _.
-
copyLiteral
Inserts a String literal in the output buffer.- Parameters:
txt
- The text to insert.- Throws:
SQLException
-
copyString
private void copyString()Copies over an embedded string literal unchanged. -
copyKeyWord
Copies over possible SQL keyword eg 'SELECT' -
copyParam
Builds a new parameter item.- Parameters:
name
- Optional parameter name or null.pos
- The parameter marker position in the output buffer.- Throws:
SQLException
-
copyProcName
Copies an embedded stored procedure identifier over to the output buffer.- Returns:
- The identifier as a
String
. - Throws:
SQLException
-
copyParamName
Copies an embedded parameter name to the output buffer.- Returns:
- The identifier as a
String
.
-
copyWhiteSpace
private void copyWhiteSpace()Copies over white space. -
mustbe
Checks that the next character is as expected.- Parameters:
c
- The expected character.copy
- True if found character should be copied.- Throws:
SQLException
- if expected characeter not found.
-
skipWhiteSpace
- Throws:
SQLException
-
skipSingleComments
private void skipSingleComments()Skips single-line comments. -
skipMultiComments
Skips multi-line comments- Throws:
SQLException
-
callEscape
Processes the JDBC {call procedure [(?,?,?)]} type escape.- Throws:
SQLException
- if an error occurs
-
getDateTimeField
Utility routine to validate date and time escapes.- Parameters:
mask
- The validation mask- Returns:
- True if the escape was valid and processed OK.
- Throws:
SQLException
-
outerJoinEscape
Processes the JDBC escape {oj left outer join etc}.- Throws:
SQLException
-
functionEscape
Processes the JDBC escape {fn function()}.- Throws:
SQLException
-
likeEscape
Processes the JDBC escape {escape 'X'}.- Throws:
SQLException
-
escape
Processes the JDBC escape sequences.- Throws:
SQLException
-
getTableName
Extracts the first table name following the keyword FROM.- Returns:
- the table name as a
String
- Throws:
SQLException
-
append
private final void append(char[] chars) -
append
private final void append(char character) -
parse
Parses the SQL statement processing JDBC escapes and parameter markers.- Parameters:
extractTable
- true to return the first table name in the FROM clause of a select- Returns:
- The processed SQL statement, any procedure name, the first
SQL keyword and (optionally) the first table name as elements 0 1, 2 and 3 of the
returned
String[]
. - Throws:
SQLException
-