1616
1717package org .springframework .r2dbc .core ;
1818
19- import java .util .LinkedHashMap ;
2019import java .util .List ;
21- import java .util .Map ;
22-
23- import org .apache .commons .logging .Log ;
24- import org .apache .commons .logging .LogFactory ;
2520
2621import org .springframework .r2dbc .core .binding .BindMarkersFactory ;
22+ import org .springframework .util .ConcurrentLruCache ;
2723
2824
2925/**
4036 * <p><b>NOTE: An instance of this class is thread-safe once configured.</b>
4137 *
4238 * @author Mark Paluch
39+ * @author Juergen Hoeller
4340 */
4441class NamedParameterExpander {
4542
@@ -48,68 +45,19 @@ class NamedParameterExpander {
4845 */
4946 public static final int DEFAULT_CACHE_LIMIT = 256 ;
5047
48+ /** Cache of original SQL String to ParsedSql representation. */
49+ private final ConcurrentLruCache <String , ParsedSql > parsedSqlCache =
50+ new ConcurrentLruCache <>(DEFAULT_CACHE_LIMIT , NamedParameterUtils ::parseSqlStatement );
5151
52- private volatile int cacheLimit = DEFAULT_CACHE_LIMIT ;
53-
54- private final Log logger = LogFactory .getLog (getClass ());
55-
56- /**
57- * Cache of original SQL String to ParsedSql representation.
58- */
59- @ SuppressWarnings ("serial" )
60- private final Map <String , ParsedSql > parsedSqlCache = new LinkedHashMap <String , ParsedSql >(
61- DEFAULT_CACHE_LIMIT , 0.75f , true ) {
62- @ Override
63- protected boolean removeEldestEntry (Map .Entry <String , ParsedSql > eldest ) {
64- return size () > getCacheLimit ();
65- }
66- };
67-
68-
69- /**
70- * Create a new enabled instance of {@link NamedParameterExpander}.
71- */
72- public NamedParameterExpander () {}
73-
74-
75- /**
76- * Specify the maximum number of entries for the SQL cache. Default is 256.
77- */
78- public void setCacheLimit (int cacheLimit ) {
79- this .cacheLimit = cacheLimit ;
80- }
81-
82- /**
83- * Return the maximum number of entries for the SQL cache.
84- */
85- public int getCacheLimit () {
86- return this .cacheLimit ;
87- }
8852
8953 /**
9054 * Obtain a parsed representation of the given SQL statement.
91- * <p>
92- * The default implementation uses an LRU cache with an upper limit of 256 entries.
93- *
55+ * <p>The default implementation uses an LRU cache with an upper limit of 256 entries.
9456 * @param sql the original SQL statement
9557 * @return a representation of the parsed SQL statement
9658 */
9759 private ParsedSql getParsedSql (String sql ) {
98-
99- if (getCacheLimit () <= 0 ) {
100- return NamedParameterUtils .parseSqlStatement (sql );
101- }
102-
103- synchronized (this .parsedSqlCache ) {
104-
105- ParsedSql parsedSql = this .parsedSqlCache .get (sql );
106- if (parsedSql == null ) {
107-
108- parsedSql = NamedParameterUtils .parseSqlStatement (sql );
109- this .parsedSqlCache .put (sql , parsedSql );
110- }
111- return parsedSql ;
112- }
60+ return this .parsedSqlCache .get (sql );
11361 }
11462
11563 /**
@@ -119,11 +67,9 @@ private ParsedSql getParsedSql(String sql) {
11967 * lists may contain an array of objects, and in that case the placeholders
12068 * will be grouped and enclosed with parentheses. This allows for the use of
12169 * "expression lists" in the SQL statement like:
122- *
12370 * <pre class="code">
12471 * select id, name, state from table where (name, age) in (('John', 35), ('Ann', 50))
12572 * </pre>
126- *
12773 * <p>The parameter values passed in are used to determine the number of
12874 * placeholders to be used for a select list. Select lists should be limited
12975 * to 100 or fewer elements. A larger number of elements is not guaranteed to be
@@ -134,26 +80,17 @@ private ParsedSql getParsedSql(String sql) {
13480 * @return the expanded sql that accepts bind parameters and allows for execution
13581 * without further translation wrapped as {@link PreparedOperation}.
13682 */
137- public PreparedOperation <String > expand (String sql , BindMarkersFactory bindMarkersFactory ,
138- BindParameterSource paramSource ) {
83+ public PreparedOperation <String > expand (
84+ String sql , BindMarkersFactory bindMarkersFactory , BindParameterSource paramSource ) {
13985
14086 ParsedSql parsedSql = getParsedSql (sql );
141-
142- PreparedOperation <String > expanded = NamedParameterUtils .substituteNamedParameters (parsedSql , bindMarkersFactory ,
143- paramSource );
144-
145- if (logger .isDebugEnabled ()) {
146- logger .debug (String .format ("Expanding SQL statement [%s] to [%s]" , sql , expanded .toQuery ()));
147- }
148-
149- return expanded ;
87+ return NamedParameterUtils .substituteNamedParameters (parsedSql , bindMarkersFactory , paramSource );
15088 }
15189
15290 /**
153- * Parse the SQL statement and locate any placeholders or named parameters. Named parameters are returned as result of
154- * this method invocation.
155- *
156- * @return the parameter names.
91+ * Parse the SQL statement and locate any placeholders or named parameters.
92+ * Named parameters are returned as result of this method invocation.
93+ * @return the parameter names
15794 */
15895 public List <String > getParameterNames (String sql ) {
15996 return getParsedSql (sql ).getParameterNames ();
0 commit comments