16
16
17
17
package org .springframework .r2dbc .core ;
18
18
19
- import java .util .LinkedHashMap ;
20
19
import java .util .List ;
21
- import java .util .Map ;
22
-
23
- import org .apache .commons .logging .Log ;
24
- import org .apache .commons .logging .LogFactory ;
25
20
26
21
import org .springframework .r2dbc .core .binding .BindMarkersFactory ;
22
+ import org .springframework .util .ConcurrentLruCache ;
27
23
28
24
29
25
/**
40
36
* <p><b>NOTE: An instance of this class is thread-safe once configured.</b>
41
37
*
42
38
* @author Mark Paluch
39
+ * @author Juergen Hoeller
43
40
*/
44
41
class NamedParameterExpander {
45
42
@@ -48,68 +45,19 @@ class NamedParameterExpander {
48
45
*/
49
46
public static final int DEFAULT_CACHE_LIMIT = 256 ;
50
47
48
+ /** Cache of original SQL String to ParsedSql representation. */
49
+ private final ConcurrentLruCache <String , ParsedSql > parsedSqlCache =
50
+ new ConcurrentLruCache <>(DEFAULT_CACHE_LIMIT , NamedParameterUtils ::parseSqlStatement );
51
51
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
- }
88
52
89
53
/**
90
54
* 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.
94
56
* @param sql the original SQL statement
95
57
* @return a representation of the parsed SQL statement
96
58
*/
97
59
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 );
113
61
}
114
62
115
63
/**
@@ -119,11 +67,9 @@ private ParsedSql getParsedSql(String sql) {
119
67
* lists may contain an array of objects, and in that case the placeholders
120
68
* will be grouped and enclosed with parentheses. This allows for the use of
121
69
* "expression lists" in the SQL statement like:
122
- *
123
70
* <pre class="code">
124
71
* select id, name, state from table where (name, age) in (('John', 35), ('Ann', 50))
125
72
* </pre>
126
- *
127
73
* <p>The parameter values passed in are used to determine the number of
128
74
* placeholders to be used for a select list. Select lists should be limited
129
75
* to 100 or fewer elements. A larger number of elements is not guaranteed to be
@@ -134,26 +80,17 @@ private ParsedSql getParsedSql(String sql) {
134
80
* @return the expanded sql that accepts bind parameters and allows for execution
135
81
* without further translation wrapped as {@link PreparedOperation}.
136
82
*/
137
- public PreparedOperation <String > expand (String sql , BindMarkersFactory bindMarkersFactory ,
138
- BindParameterSource paramSource ) {
83
+ public PreparedOperation <String > expand (
84
+ String sql , BindMarkersFactory bindMarkersFactory , BindParameterSource paramSource ) {
139
85
140
86
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 );
150
88
}
151
89
152
90
/**
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
157
94
*/
158
95
public List <String > getParameterNames (String sql ) {
159
96
return getParsedSql (sql ).getParameterNames ();
0 commit comments