forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutoQueryFeature.AutoCrud.cs
More file actions
879 lines (732 loc) · 38.2 KB
/
AutoQueryFeature.AutoCrud.cs
File metadata and controls
879 lines (732 loc) · 38.2 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
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
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using ServiceStack.Configuration;
using ServiceStack.MiniProfiler;
using ServiceStack.Data;
using ServiceStack.DataAnnotations;
using ServiceStack.OrmLite;
using ServiceStack.Text;
using ServiceStack.Web;
namespace ServiceStack
{
public partial class AutoQueryFeature
{
public string AccessRole { get; set; } = RoleNames.Admin;
public Dictionary<Type, string[]> ServiceRoutes { get; set; } = new Dictionary<Type, string[]> {
{ typeof(GetCrudEventsService), new []{ "/" + "crudevents".Localize() + "/{Model}" } },
{ typeof(CheckCrudEventService), new []{ "/" + "crudevents".Localize() + "/check" } },
};
protected void OnRegister(IAppHost appHost)
{
if (AccessRole != null && appHost.GetContainer().Exists<ICrudEvents>())
{
appHost.RegisterServices(ServiceRoutes);
}
}
}
/* Allow metadata discovery & code-gen in *.Source.csproj builds */
#if !SOURCE
[Restrict(VisibilityTo = RequestAttributes.None)]
public partial class GetCrudEventsService {}
[Restrict(VisibilityTo = RequestAttributes.None)]
public partial class CheckCrudEventService {}
#endif
[DefaultRequest(typeof(GetCrudEvents))]
public partial class GetCrudEventsService : Service
{
public IAutoQueryDb AutoQuery { get; set; }
public IDbConnectionFactory DbFactory { get; set; }
public async Task<object> Any(GetCrudEvents request)
{
var appHost = HostContext.AppHost;
var feature = appHost.AssertPlugin<AutoQueryFeature>();
await RequestUtils.AssertAccessRoleAsync(base.Request, accessRole:feature.AccessRole, authSecret:request.AuthSecret);
if (string.IsNullOrEmpty(request.Model))
throw new ArgumentNullException(nameof(request.Model));
var dto = appHost.Metadata.FindDtoType(request.Model);
var namedConnection = dto?.FirstAttribute<NamedConnectionAttribute>()?.Name;
using var useDb = namedConnection != null
? await DbFactory.OpenDbConnectionAsync(namedConnection).ConfigAwait()
: await DbFactory.OpenDbConnectionAsync().ConfigAwait();
var q = AutoQuery.CreateQuery(request, Request, useDb);
var response = await AutoQuery.ExecuteAsync(request, q, Request, useDb).ConfigAwait();
return response;
}
}
[DefaultRequest(typeof(CheckCrudEvents))]
public partial class CheckCrudEventService : Service
{
public IDbConnectionFactory DbFactory { get; set; }
public async Task<object> Any(CheckCrudEvents request)
{
var appHost = HostContext.AppHost;
var feature = appHost.AssertPlugin<AutoQueryFeature>();
await RequestUtils.AssertAccessRoleAsync(base.Request, accessRole:feature.AccessRole, authSecret:request.AuthSecret);
if (string.IsNullOrEmpty(request.Model))
throw new ArgumentNullException(nameof(request.Model));
var ids = request.Ids?.Count > 0
? request.Ids
: throw new ArgumentNullException(nameof(request.Ids));
var dto = appHost.Metadata.FindDtoType(request.Model);
var namedConnection = dto?.FirstAttribute<NamedConnectionAttribute>()?.Name;
using var useDb = namedConnection != null
? await DbFactory.OpenDbConnectionAsync(namedConnection).ConfigAwait()
: await DbFactory.OpenDbConnectionAsync().ConfigAwait();
var q = useDb.From<CrudEvent>()
.Where(x => x.Model == request.Model)
.And(x => ids.Contains(x.ModelId))
.SelectDistinct(x => x.ModelId);
var results = await useDb.ColumnAsync<string>(q).ConfigAwait();
return new CheckCrudEventsResponse {
Results = results.ToList(),
};
}
}
public class CrudContext
{
public IRequest Request { get; private set; }
public IDbConnection Db { get; private set; }
public ICrudEvents Events { get; private set; }
public string Operation { get; set; }
public object Dto { get; private set; }
public Type ModelType { get; private set; }
public Type RequestType { get; private set; }
public Type ResponseType { get; private set; }
public ModelDefinition ModelDef { get; private set; }
public PropertyAccessor IdProp { get; private set; }
public PropertyAccessor ResultProp { get; private set; }
public PropertyAccessor CountProp { get; private set; }
public PropertyAccessor RowVersionProp { get; private set; }
public object Id { get; set; }
public long? RowsUpdated { get; set; }
internal void SetResult(AutoQuery.ExecValue result)
{
Id = result.Id;
RowsUpdated = result.RowsUpdated;
}
internal GetMemberDelegate RequestIdGetter() =>
TypeProperties.Get(RequestType).GetPublicGetter(ModelDef.PrimaryKey.Name);
internal void ThrowPrimaryKeyRequiredForRowVersion() =>
throw new NotSupportedException($"Could not resolve Primary Key from '{RequestType.Name}' to be able to resolve RowVersion");
internal static CrudContext Create<Table>(IRequest request, IDbConnection db, object dto, string operation)
{
var appHost = HostContext.AppHost;
var requestType = dto?.GetType() ?? throw new ArgumentNullException(nameof(dto));
var responseType = appHost.Metadata.GetOperation(requestType)?.ResponseType;
var responseProps = responseType == null ? null : TypeProperties.Get(responseType);
return new CrudContext {
Operation = operation,
Request = request ?? throw new ArgumentNullException(nameof(request)),
Db = db ?? throw new ArgumentNullException(nameof(db)),
Events = appHost.TryResolve<ICrudEvents>(),
Dto = dto,
ModelType = typeof(Table),
RequestType = requestType,
ModelDef = typeof(Table).GetModelMetadata(),
ResponseType = responseType,
IdProp = responseProps?.GetAccessor(Keywords.Id),
CountProp = responseProps?.GetAccessor(Keywords.Count),
ResultProp = responseProps?.GetAccessor(Keywords.Result),
RowVersionProp = responseProps?.GetAccessor(Keywords.RowVersion),
};
}
}
public partial class AutoQuery : IAutoCrudDb
{
public object Create<Table>(ICreateDb<Table> dto, IRequest req)
{
//TODO: Allow Create to use Default Values
using var db = GetDb<Table>(req);
using var profiler = Profiler.Current.Step("AutoQuery.Create");
var response = ExecAndReturnResponse<Table>(CrudContext.Create<Table>(req,db,dto,AutoCrudOperation.Create),
ctx => {
var dtoValues = ResolveDtoValues(req, dto);
var pkField = ctx.ModelDef.PrimaryKey;
var selectIdentity = ctx.IdProp != null || ctx.ResultProp != null || ctx.Events != null;
//Use same Id if being executed from id
if (req.Items.TryGetValue(Keywords.EventModelId, out var eventId) && eventId != null
&& !dtoValues.ContainsKey(pkField.Name))
{
dtoValues[pkField.Name] = eventId.ConvertTo(pkField.PropertyInfo.PropertyType);
selectIdentity = false;
}
var autoIntId = db.Insert<Table>(dtoValues, selectIdentity: selectIdentity);
return CreateInternal(dtoValues, pkField, selectIdentity, autoIntId);
});
return response;
}
public async Task<object> CreateAsync<Table>(ICreateDb<Table> dto, IRequest req)
{
//TODO: Allow Create to use Default Values
using var db = GetDb<Table>(req);
using var profiler = Profiler.Current.Step("AutoQuery.Create");
var response = await ExecAndReturnResponseAsync<Table>(CrudContext.Create<Table>(req,db,dto,AutoCrudOperation.Create),
async ctx => {
var dtoValues = ResolveDtoValues(ctx.Request, ctx.Dto);
var pkField = ctx.ModelDef.PrimaryKey;
var selectIdentity = ctx.IdProp != null || ctx.ResultProp != null || ctx.Events != null;
//Use same Id if being executed from id
if (req.Items.TryGetValue(Keywords.EventModelId, out var eventId) && eventId != null
&& !dtoValues.ContainsKey(pkField.Name))
{
dtoValues[pkField.Name] = eventId.ConvertTo(pkField.PropertyInfo.PropertyType);
selectIdentity = false;
}
var autoIntId = await db.InsertAsync<Table>(dtoValues, selectIdentity: selectIdentity).ConfigAwait();
return CreateInternal(dtoValues, pkField, selectIdentity, autoIntId);
}).ConfigAwait();
return response;
}
private static ExecValue CreateInternal(Dictionary<string, object> dtoValues,
FieldDefinition pkField, bool selectIdentity, long autoIntId)
{
// [AutoId] Guid's populate the PK Property or return Id if provided
var isAutoId = pkField?.AutoId == true;
var providedId = pkField != null && dtoValues.ContainsKey(pkField.Name);
if (isAutoId || providedId)
return new ExecValue(pkField.GetValue(dtoValues), selectIdentity ? 1 : autoIntId);
return selectIdentity
? new ExecValue(autoIntId, 1)
: pkField != null && dtoValues.TryGetValue(pkField.Name, out var idValue)
? new ExecValue(idValue, autoIntId)
: new ExecValue(null, autoIntId);
}
public object Update<Table>(IUpdateDb<Table> dto, IRequest req)
{
return UpdateInternal<Table>(req, dto,AutoCrudOperation.Update);
}
public Task<object> UpdateAsync<Table>(IUpdateDb<Table> dto, IRequest req)
{
return UpdateInternalAsync<Table>(req, dto, AutoCrudOperation.Update);
}
public object Patch<Table>(IPatchDb<Table> dto, IRequest req)
{
return UpdateInternal<Table>(req, dto, AutoCrudOperation.Patch);
}
public Task<object> PatchAsync<Table>(IPatchDb<Table> dto, IRequest req)
{
return UpdateInternalAsync<Table>(req, dto, AutoCrudOperation.Patch);
}
public object Delete<Table>(IDeleteDb<Table> dto, IRequest req)
{
using var db = GetDb<Table>(req);
using var profiler = Profiler.Current.Step("AutoQuery.Delete");
var response = ExecAndReturnResponse<Table>(CrudContext.Create<Table>(req,db,dto,AutoCrudOperation.Delete),
ctx => {
var dtoValues = ResolveDtoValues(ctx.Request, ctx.Dto, skipDefaults:true);
var idValue = ctx.ModelDef.PrimaryKey != null && dtoValues.TryGetValue(ctx.ModelDef.PrimaryKey.Name, out var oId)
? oId
: null;
var q = DeleteInternal<Table>(ctx, dtoValues);
if (q != null)
return new ExecValue(idValue, ctx.Db.Delete(q));
return new ExecValue(idValue, ctx.Db.Delete<Table>(dtoValues));
});
return response;
}
public async Task<object> DeleteAsync<Table>(IDeleteDb<Table> dto, IRequest req)
{
using var db = GetDb<Table>(req);
using var profiler = Profiler.Current.Step("AutoQuery.Delete");
var response = await ExecAndReturnResponseAsync<Table>(CrudContext.Create<Table>(req,db,dto,AutoCrudOperation.Delete),
async ctx => {
var dtoValues = ResolveDtoValues(req, dto, skipDefaults:true);
var idValue = ctx.ModelDef.PrimaryKey != null && dtoValues.TryGetValue(ctx.ModelDef.PrimaryKey.Name, out var oId)
? oId
: null;
var q = DeleteInternal<Table>(ctx, dtoValues);
if (q != null)
return new ExecValue(idValue, await ctx.Db.DeleteAsync(q).ConfigAwait());
return new ExecValue(idValue, await ctx.Db.DeleteAsync<Table>(dtoValues).ConfigAwait());
}).ConfigAwait();
return response;
}
internal SqlExpression<Table> DeleteInternal<Table>(CrudContext ctx, Dictionary<string, object> dtoValues)
{
//Should have at least 1 non-default filter
if (dtoValues.Count == 0)
throw new NotSupportedException($"'{ctx.RequestType.Name}' did not contain any filters");
// Should only update a Single Row
if (GetAutoFilterExpressions(ctx, dtoValues, out var expr, out var exprParams))
{
//If there were Auto Filters, construct filter expression manually by adding any remaining DTO values
foreach (var entry in dtoValues)
{
var fieldDef = ctx.ModelDef.GetFieldDefinition(entry.Key);
if (fieldDef == null)
throw new NotSupportedException($"Unknown '{entry.Key}' Field in '{ctx.RequestType.Name}' IDeleteDb<{typeof(Table).Name}> Request");
if (expr.Length > 0)
expr += " AND ";
var quotedColumn = ctx.Db.GetDialectProvider().GetQuotedColumnName(ctx.ModelDef, fieldDef);
expr += quotedColumn + " = {" + exprParams.Count + "}";
exprParams.Add(entry.Value);
}
var q = ctx.Db.From<Table>();
q.Where(expr, exprParams.ToArray());
return q;
}
return null;
}
public object Save<Table>(ISaveDb<Table> dto, IRequest req)
{
using var db = GetDb<Table>(req);
using var profiler = Profiler.Current.Step("AutoQuery.Save");
var row = dto.ConvertTo<Table>();
var response = ExecAndReturnResponse<Table>(CrudContext.Create<Table>(req,db,dto,AutoCrudOperation.Save),
ctx => {
ctx.Db.Save(row);
return SaveInternal(dto, ctx);
});
return response;
}
public async Task<object> SaveAsync<Table>(ISaveDb<Table> dto, IRequest req)
{
using var db = GetDb<Table>(req);
using var profiler = Profiler.Current.Step("AutoQuery.Save");
var row = dto.ConvertTo<Table>();
var response = await ExecAndReturnResponseAsync<Table>(CrudContext.Create<Table>(req,db,dto,AutoCrudOperation.Save),
async ctx => {
await ctx.Db.SaveAsync(row).ConfigAwait();
return SaveInternal(dto, ctx);
}).ConfigAwait();
return response;
}
private static ExecValue SaveInternal<Table>(ISaveDb<Table> dto, CrudContext ctx)
{
//TODO: Use Upsert when available
object idValue = null;
var pkField = ctx.ModelDef.PrimaryKey;
if (pkField != null)
{
var propGetter = TypeProperties.Get(dto.GetType()).GetPublicGetter(pkField.Name);
if (propGetter != null)
idValue = propGetter(dto);
}
return new ExecValue(idValue, 1);
}
internal struct ExecValue
{
internal object Id;
internal long? RowsUpdated;
public ExecValue(object id, long? rowsUpdated)
{
Id = id;
RowsUpdated = rowsUpdated;
}
}
private object ExecAndReturnResponse<Table>(CrudContext context, Func<CrudContext,ExecValue> fn)
{
var ignoreEvent = context.Request.Items.ContainsKey(Keywords.IgnoreEvent);
var trans = context.Events != null && !ignoreEvent
? context.Db.OpenTransaction()
: null;
using (trans)
{
context.SetResult(fn(context));
if (context.Events != null && !ignoreEvent)
context.Events?.Record(context);
trans?.Commit();
}
if (context.ResponseType == null)
return null;
object idValue = null;
var response = context.ResponseType.CreateInstance();
if (context.IdProp != null && context.Id != null)
{
idValue = context.Id.ConvertTo(context.IdProp.PropertyInfo.PropertyType);
context.IdProp.PublicSetter(response, idValue);
}
if (context.CountProp != null && context.RowsUpdated != null)
{
context.CountProp.PublicSetter(response, context.RowsUpdated.ConvertTo(context.CountProp.PropertyInfo.PropertyType));
}
if (context.ResultProp != null && context.Id != null)
{
var result = context.Db.SingleById<Table>(context.Id);
context.ResultProp.PublicSetter(response, result.ConvertTo(context.ResultProp.PropertyInfo.PropertyType));
}
if (context.RowVersionProp != null)
{
if (AutoMappingUtils.IsDefaultValue(idValue))
{
var dtoIdGetter = context.RequestIdGetter();
if (dtoIdGetter != null)
idValue = dtoIdGetter(context.Dto);
}
if (AutoMappingUtils.IsDefaultValue(idValue))
context.ThrowPrimaryKeyRequiredForRowVersion();
var rowVersion = context.Db.GetRowVersion<Table>(idValue);
context.RowVersionProp.PublicSetter(response, rowVersion.ConvertTo(context.RowVersionProp.PropertyInfo.PropertyType));
}
return response;
}
private async Task<object> ExecAndReturnResponseAsync<Table>(CrudContext context, Func<CrudContext,Task<ExecValue>> fn)
{
var ignoreEvent = context.Request.Items.ContainsKey(Keywords.IgnoreEvent);
var trans = context.Events != null && !ignoreEvent
? context.Db.OpenTransaction()
: null;
using (trans)
{
context.SetResult(await fn(context).ConfigAwait());
if (context.Events != null && !ignoreEvent)
await context.Events.RecordAsync(context).ConfigAwait();
trans?.Commit();
}
if (context.ResponseType == null)
return null;
object idValue = null;
var response = context.ResponseType.CreateInstance();
if (context.IdProp != null && context.Id != null)
{
idValue = context.Id.ConvertTo(context.IdProp.PropertyInfo.PropertyType);
context.IdProp.PublicSetter(response, idValue);
}
if (context.CountProp != null && context.RowsUpdated != null)
{
context.CountProp.PublicSetter(response, context.RowsUpdated.ConvertTo(context.CountProp.PropertyInfo.PropertyType));
}
if (context.ResultProp != null && context.Id != null)
{
var result = await context.Db.SingleByIdAsync<Table>(context.Id).ConfigAwait();
context.ResultProp.PublicSetter(response, result.ConvertTo(context.ResultProp.PropertyInfo.PropertyType));
}
if (context.RowVersionProp != null)
{
if (AutoMappingUtils.IsDefaultValue(idValue))
{
var dtoIdGetter = context.RequestIdGetter();
if (dtoIdGetter != null)
idValue = dtoIdGetter(context.Dto);
}
if (AutoMappingUtils.IsDefaultValue(idValue))
context.ThrowPrimaryKeyRequiredForRowVersion();
var rowVersion = await context.Db.GetRowVersionAsync<Table>(idValue).ConfigAwait();
context.RowVersionProp.PublicSetter(response, rowVersion.ConvertTo(context.RowVersionProp.PropertyInfo.PropertyType));
}
return response;
}
internal bool GetAutoFilterExpressions(CrudContext ctx, Dictionary<string, object> dtoValues, out string expr, out List<object> exprParams)
{
var meta = AutoCrudMetadata.Create(ctx.RequestType);
if (meta.AutoFilters != null)
{
var dialectProvider = ctx.Db.GetDialectProvider();
var sb = StringBuilderCache.Allocate();
var exprParamsList = new List<object>();
//Update's require PK's, Delete's don't need to
if (dtoValues.TryRemove(meta.ModelDef.PrimaryKey.Name, out var idValue))
{
var idColumn = dialectProvider.GetQuotedColumnName(meta.ModelDef, meta.ModelDef.PrimaryKey);
sb.Append(idColumn + " = {0}");
exprParamsList.Add(idValue);
}
var appHost = HostContext.AppHost;
for (var i = 0; i < meta.AutoFilters.Count; i++)
{
var filter = meta.AutoFilters[i];
var dbAttr = meta.AutoFiltersDbFields[i];
var fieldDef = meta.ModelDef.GetFieldDefinition(filter.Field);
if (fieldDef == null)
throw new NotSupportedException($"{ctx.RequestType.Name} '{filter.Field}' AutoFilter was not found on '{ctx.ModelType.Name}'");
var quotedColumn = dialectProvider.GetQuotedColumnName(meta.ModelDef, fieldDef);
var value = appHost.EvalScriptValue(filter, ctx.Request);
var ret = ExprResult.CreateExpression("AND", quotedColumn, value, dbAttr);
if (ret != null)
{
if (sb.Length > 0)
sb.Append(" AND ");
var exprResult = ret.Value;
if (exprResult.Format.IndexOf("{1}", StringComparison.Ordinal) >= 0)
throw new NotSupportedException($"SQL Template '{exprResult.Format}' with multiple arguments is not supported");
if (exprResult.Values != null)
{
for (var index = 0; index < exprResult.Values.Length; index++)
{
sb.Append(exprResult.Format.Replace("{" + index + "}", "{" + exprParamsList.Count + "}"));
exprParamsList.Add(exprResult.Values[index]);
}
}
}
expr = StringBuilderCache.ReturnAndFree(sb);
exprParams = exprParamsList;
return true;
}
}
expr = null;
exprParams = null;
return false;
}
private object UpdateInternal<Table>(IRequest req, object dto, string operation)
{
var skipDefaults = operation == AutoCrudOperation.Patch;
using var db = GetDb<Table>(req);
using (Profiler.Current.Step("AutoQuery.Update"))
{
var response = ExecAndReturnResponse<Table>(CrudContext.Create<Table>(req,db,dto,operation),
ctx => {
var dtoValues = ResolveDtoValues(req, dto, skipDefaults);
var pkField = ctx.ModelDef?.PrimaryKey;
if (pkField == null)
throw new NotSupportedException($"Table '{typeof(Table).Name}' does not have a primary key");
if (!dtoValues.TryGetValue(pkField.Name, out var idValue) || AutoMappingUtils.IsDefaultValue(idValue))
throw new ArgumentNullException(pkField.Name);
// Should only update a Single Row
var rowsUpdated = GetAutoFilterExpressions(ctx, dtoValues, out var expr, out var exprParams)
? ctx.Db.UpdateOnly<Table>(dtoValues, expr, exprParams.ToArray())
: ctx.Db.UpdateOnly<Table>(dtoValues);
if (rowsUpdated != 1)
throw new OptimisticConcurrencyException($"{rowsUpdated} rows were updated by '{dto.GetType().Name}'");
return new ExecValue(idValue, rowsUpdated);
}); //TODO: UpdateOnly
return response;
}
}
private async Task<object> UpdateInternalAsync<Table>(IRequest req, object dto, string operation)
{
var skipDefaults = operation == AutoCrudOperation.Patch;
using var db = GetDb<Table>(req);
using (Profiler.Current.Step("AutoQuery.Update"))
{
var response = await ExecAndReturnResponseAsync<Table>(CrudContext.Create<Table>(req,db,dto,operation),
async ctx => {
var dtoValues = ResolveDtoValues(req, dto, skipDefaults);
var pkField = ctx.ModelDef?.PrimaryKey;
if (pkField == null)
throw new NotSupportedException($"Table '{typeof(Table).Name}' does not have a primary key");
if (!dtoValues.TryGetValue(pkField.Name, out var idValue) || AutoMappingUtils.IsDefaultValue(idValue))
throw new ArgumentNullException(pkField.Name);
// Should only update a Single Row
var rowsUpdated = GetAutoFilterExpressions(ctx, dtoValues, out var expr, out var exprParams)
? await ctx.Db.UpdateOnlyAsync<Table>(dtoValues, expr, exprParams.ToArray()).ConfigAwait()
: await ctx.Db.UpdateOnlyAsync<Table>(dtoValues).ConfigAwait();
if (rowsUpdated != 1)
throw new OptimisticConcurrencyException($"{rowsUpdated} rows were updated by '{dto.GetType().Name}'");
return new ExecValue(idValue, rowsUpdated);
}).ConfigAwait(); //TODO: UpdateOnly
return response;
}
}
internal class AutoCrudMetadata
{
internal Type DtoType;
internal Type ModelType;
internal ModelDefinition ModelDef;
internal TypeProperties DtoProps;
internal List<AutoPopulateAttribute> PopulateAttrs;
internal List<AutoFilterAttribute> AutoFilters;
internal List<QueryDbFieldAttribute> AutoFiltersDbFields;
internal Dictionary<string, AutoUpdateAttribute> UpdateAttrs;
internal Dictionary<string, AutoDefaultAttribute> DefaultAttrs;
internal Dictionary<string, AutoMapAttribute> MapAttrs;
internal HashSet<string> NullableProps;
internal GetMemberDelegate RowVersionGetter;
internal List<string> RemoveDtoProps;
static readonly ConcurrentDictionary<Type, AutoCrudMetadata> cache =
new ConcurrentDictionary<Type, AutoCrudMetadata>();
internal static AutoCrudMetadata Create(Type dtoType)
{
if (cache.TryGetValue(dtoType, out var to))
return to;
to = new AutoCrudMetadata {
DtoType = dtoType,
ModelType = AutoCrudOperation.GetModelType(dtoType),
DtoProps = TypeProperties.Get(dtoType),
};
if (to.ModelType != null)
to.ModelDef = to.ModelType.GetModelMetadata();
to.RowVersionGetter = to.DtoProps.GetPublicGetter(Keywords.RowVersion);
var dtoAttrs = dtoType.AllAttributes();
foreach (var dtoAttr in dtoAttrs)
{
if (dtoAttr is AutoPopulateAttribute populateAttr)
{
to.PopulateAttrs ??= new List<AutoPopulateAttribute>();
to.PopulateAttrs.Add(populateAttr);
}
else if (dtoAttr is AutoFilterAttribute filterAttr)
{
to.AutoFilters ??= new List<AutoFilterAttribute>();
to.AutoFiltersDbFields ??= new List<QueryDbFieldAttribute>();
to.AutoFilters.Add(filterAttr);
to.AutoFiltersDbFields.Add(ExprResult.ToDbFieldAttribute(filterAttr));
}
}
foreach (var pi in to.DtoProps.PublicPropertyInfos)
{
var allAttrs = pi.AllAttributes();
var propName = pi.Name;
if (allAttrs.FirstOrDefault(x => x is AutoMapAttribute) is AutoMapAttribute mapAttr)
{
to.MapAttrs ??= new Dictionary<string, AutoMapAttribute>();
to.MapAttrs[propName] = mapAttr;
propName = mapAttr.To;
}
if (allAttrs.FirstOrDefault(x => x is AutoUpdateAttribute) is AutoUpdateAttribute updateAttr)
{
to.UpdateAttrs ??= new Dictionary<string, AutoUpdateAttribute>();
to.UpdateAttrs[propName] = updateAttr;
}
if (allAttrs.FirstOrDefault(x => x is AutoDefaultAttribute) is AutoDefaultAttribute defaultAttr)
{
to.DefaultAttrs ??= new Dictionary<string, AutoDefaultAttribute>();
to.DefaultAttrs[propName] = defaultAttr;
}
if (pi.PropertyType.IsNullableType())
{
to.NullableProps ??= new HashSet<string>();
to.NullableProps.Add(propName);
}
if (!IncludeCrudProperties.Contains(propName))
{
var hasProp = to.ModelDef.GetFieldDefinition(propName) != null;
if (!hasProp
|| (IgnoreCrudProperties.Contains(pi.Name) && !hasProp)
|| pi.HasAttribute<AutoIgnoreAttribute>())
{
to.RemoveDtoProps ??= new List<string>();
to.RemoveDtoProps.Add(pi.Name);
}
}
}
return cache[dtoType] = to;
}
}
public static HashSet<string> IgnoreCrudProperties { get; } = new HashSet<string> {
nameof(IHasSessionId.SessionId),
nameof(IHasBearerToken.BearerToken),
nameof(IHasVersion.Version),
};
public static HashSet<string> IncludeCrudProperties { get; set; } = new HashSet<string> {
Keywords.Reset,
Keywords.RowVersion,
};
private Dictionary<string, object> ResolveDtoValues(IRequest req, object dto, bool skipDefaults=false)
{
var dtoValues = dto.ToObjectDictionary();
var meta = AutoCrudMetadata.Create(dto.GetType());
if (meta.MapAttrs != null)
{
foreach (var entry in meta.MapAttrs)
{
if (dtoValues.TryRemove(entry.Key, out var value))
{
dtoValues[entry.Value.To] = value;
}
}
}
List<string> removeKeys = null;
if (meta.RemoveDtoProps != null)
{
foreach (var removeDtoProp in meta.RemoveDtoProps)
{
removeKeys ??= new List<string>();
removeKeys.Add(removeDtoProp);
}
}
var appHost = HostContext.AppHost;
if (skipDefaults || meta.UpdateAttrs != null || meta.DefaultAttrs != null)
{
Dictionary<string, object> replaceValues = null;
foreach (var entry in dtoValues)
{
var isNullable = meta.NullableProps?.Contains(entry.Key) == true;
var isDefaultValue = entry.Value == null || (!isNullable && AutoMappingUtils.IsDefaultValue(entry.Value));
if (isDefaultValue)
{
var handled = false;
if (meta.DefaultAttrs != null && meta.DefaultAttrs.TryGetValue(entry.Key, out var defaultAttr))
{
handled = true;
replaceValues ??= new Dictionary<string, object>();
replaceValues[entry.Key] = appHost.EvalScriptValue(defaultAttr, req);
}
if (!handled)
{
if (skipDefaults ||
(meta.UpdateAttrs != null && meta.UpdateAttrs.TryGetValue(entry.Key, out var attr) &&
attr.Style == AutoUpdateStyle.NonDefaults))
{
removeKeys ??= new List<string>();
removeKeys.Add(entry.Key);
}
}
}
}
if (replaceValues != null)
{
foreach (var entry in replaceValues)
{
dtoValues[entry.Key] = entry.Value;
}
}
}
if (removeKeys != null)
{
foreach (var key in removeKeys)
{
dtoValues.RemoveKey(key);
}
}
if (meta.PopulateAttrs != null)
{
foreach (var populateAttr in meta.PopulateAttrs)
{
dtoValues[populateAttr.Field] = appHost.EvalScriptValue(populateAttr, req);
}
}
var populatorFn = AutoMappingUtils.GetPopulator(
typeof(Dictionary<string, object>), meta.DtoType);
populatorFn?.Invoke(dtoValues, dto);
IEnumerable<string> asStrings(object o) => o == null
? null
: o is string s
? s.Split(',').Map(x => x.Trim()).Where(x => !string.IsNullOrEmpty(x))
: o is IEnumerable<string> e
? e
: throw new NotSupportedException($"'{Keywords.Reset}' is not a list of field names");
var resetField = meta.ModelDef.GetFieldDefinition(Keywords.Reset);
var reset = resetField == null
? (dtoValues.TryRemove(Keywords.Reset, out var oReset)
? asStrings(oReset)
: dtoValues.TryRemove(Keywords.reset, out oReset)
? asStrings(oReset)
: null)
?? asStrings(req.GetParam(Keywords.reset))
: null;
if (reset != null)
{
foreach (var fieldName in reset)
{
var field = meta.ModelDef.GetFieldDefinition(fieldName);
if (field == null)
throw new NotSupportedException($"Reset field '{fieldName}' does not exist");
if (field.IsPrimaryKey)
throw new NotSupportedException($"Cannot reset primary key field '{fieldName}'");
dtoValues[field.Name] = field.FieldTypeDefaultValue;
}
}
// Ensure RowVersion is always populated if defined on Request DTO
if (meta.RowVersionGetter != null && !dtoValues.ContainsKey(Keywords.RowVersion))
dtoValues[Keywords.RowVersion] = default(uint);
return dtoValues;
}
}
public abstract partial class AutoQueryServiceBase
{
public virtual object Create<Table>(ICreateDb<Table> dto) => AutoQuery.Create(dto, Request);
public virtual Task<object> CreateAsync<Table>(ICreateDb<Table> dto) => AutoQuery.CreateAsync(dto, Request);
public virtual object Update<Table>(IUpdateDb<Table> dto) => AutoQuery.Update(dto, Request);
public virtual Task<object> UpdateAsync<Table>(IUpdateDb<Table> dto) => AutoQuery.UpdateAsync(dto, Request);
public virtual object Patch<Table>(IPatchDb<Table> dto) => AutoQuery.Patch(dto, Request);
public virtual Task<object> PatchAsync<Table>(IPatchDb<Table> dto) => AutoQuery.PatchAsync(dto, Request);
public virtual object Delete<Table>(IDeleteDb<Table> dto) => AutoQuery.Delete(dto, Request);
public virtual Task<object> DeleteAsync<Table>(IDeleteDb<Table> dto) => AutoQuery.DeleteAsync(dto, Request);
public virtual object Save<Table>(ISaveDb<Table> dto) => AutoQuery.Save(dto, Request);
public virtual Task<object> SaveAsync<Table>(ISaveDb<Table> dto) => AutoQuery.SaveAsync(dto, Request);
}
}