org.springframework.jdbc.core.namedparam
Class NamedParameterUtils

java.lang.Object
  extended by org.springframework.jdbc.core.namedparam.NamedParameterUtils

public abstract class NamedParameterUtils
extends java.lang.Object

Helper methods for named parameter parsing. Only intended for internal use within Spring's JDBC framework.

Since:
2.0
Author:
Thomas Risberg, Juergen Hoeller

Field Summary
private static char[] PARAMETER_SEPARATORS
          Set of characters that qualify as parameter separators, indicating that a parameter name in a SQL String has ended.
private static java.lang.String[] START_SKIP
          Set of characters that qualify as comment or quotes starting characters.
private static java.lang.String[] STOP_SKIP
          Set of characters that at are the corresponding comment or quotes ending characters.
 
Constructor Summary
NamedParameterUtils()
           
 
Method Summary
static java.util.List<SqlParameter> buildSqlParameterList(ParsedSql parsedSql, SqlParameterSource paramSource)
          Convert parameter declarations from an SqlParameterSource to a corresponding List of SqlParameters.
static int[] buildSqlTypeArray(ParsedSql parsedSql, SqlParameterSource paramSource)
          Convert parameter types from an SqlParameterSource into a corresponding int array.
static java.lang.Object[] buildValueArray(ParsedSql parsedSql, SqlParameterSource paramSource, java.util.List<SqlParameter> declaredParams)
          Convert a Map of named parameter values to a corresponding array.
static java.lang.Object[] buildValueArray(java.lang.String sql, java.util.Map<java.lang.String,?> paramMap)
          Convert a Map of named parameter values to a corresponding array.
private static SqlParameter findParameter(java.util.List<SqlParameter> declaredParams, java.lang.String paramName, int paramIndex)
          Find a matching parameter in the given list of declared parameters.
private static boolean isParameterSeparator(char c)
          Determine whether a parameter name ends at the current position, that is, whether the given character qualifies as a separator.
static ParsedSql parseSqlStatement(java.lang.String sql)
          Parse the SQL statement and locate any placeholders or named parameters.
static java.lang.String parseSqlStatementIntoString(java.lang.String sql)
          Parse the SQL statement and locate any placeholders or named parameters.
private static int skipCommentsAndQuotes(char[] statement, int position)
          Skip over comments and quoted names present in an SQL statement
static java.lang.String substituteNamedParameters(ParsedSql parsedSql, SqlParameterSource paramSource)
          Parse the SQL statement and locate any placeholders or named parameters.
static java.lang.String substituteNamedParameters(java.lang.String sql, SqlParameterSource paramSource)
          Parse the SQL statement and locate any placeholders or named parameters.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PARAMETER_SEPARATORS

private static final char[] PARAMETER_SEPARATORS
Set of characters that qualify as parameter separators, indicating that a parameter name in a SQL String has ended.


START_SKIP

private static final java.lang.String[] START_SKIP
Set of characters that qualify as comment or quotes starting characters.


STOP_SKIP

private static final java.lang.String[] STOP_SKIP
Set of characters that at are the corresponding comment or quotes ending characters.

Constructor Detail

NamedParameterUtils

public NamedParameterUtils()
Method Detail

parseSqlStatement

public static ParsedSql parseSqlStatement(java.lang.String sql)
Parse the SQL statement and locate any placeholders or named parameters. Named parameters are substituted for a JDBC placeholder.

Parameters:
sql - the SQL statement
Returns:
the parsed statement, represented as ParsedSql instance

skipCommentsAndQuotes

private static int skipCommentsAndQuotes(char[] statement,
                                         int position)
Skip over comments and quoted names present in an SQL statement

Parameters:
statement - character array containing SQL statement
position - current position of statement
Returns:
next position to process after any comments or quotes are skipped

substituteNamedParameters

public static java.lang.String substituteNamedParameters(ParsedSql parsedSql,
                                                         SqlParameterSource paramSource)
