forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAbstractQueryImpl.cs
1065 lines (921 loc) · 26.4 KB
/
AbstractQueryImpl.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections;
using System.Collections.Generic;
using NHibernate.Engine;
using NHibernate.Engine.Query;
using NHibernate.Hql;
using NHibernate.Multi;
using NHibernate.Proxy;
using NHibernate.Transform;
using NHibernate.Type;
using NHibernate.Util;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
namespace NHibernate.Impl
{
/// <summary>
/// Abstract implementation of the IQuery interface.
/// </summary>
public abstract partial class AbstractQueryImpl : IQuery
{
private readonly string queryString;
protected readonly ISessionImplementor session;
protected internal ParameterMetadata parameterMetadata;
private readonly RowSelection selection;
private readonly List<object> values = new List<object>(4);
private readonly List<IType> types = new List<IType>(4);
private readonly Dictionary<string, TypedValue> namedParameters = new Dictionary<string, TypedValue>(4);
protected readonly Dictionary<string, TypedValue> namedParameterLists = new Dictionary<string, TypedValue>(4);
private bool cacheable;
private string cacheRegion;
private bool? readOnly;
private static readonly object UNSET_PARAMETER = new object();
private static readonly IType UNSET_TYPE = null;
private object optionalId;
private object optionalObject;
private string optionalEntityName;
private FlushMode flushMode = FlushMode.Unspecified;
private FlushMode sessionFlushMode = FlushMode.Unspecified;
private object collectionKey;
private IResultTransformer resultTransformer;
private bool shouldIgnoredUnknownNamedParameters;
private CacheMode? cacheMode;
private CacheMode? sessionCacheMode;
private string comment;
protected AbstractQueryImpl(string queryString, FlushMode flushMode, ISessionImplementor session,
ParameterMetadata parameterMetadata)
{
this.session = session;
this.queryString = queryString;
selection = new RowSelection();
this.flushMode = flushMode;
cacheMode = null;
this.parameterMetadata = parameterMetadata;
}
public bool Cacheable
{
get { return cacheable; }
}
public string CacheRegion
{
get { return cacheRegion; }
}
public bool HasNamedParameters
{
get { return parameterMetadata.NamedParameterNames.Count > 0; }
}
protected internal virtual void VerifyParameters()
{
VerifyParameters(false);
}
/// <summary>
/// Perform parameters validation. Flatten them if needed. Used prior to executing the encapsulated query.
/// </summary>
/// <param name="reserveFirstParameter">
/// If true, the first positional parameter will not be verified since
/// its needed for e.g. callable statements returning an out parameter.
/// </param>
protected internal virtual void VerifyParameters(bool reserveFirstParameter)
{
if (parameterMetadata.NamedParameterNames.Count != namedParameters.Count + namedParameterLists.Count)
{
var missingParams = new HashSet<string>(parameterMetadata.NamedParameterNames);
missingParams.ExceptWith(namedParameterLists.Keys);
missingParams.ExceptWith(namedParameters.Keys);
throw new QueryException("Not all named parameters have been set: " + CollectionPrinter.ToString(missingParams), QueryString);
}
var positionalValueSpan = 0;
// Values and Types may be overriden to yield refined parameters, check them
// instead of the fields.
var values = Values;
var types = Types;
for (var i = 0; i < values.Count; i++)
{
var type = types[i];
if (values[i] == UNSET_PARAMETER || type == UNSET_TYPE)
{
if (reserveFirstParameter && i == 0)
{
continue;
}
else
{
throw new QueryException("Unset positional parameter at position: " + i, QueryString);
}
}
positionalValueSpan++;
}
if (parameterMetadata.OrdinalParameterCount != positionalValueSpan)
{
if (reserveFirstParameter && parameterMetadata.OrdinalParameterCount - 1 != positionalValueSpan)
{
throw new QueryException(
"Expected positional parameter count: " + (parameterMetadata.OrdinalParameterCount - 1) + ", actual parameters: "
+ CollectionPrinter.ToString(values), QueryString);
}
else if (!reserveFirstParameter)
{
throw new QueryException(
"Expected positional parameter count: " + parameterMetadata.OrdinalParameterCount + ", actual parameters: "
+ CollectionPrinter.ToString(values), QueryString);
}
}
}
protected internal virtual IType DetermineType(int paramPosition, object paramValue, IType defaultType)
{
IType type = parameterMetadata.GetOrdinalParameterExpectedType(paramPosition + 1) ?? defaultType;
return type;
}
protected internal virtual IType DetermineType(int paramPosition, object paramValue)
{
IType type = parameterMetadata.GetOrdinalParameterExpectedType(paramPosition + 1) ??
ParameterHelper.GuessType(paramValue, session.Factory);
return type;
}
protected internal virtual IType DetermineType(string paramName, object paramValue, IType defaultType)
{
IType type = parameterMetadata.GetNamedParameterExpectedType(paramName) ?? defaultType;
return type;
}
protected internal virtual IType DetermineType(string paramName, object paramValue)
{
IType type = parameterMetadata.GetNamedParameterExpectedType(paramName) ??
ParameterHelper.GuessType(paramValue, session.Factory);
return type;
}
protected internal virtual IType DetermineType(string paramName, System.Type clazz)
{
IType type = parameterMetadata.GetNamedParameterExpectedType(paramName) ??
ParameterHelper.GuessType(clazz, session.Factory);
return type;
}
/// <summary>
/// Warning: adds new parameters to the argument by side-effect, as well as mutating the query string!
/// </summary>
protected internal virtual string ExpandParameterLists(IDictionary<string, TypedValue> namedParamsCopy)
{
string query = queryString;
foreach (var me in namedParameterLists)
query = ExpandParameterList(query, me.Key, me.Value, namedParamsCopy);
return query;
}
/// <summary>
/// Warning: adds new parameters to the argument by side-effect, as well as mutating the query string!
/// </summary>
private string ExpandParameterList(string query, string name, TypedValue typedList, IDictionary<string, TypedValue> namedParamsCopy)
{
var vals = (IEnumerable) typedList.Value;
var type = typedList.Type;
var typedValues = (from object value in vals
select new TypedValue(type, value, false))
.ToList();
if (typedValues.Count == 1)
{
namedParamsCopy[name] = typedValues[0];
return query;
}
var isJpaPositionalParam = parameterMetadata.GetNamedParameterDescriptor(name).JpaStyle;
var aliases = new string[typedValues.Count];
for (var index = 0; index < typedValues.Count; index++)
{
var value = typedValues[index];
var alias = (isJpaPositionalParam ? 'x' + name : name + StringHelper.Underscore) + index + StringHelper.Underscore;
namedParamsCopy[alias] = value;
aliases[index] = ParserHelper.HqlVariablePrefix + alias;
}
var paramPrefix = isJpaPositionalParam ? StringHelper.SqlParameter : ParserHelper.HqlVariablePrefix;
return Regex.Replace(
query,
Regex.Escape(paramPrefix + name) + @"\b",
string.Join(StringHelper.CommaSpace, aliases));
}
#region Parameters
public IQuery SetParameter(int position, object val, IType type)
{
CheckPositionalParameter(position);
int size = values.Count;
if (position < size)
{
values[position] = val;
types[position] = type;
}
else
{
// prepend value and type list with null for any positions before the wanted position.
for (int i = 0; i < position - size; i++)
{
values.Add(UNSET_PARAMETER);
types.Add(UNSET_TYPE);
}
values.Add(val);
types.Add(type);
}
return this;
}
public IQuery SetParameter(string name, object val, IType type)
{
return SetParameter(name, val, type, false);
}
//TODO 6.0: Add to IQuery interface
public IQuery SetParameter(string name, object val, IType type, bool preferMetadataType)
{
if (CheckParameterIgnored(name))
return this;
if (type == null || preferMetadataType)
{
type = parameterMetadata.GetNamedParameterExpectedType(name) ?? type ?? ParameterHelper.GuessType(val, session.Factory);
}
namedParameters[name] = new TypedValue(type, val, false);
return this;
}
private bool CheckParameterIgnored(string name)
{
if (parameterMetadata.NamedParameterNames.Contains(name))
return false;
if (shouldIgnoredUnknownNamedParameters) //just ignore it
return true;
throw new ArgumentException("Parameter " + name + " does not exist as a named parameter in [" + QueryString + "]");
}
public IQuery SetParameter<T>(int position, T val)
{
CheckPositionalParameter(position);
return SetParameter(
position,
val,
parameterMetadata.GetOrdinalParameterExpectedType(position + 1) ??
ParameterHelper.GuessType(typeof(T), session.Factory));
}
private void CheckPositionalParameter(int position)
{
if (parameterMetadata.OrdinalParameterCount == 0)
{
throw new ArgumentException("No positional parameters in query: " + QueryString);
}
if (position < 0 || position > parameterMetadata.OrdinalParameterCount - 1)
{
throw new ArgumentException("Positional parameter does not exist: " + position + " in query: " + QueryString);
}
}
public IQuery SetParameter<T>(string name, T val)
{
return SetParameter(
name,
val,
parameterMetadata.GetNamedParameterExpectedType(name) ??
ParameterHelper.GuessType(typeof(T), session.Factory));
}
public IQuery SetParameter(string name, object val)
{
return SetParameter(name, val, null, true);
}
public IQuery SetParameter(int position, object val)
{
if (val == null)
{
throw new ArgumentNullException("val",
"A type specific Set(position, val) should be called because the Type can not be guessed from a null value.");
}
else
{
SetParameter(position, val, DetermineType(position, val));
}
return this;
}
public IQuery SetAnsiString(int position, string val)
{
SetParameter(position, val, NHibernateUtil.AnsiString);
return this;
}
public IQuery SetString(int position, string val)
{
SetParameter(position, val, NHibernateUtil.String);
return this;
}
public IQuery SetCharacter(int position, char val)
{
SetParameter(position, val, NHibernateUtil.Character); // );
return this;
}
public IQuery SetBoolean(int position, bool val)
{
SetParameter(position, val, NHibernateUtil.Boolean); // );
return this;
}
public IQuery SetByte(int position, byte val)
{
SetParameter(position, val, NHibernateUtil.Byte);
return this;
}
public IQuery SetInt16(int position, short val)
{
SetParameter(position, val, NHibernateUtil.Int16);
return this;
}
public IQuery SetInt32(int position, int val)
{
SetParameter(position, val, NHibernateUtil.Int32);
return this;
}
public IQuery SetInt64(int position, long val)
{
SetParameter(position, val, NHibernateUtil.Int64);
return this;
}
public IQuery SetSingle(int position, float val)
{
SetParameter(position, val, NHibernateUtil.Single);
return this;
}
public IQuery SetDouble(int position, double val)
{
SetParameter(position, val, NHibernateUtil.Double);
return this;
}
public IQuery SetBinary(int position, byte[] val)
{
SetParameter(position, val, NHibernateUtil.Binary);
return this;
}
public IQuery SetDateTimeOffset(string name, DateTimeOffset val)
{
SetParameter(name, val, NHibernateUtil.DateTimeOffset);
return this;
}
public IQuery SetDecimal(int position, decimal val)
{
SetParameter(position, val, NHibernateUtil.Decimal);
return this;
}
public IQuery SetDateTime(int position, DateTime val)
{
SetParameter(position, val, NHibernateUtil.DateTime);
return this;
}
public IQuery SetDateTimeNoMs(int position, DateTime val)
{
SetParameter(position, val, NHibernateUtil.DateTimeNoMs);
return this;
}
// Since v5.0
[Obsolete("Use SetDateTime instead, it uses DateTime2 with dialects supporting it.")]
public IQuery SetDateTime2(int position, DateTime val)
{
SetParameter(position, val, NHibernateUtil.DateTime2);
return this;
}
public IQuery SetTime(int position, DateTime val)
{
SetParameter(position, val, NHibernateUtil.Time);
return this;
}
// Since v5.0
[Obsolete("Use SetDateTime instead.")]
public IQuery SetTimestamp(int position, DateTime val)
{
SetParameter(position, val, NHibernateUtil.Timestamp);
return this;
}
public IQuery SetEntity(int position, object val)
{
SetParameter(position, val, NHibernateUtil.Entity(NHibernateProxyHelper.GuessClass(val)));
return this;
}
public IQuery SetEnum(int position, Enum val)
{
SetParameter(position, val, NHibernateUtil.Enum(val.GetType()));
return this;
}
public IQuery SetAnsiString(string name, string val)
{
SetParameter(name, val, NHibernateUtil.AnsiString);
return this;
}
public IQuery SetString(string name, string val)
{
SetParameter(name, val, NHibernateUtil.String);
return this;
}
public IQuery SetCharacter(string name, char val)
{
SetParameter(name, val, NHibernateUtil.Character);
return this;
}
public IQuery SetBoolean(string name, bool val)
{
SetParameter(name, val, NHibernateUtil.Boolean);
return this;
}
public IQuery SetByte(string name, byte val)
{
SetParameter(name, val, NHibernateUtil.Byte);
return this;
}
public IQuery SetInt16(string name, short val)
{
SetParameter(name, val, NHibernateUtil.Int16);
return this;
}
public IQuery SetInt32(string name, int val)
{
SetParameter(name, val, NHibernateUtil.Int32);
return this;
}
public IQuery SetInt64(string name, long val)
{
SetParameter(name, val, NHibernateUtil.Int64);
return this;
}
public IQuery SetSingle(string name, float val)
{
SetParameter(name, val, NHibernateUtil.Single);
return this;
}
public IQuery SetDouble(string name, double val)
{
SetParameter(name, val, NHibernateUtil.Double);
return this;
}
public IQuery SetBinary(string name, byte[] val)
{
SetParameter(name, val, NHibernateUtil.Binary);
return this;
}
public IQuery SetDecimal(string name, decimal val)
{
SetParameter(name, val, NHibernateUtil.Decimal);
return this;
}
public IQuery SetDateTime(string name, DateTime val)
{
SetParameter(name, val, NHibernateUtil.DateTime);
return this;
}
public IQuery SetDateTimeNoMs(string name, DateTime val)
{
SetParameter(name, val, NHibernateUtil.DateTimeNoMs);
return this;
}
// Since v5.0
[Obsolete("Use SetDateTime instead, it uses DateTime2 with dialects supporting it.")]
public IQuery SetDateTime2(string name, DateTime val)
{
SetParameter(name, val, NHibernateUtil.DateTime2);
return this;
}
public IQuery SetTimeSpan(int position, TimeSpan val)
{
SetParameter(position, val, NHibernateUtil.TimeSpan);
return this;
}
public IQuery SetTimeSpan(string name, TimeSpan val)
{
SetParameter(name, val, NHibernateUtil.TimeSpan);
return this;
}
public IQuery SetTimeAsTimeSpan(int position, TimeSpan val)
{
SetParameter(position, val, NHibernateUtil.TimeAsTimeSpan);
return this;
}
public IQuery SetTimeAsTimeSpan(string name, TimeSpan val)
{
SetParameter(name, val, NHibernateUtil.TimeAsTimeSpan);
return this;
}
public IQuery SetDateTimeOffset(int position, DateTimeOffset val)
{
SetParameter(position, val, NHibernateUtil.DateTimeOffset);
return this;
}
public IQuery SetTime(string name, DateTime val)
{
SetParameter(name, val, NHibernateUtil.Time);
return this;
}
// Since v5.0
[Obsolete("Use SetDateTime instead.")]
public IQuery SetTimestamp(string name, DateTime val)
{
SetParameter(name, val, NHibernateUtil.Timestamp);
return this;
}
public IQuery SetGuid(string name, Guid val)
{
SetParameter(name, val, NHibernateUtil.Guid);
return this;
}
public IQuery SetGuid(int position, Guid val)
{
SetParameter(position, val, NHibernateUtil.Guid);
return this;
}
public IQuery SetEntity(string name, object val)
{
SetParameter(name, val, NHibernateUtil.Entity(NHibernateProxyHelper.GuessClass(val)));
return this;
}
public IQuery SetEnum(string name, Enum val)
{
SetParameter(name, val, NHibernateUtil.Enum(val.GetType()));
return this;
}
// Since 5.3
[Obsolete("This method was never surfaced to a query interface. Use the overload taking an object instead, and supply to it a generic IDictionary<string, object>.")]
public IQuery SetProperties(IDictionary map)
{
string[] @params = NamedParameters;
for (int i = 0; i < @params.Length; i++)
{
var namedParam = @params[i];
var obj = map[namedParam];
if (obj == null)
{
continue;
}
if (obj is IEnumerable && !(obj is string))
{
SetParameterList(namedParam, (IEnumerable) obj);
}
else
{
SetParameter(namedParam, obj, DetermineType(namedParam, obj.GetType()));
}
}
return this;
}
private IQuery SetParameters(IDictionary<string, object> map)
{
foreach (var namedParam in NamedParameters)
{
if (map.TryGetValue(namedParam, out var obj))
{
switch (obj)
{
case IEnumerable enumerable when !(enumerable is string):
SetParameterList(namedParam, enumerable);
break;
default:
SetParameter(namedParam, obj);
break;
}
}
}
return this;
}
private IQuery SetParameters(IDictionary map)
{
foreach (var namedParam in NamedParameters)
{
var obj = map[namedParam];
switch (obj)
{
case IEnumerable enumerable when !(enumerable is string):
SetParameterList(namedParam, enumerable);
break;
case null when map.Contains(namedParam):
default:
SetParameter(namedParam, obj);
break;
}
}
return this;
}
public IQuery SetProperties(object bean)
{
if (bean is IDictionary<string, object> map)
{
return SetParameters(map);
}
if (bean is IDictionary hashtable)
{
return SetParameters(hashtable);
}
System.Type clazz = bean.GetType();
string[] @params = NamedParameters;
for (int i = 0; i < @params.Length; i++)
{
string namedParam = @params[i];
try
{
var getter = ReflectHelper.GetGetter(clazz, namedParam, "property");
var retType = getter.ReturnType;
var obj = getter.Get(bean);
if (typeof(IEnumerable).IsAssignableFrom(retType) && retType != typeof(string))
{
SetParameterList(namedParam, (IEnumerable) obj);
}
else
{
SetParameter(namedParam, obj, DetermineType(namedParam, retType));
}
}
catch (PropertyNotFoundException)
{
// ignore
}
}
return this;
}
public IQuery SetParameterList(string name, IEnumerable vals, IType type)
{
if (!parameterMetadata.NamedParameterNames.Contains(name))
{
if (shouldIgnoredUnknownNamedParameters)//just ignore it
return this;
throw new ArgumentException("Parameter " + name + " does not exist as a named parameter in [" + QueryString + "]");
}
if (type == null)
{
throw new ArgumentNullException("type","Can't determine the type of parameter-list elements.");
}
if(!vals.Cast<object>().Any())
{
throw new QueryException(string.Format("An empty parameter-list generates wrong SQL; parameter name '{0}'", name));
}
namedParameterLists[name] = new TypedValue(type, vals, true);
return this;
}
public IQuery SetParameterList(string name, IEnumerable vals)
{
if (vals == null)
{
throw new ArgumentNullException("vals");
}
if (!parameterMetadata.NamedParameterNames.Contains(name))
{
if (shouldIgnoredUnknownNamedParameters)//just ignore it
return this;
}
object firstValue = vals.Cast<object>().FirstOrDefault();
SetParameterList(
name,
vals,
firstValue == null
? ParameterHelper.GuessType(vals.GetCollectionElementType(), session.Factory)
: DetermineType(name, firstValue));
return this;
}
#endregion
#region Query properties
public string QueryString
{
get { return queryString; }
}
protected internal IDictionary<string, TypedValue> NamedParams
{
// NB The java one always returns a copy, so I'm going to reproduce that behaviour
get { return new Dictionary<string, TypedValue>(namedParameters); }
}
protected IDictionary NamedParameterLists
{
get { return namedParameterLists; }
}
// TODO 6.0: Change type to IList<object>
protected virtual IList Values
{
get { return values; }
}
protected virtual IList<IType> Types
{
get { return types; }
}
public virtual IType[] ReturnTypes
{
get { return session.Factory.GetReturnTypes(queryString); }
}
public virtual string[] ReturnAliases
{
get { return session.Factory.GetReturnAliases(queryString); }
}
// TODO: maybe call it RowSelection ?
public RowSelection Selection
{
get { return selection; }
}
public IQuery SetMaxResults(int maxResults)
{
selection.MaxRows = maxResults;
return this;
}
public IQuery SetTimeout(int timeout)
{
selection.Timeout = timeout;
return this;
}
public IQuery SetFetchSize(int fetchSize)
{
selection.FetchSize = fetchSize;
return this;
}
public IQuery SetFirstResult(int firstResult)
{
selection.FirstRow = firstResult;
return this;
}
public string[] NamedParameters
{
get
{
return parameterMetadata.NamedParameterNames.ToArray();
}
}
public abstract IQuery SetLockMode(string alias, LockMode lockMode);
public IQuery SetComment(string comment)
{
this.comment = comment;
return this;
}
internal protected ISessionImplementor Session
{
get { return session; }
}
protected RowSelection RowSelection
{
get { return selection; }
}
public IQuery SetCacheable(bool cacheable)
{
this.cacheable = cacheable;
return this;
}
public IQuery SetCacheRegion(string cacheRegion)
{
if (cacheRegion != null)
this.cacheRegion = cacheRegion.Trim();
return this;
}
/// <inheritdoc />
public bool IsReadOnly
{
get { return readOnly == null ? Session.PersistenceContext.DefaultReadOnly : readOnly.Value; }
}
/// <inheritdoc />
public IQuery SetReadOnly(bool readOnly)
{
this.readOnly = readOnly;
return this;
}
public void SetOptionalId(object optionalId)
{
this.optionalId = optionalId;
}
public void SetOptionalObject(object optionalObject)
{
this.optionalObject = optionalObject;
}
public void SetOptionalEntityName(string optionalEntityName)
{
this.optionalEntityName = optionalEntityName;
}
public IQuery SetFlushMode(FlushMode flushMode)
{
this.flushMode = flushMode;
return this;
}
public IQuery SetCollectionKey(object collectionKey)
{
this.collectionKey = collectionKey;
return this;
}
public IQuery SetResultTransformer(IResultTransformer transformer)
{
resultTransformer = transformer;
return this;
}
public IFutureEnumerable<T> Future<T>()
{
return session.GetFutureBatch().AddAsFuture<T>(this);
}
public IFutureValue<T> FutureValue<T>()
{
return session.GetFutureBatch().AddAsFutureValue<T>(this);
}
/// <summary> Override the current session cache mode, just for this query.
/// </summary>
/// <param name="cacheMode">The cache mode to use. </param>
/// <returns> this (for method chaining) </returns>
public IQuery SetCacheMode(CacheMode cacheMode)
{
this.cacheMode = cacheMode;
return this;
}
public IQuery SetIgnoreUknownNamedParameters(bool ignoredUnknownNamedParameters)
{
shouldIgnoredUnknownNamedParameters = ignoredUnknownNamedParameters;
return this;
}
protected internal abstract IDictionary<string, LockMode> LockModes { get; }
#endregion
#region Execution methods
public abstract int ExecuteUpdate();
public abstract IEnumerable Enumerable();
public abstract IEnumerable<T> Enumerable<T>();
public abstract IList List();
public abstract void List(IList results);
public abstract IList<T> List<T>();
public T UniqueResult<T>()
{
object result = UniqueResult();
if (result == null && typeof(T).IsValueType)
{
return default(T);
}
else
{
return (T)result;
}
}
public object UniqueResult()
{
return UniqueElement(List());
}
internal static object UniqueElement(IList list)
{
int size = list.Count;
if (size == 0)
{
return null;
}
object first = list[0];
for (int i = 1; i < size; i++)
{
if (list[i] != first)
{
throw new NonUniqueResultException(size);
}
}
return first;
}
public virtual IType[] TypeArray()
{
return types.ToArray();
}
public virtual object[] ValueArray()
{
return values.ToArray();
}
public virtual QueryParameters GetQueryParameters()
{
return GetQueryParameters(NamedParams);
}