Top Description Inners Fields Constructors Methods
org.springframework.jdbc.core.namedparam

public abstract Class NamedParameterUtils

extends Object
Class Inheritance
Imports
java.util.ArrayList, .HashSet, .Iterator, .List, .Map, .Set, org.springframework.dao.InvalidDataAccessApiUsageException, org.springframework.jdbc.core.SqlParameter, .SqlParameterValue, org.springframework.lang.Nullable, org.springframework.util.Assert

Helper methods for named parameter parsing.

Only intended for internal use within Spring's JDBC framework.

Authors
Thomas Risberg, Juergen Hoeller, Yanming Zhou
Since
2.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class

Field Summary

Modifier and TypeField and Description
private static final String
PARAMETER_SEPARATORS

Set of characters that qualify as parameter separators, indicating that a parameter name in an SQL String has ended.

private static final boolean[]
separatorIndex

An index with separator flags per character code.

private static final String[]
START_SKIP

Set of characters that qualify as comment or quotes starting characters.

private static final String[]
STOP_SKIP

Set of characters that at are the corresponding comment or quotes ending characters.

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
private static int
addNamedParameter(List<NamedParameterUtils.ParameterHolder> parameterList, int totalParameterCount, int escapes, int i, int j, String parameter)

private static int
addNewNamedParameter(Set<String> namedParameters, int namedParameterCount, String parameter)

public static List<SqlParameter>
buildSqlParameterList(ParsedSql
the parsed SQL statement
parsedSql
,
SqlParameterSource
the source for named parameters
paramSource
)

Convert parameter declarations from an SqlParameterSource to a corresponding List of SqlParameters.

public static int[]
buildSqlTypeArray(ParsedSql
the parsed SQL statement
parsedSql
,
SqlParameterSource
the source for named parameters
paramSource
)

Convert parameter types from an SqlParameterSource into a corresponding int array.

public static Object[]

Returns:

the array of values
buildValueArray
(ParsedSql
the parsed SQL statement
parsedSql
,
SqlParameterSource
the source for named parameters
paramSource
,
List<SqlParameter>
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.
declaredParams
)

Convert a Map of named parameter values to a corresponding array.

public static Object[]

Returns:

the array of values
buildValueArray
(String
the SQL statement
sql
,
Map<String, ?>
the Map of parameters
paramMap
)

Convert a Map of named parameter values to a corresponding array.

private static SqlParameter

Returns:

the declared SqlParameter, or null if none found
findParameter
(List<SqlParameter>
the declared SqlParameter objects
declaredParams
,
String
the name of the desired parameter
paramName
,
int
the index of the desired parameter
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.

public static ParsedSql

Returns:

the parsed statement, represented as ParsedSql instance
parseSqlStatement
(String
the SQL statement
sql
)

Parse the SQL statement and locate any placeholders or named parameters.

public static String

Returns:

the actual (parsed) SQL statement
parseSqlStatementIntoString
(String
the SQL statement
sql
)

Parse the SQL statement and locate any placeholders or named parameters.

private static int

Returns:

next position to process after any comments or quotes are skipped
skipCommentsAndQuotes
(char[]
character array containing SQL statement
statement
,
int
current position of statement
position
)

Skip over comments and quoted names present in an SQL statement.

public static String

Returns:

the SQL statement with substituted parameters
substituteNamedParameters
(ParsedSql
the parsed representation of the SQL statement
parsedSql
,
SqlParameterSource
the source for named parameters
paramSource
)

Parse the SQL statement and locate any placeholders or named parameters.

public static String

Returns:

the SQL statement with substituted parameters
substituteNamedParameters
(String
the SQL statement
sql
,
SqlParameterSource
the source for named parameters
paramSource
)

Parse the SQL statement and locate any placeholders or named parameters.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

PARAMETER_SEPARATORSback to summary
private static final String PARAMETER_SEPARATORS

Set of characters that qualify as parameter separators, indicating that a parameter name in an SQL String has ended.

separatorIndexback to summary
private static final boolean[] separatorIndex

An index with separator flags per character code. Technically only needed between 34 and 124 at this point.

START_SKIPback to summary
private static final String[] START_SKIP

Set of characters that qualify as comment or quotes starting characters.

STOP_SKIPback to summary
private static final String[] STOP_SKIP

Set of characters that at are the corresponding comment or quotes ending characters.

Constructor Detail

NamedParameterUtilsback to summary
public NamedParameterUtils()

Method Detail

addNamedParameterback to summary
private static int addNamedParameter(List<NamedParameterUtils.ParameterHolder> parameterList, int totalParameterCount, int escapes, int i, int j, String parameter)
addNewNamedParameterback to summary
private static int addNewNamedParameter(Set<String> namedParameters, int namedParameterCount, String parameter)
buildSqlParameterListback to summary
public static 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:ParsedSql

the parsed SQL statement

paramSource:SqlParameterSource

the source for named parameters

buildSqlTypeArrayback to summary
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:ParsedSql

the parsed SQL statement

paramSource:SqlParameterSource

the source for named parameters

buildValueArrayback to summary
public static Object[] buildValueArray(ParsedSql parsedSql, SqlParameterSource paramSource, List<SqlParameter> declaredParams)

Convert a Map of named parameter values to a corresponding array.

Parameters
parsedSql:ParsedSql

the parsed SQL statement

paramSource:SqlParameterSource

the source for named parameters

declaredParams:List<SqlParameter>

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:Object[]

the array of values

buildValueArrayback to summary
public static Object[] buildValueArray(String sql, Map<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:String

the SQL statement

paramMap:Map<String, ?>

the Map of parameters

Returns:Object[]

the array of values

findParameterback to summary
private static SqlParameter findParameter(List<SqlParameter> declaredParams, String paramName, int paramIndex)

Find a matching parameter in the given list of declared parameters.

Parameters
declaredParams:List<SqlParameter>

the declared SqlParameter objects

paramName:String

the name of the desired parameter

paramIndex:int

the index of the desired parameter

Returns:SqlParameter

the declared SqlParameter, or null if none found

Annotations
@Nullable
isParameterSeparatorback to summary
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.

parseSqlStatementback to summary
public static ParsedSql parseSqlStatement(String sql)

Parse the SQL statement and locate any placeholders or named parameters. Named parameters are substituted for a JDBC placeholder.

Parameters
sql:String

the SQL statement

Returns:ParsedSql

the parsed statement, represented as ParsedSql instance

parseSqlStatementIntoStringback to summary
public static String parseSqlStatementIntoString(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:String

the SQL statement

Returns:String

the actual (parsed) SQL statement

skipCommentsAndQuotesback to summary
private static int skipCommentsAndQuotes(char[] statement, int position)

Skip over comments and quoted names present in an SQL statement.

Parameters
statement:char[]

character array containing SQL statement

position:int

current position of statement

Returns:int

next position to process after any comments or quotes are skipped

substituteNamedParametersback to summary
public static 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 parentheses. 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 placeholders to be used for a select list. Select lists should be limited to 100 or fewer elements. A larger number of elements is not guaranteed to be supported by the database and is strictly vendor-dependent.

Parameters
parsedSql:ParsedSql

the parsed representation of the SQL statement

paramSource:SqlParameterSource

the source for named parameters

Returns:String

the SQL statement with substituted parameters

See Also
parseSqlStatement
substituteNamedParametersback to summary
public static String substituteNamedParameters(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:String

the SQL statement

paramSource:SqlParameterSource

the source for named parameters

Returns:String

the SQL statement with substituted parameters

org.springframework.jdbc.core.namedparam back to summary

private Class NamedParameterUtils.ParameterHolder

extends Object
Class Inheritance
  • java.lang.Object
  • org.springframework.jdbc.core.namedparam.NamedParameterUtils.ParameterHolder

Field Summary

Modifier and TypeField and Description
private final int
private final String
private final int

Constructor Summary

AccessConstructor and Description
public
ParameterHolder(String parameterName, int startIndex, int endIndex)

Method Summary

Modifier and TypeMethod and Description
public int
public String
public int
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

endIndexback to summary
private final int endIndex
parameterNameback to summary
private final String parameterName
startIndexback to summary
private final int startIndex

Constructor Detail

ParameterHolderback to summary
public ParameterHolder(String parameterName, int startIndex, int endIndex)

Method Detail

getEndIndexback to summary
public int getEndIndex()
getParameterNameback to summary
public String getParameterName()
getStartIndexback to summary
public int getStartIndex()