Parse the SQL statement and locate any placeholders or named parameters. Named parameters are substituted for a JDBC placeholder and any select list is expanded to the required number of placeholders. Select lists may contain an array of objects and in that case the placeholders will be grouped and enclosed with parantheses. This allows for the use of "expression lists" in the SQL statement like:
select id, name, state from table where (name, age) in (('John', 35), ('Ann', 50))

The parameter values passed in are used to determine the number of placeholder to be used for a select list. Select lists should be limited to 100 or fewer elements. A larger number of elements is not guaramteed to be supported by the database and is strictly vendor-dependent.

Parameters:
parsedSql - the parsed represenation of the SQL statement
paramSource - the source for named parameters
Returns:
the SQL statement with substituted parameters
See Also:
parseSqlStatement(java.lang.String)

buildValueArray

public static java.lang.Object[] buildValueArray(ParsedSql parsedSql,
                                                 SqlParameterSource paramSource,
                                                 java.util.List<SqlParameter> declaredParams)
Convert a Map of named parameter values to a corresponding array.

Parameters:
parsedSql - the parsed SQL statement
paramSource - the source for named parameters
declaredParams - the List of declared SqlParameter objects (may be null). If specified, the parameter metadata will be built into the value array in the form of SqlParameterValue objects.
Returns:
the array of values

findParameter

private static SqlParameter findParameter(java.util.List<SqlParameter> declaredParams,
                                          java.lang.String paramName,
                                          int paramIndex)
Find a matching parameter in the given list of declared parameters.

Parameters:
declaredParams - the declared SqlParameter objects
paramName - the name of the desired parameter
paramIndex - the index of the desired parameter
Returns:
the declared SqlParameter, or null if none found

isParameterSeparator

private static boolean isParameterSeparator(char c)
Determine whether a parameter name ends at the current position, that is, whether the given character qualifies as a separator.


buildSqlTypeArray

public static int[] buildSqlTypeArray(ParsedSql parsedSql,
                                      SqlParameterSource paramSource)
Convert parameter types from an SqlParameterSource into a corresponding int array. This is necessary in order to reuse existing methods on JdbcTemplate. Any named parameter types are placed in the correct position in the Object array based on the parsed SQL statement info.

Parameters:
parsedSql - the parsed SQL statement
paramSource - the source for named parameters

buildSqlParameterList

public static java.util.List<SqlParameter> buildSqlParameterList(ParsedSql parsedSql,
                                                                 SqlParameterSource paramSource)
Convert parameter declarations from an SqlParameterSource to a corresponding List of SqlParameters. This is necessary in order to reuse existing methods on JdbcTemplate. The SqlParameter for a named parameter is placed in the correct position in the resulting list based on the parsed SQL statement info.

Parameters:
parsedSql - the parsed SQL statement
paramSource - the source for named parameters

parseSqlStatementIntoString

public static java.lang.String parseSqlStatementIntoString(java.lang.String sql)
Parse the SQL statement and locate any placeholders or named parameters. Named parameters are substituted for a JDBC placeholder.

This is a shortcut version of parseSqlStatement(String) in combination with substituteNamedParameters(ParsedSql, SqlParameterSource).

Parameters:
sql - the SQL statement
Returns:
the actual (parsed) SQL statement

substituteNamedParameters

public static java.lang.String substituteNamedParameters(java.lang.String sql,
                                                         SqlParameterSource paramSource)
Parse the SQL statement and locate any placeholders or named parameters. Named parameters are substituted for a JDBC placeholder and any select list is expanded to the required number of placeholders.

This is a shortcut version of substituteNamedParameters(ParsedSql, SqlParameterSource).

Parameters:
sql - the SQL statement
paramSource - the source for named parameters
Returns:
the SQL statement with substituted parameters

buildValueArray

public static java.lang.Object[] buildValueArray(java.lang.String sql,
                                                 java.util.Map<java.lang.String,?> paramMap)
Convert a Map of named parameter values to a corresponding array.

This is a shortcut version of buildValueArray(ParsedSql, SqlParameterSource, java.util.List).

Parameters:
sql - the SQL statement
paramMap - the Map of parameters
Returns:
the array of values