forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutoQueryFeature.cs
More file actions
740 lines (627 loc) · 29.4 KB
/
AutoQueryFeature.cs
File metadata and controls
740 lines (627 loc) · 29.4 KB
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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Threading;
using Funq;
using ServiceStack.Host;
using ServiceStack.MiniProfiler;
using ServiceStack.Reflection;
using ServiceStack.Text;
using ServiceStack.Web;
using ServiceStack.Data;
using ServiceStack.OrmLite;
namespace ServiceStack
{
public delegate ISqlExpression QueryFilterDelegate(IRequest request, ISqlExpression sqlExpression, IQuery model);
public class AutoQueryFeature : IPlugin, IPostInitPlugin
{
public HashSet<string> IgnoreProperties { get; set; }
public HashSet<string> IllegalSqlFragmentTokens { get; set; }
public HashSet<Assembly> LoadFromAssemblies { get; set; }
public int? MaxLimit { get; set; }
public string UseNamedConnection { get; set; }
public bool EnableUntypedQueries { get; set; }
public bool EnableRawSqlFilters { get; set; }
public bool OrderByPrimaryKeyOnPagedQuery { get; set; }
public Type AutoQueryServiceBaseType { get; set; }
public Dictionary<Type, QueryFilterDelegate> QueryFilters { get; set; }
public const string GreaterThanOrEqualFormat = "{Field} >= {Value}";
public const string GreaterThanFormat = "{Field} > {Value}";
public const string LessThanFormat = "{Field} < {Value}";
public const string LessThanOrEqualFormat = "{Field} <= {Value}";
public Dictionary<string, string> ImplicitConventions = new Dictionary<string, string>
{
{"%Above%", GreaterThanFormat},
{"Begin%", GreaterThanFormat},
{"%Beyond%", GreaterThanFormat},
{"%Over%", GreaterThanFormat},
{"%OlderThan", GreaterThanFormat},
{"%After%", GreaterThanFormat},
{"OnOrAfter%", GreaterThanOrEqualFormat},
{"%From%", GreaterThanOrEqualFormat},
{"Since%", GreaterThanOrEqualFormat},
{"Start%", GreaterThanOrEqualFormat},
{"%Higher%", GreaterThanOrEqualFormat},
{">%", GreaterThanOrEqualFormat},
{"%>", GreaterThanFormat},
{"%GreaterThanOrEqualTo%", GreaterThanOrEqualFormat},
{"%GreaterThan%", GreaterThanFormat},
{"%LessThan%", LessThanFormat},
{"%LessThanOrEqualTo%", LessThanOrEqualFormat},
{"Behind%", LessThanFormat},
{"%Below%", LessThanFormat},
{"%Under%", LessThanFormat},
{"%Lower%", LessThanFormat},
{"%Before%", LessThanFormat},
{"%YoungerThan", LessThanFormat},
{"OnOrBefore%", LessThanOrEqualFormat},
{"End%", LessThanOrEqualFormat},
{"Stop%", LessThanOrEqualFormat},
{"To%", LessThanOrEqualFormat},
{"Until%", LessThanOrEqualFormat},
{"%<", LessThanOrEqualFormat},
{"<%", LessThanFormat},
{"Like%", "UPPER({Field}) LIKE UPPER({Value})"},
{"%In", "{Field} IN ({Values})"},
{"%Ids", "{Field} IN ({Values})"},
{"%Between%", "{Field} BETWEEN {Value1} AND {Value2}"},
};
public Dictionary<string, QueryFieldAttribute> StartsWithConventions =
new Dictionary<string, QueryFieldAttribute>();
public Dictionary<string, QueryFieldAttribute> EndsWithConventions = new Dictionary<string, QueryFieldAttribute>
{
{ "StartsWith", new QueryFieldAttribute { Template = "UPPER({Field}) LIKE UPPER({Value})", ValueFormat = "{0}%" }},
{ "Contains", new QueryFieldAttribute { Template = "UPPER({Field}) LIKE UPPER({Value})", ValueFormat = "%{0}%" }},
{ "EndsWith", new QueryFieldAttribute { Template = "UPPER({Field}) LIKE UPPER({Value})", ValueFormat = "%{0}" }},
};
public AutoQueryFeature()
{
IgnoreProperties = new HashSet<string>(new[] { "Skip", "Take", "OrderBy", "OrderByDesc", "_select", "_from", "_join", "_where" },
StringComparer.OrdinalIgnoreCase);
IllegalSqlFragmentTokens = new HashSet<string>();
AutoQueryServiceBaseType = typeof(AutoQueryServiceBase);
QueryFilters = new Dictionary<Type, QueryFilterDelegate>();
EnableUntypedQueries = true;
OrderByPrimaryKeyOnPagedQuery = true;
LoadFromAssemblies = new HashSet<Assembly>();
}
public void Register(IAppHost appHost)
{
foreach (var entry in ImplicitConventions)
{
var key = entry.Key.Trim('%');
var fmt = entry.Value;
var query = new QueryFieldAttribute { Template = fmt }.Init();
if (entry.Key.EndsWith("%"))
StartsWithConventions[key] = query;
if (entry.Key.StartsWith("%"))
EndsWithConventions[key] = query;
}
appHost.GetContainer().Register<IAutoQuery>(c =>
new AutoQuery
{
IgnoreProperties = IgnoreProperties,
IllegalSqlFragmentTokens = IllegalSqlFragmentTokens,
MaxLimit = MaxLimit,
EnableUntypedQueries = EnableUntypedQueries,
EnableSqlFilters = EnableRawSqlFilters,
OrderByPrimaryKeyOnLimitQuery = OrderByPrimaryKeyOnPagedQuery,
QueryFilters = QueryFilters,
StartsWithConventions = StartsWithConventions,
EndsWithConventions = EndsWithConventions,
Db = UseNamedConnection != null
? c.Resolve<IDbConnectionFactory>().OpenDbConnection(UseNamedConnection)
: c.Resolve<IDbConnectionFactory>().OpenDbConnection(),
})
.ReusedWithin(ReuseScope.None);
appHost.Metadata.GetOperationAssemblies()
.Each(x => LoadFromAssemblies.Add(x));
}
public void AfterPluginsLoaded(IAppHost appHost)
{
var scannedTypes = LoadFromAssemblies.SelectMany(x => x.GetTypes());
var misingRequestTypes = scannedTypes
.Where(x => x.HasInterface(typeof(IQuery)))
.Where(x => !appHost.Metadata.OperationsMap.ContainsKey(x))
.ToList();
if (misingRequestTypes.Count == 0)
return;
var serviceType = GenerateMissingServices(misingRequestTypes);
appHost.RegisterService(serviceType);
}
Type GenerateMissingServices(IEnumerable<Type> misingRequestTypes)
{
var assemblyName = new AssemblyName { Name = "tmpAssembly" };
var typeBuilder =
Thread.GetDomain().DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run)
.DefineDynamicModule("tmpModule")
.DefineType("__AutoQueryServices",
TypeAttributes.Public | TypeAttributes.Class,
AutoQueryServiceBaseType);
var emptyCtor = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, Type.EmptyTypes);
var baseType = AutoQueryServiceBaseType.BaseType;
if (baseType != null)
{
var ctorIl = emptyCtor.GetILGenerator();
ctorIl.Emit(OpCodes.Ldarg_0);
ctorIl.Emit(OpCodes.Call, baseType.GetEmptyConstructor());
ctorIl.Emit(OpCodes.Ret);
}
foreach (var requestType in misingRequestTypes)
{
var method = typeBuilder.DefineMethod("Any", MethodAttributes.Public | MethodAttributes.Virtual,
CallingConventions.Standard,
returnType: typeof(object),
parameterTypes: new[] { requestType });
var genericDef = requestType.GetTypeWithGenericTypeDefinitionOf(typeof(IQuery<,>));
var hasExplicitInto = genericDef != null;
if (genericDef == null)
genericDef = requestType.GetTypeWithGenericTypeDefinitionOf(typeof(IQuery<>));
var il = method.GetILGenerator();
var genericArgs = genericDef.GetGenericArguments();
var mi = AutoQueryServiceBaseType.GetMethods()
.First(x => x.GetGenericArguments().Length == genericArgs.Length);
var genericMi = mi.MakeGenericMethod(genericArgs);
var queryType = hasExplicitInto
? typeof(IQuery<,>).MakeGenericType(genericArgs)
: typeof(IQuery<>).MakeGenericType(genericArgs);
il.Emit(OpCodes.Nop);
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Ldarg_1);
il.Emit(OpCodes.Box, queryType);
il.Emit(OpCodes.Callvirt, genericMi);
il.Emit(OpCodes.Ret);
}
var servicesType = typeBuilder.CreateType();
return servicesType;
}
public AutoQueryFeature RegisterQueryFilter<Request, From>(Func<IRequest, SqlExpression<From>, Request, SqlExpression<From>> filterFn)
{
QueryFilters[typeof(Request)] = (req, expression, model) =>
filterFn(req, (SqlExpression<From>)expression, (Request)model);
return this;
}
}
public interface IAutoQuery
{
SqlExpression<From> CreateQuery<From>(IQuery<From> model, Dictionary<string, string> dynamicParams, IRequest request = null);
QueryResponse<From> Execute<From>(IQuery<From> model, SqlExpression<From> query);
SqlExpression<From> CreateQuery<From, Into>(IQuery<From, Into> model, Dictionary<string, string> dynamicParams, IRequest request = null);
QueryResponse<Into> Execute<From, Into>(IQuery<From, Into> model, SqlExpression<From> query);
}
public abstract class AutoQueryServiceBase : Service
{
public IAutoQuery AutoQuery { get; set; }
public virtual object Exec<From>(IQuery<From> dto)
{
SqlExpression<From> q;
using (Profiler.Current.Step("AutoQuery.CreateQuery"))
{
q = AutoQuery.CreateQuery(dto, Request.GetRequestParams());
}
using (Profiler.Current.Step("AutoQuery.Execute"))
{
return AutoQuery.Execute(dto, q);
}
}
public virtual object Exec<From, Into>(IQuery<From, Into> dto)
{
SqlExpression<From> q;
using (Profiler.Current.Step("AutoQuery.CreateQuery"))
{
q = AutoQuery.CreateQuery(dto, Request.GetRequestParams());
}
using (Profiler.Current.Step("AutoQuery.Execute"))
{
return AutoQuery.Execute(dto, q);
}
}
}
public interface IAutoQueryOptions
{
int? MaxLimit { get; set; }
bool EnableUntypedQueries { get; set; }
bool EnableSqlFilters { get; set; }
bool OrderByPrimaryKeyOnLimitQuery { get; set; }
HashSet<string> IgnoreProperties { get; set; }
HashSet<string> IllegalSqlFragmentTokens { get; set; }
Dictionary<string, QueryFieldAttribute> StartsWithConventions { get; set; }
Dictionary<string, QueryFieldAttribute> EndsWithConventions { get; set; }
}
public class AutoQuery : IAutoQuery, IAutoQueryOptions, IDisposable
{
public int? MaxLimit { get; set; }
public bool EnableUntypedQueries { get; set; }
public bool EnableSqlFilters { get; set; }
public bool OrderByPrimaryKeyOnLimitQuery { get; set; }
public string RequiredRoleForRawSqlFilters { get; set; }
public HashSet<string> IgnoreProperties { get; set; }
public HashSet<string> IllegalSqlFragmentTokens { get; set; }
public Dictionary<string, QueryFieldAttribute> StartsWithConventions { get; set; }
public Dictionary<string, QueryFieldAttribute> EndsWithConventions { get; set; }
public virtual IDbConnection Db { get; set; }
public Dictionary<Type, QueryFilterDelegate> QueryFilters { get; set; }
public virtual void Dispose()
{
if (Db != null)
Db.Dispose();
}
private static Dictionary<Type, ITypedQuery> TypedQueries = new Dictionary<Type, ITypedQuery>();
public static ITypedQuery GetTypedQuery(Type dtoType, Type fromType)
{
ITypedQuery defaultValue;
if (TypedQueries.TryGetValue(dtoType, out defaultValue)) return defaultValue;
var genericType = typeof(TypedQuery<,>).MakeGenericType(dtoType, fromType);
defaultValue = genericType.CreateInstance<ITypedQuery>();
Dictionary<Type, ITypedQuery> snapshot, newCache;
do
{
snapshot = TypedQueries;
newCache = new Dictionary<Type, ITypedQuery>(TypedQueries);
newCache[dtoType] = defaultValue;
} while (!ReferenceEquals(
Interlocked.CompareExchange(ref TypedQueries, newCache, snapshot), snapshot));
return defaultValue;
}
public SqlExpression<From> Filter<From>(IRequest request, ISqlExpression expr, IQuery model)
{
if (QueryFilters == null)
return (SqlExpression<From>)expr;
QueryFilterDelegate filterFn = null;
if (!QueryFilters.TryGetValue(model.GetType(), out filterFn))
{
foreach (var type in model.GetType().GetInterfaces())
{
if (QueryFilters.TryGetValue(type, out filterFn))
break;
}
}
if (filterFn != null)
return (SqlExpression<From>)(filterFn(request, expr, model) ?? expr);
return (SqlExpression<From>)expr;
}
public SqlExpression<From> CreateQuery<From>(IQuery<From> model, Dictionary<string, string> dynamicParams, IRequest request = null)
{
var typedQuery = GetTypedQuery(model.GetType(), typeof(From));
return Filter<From>(request, typedQuery.CreateQuery(Db, model, dynamicParams, this), model);
}
public QueryResponse<From> Execute<From>(IQuery<From> model, SqlExpression<From> query)
{
var typedQuery = GetTypedQuery(model.GetType(), typeof(From));
return typedQuery.Execute<From>(Db, query);
}
public SqlExpression<From> CreateQuery<From, Into>(IQuery<From, Into> model, Dictionary<string, string> dynamicParams, IRequest request = null)
{
var typedQuery = GetTypedQuery(model.GetType(), typeof(From));
return Filter<From>(request, typedQuery.CreateQuery(Db, model, dynamicParams, this), model);
}
public QueryResponse<Into> Execute<From, Into>(IQuery<From, Into> model, SqlExpression<From> query)
{
var typedQuery = GetTypedQuery(model.GetType(), typeof(From));
return typedQuery.Execute<Into>(Db, query);
}
}
public interface ITypedQuery
{
ISqlExpression CreateQuery(
IDbConnection db,
IQuery model,
Dictionary<string, string> dynamicParams,
IAutoQueryOptions options=null);
QueryResponse<Into> Execute<Into>(
IDbConnection db,
ISqlExpression query);
}
public class TypedQuery<QueryModel, From> : ITypedQuery
{
static readonly Dictionary<string, Func<object, object>> PropertyGetters =
new Dictionary<string, Func<object, object>>();
static readonly Dictionary<string, QueryFieldAttribute> QueryFieldMap =
new Dictionary<string, QueryFieldAttribute>();
static TypedQuery()
{
foreach (var pi in typeof(QueryModel).GetPublicProperties())
{
var fn = pi.GetValueGetter(typeof(QueryModel));
PropertyGetters[pi.Name] = fn;
var queryAttr = pi.FirstAttribute<QueryFieldAttribute>();
if (queryAttr != null)
QueryFieldMap[pi.Name] = queryAttr.Init();
}
}
public ISqlExpression CreateQuery(
IDbConnection db,
IQuery model,
Dictionary<string, string> dynamicParams,
IAutoQueryOptions options=null)
{
dynamicParams = new Dictionary<string, string>(dynamicParams, StringComparer.OrdinalIgnoreCase);
var q = db.From<From>();
if (options != null && options.EnableSqlFilters)
{
AppendSqlFilters(q, model, dynamicParams, options);
}
AppendJoins(q, model);
AppendLimits(q, model, options);
var dtoAttr = model.GetType().FirstAttribute<QueryAttribute>();
var defaultTerm = dtoAttr != null && dtoAttr.DefaultTerm == QueryTerm.Or ? "OR" : "AND";
AppendTypedQueries(q, model, dynamicParams, defaultTerm, options);
if (options != null && options.EnableUntypedQueries)
{
AppendUntypedQueries(q, dynamicParams, defaultTerm, options);
}
if (defaultTerm == "OR" && q.WhereExpression == null)
{
q.Where("1=0"); //Empty OR queries should be empty
}
return q;
}
private void AppendSqlFilters(SqlExpression<From> q, IQuery model, Dictionary<string, string> dynamicParams, IAutoQueryOptions options)
{
string select, from, where;
dynamicParams.TryGetValue("_select", out select);
if (select != null)
{
dynamicParams.Remove("_select");
select.SqlVerifyFragment(options.IllegalSqlFragmentTokens);
q.Select(select);
}
dynamicParams.TryGetValue("_from", out from);
if (from != null)
{
dynamicParams.Remove("_from");
from.SqlVerifyFragment(options.IllegalSqlFragmentTokens);
q.From(from);
}
dynamicParams.TryGetValue("_where", out where);
if (where != null)
{
dynamicParams.Remove("_where");
where.SqlVerifyFragment(options.IllegalSqlFragmentTokens);
q.Where(where);
}
}
private static readonly char[] FieldSeperators = new[] {',', ';'};
private static void AppendLimits(SqlExpression<From> q, IQuery model, IAutoQueryOptions options)
{
var maxLimit = options != null ? options.MaxLimit : null;
var take = model.Take ?? maxLimit;
if (take > maxLimit)
take = maxLimit;
q.Limit(model.Skip, take);
if (model.OrderBy != null)
{
var fieldNames = model.OrderBy.Split(FieldSeperators, StringSplitOptions.RemoveEmptyEntries);
q.OrderByFields(fieldNames);
}
else if (model.OrderByDesc != null)
{
var fieldNames = model.OrderByDesc.Split(FieldSeperators, StringSplitOptions.RemoveEmptyEntries);
q.OrderByFieldsDescending(fieldNames);
}
else if ((model.Skip != null || model.Take != null)
&& (options != null && options.OrderByPrimaryKeyOnLimitQuery))
{
q.OrderByFields(typeof(From).GetModelMetadata().PrimaryKey);
}
}
private static void AppendJoins(SqlExpression<From> q, IQuery model)
{
if (model is IJoin)
{
var dtoInterfaces = model.GetType().GetInterfaces();
foreach(var innerJoin in dtoInterfaces.Where(x => x.Name.StartsWith("IJoin`")))
{
var joinTypes = innerJoin.GetGenericArguments();
for (var i = 1; i < joinTypes.Length; i++)
{
q.Join(joinTypes[i - 1], joinTypes[i]);
}
}
foreach(var leftJoin in dtoInterfaces.Where(x => x.Name.StartsWith("ILeftJoin`")))
{
var joinTypes = leftJoin.GetGenericArguments();
for (var i = 1; i < joinTypes.Length; i++)
{
q.LeftJoin(joinTypes[i - 1], joinTypes[i]);
}
}
}
}
private static void AppendTypedQueries(SqlExpression<From> q, IQuery model,
Dictionary<string, string> dynamicParams, string defaultTerm, IAutoQueryOptions options)
{
foreach (var entry in PropertyGetters)
{
var name = entry.Key;
QueryFieldAttribute implicitQuery;
QueryFieldMap.TryGetValue(name, out implicitQuery);
if (implicitQuery != null && implicitQuery.Field != null)
name = implicitQuery.Field;
var match = GetQueryMatch(q, name, options);
if (match == null)
continue;
if (implicitQuery == null)
implicitQuery = match.ImplicitQuery;
var quotedColumn = q.DialectProvider.GetQuotedColumnName(match.ModelDef, match.FieldDef);
var value = entry.Value(model);
if (value == null)
continue;
dynamicParams.Remove(entry.Key);
AddCondition(q, defaultTerm, quotedColumn, value, implicitQuery);
}
}
private static void AddCondition(SqlExpression<From> q, string defaultTerm, string quotedColumn, object value, QueryFieldAttribute implicitQuery)
{
var seq = value as IEnumerable;
if (value is string)
seq = null;
var format = seq == null
? quotedColumn + " = {0}"
: quotedColumn + " IN ({0})";
if (implicitQuery != null)
{
var operand = implicitQuery.Operand ?? "=";
if (implicitQuery.Term == QueryTerm.Or)
defaultTerm = "OR";
else if (implicitQuery.Term == QueryTerm.And)
defaultTerm = "AND";
format = quotedColumn + " " + operand + " {0}";
if (implicitQuery.Template != null)
{
format = implicitQuery.Template.Replace("{Field}", quotedColumn);
if (implicitQuery.ValueStyle == ValueStyle.Multiple)
{
if (seq == null)
throw new ArgumentException("{0} requires {1} values"
.Fmt(implicitQuery.Field, implicitQuery.ValueArity));
var args = new object[implicitQuery.ValueArity];
int i = 0;
foreach (var x in seq)
{
if (i < args.Length)
{
format = format.Replace("{Value" + (i + 1) + "}", "{" + i + "}");
args[i++] = x;
}
}
q.AddCondition(defaultTerm, format, args);
return;
}
if (implicitQuery.ValueStyle == ValueStyle.List)
{
if (seq == null)
throw new ArgumentException("{0} expects a list of values".Fmt(implicitQuery.Field));
format = format.Replace("{Values}", "{0}");
value = new SqlInValues(seq);
}
else
{
format = format.Replace("{Value}", "{0}");
}
if (implicitQuery.ValueFormat != null)
value = string.Format(implicitQuery.ValueFormat, value);
}
}
else
{
if (seq != null)
value = new SqlInValues(seq);
}
q.AddCondition(defaultTerm, format, value);
}
private static void AppendUntypedQueries(SqlExpression<From> q, Dictionary<string, string> dynamicParams, string defaultTerm, IAutoQueryOptions options)
{
foreach (var entry in dynamicParams)
{
var name = entry.Key;
var match = GetQueryMatch(q, name, options);
if (match == null)
continue;
var implicitQuery = match.ImplicitQuery;
var quotedColumn = q.DialectProvider.GetQuotedColumnName(match.ModelDef, match.FieldDef);
var strValue = entry.Value;
var fieldType = match.FieldDef.FieldType;
var isMultiple = (implicitQuery != null && (implicitQuery.ValueStyle > ValueStyle.Single))
|| string.Compare(name, match.FieldDef.Name + Pluralized, StringComparison.OrdinalIgnoreCase) == 0;
var value = isMultiple ?
TypeSerializer.DeserializeFromString(strValue, Array.CreateInstance(fieldType, 0).GetType())
: fieldType == typeof(string) ?
strValue
: fieldType.IsValueType ?
Convert.ChangeType(strValue, fieldType) :
TypeSerializer.DeserializeFromString(strValue, fieldType);
AddCondition(q, defaultTerm, quotedColumn, value, implicitQuery);
}
}
class MatchQuery
{
public MatchQuery(Tuple<ModelDefinition,FieldDefinition> match, QueryFieldAttribute implicitQuery)
{
ModelDef = match.Item1;
FieldDef = match.Item2;
ImplicitQuery = implicitQuery;
}
public readonly ModelDefinition ModelDef;
public readonly FieldDefinition FieldDef;
public readonly QueryFieldAttribute ImplicitQuery;
}
private const string Pluralized = "s";
private static MatchQuery GetQueryMatch(SqlExpression<From> q, string name, IAutoQueryOptions options)
{
if (options == null) return null;
var match = options.IgnoreProperties == null || !options.IgnoreProperties.Contains(name)
? q.FirstMatchingField(name) ?? (name.EndsWith(Pluralized) ? q.FirstMatchingField(name.TrimEnd('s')) : null)
: null;
if (match == null)
{
foreach (var startsWith in options.StartsWithConventions)
{
if (name.Length <= startsWith.Key.Length || !name.StartsWith(startsWith.Key)) continue;
var field = name.Substring(startsWith.Key.Length);
match = q.FirstMatchingField(field) ?? (field.EndsWith(Pluralized) ? q.FirstMatchingField(field.TrimEnd('s')) : null);
if (match != null)
return new MatchQuery(match, startsWith.Value);
}
}
if (match == null)
{
foreach (var endsWith in options.EndsWithConventions)
{
if (name.Length <= endsWith.Key.Length || !name.EndsWith(endsWith.Key)) continue;
var field = name.Substring(0, name.Length - endsWith.Key.Length);
match = q.FirstMatchingField(field) ?? (field.EndsWith(Pluralized) ? q.FirstMatchingField(field.TrimEnd('s')) : null);
if (match != null)
return new MatchQuery(match, endsWith.Value);
}
}
return match != null
? new MatchQuery(match, null)
: null;
}
public QueryResponse<Into> Execute<Into>(IDbConnection db, ISqlExpression query)
{
try
{
var q = (SqlExpression<From>)query;
var response = new QueryResponse<Into>
{
Offset = q.Offset.GetValueOrDefault(0),
Total = (int)db.Count(q),
Results = db.LoadSelect<Into, From>(q),
};
return response;
}
catch (Exception ex)
{
throw new ArgumentException(ex.Message);
}
}
}
public static class AutoQueryExtensions
{
public static QueryFieldAttribute Init(this QueryFieldAttribute query)
{
query.ValueStyle = ValueStyle.Single;
if (query.Template == null || query.ValueFormat != null) return query;
var i = 0;
while (query.Template.Contains("{Value" + (i + 1) + "}")) i++;
if (i > 0)
{
query.ValueStyle = ValueStyle.Multiple;
query.ValueArity = i;
}
else
{
query.ValueStyle = !query.Template.Contains("{Values}")
? ValueStyle.Single
: ValueStyle.List;
}
return query;
}
}
}