forked from signumsoftware/framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueryController.cs
More file actions
294 lines (239 loc) · 10.8 KB
/
QueryController.cs
File metadata and controls
294 lines (239 loc) · 10.8 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
using Signum.API.Filters;
using Signum.Engine.Maps;
using System.Text.Json.Serialization;
using System.Diagnostics.CodeAnalysis;
using Microsoft.AspNetCore.Mvc.Filters;
using Signum.DynamicQuery.Tokens;
using Microsoft.AspNetCore.Mvc;
using Signum.API.Json;
using System.ComponentModel.DataAnnotations;
namespace Signum.API.Controllers;
[ValidateModelFilter]
public class QueryController : ControllerBase
{
public static Action<QueryRequest>? AssertQuery;
[HttpGet("api/query/findLiteLike"), ProfilerActionSplitter("types")]
public async Task<List<Lite<Entity>>> FindLiteLike(string types, string subString, int count, CancellationToken token)
{
Implementations implementations = ParseImplementations(types);
return await AutocompleteUtils.FindLiteLikeAsync(implementations, subString, count, token);
}
[HttpGet("api/query/allLites"), ProfilerActionSplitter("types")]
public async Task<List<Lite<Entity>>> FetchAllLites(string types, CancellationToken token)
{
Implementations implementations = ParseImplementations(types);
foreach (var type in implementations.Types)
{
if (EntityKindCache.GetEntityData(type) == EntityData.Transactional)
throw new ArgumentNullException($"{type} is a Transactional entity");
}
return await AutocompleteUtils.FindAllLiteAsync(implementations, token);
}
private static Implementations ParseImplementations(string types)
{
return Implementations.By(types.Split(',').Select(a => TypeLogic.GetType(a.Trim())).ToArray());
}
[HttpGet("api/query/description/{queryName}"), ProfilerActionSplitter("queryName")]
public QueryDescriptionTS GetQueryDescription(string queryName)
{
var qn = QueryLogic.ToQueryName(queryName);
return new QueryDescriptionTS(QueryLogic.Queries.QueryDescription(qn));
}
[HttpGet("api/query/queryEntity/{queryName}"), ProfilerActionSplitter("queryName")]
public QueryEntity GetQueryEntity(string queryName)
{
var qn = QueryLogic.ToQueryName(queryName);
return QueryLogic.GetQueryEntity(qn);
}
[HttpPost("api/query/parseTokens")]
public List<QueryTokenTS> ParseTokens([Required, FromBody]ParseTokensRequest request)
{
var qn = QueryLogic.ToQueryName(request.queryKey);
var qd = QueryLogic.Queries.QueryDescription(qn);
var tokens = request.tokens.Select(token => QueryUtils.Parse(token, qd, SubTokensOptions.All)).ToList();
return tokens.Select(qt => new QueryTokenTS(qt, withParents: true)).ToList();
}
public class ParseTokensRequest
{
public required string queryKey;
public required List<string> tokens;
}
[HttpPost("api/query/subTokens")]
public List<QueryTokenTS> SubTokens([Required, FromBody]SubTokensRequest request)
{
var qn = QueryLogic.ToQueryName(request.queryKey);
var qd = QueryLogic.Queries.QueryDescription(qn);
var token = request.token == null ? null: QueryUtils.Parse(request.token, qd, SubTokensOptions.All);
var tokens = QueryUtils.SubTokens(token, qd, SubTokensOptions.All);
return tokens.Select(qt => QueryTokenTS.WithAutoExpand(qt, qd)).ToList();
}
public class SubTokensRequest
{
public required string queryKey;
public string? token;
}
[HttpPost("api/query/executeQuery/{queryKey}"), ProfilerActionSplitter("queryKey")]
public async Task<ResultTable> ExecuteQuery(string queryKey, [Required, FromBody]QueryRequestTS request, CancellationToken token)
{
var qr = request.ToQueryRequest(queryKey, SignumServer.JsonSerializerOptions, this.HttpContext.Request.Headers.Referer);
AssertQuery?.Invoke(qr);
var result = await QueryLogic.Queries.ExecuteQueryAsync(qr, token);
return result;
}
[HttpPost("api/query/lites/{queryKey}"), ProfilerActionSplitter("queryKey")]
public async Task<List<Lite<Entity>>> GetLites(string queryKey, [Required, FromBody]QueryEntitiesRequestTS request, CancellationToken token)
{
return await QueryLogic.Queries.GetEntitiesLite(request.ToQueryEntitiesRequest(queryKey, SignumServer.JsonSerializerOptions)).ToListAsync(token);
}
[HttpPost("api/query/entities/{queryKey}"), ProfilerActionSplitter("queryKey")]
public async Task<List<Entity>> GetEntities(string queryKey, [Required, FromBody]QueryEntitiesRequestTS request, CancellationToken token)
{
return await QueryLogic.Queries.GetEntitiesFull(request.ToQueryEntitiesRequest(queryKey, SignumServer.JsonSerializerOptions)).ToListAsync(token);
}
[HttpPost("api/query/queryValue/{queryKey}"), ProfilerActionSplitter("queryKey"), EmbeddedPropertyRouteAttribute<QueryValueResolver>]
public async Task<object?> QueryValue(string queryKey, [Required, FromBody]QueryValueRequestTS request, CancellationToken token)
{
var qvRequest = request.ToQueryValueRequest(queryKey, SignumServer.JsonSerializerOptions);
HttpContext.Items["queryValueRequests"] = qvRequest;
return await QueryLogic.Queries.ExecuteQueryValueAsync(qvRequest, token);
}
class QueryValueResolver : IEmbeddedPropertyRouteResolver
{
PropertyRoute IEmbeddedPropertyRouteResolver.GetPropertyRoute(EmbeddedEntity embedded, FilterContext filterContext)
{
var qvr = (QueryValueRequest)filterContext.HttpContext.Items["queryValueRequests"]!;
return qvr.ValueToken!.GetPropertyRoute()!;
}
}
}
public class QueryDescriptionTS
{
public string queryKey;
public Dictionary<string, QueryTokenTS> columns;
public QueryDescriptionTS(QueryDescription qd)
{
this.queryKey = QueryUtils.GetKey(qd.QueryName);
columns = new Dictionary<string, QueryTokenTS>();
var count = new AggregateToken(AggregateFunction.Count, qd.QueryName);
this.columns.Add(count.Key, QueryTokenTS.WithAutoExpand(count, qd));
var timeSeries = new TimeSeriesToken(qd.QueryName);
this.columns.Add(timeSeries.Key, QueryTokenTS.WithAutoExpand(timeSeries, qd));
this.columns.AddRange(qd.Columns, a => a.Name, cd =>
{
var token = new ColumnToken(cd, qd.QueryName);
return QueryTokenTS.WithAutoExpand(token, qd);
});
foreach (var action in AddExtension.GetInvocationListTyped())
{
action(this);
}
}
[JsonExtensionData]
public Dictionary<string, object> Extension { get; set; } = new Dictionary<string, object>();
public static Action<QueryDescriptionTS>? AddExtension;
}
public class QueryTokenTS
{
QueryTokenTS() { }
[SetsRequiredMembers]
public QueryTokenTS(QueryToken qt, bool withParents)
{
this.toStr = qt.ToString();
this.niceName = qt.NiceName();
this.key = qt.Key;
this.fullKey = qt.FullKey();
this.type = new TypeReferenceTS(qt.Type, qt.GetImplementations());
this.filterType = QueryUtils.TryGetFilterType(qt.Type);
this.format = qt.Format;
this.unit = UnitAttribute.GetTranslation(qt.Unit);
this.niceTypeName = qt.NiceTypeName;
this.queryTokenType = GetQueryTokenType(qt);
this.isGroupable = qt.IsGroupable;
this.autoExpand = qt.AutoExpand;
this.hideInAutoExpand = qt.HideInAutoExpand;
this.hasOrderAdapter = QueryUtils.OrderAdapters.Any(a => a(qt) != null);
this.tsVectorFor = qt is PgTsVectorColumnToken tsqt ? tsqt.GetColumnsRoutes().Select(a => a.ToString()).ToList() : null;
this.preferEquals = qt.Type == typeof(string) &&
qt.GetPropertyRoute() is PropertyRoute pr &&
typeof(Entity).IsAssignableFrom(pr.RootType) &&
Schema.Current.HasSomeIndex(pr);
this.propertyRoute = qt.GetPropertyRoute()?.ToString();
if (withParents && qt.Parent != null)
this.parent = new QueryTokenTS(qt.Parent, withParents);
}
public static QueryTokenTS WithAutoExpand(QueryToken qt, QueryDescription qd)
{
var qt2 = new QueryTokenTS(qt, withParents: false);
if (qt.AutoExpand)
qt2.subTokens = QueryUtils.SubTokens(qt, qd, SubTokensOptions.All).Select(st => WithAutoExpand(st, qd)).ToDictionaryEx(a => a.key);
return qt2;
}
private static QueryTokenType? GetQueryTokenType(QueryToken qt)
{
if (qt is AggregateToken)
return QueryTokenType.Aggregate;
if (qt is CollectionElementToken)
return QueryTokenType.Element;
if (qt is CollectionNestedToken)
return QueryTokenType.Nested;
if (qt is CollectionAnyAllToken)
return QueryTokenType.AnyOrAll;
if (qt is CollectionToArrayToken)
return QueryTokenType.ToArray;
if (qt is OperationsContainerToken)
return QueryTokenType.OperationContainer;
if (qt is ManualContainerToken or ManualToken)
return QueryTokenType.Manual;
if (qt is StringSnippetToken)
return QueryTokenType.Snippet;
if (qt is TimeSeriesToken)
return QueryTokenType.TimeSeries;
if (qt is IndexerContainerToken)
return QueryTokenType.IndexerContainer;
return null;
}
public required string fullKey;
public required string key;
public required string toStr;
public required string niceName;
public required string niceTypeName;
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public QueryTokenType? queryTokenType;
public required TypeReferenceTS type;
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public FilterType? filterType;
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string? format;
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string? unit;
public bool isGroupable;
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
public bool hasOrderAdapter;
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
public bool preferEquals;
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
public IReadOnlyList<string>? tsVectorFor;
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public QueryTokenTS? parent;
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string? propertyRoute;
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
public bool autoExpand;
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
public bool hideInAutoExpand;
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public Dictionary<string, QueryTokenTS>? subTokens;
}
public enum QueryTokenType
{
Aggregate,
Element,
AnyOrAll,
OperationContainer,
ToArray,
Manual,
Nested,
Snippet,
TimeSeries,
IndexerContainer,
}