org.exolab.castor.util
Class SqlBindParser

java.lang.Object
  extended by org.exolab.castor.util.SqlBindParser

public final class SqlBindParser
extends java.lang.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(java.lang.String sql)
          Create a new SqlBindParser instance to parse the expression in 'sql'.
 
Method Summary
static void bindJdbcValues(java.sql.PreparedStatement stmt, java.lang.String pre_sql, java.lang.Object[] values)
          Binds values to prepared SQL statement using the given sql string as reference for the bind variable order.
 java.lang.String getBindExpr()
          Returns the current bind variable expression, e.g.
static java.lang.String getJdbcSql(java.lang.String pre_sql)
          Creates a SQL statement from pre_sql, replacing bind expressions like "?1" by "?"
 java.lang.String getLastExpr()
          Returns the expression substring beginning after the last processed bind variable and ending just before the current bind variable.
 int getParamNumber()
          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'.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SqlBindParser

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

Parameters:
sql - expression to be parsed
Method Detail

next

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

Returns:
true, if an bind variable could be found

getLastExpr

public java.lang.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

getBindExpr

public java.lang.String 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

public static java.lang.String getJdbcSql(java.lang.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 "?"

bindJdbcValues

public static void bindJdbcValues(java.sql.PreparedStatement stmt,
                                  java.lang.String pre_sql,
                                  java.lang.Object[] values)
                           throws java.sql.SQLException
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:
java.sql.SQLException


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