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;
3using System.Threading.Tasks;
8
10{
14 public abstract class JoinedSource : IDataSource
15 {
16 private readonly IDataSource left;
17 private readonly IDataSource right;
18 private readonly ScriptNode conditions;
19
27 {
28 this.left = Left;
29 this.right = Right;
30 this.conditions = Conditions;
31 }
32
36 public IDataSource Left => this.left;
37
41 public IDataSource Right => this.right;
42
46 public ScriptNode Conditions => this.conditions;
47
59 public abstract Task<IResultSetEnumerator> Find(int Offset, int Top, bool Generic, ScriptNode Where, Variables Variables,
60 KeyValuePair<VariableReference, bool>[] Order, ScriptNode Node);
61
75 public abstract Task<bool> Process(IProcessor<object> Processor, int Offset, int Top, bool Generic,
76 ScriptNode Where, Variables Variables, KeyValuePair<VariableReference, bool>[] Order,
77 ScriptNode Node);
78
84 public Task Update(bool Lazy, IEnumerable<object> Objects)
85 {
86 throw InvalidOperation();
87 }
88
89 private static Exception InvalidOperation()
90 {
91 return new InvalidOperationException("Operation not permitted on joined sources.");
92 }
93
105 public Task<int?> Delete(bool Lazy, int Offset, int Top, ScriptNode Where, Variables Variables,
106 KeyValuePair<VariableReference, bool>[] Order, ScriptNode Node)
107 {
108 throw InvalidOperation();
109 }
110
116 public Task Insert(bool Lazy, object Object)
117 {
118 throw InvalidOperation();
119 }
120
124 public string CollectionName
125 {
126 get => throw InvalidOperation();
127 }
128
132 public string TypeName
133 {
134 get => throw InvalidOperation();
135 }
136
140 public string Name => string.Empty;
141
147 public bool IsSource(string Name)
148 {
149 return this.left.IsSource(Name) || this.right.IsSource(Name);
150 }
151
157 public async Task<bool> IsLabel(string Label)
158 {
159 return
160 await this.left.IsLabel(Label) ||
161 await this.right.IsLabel(Label);
162 }
163
171 {
172 if (Where is null)
173 return On;
174 else if (On is null)
175 return Where;
176 else
177 return new Operators.Logical.And(Where, On, 0, 0, Where.Expression);
178 }
179
186 protected static async Task<ScriptNode> Reduce(IDataSource Source, ScriptNode Where)
187 {
188 KeyValuePair<ScriptNode, int> P = await Reduce(Source, null, Where, 1);
189 return P.Key;
190 }
191
199 protected static async Task<ScriptNode> Reduce(IDataSource Source, IDataSource Source2, ScriptNode Where)
200 {
201 KeyValuePair<ScriptNode, int> P = await Reduce(Source, Source2, Where, 1);
202 return P.Key;
203 }
204
213 private static async Task<KeyValuePair<ScriptNode, int>> Reduce(IDataSource Source, IDataSource Source2, ScriptNode Where, int Mask)
214 {
215 KeyValuePair<ScriptNode, int> P1, P2;
216 ScriptNode Op1;
217 ScriptNode Op2;
218 int s1;
219 int s2;
220 int s;
221
222 if (Where is null)
223 return Null;
224 else if (Where is Operators.Comparisons.Range Range)
225 {
226 if (Range.LeftInclusive)
227 Op1 = new Operators.Comparisons.LesserThanOrEqualTo(Range.LeftOperand, Range.MiddleOperand, Range.Start, Range.Length, Range.Expression);
228 else
229 Op1 = new Operators.Comparisons.LesserThan(Range.LeftOperand, Range.MiddleOperand, Range.Start, Range.Length, Range.Expression);
230
231 if (Range.RightInclusive)
232 Op2 = new Operators.Comparisons.GreaterThanOrEqualTo(Range.MiddleOperand, Range.RightOperand, Range.Start, Range.Length, Range.Expression);
233 else
234 Op2 = new Operators.Comparisons.GreaterThan(Range.MiddleOperand, Range.RightOperand, Range.Start, Range.Length, Range.Expression);
235
236 return await Reduce(Source, Source2, new Operators.Logical.And(Op1, Op2, Range.Start, Range.Length, Range.Expression), Mask);
237 }
238 else if (Where is BinaryOperator BinOp)
239 {
240 P1 = await Reduce(Source, Source2, BinOp.LeftOperand, Mask);
241 Op1 = P1.Key;
242 s1 = P1.Value;
243
244 P2 = await Reduce(Source, Source2, BinOp.RightOperand, Mask);
245 Op2 = P2.Key;
246 s2 = P2.Value;
247
248 s = s1 | s2;
249
250 if ((s & Mask) == 0)
251 return Null;
252
253 bool IsAnd = BinOp is Operators.Logical.And || BinOp is Operators.Dual.And;
254 bool IsOr = !IsAnd && (Where is Operators.Logical.Or || Where is Operators.Dual.Or);
255
256 if (IsAnd || IsOr)
257 {
258 if (Op1 is null)
259 return P2;
260 else if (Op2 is null)
261 return P1;
262 else if (IsAnd)
263 return new KeyValuePair<ScriptNode, int>(new Operators.Logical.And(Op1, Op2, 0, 0, BinOp.Expression), s);
264 else if (IsOr)
265 return new KeyValuePair<ScriptNode, int>(new Operators.Logical.Or(Op1, Op2, 0, 0, BinOp.Expression), s);
266 }
267 else if (Op1 is null || Op2 is null)
268 return Null;
269 else if (Op1 == BinOp.LeftOperand && Op2 == BinOp.RightOperand)
270 return new KeyValuePair<ScriptNode, int>(Where, s);
271 else if (BinOp is Operators.Comparisons.EqualTo)
272 return new KeyValuePair<ScriptNode, int>(new Operators.Comparisons.EqualTo(Op1, Op2, 0, 0, BinOp.Expression), s);
273 else if (BinOp is Operators.Comparisons.GreaterThan)
274 return new KeyValuePair<ScriptNode, int>(new Operators.Comparisons.GreaterThan(Op1, Op2, 0, 0, BinOp.Expression), s);
275 else if (BinOp is Operators.Comparisons.GreaterThanOrEqualTo)
276 return new KeyValuePair<ScriptNode, int>(new Operators.Comparisons.GreaterThanOrEqualTo(Op1, Op2, 0, 0, BinOp.Expression), s);
277 else if (BinOp is Operators.Comparisons.LesserThan)
278 return new KeyValuePair<ScriptNode, int>(new Operators.Comparisons.LesserThan(Op1, Op2, 0, 0, BinOp.Expression), s);
279 else if (BinOp is Operators.Comparisons.LesserThanOrEqualTo)
280 return new KeyValuePair<ScriptNode, int>(new Operators.Comparisons.LesserThanOrEqualTo(Op1, Op2, 0, 0, BinOp.Expression), s);
281 else if (BinOp is Operators.Comparisons.Like)
282 return new KeyValuePair<ScriptNode, int>(new Operators.Comparisons.Like(Op1, Op2, 0, 0, BinOp.Expression), s);
283 else if (BinOp is Operators.Comparisons.NotEqualTo)
284 return new KeyValuePair<ScriptNode, int>(new Operators.Comparisons.NotEqualTo(Op1, Op2, 0, 0, BinOp.Expression), s);
285 else if (BinOp is Operators.Comparisons.NotLike)
286 return new KeyValuePair<ScriptNode, int>(new Operators.Comparisons.NotLike(Op1, Op2, 0, 0, BinOp.Expression), s);
287 }
288 else if (Where is Operators.Membership.NamedMember N)
289 {
290 if (N.Operand is VariableReference Ref)
291 {
292 if (Source.IsSource(Ref.VariableName))
293 return new KeyValuePair<ScriptNode, int>(N, 1);
294 else if (Source2?.IsSource(Ref.VariableName) ?? false)
295 return new KeyValuePair<ScriptNode, int>(N, 2);
296 else
297 return Null;
298 }
299 else
300 return Null;
301 }
302 else if (Where is UnaryOperator UnOp)
303 {
304 P1 = await Reduce(Source, Source2, UnOp.Operand, Mask);
305 Op1 = P1.Key;
306 s1 = P1.Value;
307
308 if (Op1 is null)
309 return Null;
310
311 if (UnOp is Operators.Logical.Not)
312 return new KeyValuePair<ScriptNode, int>(new Operators.Logical.Not(Op1, 0, 0, UnOp.Expression), s1);
313 }
314 else if (Where is VariableReference Ref)
315 {
316 if (await Source.IsLabel(Ref.VariableName))
317 return new KeyValuePair<ScriptNode, int>(Ref, 1);
318 else if (!(Source2 is null) && await Source2.IsLabel(Ref.VariableName))
319 return new KeyValuePair<ScriptNode, int>(Ref, 2);
320 else
321 return Null;
322 }
323 else if (Where is ConstantElement C)
324 return new KeyValuePair<ScriptNode, int>(C, 0);
325
326 return Null;
327 }
328
329 private static readonly KeyValuePair<ScriptNode, int> Null = new KeyValuePair<ScriptNode, int>(null, 0);
330
337 protected static async Task<KeyValuePair<VariableReference, bool>[]> Reduce(IDataSource Source,
338 KeyValuePair<VariableReference, bool>[] Order)
339 {
340 if (Order is null)
341 return null;
342
343 int i, c = Order.Length;
344
345 for (i = 0; i < c; i++)
346 {
347 if (!await Source.IsLabel(Order[i].Key.VariableName))
348 break;
349 }
350
351 if (i != c)
352 Array.Resize(ref Order, c);
353
354 return Order;
355 }
356
362 public Task CreateIndex(string Name, string[] Fields)
363 {
364 throw InvalidOperation();
365 }
366
372 public Task<bool> DropIndex(string Name)
373 {
374 throw InvalidOperation();
375 }
376
380 public Task DropCollection()
381 {
382 throw InvalidOperation();
383 }
384
385 }
386}
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:15
JoinedSource(IDataSource Left, IDataSource Right, ScriptNode Conditions)
Abstract base classes of joined sources.
Definition: JoinedSource.cs:26
Task Insert(bool Lazy, object Object)
Inserts an object.
ScriptNode Conditions
Conditions for join.
Definition: JoinedSource.cs:46
Task Update(bool Lazy, IEnumerable< object > Objects)
Updates a set of objects.
Definition: JoinedSource.cs:84
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 specified by the source.
abstract Task< bool > Process(IProcessor< object > Processor, int Offset, int Top, bool Generic, ScriptNode Where, Variables Variables, KeyValuePair< VariableReference, bool >[] Order, ScriptNode Node)
Processes objects matching filter conditions in Where .
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?> Delete(bool Lazy, int Offset, int Top, ScriptNode Where, Variables Variables, KeyValuePair< VariableReference, bool >[] Order, ScriptNode Node)
Deletes a set of objects.
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 processors of objects.
Definition: IProcessor.cs:9
Interface for data sources that can be used in SQL statements.
Definition: IDataSource.cs:14
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.