forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIQuery.cs
More file actions
352 lines (289 loc) · 11.7 KB
/
IQuery.cs
File metadata and controls
352 lines (289 loc) · 11.7 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
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
namespace ServiceStack
{
//Interfaces and DTO's used in AutoQuery
public interface IQuery : IMeta
{
/// <summary>
/// How many results to skip
/// </summary>
int? Skip { get; set; }
/// <summary>
/// How many results to return
/// </summary>
int? Take { get; set; }
/// <summary>
/// List of fields to sort by, can order by multiple fields and inverse order, e.g: Id,-Amount
/// </summary>
string OrderBy { get; set; }
/// <summary>
/// List of fields to sort by descending, can order by multiple fields and inverse order, e.g: -Id,Amount
/// </summary>
string OrderByDesc { get; set; }
/// <summary>
/// Include aggregate data like Total, COUNT(*), COUNT(DISTINCT Field), Sum(Amount), etc
/// </summary>
string Include { get; set; }
/// <summary>
/// The fields to return
/// </summary>
string Fields { get; set; }
}
public interface IQueryDb : IQuery { }
public interface IQueryData : IQuery { }
public interface IQueryDb<From> : IQueryDb { }
public interface IQueryDb<From, Into> : IQueryDb { }
public interface IQueryData<From> : IQueryData { }
public interface IQueryData<From, Into> : IQueryData { }
public interface ICrud {}
public interface ICreateDb<Table> : ICrud {}
public interface IUpdateDb<Table> : ICrud {}
public interface IPatchDb<Table> : ICrud {}
public interface IDeleteDb<Table> : ICrud {}
public interface ISaveDb<Table> : ICrud {}
public interface IJoin { }
public interface IJoin<Source, Join1> : IJoin { }
public interface IJoin<Source, Join1, Join2> : IJoin { }
public interface IJoin<Source, Join1, Join2, Join3> : IJoin { }
public interface IJoin<Source, Join1, Join2, Join3, Join4> : IJoin { }
public interface ILeftJoin<Source, Join1> : IJoin { }
public interface ILeftJoin<Source, Join1, Join2> : IJoin { }
public interface ILeftJoin<Source, Join1, Join2, Join3> : IJoin { }
public interface ILeftJoin<Source, Join1, Join2, Join3, Join4> : IJoin { }
/// <summary>
/// How the filter should be applied to the query
/// </summary>
public enum QueryTerm
{
/// <summary>
/// Defaults to 'And'
/// </summary>
Default = 0,
/// <summary>
/// Apply filter to query using 'AND' to further filter resultset
/// </summary>
And = 1,
/// <summary>
/// Apply inclusive filter to query using 'OR' to further expand resultset
/// </summary>
Or = 2,
/// <summary>
/// Ensure filter is always applied even if other 'OR' filters are included (uses OrmLite's Ensure API)
/// </summary>
Ensure = 3,
}
/// <summary>
/// Type of Value used in the SQL Template
/// </summary>
public enum ValueStyle
{
/// <summary>
/// Standard SQL Condition, e.g: '{Field} = {Value}'
/// </summary>
Single = 0,
/// <summary>
/// SQL Template uses {ValueN} e.g. '{Field} BETWEEN {Value1} AND {Value2}'
/// </summary>
Multiple = 1,
/// <summary>
/// SQL Template uses collection parameter, e.g: '{Field} IN ({Values})'
/// </summary>
List = 2,
}
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
public class QueryDbAttribute : AttributeBase
{
public QueryDbAttribute() { }
public QueryDbAttribute(QueryTerm defaultTerm)
{
DefaultTerm = defaultTerm;
}
public QueryTerm DefaultTerm { get; set; }
}
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
public class QueryDataAttribute : AttributeBase
{
public QueryDataAttribute() { }
public QueryDataAttribute(QueryTerm defaultTerm)
{
DefaultTerm = defaultTerm;
}
public QueryTerm DefaultTerm { get; set; }
}
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
public class QueryDbFieldAttribute : AttributeBase
{
/// <summary>
/// Should this filter be applied with 'AND' or 'OR' or always filtered with 'Ensure'
/// </summary>
public QueryTerm Term { get; set; }
/// <summary>
/// For Simple Filters to change Operand used in default Template, e.g. For Greater Than: Operand=">"
/// </summary>
public string Operand { get; set; }
/// <summary>
/// Use a Custom SQL Filter, Use <see cref="SqlTemplate"/> for common templates, e.g: Template=SqlTemplate.IsNotNull
/// </summary>
public string Template { get; set; }
/// <summary>
/// The name of the DB Field to query
/// </summary>
public string Field { get; set; }
/// <summary>
/// Value modifier, e.g. implement StartsWith with 'Name LIKE {Value}', ValueFormat="{0}%"
/// </summary>
public string ValueFormat { get; set; }
/// <summary>
/// Type of Value used in the SQL Template
/// </summary>
public ValueStyle ValueStyle { get; set; }
public int ValueArity { get; set; }
}
/// <summary>
/// Apply additional Filter to AutoQuery Requests
/// </summary>
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)]
public class AutoFilterAttribute : ScriptValueAttribute
{
/// <summary>
/// Should this filter be applied with 'AND' or 'OR' or always filtered with 'Ensure'
/// </summary>
public QueryTerm Term { get; set; }
/// <summary>
/// The name of the DB Field to query
/// </summary>
public string Field { get; set; }
/// <summary>
/// For Simple Filters to change Operand used in default Template, e.g. For Greater Than: Operand=">"
/// </summary>
public string Operand { get; set; }
/// <summary>
/// Use a Custom SQL Filter, Use <see cref="SqlTemplate"/> for common templates, e.g: Template=SqlTemplate.IsNotNull
/// </summary>
public string Template { get; set; }
/// <summary>
/// Value modifier, e.g. implement StartsWith with 'Name LIKE {Value}', ValueFormat="{0}%"
/// </summary>
public string ValueFormat { get; set; }
public AutoFilterAttribute(string field) => Field = field ?? throw new ArgumentNullException(nameof(field));
public AutoFilterAttribute(string field, string template)
{
Field = field;
Template = template;
}
public AutoFilterAttribute(QueryTerm term, string field)
{
Term = term;
Field = field;
}
public AutoFilterAttribute(QueryTerm term, string field, string template)
{
Term = term;
Field = field;
Template = template;
}
}
/// <summary>
/// Common AutoQuery SQL Filter Templates
/// </summary>
public static class SqlTemplate
{
public const string IsNull = "{Field} IS NULL";
public const string IsNotNull = "{Field} IS NOT NULL";
public const string GreaterThanOrEqual = "{Field} >= {Value}";
public const string GreaterThan = "{Field} > {Value}";
public const string LessThan = "{Field} < {Value}";
public const string LessThanOrEqual = "{Field} <= {Value}";
public const string NotEqual = "{Field} <> {Value}";
public const string CaseSensitiveLike = "{Field} LIKE {Value}";
public const string CaseInsensitiveLike = "UPPER({Field}) LIKE UPPER({Value})";
}
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
public class QueryDataFieldAttribute : AttributeBase
{
public QueryTerm Term { get; set; }
public string Condition { get; set; }
public string Field { get; set; }
}
[DataContract]
public abstract class QueryBase : IQuery
{
[DataMember(Order = 1)]
public virtual int? Skip { get; set; }
[DataMember(Order = 2)]
public virtual int? Take { get; set; }
[DataMember(Order = 3)]
public virtual string OrderBy { get; set; }
[DataMember(Order = 4)]
public virtual string OrderByDesc { get; set; }
[DataMember(Order = 5)]
public virtual string Include { get; set; }
[DataMember(Order = 6)]
public virtual string Fields { get; set; }
[DataMember(Order = 7)]
public virtual Dictionary<string, string> Meta { get; set; }
// note: the number of fields here must fit inside the reserved chunk
// from GrpcServiceClient; see CreateMetaType
}
public abstract class QueryDb<T> : QueryBase, IQueryDb<T>, IReturn<QueryResponse<T>> { }
public abstract class QueryDb<From, Into> : QueryBase, IQueryDb<From, Into>, IReturn<QueryResponse<Into>> { }
public abstract class QueryData<T> : QueryBase, IQueryData<T>, IReturn<QueryResponse<T>> { }
public abstract class QueryData<From, Into> : QueryBase, IQueryData<From, Into>, IReturn<QueryResponse<Into>> { }
public interface IQueryResponse : IHasResponseStatus, IMeta
{
int Offset { get; set; }
int Total { get; set; }
}
[DataContract]
public class QueryResponse<T> : IQueryResponse
{
[DataMember(Order = 1)]
public virtual int Offset { get; set; }
/// <summary>
/// Populate with Include=Total or if registered with: AutoQueryFeature { IncludeTotal = true }
/// </summary>
[DataMember(Order = 2)]
public virtual int Total { get; set; }
[DataMember(Order = 3)]
public virtual List<T> Results { get; set; }
[DataMember(Order = 4)]
public virtual Dictionary<string, string> Meta { get; set; }
[DataMember(Order = 5)]
public virtual ResponseStatus ResponseStatus { get; set; }
}
/* AutoCrud */
public enum AutoUpdateStyle
{
Always,
NonDefaults,
}
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
public class AutoUpdateAttribute : AttributeBase
{
public AutoUpdateStyle Style { get; set; }
public AutoUpdateAttribute(AutoUpdateStyle style) => Style = style;
}
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
public class AutoDefaultAttribute : ScriptValueAttribute
{
}
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
public class AutoMapAttribute : AttributeBase
{
public string To { get; set; }
public AutoMapAttribute(string to) => To = to ?? throw new ArgumentNullException(nameof(to));
}
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)]
public class AutoPopulateAttribute : ScriptValueAttribute
{
/// <summary>
/// Name of Class Property to Populate
/// </summary>
public string Field { get; set; }
public AutoPopulateAttribute(string field) => Field = field ?? throw new ArgumentNullException(nameof(field));
}
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
public class AutoIgnoreAttribute : AttributeBase {}
}