Top Description Fields Constructors Methods
org.springframework.r2dbc.core

pack-priv Class NamedParameterExpander

extends Object
Class Inheritance
Imports
java.util.List, org.springframework.r2dbc.core.binding.BindMarkersFactory, org.springframework.util.ConcurrentLruCache

SQL translation support allowing the use of named parameters rather than native placeholders.

This class expands SQL from named parameters to native style placeholders at execution time. It also allows for expanding a List of values to the appropriate number of placeholders.

References to the same parameter name are substituted with the same bind marker placeholder if a BindMarkersFactory uses identifiable placeholders.

Note

An instance of this class is thread-safe once configured.

Authors
Mark Paluch, Juergen Hoeller

Field Summary

Modifier and TypeField and Description
public static final int
DEFAULT_CACHE_LIMIT

Default maximum number of entries for the SQL cache: 256.

private final ConcurrentLruCache<String, ParsedSql>
parsedSqlCache

Cache of original SQL String to ParsedSql representation.

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public PreparedOperation<String>

Returns:

the expanded sql that accepts bind parameters and allows for execution without further translation wrapped as PreparedOperation.
expand
(String
the original SQL statement
sql
,
BindMarkersFactory
the bind marker factory
bindMarkersFactory
,
BindParameterSource
the source for named parameters
paramSource
)

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

public List<String>

Returns:

the parameter names
getParameterNames
(String sql)

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

private ParsedSql

Returns:

a representation of the parsed SQL statement
getParsedSql
(String
the original SQL statement
sql
)

Obtain a parsed representation of the given SQL statement.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

DEFAULT_CACHE_LIMITback to summary
public static final int DEFAULT_CACHE_LIMIT

Default maximum number of entries for the SQL cache: 256.

parsedSqlCacheback to summary
private final ConcurrentLruCache<String, ParsedSql> parsedSqlCache

Cache of original SQL String to ParsedSql representation.

Constructor Detail

NamedParameterExpanderback to summary
pack-priv NamedParameterExpander()

Method Detail

expandback to summary
public PreparedOperation<String> expand(String sql, BindMarkersFactory bindMarkersFactory, BindParameterSource paramSource)

Parse the SQL statement and locate any placeholders or named parameters. Named parameters are substituted for a native 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
sql:String

the original SQL statement

bindMarkersFactory:BindMarkersFactory

the bind marker factory

paramSource:BindParameterSource

the source for named parameters

Returns:PreparedOperation<String>

the expanded sql that accepts bind parameters and allows for execution without further translation wrapped as PreparedOperation.

getParameterNamesback to summary
public List<String> getParameterNames(String sql)

Parse the SQL statement and locate any placeholders or named parameters. Named parameters are returned as result of this method invocation.

Returns:List<String>

the parameter names

getParsedSqlback to summary
private ParsedSql getParsedSql(String sql)

Obtain a parsed representation of the given SQL statement.

The default implementation uses an LRU cache with an upper limit of 256 entries.

Parameters
sql:String

the original SQL statement

Returns:ParsedSql

a representation of the parsed SQL statement