Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
InnerJoinedSource.cs
1using System;
3using System.Threading.Tasks;
9
11{
16 {
24 : base(Left, Right, Conditions)
25 {
26 }
27
39 public override async Task<IResultSetEnumerator> Find(int Offset, int Top, bool Generic, ScriptNode Where, Variables Variables,
40 KeyValuePair<VariableReference, bool>[] Order, ScriptNode Node)
41 {
42 ScriptNode LeftWhere = await Reduce(this.Left, Where);
43 KeyValuePair<VariableReference, bool>[] LeftOrder = await Reduce(this.Left, Order);
44
45 IResultSetEnumerator e = await this.Left.Find(0, int.MaxValue, Generic, LeftWhere, Variables, LeftOrder, Node);
46
47 ScriptNode RightWhere = await Reduce(this.Right, this.Left, Where);
48 RightWhere = this.Combine(RightWhere, this.Conditions);
49
50 e = new InnerJoinEnumerator(e, this.Left.Name, this.Right, this.Right.Name, Generic, RightWhere, Variables);
51
52 if (!(Where is null))
53 e = new ConditionalEnumerator(e, Variables, Where);
54
55 if (Offset > 0)
56 e = new OffsetEnumerator(e, Offset);
57
58 if (Top != int.MaxValue)
59 e = new MaxCountEnumerator(e, Top);
60
61 return e;
62 }
63
64 private class InnerJoinEnumerator : IResultSetEnumerator
65 {
66 private readonly IResultSetEnumerator left;
67 private readonly IDataSource rightSource;
68 private readonly ScriptNode conditions;
69 private readonly Variables variables;
70 private readonly string leftName;
71 private readonly string rightName;
72 private readonly bool hasLeftName;
73 private readonly bool generic;
74 private IResultSetEnumerator right;
75 private JoinedObject current = null;
76 private ObjectProperties leftVariables = null;
77
78 public InnerJoinEnumerator(IResultSetEnumerator Left, string LeftName, IDataSource RightSource, string RightName,
80 {
81 this.left = Left;
82 this.leftName = LeftName;
83 this.rightName = RightName;
84 this.rightSource = RightSource;
85 this.generic = Generic;
86 this.conditions = Conditions;
87 this.variables = Variables;
88 this.hasLeftName = !string.IsNullOrEmpty(this.leftName);
89 }
90
91 public object Current => this.current;
92
93 public void Dispose()
94 {
95 this.left.Dispose();
96 }
97
98 public bool MoveNext()
99 {
100 return this.MoveNextAsync().Result;
101 }
102
103 public async Task<bool> MoveNextAsync()
104 {
105 while (true)
106 {
107 if (!(this.right is null))
108 {
109 if (await this.right.MoveNextAsync())
110 {
111 this.current = new JoinedObject(this.left.Current, this.leftName,
112 this.right.Current, this.rightName);
113
114 return true;
115 }
116 else
117 this.right = null;
118 }
119
120 if (!await this.left.MoveNextAsync())
121 return false;
122
123 if (this.leftVariables is null)
124 this.leftVariables = new ObjectProperties(this.left.Current, this.variables);
125 else
126 this.leftVariables.Object = this.left.Current;
127
128 if (this.hasLeftName)
129 this.leftVariables[this.leftName] = this.left.Current;
130
131 this.right = await this.rightSource.Find(0, int.MaxValue, this.generic, this.conditions, this.leftVariables,
132 null, this.conditions);
133 }
134 }
135
136 public void Reset()
137 {
138 this.current = null;
139 this.right = null;
140 this.left.Reset();
141 }
142 }
143
157 public override async Task<bool> Process(IProcessor<object> Processor, int Offset, int Top, bool Generic,
158 ScriptNode Where, Variables Variables, KeyValuePair<VariableReference, bool>[] Order,
159 ScriptNode Node)
160 {
161 ScriptNode LeftWhere = await Reduce(this.Left, Where);
162 KeyValuePair<VariableReference, bool>[] LeftOrder = await Reduce(this.Left, Order);
163
164 ScriptNode RightWhere = await Reduce(this.Right, this.Left, Where);
165 RightWhere = this.Combine(RightWhere, this.Conditions);
166
167 if (Top != int.MaxValue)
168 Processor = new MaxCountProcessor(Processor, Top);
169
170 if (Offset > 0)
171 Processor = new OffsetProcessor(Processor, Offset);
172
173 if (!(Where is null))
174 Processor = new ConditionalProcessor(Processor, Variables, Where);
175
176 Processor = new InnerJoinLeftProcessor(Processor, this.Left.Name, this.Right,
177 this.Right.Name, Generic, RightWhere, Variables);
178
179 return await this.Left.Process(Processor, 0, int.MaxValue, Generic,
180 LeftWhere, Variables, LeftOrder, Node);
181 }
182
183 private class InnerJoinLeftProcessor : IProcessor<object>
184 {
185 private readonly InnerJoinRightProcessor rightProcessor;
186 private readonly IDataSource rightSource;
187 private readonly ScriptNode conditions;
188 private readonly Variables variables;
189 private readonly string leftName;
190 private readonly bool hasLeftName;
191 private readonly bool generic;
192 private ObjectProperties leftVariables = null;
193
194 public InnerJoinLeftProcessor(IProcessor<object> Processor, string LeftName,
195 IDataSource RightSource, string RightName, bool Generic,
197 {
198 this.rightProcessor = new InnerJoinRightProcessor(Processor, LeftName, RightName);
199 this.leftName = LeftName;
200 this.rightSource = RightSource;
201 this.generic = Generic;
202 this.conditions = Conditions;
203 this.variables = Variables;
204 this.hasLeftName = !string.IsNullOrEmpty(this.leftName);
205 }
206
207 public bool IsAsynchronous => true;
208 public bool Process(object Object) => this.ProcessAsync(Object).Result;
209 public bool Flush() => this.rightProcessor.Flush();
210 public Task<bool> FlushAsync() => this.rightProcessor.FlushAsync();
211
212 public async Task<bool> ProcessAsync(object Object)
213 {
214 if (this.leftVariables is null)
215 this.leftVariables = new ObjectProperties(Object, this.variables);
216 else
217 this.leftVariables.Object = Object;
218
219 if (this.hasLeftName)
220 this.leftVariables[this.leftName] = Object;
221
222 this.rightProcessor.CurrentLeft = Object;
223
224 return await this.rightSource.Process(this.rightProcessor, 0, int.MaxValue,
225 this.generic, this.conditions, this.leftVariables, null, this.conditions);
226 }
227 }
228
229 private class InnerJoinRightProcessor : IProcessor<object>
230 {
231 private readonly IProcessor<object> processor;
232 private readonly string leftName;
233 private readonly string rightName;
234
235 public InnerJoinRightProcessor(IProcessor<object> Processor, string LeftName,
236 string RightName)
237 {
238 this.processor = Processor;
239 this.leftName = LeftName;
240 this.rightName = RightName;
241 }
242
243 public object CurrentLeft { get; set; }
244
245 public bool IsAsynchronous => this.processor.IsAsynchronous;
246 public bool Flush() => this.processor.Flush();
247 public Task<bool> FlushAsync() => this.processor.FlushAsync();
248
249 public bool Process(object Object)
250 {
251 return this.processor.Process(new JoinedObject(this.CurrentLeft,
252 this.leftName, Object, this.rightName));
253 }
254
255 public Task<bool> ProcessAsync(object Object)
256 {
257 return this.processor.ProcessAsync(new JoinedObject(this.CurrentLeft,
258 this.leftName, Object, this.rightName));
259 }
260 }
261
262 }
263}
Base class for all nodes in a parsed script tree.
Definition: ScriptNode.cs:69
Enumerator that only returns elements matching a set of conditions.
Enumerator that limits the return set to a maximum number of records.
Enumerator that skips a given number of result records.
Processor that only processes elements matching a set of conditions.
Processor that limits the return set to a maximum number of records.
Processor that skips a given number of result records.
Data source formed through an INNER JOIN of two sources.
InnerJoinedSource(IDataSource Left, IDataSource Right, ScriptNode Conditions)
Data source formed through an INNER JOIN of two sources.
override async 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 .
override async 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 .
Abstract base classes of joined sources.
Definition: JoinedSource.cs:15
ScriptNode Conditions
Conditions for join.
Definition: JoinedSource.cs:46
ScriptNode Combine(ScriptNode Where, ScriptNode On)
Combines one or two restrictions.
static async Task< ScriptNode > Reduce(IDataSource Source, ScriptNode Where)
Reduces a where clause to fit the current data source.
Collection of variables.
Definition: Variables.cs:25
Task< bool > MoveNextAsync()
Advances the enumerator to the next element of the collection.
Interface for processors of objects.
Definition: IProcessor.cs:9
bool IsAsynchronous
If the processor operates asynchronously.
Definition: IProcessor.cs:13
Task< bool > ProcessAsync(T Object)
Processes an object asynchronously.
Interface for data sources that can be used in SQL statements.
Definition: IDataSource.cs:14
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 .
string Name
Collection name or alias.
Definition: IDataSource.cs:94