Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
LeftOuterJoinedSource.cs
1using System;
2using System.Collections.Generic;
3using System.Threading.Tasks;
7
9{
14 {
22 : base(Left, Right, Conditions)
23 {
24 }
25
29 protected virtual bool Flipped => false;
30
42 public override async Task<IResultSetEnumerator> Find(int Offset, int Top, bool Generic, ScriptNode Where, Variables Variables,
43 KeyValuePair<VariableReference, bool>[] Order, ScriptNode Node)
44 {
45 ScriptNode LeftWhere = await Reduce(this.Left, Where);
46 KeyValuePair<VariableReference, bool>[] LeftOrder = await Reduce(this.Left, Order);
47
48 IResultSetEnumerator e = await this.Left.Find(0, int.MaxValue, Generic, LeftWhere, Variables, LeftOrder, Node);
49
50 ScriptNode RightWhere = this.Combine(await Reduce(this.Right, this.Left, Where), this.Conditions);
51
52 e = new LeftOuterJoinEnumerator(e, this.Left.Name, this.Right, this.Right.Name, Generic, RightWhere, Variables, this.Flipped);
53
54 if (!(Where is null))
55 e = new ConditionalEnumerator(e, Variables, Where);
56
57 if (Offset > 0)
58 e = new OffsetEnumerator(e, Offset);
59
60 if (Top != int.MaxValue)
61 e = new MaxCountEnumerator(e, Top);
62
63 return e;
64 }
65
66 internal class LeftOuterJoinEnumerator : IResultSetEnumerator
67 {
68 private readonly IResultSetEnumerator left;
69 private readonly IDataSource rightSource;
70 private readonly ScriptNode conditions;
71 private readonly Variables variables;
72 private readonly string leftName;
73 private readonly string rightName;
74 private readonly bool hasLeftName;
75 private readonly bool flipped;
76 private readonly bool generic;
77 private bool rightFirst;
78 private IResultSetEnumerator right;
79 private JoinedObject current = null;
80 private GenericObject defaultRight = null;
81 private ObjectProperties leftVariables = null;
82
83 public LeftOuterJoinEnumerator(IResultSetEnumerator Left, string LeftName, IDataSource RightSource, string RightName,
84 bool Generic, ScriptNode Conditions, Variables Variables, bool Flipped)
85 {
86 this.left = Left;
87 this.leftName = LeftName;
88 this.rightName = RightName;
89 this.rightSource = RightSource;
90 this.generic = Generic;
91 this.conditions = Conditions;
92 this.variables = Variables;
93 this.hasLeftName = !string.IsNullOrEmpty(this.leftName);
94 this.flipped = Flipped;
95 }
96
97 public object Current => this.current;
98
99 public bool MoveNext()
100 {
101 return this.MoveNextAsync().Result;
102 }
103
104 public async Task<bool> MoveNextAsync()
105 {
106 while (true)
107 {
108 if (!(this.right is null))
109 {
110 bool First = this.rightFirst;
111 this.rightFirst = false;
112
113 if (await this.right.MoveNextAsync())
114 {
115 if (this.flipped)
116 this.current = new JoinedObject(this.right.Current, this.rightName, this.left.Current, this.leftName);
117 else
118 this.current = new JoinedObject(this.left.Current, this.leftName, this.right.Current, this.rightName);
119
120 return true;
121 }
122 else
123 {
124 this.right = null;
125
126 if (First)
127 {
128 if (this.defaultRight is null)
129 {
130 this.defaultRight = new GenericObject(this.rightSource.CollectionName,
131 typeof(GenericObject).FullName, Guid.Empty, new KeyValuePair<string, object>[0]);
132 }
133
134 if (this.flipped)
135 this.current = new JoinedObject(this.defaultRight, this.rightName, this.left.Current, this.leftName);
136 else
137 this.current = new JoinedObject(this.left.Current, this.leftName, this.defaultRight, this.rightName);
138
139 return true;
140 }
141 }
142 }
143
144 if (!await this.left.MoveNextAsync())
145 return false;
146
147 if (this.leftVariables is null)
148 this.leftVariables = new ObjectProperties(this.left.Current, this.variables);
149 else
150 this.leftVariables.Object = this.left.Current;
151
152 if (this.hasLeftName)
153 this.leftVariables[this.leftName] = this.left.Current;
154
155 this.right = await this.rightSource.Find(0, int.MaxValue, this.generic, this.conditions, this.leftVariables,
156 null, this.conditions);
157
158 this.rightFirst = true;
159 }
160 }
161
162 public void Reset()
163 {
164 this.current = null;
165 this.right = null;
166 this.left.Reset();
167 }
168 }
169
170 }
171}
Generic object. Contains a sequence of properties.
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.
Abstract base classes of joined sources.
Definition: JoinedSource.cs:14
ScriptNode Conditions
Conditions for join.
Definition: JoinedSource.cs:45
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.
Data source formed through an LEFT [OUTER] JOIN of two sources.
LeftOuterJoinedSource(IDataSource Left, IDataSource Right, ScriptNode Conditions)
Data source formed through an LEFT [OUTER] JOIN of two sources.
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 .
virtual bool Flipped
If sources should be flipped in the JoinedObject instances created.
Collection of variables.
Definition: Variables.cs:25
Task< bool > MoveNextAsync()
Advances the enumerator to the next element of the collection.
Interface for data sources that can be used in SQL statements.
Definition: IDataSource.cs:13
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:76