Package org.exolab.castor.jdo.engine
Class SqlBindParser
java.lang.Object
org.exolab.castor.jdo.engine.SqlBindParser
Utility class to parse an SQL or OQL expression for bind variables
Bind variables are subexpressions of the form "$n", where n is a
positive integer number.
To parse the expression, call SqlBindParser.next() in a loop
until no more bind variable can be found. Each call moves on to
the next bind variable and returns true if another could be found.
Inside the loop call SqlBindParser.getBindExpr() to access the
current bind variable expression. ("$1", "$2", ...)
SqlBindParser.getParamNumber() can be used to read the parameter
number (1, 2, ...).
If you are interested in the remainder of the expression string,
just call getLastExpr() to get the last processed substring.
For example, when parsing the expression
"select * from x where id between $1 and $2"
this gives you the following function returns:
next() -> true
getLastExpr() -> "select * from x where id between "
getBindExpr() -> "$1"
getParamNumber() -> 1
next() -> true
getLastExpr() -> " and "
getBindExpr() -> "$2"
getParamNumber() -> 2
next() -> false
getLastExpr() -> ""
- Author:
- Martin Fuchs
-
Constructor Summary
ConstructorsConstructorDescriptionSqlBindParser
(String sql) Create a new SqlBindParser instance to parse the expression in 'sql'. -
Method Summary
Modifier and TypeMethodDescriptionstatic void
bindJdbcValues
(PreparedStatement stmt, String preSQL, Object[] values) Binds values to prepared SQL statement using the given sql string as reference for the bind variable order.Returns the current bind variable expression, e.g.static String
getJdbcSql
(String preSQL) Creates a SQL statement from pre_sql, replacing bind expressions like "?1" by "?".Returns the expression substring beginning after the last processed bind variable and ending just before the current bind variable.int
Returns the parameter number of the current bind variable, for example 1 a "$1" bind variable.boolean
next()
Move on to the next bind variable in '_sql'.
-
Constructor Details
-
SqlBindParser
Create a new SqlBindParser instance to parse the expression in 'sql'.- Parameters:
sql
- expression to be parsed
-
-
Method Details
-
next
public boolean next()Move on to the next bind variable in '_sql'.- Returns:
- true, if an bind variable could be found
-
getLastExpr
Returns the expression substring beginning after the last processed bind variable and ending just before the current bind variable.- Returns:
- last expression substring
-
getBindExpr
Returns the current bind variable expression, e.g. "$1".- Returns:
- current bind variable expression
-
getParamNumber
public int getParamNumber()Returns the parameter number of the current bind variable, for example 1 a "$1" bind variable. If an un-numbered bind variable "$" is found, 0 is returned.- Returns:
- parameter number of current bind variable
-
getJdbcSql
Creates a SQL statement from pre_sql, replacing bind expressions like "?1" by "?".- Parameters:
preSQL
- SQL statement string with bind variables of the form "?1".- Returns:
- SQL statement string with bind variables of the form "?".
-
bindJdbcValues
public static void bindJdbcValues(PreparedStatement stmt, String preSQL, Object[] values) throws SQLException Binds values to prepared SQL statement using the given sql string as reference for the bind variable order.- Parameters:
stmt
- JDBC statementpreSQL
- SQL statement string with bind variables of the form "?1"values
- array of bind values- Throws:
SQLException
-