/
EntityQueryExtensions.cs
175 lines (149 loc) · 6.04 KB
/
EntityQueryExtensions.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
namespace Serenity.Data;
/// <summary>
/// Extensions for objects implementing IDbWhere interface.</summary>
public static class EntityQueryExtensions
{
/// <summary>
/// Adds all field values in a row to where clause with equality operator and auto named parameters
/// (field name prefixed with '@').</summary>
/// <param field="row">
/// The row with modified field values to be added to the where clause (key row). Must be in TrackAssignments mode,
/// or an exception is raised.</param>
/// <returns>
/// Object itself.</returns>
public static T WhereEqual<T>(this T self, IRow row) where T : IFilterableQuery
{
if (row == null)
throw new ArgumentNullException("row");
if (!row.TrackAssignments)
throw new ArgumentException("row must be in TrackAssignments mode to determine modified fields.");
foreach (var field in row.Fields)
if (row.IsAssigned(field))
self.Where(new Criteria(field) == self.AddParam(field.AsSqlValue(row)));
return self;
}
/// <summary>
/// Sets all field values in a row with auto named parameters (field name prefixed with '@').</summary>
/// <param field="row">
/// The row with modified field values. Must be in TrackAssignments mode, or an exception is raised.</param>
/// <returns>
/// Object itself.</returns>
public static T Set<T>(this T self, IRow row, IField exclude = null) where T : ISetFieldByStatement
{
if (row == null)
throw new ArgumentNullException("row");
if (!row.TrackAssignments)
throw new ArgumentException("row must be in TrackAssignments mode to determine modified fields.");
foreach (var field in row.Fields)
if (!ReferenceEquals(field, exclude) && row.IsAssigned(field))
self.Set(field, field.AsSqlValue(row));
return self;
}
/// <summary>
/// Adds actual table fields in a row to select list of a query.</summary>
/// <param name="query">
/// Query to select fields into (required).</param>
/// <param name="row">
/// Row with fields to be selected (required).</param>
/// <param name="exclude">
/// Fields to be excluded (optional).</param>
public static SqlQuery SelectTableFields(this SqlQuery query, IRow row, params Field[] exclude)
{
if (query == null)
throw new ArgumentNullException("query");
if (row == null)
throw new ArgumentNullException("row");
HashSet<Field> excludeFields =
(exclude != null && exclude.Length > 0) ? new HashSet<Field>(exclude) : null;
var fields = row.Fields;
for (int i = 0; i < row.Fields.Count; i++)
{
Field field = fields[i];
if (EntityFieldExtensions.IsTableField(field))
{
if (excludeFields == null ||
!excludeFields.Contains(field))
query.Select(field);
}
}
return query;
}
/// <summary>
/// Adds foreign / calculated table fields in a row to select list of a query.</summary>
/// <param name="query">
/// Query to select fields into (required).</param>
/// <param name="row">
/// Row with fields to be selected (required).</param>
/// <param name="exclude">
/// Fields to be excluded (optional).</param>
public static SqlQuery SelectForeignFields(this SqlQuery query, IRow row, params Field[] exclude)
{
if (query == null)
throw new ArgumentNullException("query");
if (row == null)
throw new ArgumentNullException("row");
HashSet<Field> excludeFields =
(exclude != null && exclude.Length > 0) ? new HashSet<Field>(exclude) : null;
var fields = row.Fields;
for (int i = 0; i < fields.Count; i++)
{
Field field = fields[i];
if (!EntityFieldExtensions.IsTableField(field) &&
(field.Flags & FieldFlags.NotMapped) != FieldFlags.NotMapped)
{
if (excludeFields == null ||
!excludeFields.Contains(field))
query.Select(field);
}
}
return query;
}
/// <summary>
/// Adds foreign / calculated table fields in a row to select list of a query.</summary>
/// <param name="query">
/// Query to select fields into (required).</param>
public static SqlQuery SelectNonTableFields(this SqlQuery query)
{
if (query == null)
throw new ArgumentNullException("query");
var ext = (ISqlQueryExtensible)query;
foreach (var field in ((IRow)ext.FirstIntoRow).Fields)
{
if (!EntityFieldExtensions.IsTableField(field) &&
(field.Flags & FieldFlags.NotMapped) != FieldFlags.NotMapped)
{
query.Select(field);
}
}
return query;
}
/// <summary>
/// Adds actual table fields in a row to select list of a query.</summary>
/// <param name="query">
/// Query to select fields into (required).</param>
/// <param name="exclude">
/// Fields to be excluded (optional).</param>
public static SqlQuery SelectTableFields(this SqlQuery query, params Field[] exclude)
{
if (query == null)
throw new ArgumentNullException("query");
var ext = (ISqlQueryExtensible)query;
return SelectTableFields(query, (IRow)ext.FirstIntoRow, exclude);
}
/// <summary>
/// Sets a field value with a parameter.</summary>
/// <param field="field">
/// Field name.</param>
/// <param field="param">
/// Parameter name</param>
/// <param field="value">
/// Parameter value</param>
/// <returns>
/// Object itself.</returns>
public static T Set<T>(this T self, IField field, object value) where T : ISetFieldByStatement
{
var param = self.AddParam(value);
self.SetTo(field.Name, param.Name);
return self;
}
}