org.exolab.castor.util

Class SqlBindParser

public final class SqlBindParser extends Object

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
SqlBindParser(String sql)
Create a new SqlBindParser instance to parse the expression in 'sql'.
Method Summary
static voidbindJdbcValues(PreparedStatement stmt, String pre_sql, Object[] values)
Binds values to prepared SQL statement using the given sql string as reference for the bind variable order.
StringgetBindExpr()
Returns the current bind variable expression, e.g.
static StringgetJdbcSql(String pre_sql)
Creates a SQL statement from pre_sql, replacing bind expressions like "?
StringgetLastExpr()
Returns the expression substring beginning after the last processed bind variable and ending just before the current bind variable.
intgetParamNumber()
Returns the parameter number of the current bind variable, for example 1 a "$1" bind variable.
booleannext()
Move on to the next bind variable in '_sql'.

Constructor Detail

SqlBindParser

public SqlBindParser(String sql)
Create a new SqlBindParser instance to parse the expression in 'sql'.

Parameters: sql expression to be parsed

Method Detail

bindJdbcValues

public static void bindJdbcValues(PreparedStatement stmt, String pre_sql, Object[] values)
Binds values to prepared SQL statement using the given sql string as reference for the bind variable order.

Parameters: stmt JDBC statement pre_sql SQL statement string with bind variables of the form "?1" values array of bind values

Throws: SQLException

getBindExpr

public String getBindExpr()
Returns the current bind variable expression, e.g. "$1".

Returns: current bind variable expression

getJdbcSql

public static String getJdbcSql(String pre_sql)
Creates a SQL statement from pre_sql, replacing bind expressions like "?1" by "?"

Parameters: pre_sql SQL statement string with bind variables of the form "?1"

Returns: SQL statement string with bind variables of the form "?"

getLastExpr

public String getLastExpr()
Returns the expression substring beginning after the last processed bind variable and ending just before the current bind variable.

Returns: last expression substring

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

next

public boolean next()
Move on to the next bind variable in '_sql'.

Returns: true, if an bind variable could be found

Intalio Inc. (C) 1999-2004. All rights reserved http://www.intalio.com