Class SqlBindParser

java.lang.Object
org.exolab.castor.jdo.engine.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

    Constructors
    Constructor
    Description
    Create a new SqlBindParser instance to parse the expression in 'sql'.
  • Method Summary

    Modifier and Type
    Method
    Description
    static 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
    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
    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 Details

    • SqlBindParser

      public SqlBindParser(String sql)
      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

      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
    • getBindExpr

      public 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 String getJdbcSql(String preSQL)
      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 statement
      preSQL - SQL statement string with bind variables of the form "?1"
      values - array of bind values
      Throws:
      SQLException