@@ -138,6 +138,70 @@ enumerable is IEnumerable<KeyValuePair<string, object>> ||
138
138
) ? enumerable : null ;
139
139
}
140
140
141
+ internal static IDbCommand SetParameters ( this IDbCommand dbCmd , Dictionary < string , object > dict , bool excludeDefaults , ref string sql )
142
+ {
143
+ if ( dict == null )
144
+ return dbCmd ;
145
+
146
+ dbCmd . Parameters . Clear ( ) ;
147
+ lastQueryType = null ;
148
+ var dialectProvider = dbCmd . GetDialectProvider ( ) ;
149
+
150
+ var paramIndex = 0 ;
151
+ var sqlCopy = sql ; //C# doesn't allow changing ref params in lambda's
152
+
153
+ foreach ( var kvp in dict )
154
+ {
155
+ var value = kvp . Value ;
156
+ var propName = kvp . Key ;
157
+ if ( excludeDefaults && value == null ) continue ;
158
+
159
+ var inValues = sql != null ? GetMultiValues ( value ) : null ;
160
+ if ( inValues != null )
161
+ {
162
+ var propType = value ? . GetType ( ) ?? typeof ( object ) ;
163
+ var sb = StringBuilderCache . Allocate ( ) ;
164
+ foreach ( var item in inValues )
165
+ {
166
+ var p = dbCmd . CreateParameter ( ) ;
167
+ p . ParameterName = "v" + paramIndex ++ ;
168
+
169
+ if ( sb . Length > 0 )
170
+ sb . Append ( ',' ) ;
171
+ sb . Append ( dialectProvider . ParamString + p . ParameterName ) ;
172
+
173
+ p . Direction = ParameterDirection . Input ;
174
+ dialectProvider . InitDbParam ( p , item . GetType ( ) ) ;
175
+
176
+ dialectProvider . SetParamValue ( p , item , propType ) ;
177
+
178
+ dbCmd . Parameters . Add ( p ) ;
179
+ }
180
+
181
+ var sqlIn = StringBuilderCache . ReturnAndFree ( sb ) ;
182
+ if ( string . IsNullOrEmpty ( sqlIn ) )
183
+ sqlIn = "NULL" ;
184
+ sqlCopy = sqlCopy ? . Replace ( dialectProvider . ParamString + propName , sqlIn ) ;
185
+ if ( dialectProvider . ParamString != "@" )
186
+ sqlCopy = sqlCopy ? . Replace ( "@" + propName , sqlIn ) ;
187
+ }
188
+ else
189
+ {
190
+ var p = dbCmd . CreateParameter ( ) ;
191
+ p . ParameterName = propName ;
192
+
193
+ p . Direction = ParameterDirection . Input ;
194
+ p . Value = value ?? DBNull . Value ;
195
+ if ( value != null )
196
+ dialectProvider . InitDbParam ( p , value . GetType ( ) ) ;
197
+
198
+ dbCmd . Parameters . Add ( p ) ;
199
+ }
200
+ }
201
+
202
+ return dbCmd ;
203
+ }
204
+
141
205
internal static IDbCommand SetParameters ( this IDbCommand dbCmd , Type type , object anonType , bool excludeDefaults , ref string sql )
142
206
{
143
207
if ( anonType == null )
@@ -154,18 +218,18 @@ internal static IDbCommand SetParameters(this IDbCommand dbCmd, Type type, objec
154
218
155
219
var sqlCopy = sql ; //C# doesn't allow changing ref params in lambda's
156
220
221
+ var paramIndex = 0 ;
157
222
anonType . ToObjectDictionary ( ) . ForEachParam ( modelDef , excludeDefaults , ( propName , columnName , value ) =>
158
223
{
159
224
var propType = value ? . GetType ( ) ?? typeof ( object ) ;
160
225
var inValues = GetMultiValues ( value ) ;
161
226
if ( inValues != null )
162
227
{
163
- var i = 0 ;
164
228
var sb = StringBuilderCache . Allocate ( ) ;
165
229
foreach ( var item in inValues )
166
230
{
167
231
var p = dbCmd . CreateParameter ( ) ;
168
- p . ParameterName = "v" + i ++ ;
232
+ p . ParameterName = "v" + paramIndex ++ ;
169
233
170
234
if ( sb . Length > 0 )
171
235
sb . Append ( ',' ) ;
@@ -284,33 +348,6 @@ internal static Dictionary<string, object> NonDefaultsOnly(this Dictionary<strin
284
348
return map ;
285
349
}
286
350
287
- internal static IDbCommand SetParameters ( this IDbCommand dbCmd , Dictionary < string , object > dict , bool excludeDefaults )
288
- {
289
- if ( dict == null )
290
- return dbCmd ;
291
-
292
- dbCmd . Parameters . Clear ( ) ;
293
- lastQueryType = null ;
294
- var dialectProvider = dbCmd . GetDialectProvider ( ) ;
295
-
296
- foreach ( var kvp in dict )
297
- {
298
- var value = kvp . Value ;
299
- if ( excludeDefaults && value == null ) continue ;
300
- var p = dbCmd . CreateParameter ( ) ;
301
- p . ParameterName = kvp . Key ;
302
-
303
- p . Direction = ParameterDirection . Input ;
304
- p . Value = value ?? DBNull . Value ;
305
- if ( value != null )
306
- dialectProvider . InitDbParam ( p , value . GetType ( ) ) ;
307
-
308
- dbCmd . Parameters . Add ( p ) ;
309
- }
310
-
311
- return dbCmd ;
312
- }
313
-
314
351
public static IDbCommand SetFilters < T > ( this IDbCommand dbCmd , object anonType )
315
352
{
316
353
return dbCmd . SetFilters < T > ( anonType , excludeDefaults : false ) ;
@@ -443,7 +480,7 @@ internal static List<T> Select<T>(this IDbCommand dbCmd, string sql, object anon
443
480
444
481
internal static List < T > Select < T > ( this IDbCommand dbCmd , string sql , Dictionary < string , object > dict )
445
482
{
446
- if ( dict != null ) SetParameters ( dbCmd , dict , ( bool ) false ) ;
483
+ if ( dict != null ) SetParameters ( dbCmd , dict , ( bool ) false , sql : ref sql ) ;
447
484
dbCmd . CommandText = dbCmd . GetDialectProvider ( ) . ToSelectStatement ( typeof ( T ) , sql ) ;
448
485
449
486
return dbCmd . ConvertToList < T > ( ) ;
@@ -495,7 +532,7 @@ internal static List<T> SqlList<T>(this IDbCommand dbCmd, string sql, object ano
495
532
496
533
internal static List < T > SqlList < T > ( this IDbCommand dbCmd , string sql , Dictionary < string , object > dict )
497
534
{
498
- if ( dict != null ) SetParameters ( dbCmd , dict , false ) ;
535
+ if ( dict != null ) SetParameters ( dbCmd , dict , false , sql : ref sql ) ;
499
536
dbCmd . CommandText = sql ;
500
537
501
538
return dbCmd . ConvertToList < T > ( ) ;
@@ -523,7 +560,7 @@ internal static List<T> SqlColumn<T>(this IDbCommand dbCmd, string sql, object a
523
560
524
561
internal static List < T > SqlColumn < T > ( this IDbCommand dbCmd , string sql , Dictionary < string , object > dict )
525
562
{
526
- if ( dict != null ) SetParameters ( dbCmd , dict , false ) ;
563
+ if ( dict != null ) SetParameters ( dbCmd , dict , false , sql : ref sql ) ;
527
564
dbCmd . CommandText = sql ;
528
565
529
566
return dbCmd . ConvertToList < T > ( ) ;
@@ -543,7 +580,7 @@ internal static T SqlScalar<T>(this IDbCommand dbCmd, string sql, object anonTyp
543
580
544
581
internal static T SqlScalar < T > ( this IDbCommand dbCmd , string sql , Dictionary < string , object > dict )
545
582
{
546
- if ( dict != null ) SetParameters ( dbCmd , dict , false ) ;
583
+ if ( dict != null ) SetParameters ( dbCmd , dict , false , sql : ref sql ) ;
547
584
548
585
return dbCmd . Scalar < T > ( sql ) ;
549
586
}
@@ -766,7 +803,7 @@ internal static HashSet<T> ColumnDistinct<T>(this IDataReader reader, IOrmLiteDi
766
803
767
804
internal static Dictionary < K , List < V > > Lookup < K , V > ( this IDbCommand dbCmd , string sql , object anonType = null )
768
805
{
769
- return dbCmd . SetParameters ( anonType . ToObjectDictionary ( ) , false ) . Lookup < K , V > ( sql ) ;
806
+ return dbCmd . SetParameters ( anonType . ToObjectDictionary ( ) , false , sql : ref sql ) . Lookup < K , V > ( sql ) ;
770
807
}
771
808
772
809
internal static Dictionary < K , List < V > > Lookup < K , V > ( this IDataReader reader , IOrmLiteDialectProvider dialectProvider )
@@ -791,7 +828,7 @@ internal static Dictionary<K, List<V>> Lookup<K, V>(this IDataReader reader, IOr
791
828
792
829
internal static Dictionary < K , V > Dictionary < K , V > ( this IDbCommand dbCmd , string sql , object anonType = null )
793
830
{
794
- if ( anonType != null ) SetParameters ( dbCmd , anonType . ToObjectDictionary ( ) , excludeDefaults : false ) ;
831
+ if ( anonType != null ) SetParameters ( dbCmd , anonType . ToObjectDictionary ( ) , excludeDefaults : false , sql : ref sql ) ;
795
832
796
833
return dbCmd . Dictionary < K , V > ( sql ) ;
797
834
}
@@ -813,17 +850,18 @@ internal static Dictionary<K, V> Dictionary<K, V>(this IDataReader reader, IOrmL
813
850
814
851
internal static bool Exists < T > ( this IDbCommand dbCmd , object anonType )
815
852
{
816
- if ( anonType != null ) SetParameters ( dbCmd , anonType . ToObjectDictionary ( ) , excludeDefaults : true ) ;
853
+ string sql = null ;
854
+ if ( anonType != null ) SetParameters ( dbCmd , anonType . ToObjectDictionary ( ) , excludeDefaults : true , sql : ref sql ) ;
817
855
818
- var sql = GetFilterSql < T > ( dbCmd ) ;
856
+ sql = GetFilterSql < T > ( dbCmd ) ;
819
857
820
858
var result = dbCmd . Scalar ( sql ) ;
821
859
return result != null ;
822
860
}
823
861
824
862
internal static bool Exists < T > ( this IDbCommand dbCmd , string sql , object anonType = null )
825
863
{
826
- if ( anonType != null ) SetParameters ( dbCmd , anonType . ToObjectDictionary ( ) , ( bool ) false ) ;
864
+ if ( anonType != null ) SetParameters ( dbCmd , anonType . ToObjectDictionary ( ) , ( bool ) false , sql : ref sql ) ;
827
865
828
866
var result = dbCmd . Scalar ( dbCmd . GetDialectProvider ( ) . ToSelectStatement ( typeof ( T ) , sql ) ) ;
829
867
return result != null ;
@@ -1020,7 +1058,8 @@ internal static IDbCommand SqlProc(this IDbCommand dbCmd, string name, object in
1020
1058
dbCmd . CommandType = CommandType . StoredProcedure ;
1021
1059
dbCmd . CommandText = name ;
1022
1060
1023
- dbCmd . SetParameters ( inParams . ToObjectDictionary ( ) , excludeDefaults ) ;
1061
+ string sql = null ;
1062
+ dbCmd . SetParameters ( inParams . ToObjectDictionary ( ) , excludeDefaults , sql : ref sql ) ;
1024
1063
1025
1064
return dbCmd ;
1026
1065
}
0 commit comments