Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
JoinedSource.cs
1using System;
2using System.Collections.Generic;
3using System.Threading.Tasks;
7
9{
13 public abstract class JoinedSource : IDataSource
14 {
15 private readonly IDataSource left;
16 private readonly IDataSource right;
17 private readonly ScriptNode conditions;
18
26 {
27 this.left = Left;
28 this.right = Right;
29 this.conditions = Conditions;
30 }
31
35 public IDataSource Left => this.left;
36
40 public IDataSource Right => this.right;
41
45 public ScriptNode Conditions => this.conditions;
46
58 public abstract Task<IResultSetEnumerator> Find(int Offset, int Top, bool Generic, ScriptNode Where, Variables Variables,
59 KeyValuePair<VariableReference, bool>[] Order, ScriptNode Node);
60
66 public Task Update(bool Lazy, IEnumerable<object> Objects)
67 {
68 throw InvalidOperation();
69 }
70
71 private static Exception InvalidOperation()
72 {
73 return new InvalidOperationException("Operation not permitted on joined sources.");
74 }
75
87 public Task<int?> FindDelete(bool Lazy, int Offset, int Top, ScriptNode Where, Variables Variables,
88 KeyValuePair<VariableReference, bool>[] Order, ScriptNode Node)
89 {
90 throw InvalidOperation();
91 }
92
98 public Task Insert(bool Lazy, object Object)
99 {
100 throw InvalidOperation();
101 }
102
106 public string CollectionName
107 {
108 get => throw InvalidOperation();
109 }
110
114 public string TypeName
115 {
116 get => throw InvalidOperation();
117 }
118
122 public string Name => string.Empty;
123
129 public bool IsSource(string Name)
130 {
131 return this.left.IsSource(Name) || this.right.IsSource(Name);
132 }
133
139 public async Task<bool> IsLabel(string Label)
140 {
141 return
142 await this.left.IsLabel(Label) ||
143 await this.right.IsLabel(Label);
144 }
145
153 {
154 if (Where is null)
155 return On;
156 else if (On is null)
157 return Where;
158 else
159 return new Operators.Logical.And(Where, On, 0, 0, Where.Expression);
160 }
161
168 protected static async Task<ScriptNode> Reduce(IDataSource Source, ScriptNode Where)
169 {
170 KeyValuePair<ScriptNode, int> P = await Reduce(Source, null, Where, 1);
171 return P.Key;
172 }
173
181 protected static async Task<ScriptNode> Reduce(IDataSource Source, IDataSource Source2, ScriptNode Where)
182 {
183 KeyValuePair<ScriptNode, int> P = await Reduce(Source, Source2, Where, 1);
184 return P.Key;
185 }
186
195 private static async Task<KeyValuePair<ScriptNode, int>> Reduce(IDataSource Source, IDataSource Source2, ScriptNode Where, int Mask)
196 {
197 KeyValuePair<ScriptNode, int> P1, P2;
198 ScriptNode Op1;
199 ScriptNode Op2;
200 int s1;
201 int s2;
202 int s;
203
204 if (Where is null)
205 return Null;
206 else if (Where is Operators.Comparisons.Range Range)
207 {
208 if (Range.LeftInclusive)
209 Op1 = new Operators.Comparisons.LesserThanOrEqualTo(Range.LeftOperand, Range.MiddleOperand, Range.Start, Range.Length, Range.Expression);
210 else
211 Op1 = new Operators.Comparisons.LesserThan(Range.LeftOperand, Range.MiddleOperand, Range.Start, Range.Length, Range.Expression);
212
213 if (Range.RightInclusive)
214 Op2 = new Operators.Comparisons.GreaterThanOrEqualTo(Range.MiddleOperand, Range.RightOperand, Range.Start, Range.Length, Range.Expression);
215 else
216 Op2 = new Operators.Comparisons.GreaterThan(Range.MiddleOperand, Range.RightOperand, Range.Start, Range.Length, Range.Expression);
217
218 return await Reduce(Source, Source2, new Operators.Logical.And(Op1, Op2, Range.Start, Range.Length, Range.Expression), Mask);
219 }
220 else if (Where is BinaryOperator BinOp)
221 {
222 P1 = await Reduce(Source, Source2, BinOp.LeftOperand, Mask);
223 Op1 = P1.Key;
224 s1 = P1.Value;
225
226 P2 = await Reduce(Source, Source2, BinOp.RightOperand, Mask);
227 Op2 = P2.Key;
228 s2 = P2.Value;
229
230 s = s1 | s2;
231
232 if ((s & Mask) == 0)
233 return Null;
234
235 bool IsAnd = BinOp is Operators.Logical.And || BinOp is Operators.Dual.And;
236 bool IsOr = !IsAnd && (Where is Operators.Logical.Or || Where is Operators.Dual.Or);
237
238 if (IsAnd || IsOr)
239 {
240 if (Op1 is null)
241 return P2;
242 else if (Op2 is null)
243 return P1;
244 else if (IsAnd)
245 return new KeyValuePair<ScriptNode, int>(new Operators.Logical.And(Op1, Op2, 0, 0, BinOp.Expression), s);
246 else if (IsOr)
247 return new KeyValuePair<ScriptNode, int>(new Operators.Logical.Or(Op1, Op2, 0, 0, BinOp.Expression), s);
248 }
249 else if (Op1 is null || Op2 is null)
250 return Null;
251 else if (Op1 == BinOp.LeftOperand && Op2 == BinOp.RightOperand)
252 return new KeyValuePair<ScriptNode, int>(Where, s);
253 else if (BinOp is Operators.Comparisons.EqualTo)
254 return new KeyValuePair<ScriptNode, int>(new Operators.Comparisons.EqualTo(Op1, Op2, 0, 0, BinOp.Expression), s);
255 else if (BinOp is Operators.Comparisons.GreaterThan)
256 return new KeyValuePair<ScriptNode, int>(new Operators.Comparisons.GreaterThan(Op1, Op2, 0, 0, BinOp.Expression), s);
257 else if (BinOp is Operators.Comparisons.GreaterThanOrEqualTo)
258 return new KeyValuePair<ScriptNode, int>(new Operators.Comparisons.GreaterThanOrEqualTo(Op1, Op2, 0, 0, BinOp.Expression), s);
259 else if (BinOp is Operators.Comparisons.LesserThan)
260 return new KeyValuePair<ScriptNode, int>(new Operators.Comparisons.LesserThan(Op1, Op2, 0, 0, BinOp.Expression), s);
261 else if (BinOp is Operators.Comparisons.LesserThanOrEqualTo)
262 return new KeyValuePair<ScriptNode, int>(new Operators.Comparisons.LesserThanOrEqualTo(Op1, Op2, 0, 0, BinOp.Expression), s);
263 else if (BinOp is Operators.Comparisons.Like)
264 return new KeyValuePair<ScriptNode, int>(new Operators.Comparisons.Like(Op1, Op2, 0, 0, BinOp.Expression), s);
265 else if (BinOp is Operators.Comparisons.NotEqualTo)
266 return new KeyValuePair<ScriptNode, int>(new Operators.Comparisons.NotEqualTo(Op1, Op2, 0, 0, BinOp.Expression), s);
267 else if (BinOp is Operators.Comparisons.NotLike)
268 return new KeyValuePair<ScriptNode, int>(new Operators.Comparisons.NotLike(Op1, Op2, 0, 0, BinOp.Expression), s);
269 }
270 else if (Where is Operators.Membership.NamedMember N)
271 {
272 if (N.Operand is VariableReference Ref)
273 {
274 if (Source.IsSource(Ref.VariableName))
275 return new KeyValuePair<ScriptNode, int>(N, 1);
276 else if (Source2?.IsSource(Ref.VariableName) ?? false)
277 return new KeyValuePair<ScriptNode, int>(N, 2);
278 else
279 return Null;
280 }
281 else
282 return Null;
283 }
284 else if (Where is UnaryOperator UnOp)
285 {
286 P1 = await Reduce(Source, Source2, UnOp.Operand, Mask);
287 Op1 = P1.Key;
288 s1 = P1.Value;
289
290 if (Op1 is null)
291 return Null;
292
293 if (UnOp is Operators.Logical.Not)
294 return new KeyValuePair<ScriptNode, int>(new Operators.Logical.Not(Op1, 0, 0, UnOp.Expression), s1);
295 }
296 else if (Where is VariableReference Ref)
297 {
298 if (await Source.IsLabel(Ref.VariableName))
299 return new KeyValuePair<ScriptNode, int>(Ref, 1);
300 else if (!(Source2 is null) && await Source2.IsLabel(Ref.VariableName))
301 return new KeyValuePair<ScriptNode, int>(Ref, 2);
302 else
303 return Null;
304 }
305 else if (Where is ConstantElement C)
306 return new KeyValuePair<ScriptNode, int>(C, 0);
307
308 return Null;
309 }
310
311 private static readonly KeyValuePair<ScriptNode, int> Null = new KeyValuePair<ScriptNode, int>(null, 0);
312
319 protected static async Task<KeyValuePair<VariableReference, bool>[]> Reduce(IDataSource Source,
320 KeyValuePair<VariableReference, bool>[] Order)
321 {
322 if (Order is null)
323 return null;
324
325 int i, c = Order.Length;
326
327 for (i = 0; i < c; i++)
328 {
329 if (!await Source.IsLabel(Order[i].Key.VariableName))
330 break;
331 }
332
333 if (i != c)
334 Array.Resize(ref Order, c);
335
336 return Order;
337 }
338
344 public Task CreateIndex(string Name, string[] Fields)
345 {
346 throw InvalidOperation();
347 }
348
354 public Task<bool> DropIndex(string Name)
355 {
356 throw InvalidOperation();
357 }
358
362 public Task DropCollection()
363 {
364 throw InvalidOperation();
365 }
366
367 }
368}
Base class for all binary operators.
Represents a constant element value.
Base class for all nodes in a parsed script tree.
Definition: ScriptNode.cs:69
Expression Expression
Expression of which the node is a part.
Definition: ScriptNode.cs:177
Base class for all unary operators.
Represents a variable reference.
And(ScriptNode Left, ScriptNode Right, int Start, int Length, Expression Expression)
Logical And.
Definition: And.cs:22
Abstract base classes of joined sources.
Definition: JoinedSource.cs:14
JoinedSource(IDataSource Left, IDataSource Right, ScriptNode Conditions)
Abstract base classes of joined sources.
Definition: JoinedSource.cs:25
Task Insert(bool Lazy, object Object)
Inserts an object.
Definition: JoinedSource.cs:98
ScriptNode Conditions
Conditions for join.
Definition: JoinedSource.cs:45
Task Update(bool Lazy, IEnumerable< object > Objects)
Updates a set of objects.
Definition: JoinedSource.cs:66
static async Task< ScriptNode > Reduce(IDataSource Source, IDataSource Source2, ScriptNode Where)
Reduces a where clause to fit the current data sources.
Task DropCollection()
Drops the collection from the source.
string CollectionName
Name of corresponding collection.
Task< bool > DropIndex(string Name)
Drops an index from the source.
ScriptNode Combine(ScriptNode Where, ScriptNode On)
Combines one or two restrictions.
abstract Task< IResultSetEnumerator > Find(int Offset, int Top, bool Generic, ScriptNode Where, Variables Variables, KeyValuePair< VariableReference, bool >[] Order, ScriptNode Node)
Finds objects matching filter conditions in Where .
Task CreateIndex(string Name, string[] Fields)
Creates an index in the source.
Task< int?> FindDelete(bool Lazy, int Offset, int Top, ScriptNode Where, Variables Variables, KeyValuePair< VariableReference, bool >[] Order, ScriptNode Node)
Finds and Deletes a set of objects.
Definition: JoinedSource.cs:87
static async Task< ScriptNode > Reduce(IDataSource Source, ScriptNode Where)
Reduces a where clause to fit the current data source.
async Task< bool > IsLabel(string Label)
Checks if the label is a label in the source.
bool IsSource(string Name)
Checks if the name refers to the source.
static async Task< KeyValuePair< VariableReference, bool >[]> Reduce(IDataSource Source, KeyValuePair< VariableReference, bool >[] Order)
Reduces a sort order clause to fit the current data source.
string TypeName
Name of corresponding type.
Collection of variables.
Definition: Variables.cs:25
Interface for data sources that can be used in SQL statements.
Definition: IDataSource.cs:13
bool IsSource(string Name)
Checks if the name refers to the source.
Task< bool > IsLabel(string Label)
Checks if the label is a label in the source.