Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
InsertSelect.cs
1using System;
2using System.Collections.Generic;
3using System.Threading.Tasks;
12
14{
19 {
20 private SourceDefinition source;
21 private Select select;
22 private readonly bool lazy;
23
34 : base(Start, Length, Expression)
35 {
36 this.source = Source;
37 this.source?.SetParent(this);
38
39 this.select = Select;
40 this.select?.SetParent(this);
41
42 this.lazy = Lazy;
43 }
44
49 public override bool IsAsynchronous => true;
50
57 {
58 return this.EvaluateAsync(Variables).Result;
59 }
60
67 public override async Task<IElement> EvaluateAsync(Variables Variables)
68 {
69 IDataSource Source = await this.source.GetSource(Variables);
70 IElement E = await this.select.EvaluateAsync(Variables);
71 long Count = 0;
72
74 E = ToMatrix.ToMatrix();
75
76 await Database.StartBulk();
77 try
78 {
79 if (E is ObjectMatrix M)
80 {
81 string[] ColumnNames = M.ColumnNames;
82 int NrColumns = ColumnNames.Length;
83 int NrRows = M.Rows;
84 int RowIndex;
85 int ColIndex;
86
87 for (RowIndex = 0; RowIndex < NrRows; RowIndex++)
88 {
89 E = M.GetRow(RowIndex);
90
91 if (E is IVector V)
92 {
93 GenericObject Obj = new GenericObject(Source.CollectionName, Source.TypeName, Guid.Empty);
94
95 ColIndex = 0;
96 foreach (IElement E2 in V.ChildElements)
97 Obj[ColumnNames[ColIndex++]] = E2.AssociatedObjectValue;
98
99 await Source.Insert(this.lazy, Obj);
100 }
101 else
102 await Source.Insert(this.lazy, E.AssociatedObjectValue);
103
104 Count++;
105 }
106 }
107 else if (E is IVector V)
108 {
109 foreach (IElement Obj in E.ChildElements)
110 {
111 object Item = Obj.AssociatedObjectValue;
112
113 if (Item is Dictionary<string, IElement> ObjExNihilo)
114 {
115 GenericObject Obj2 = new GenericObject(Source.CollectionName, Source.TypeName, Guid.Empty);
116
117 foreach (KeyValuePair<string, IElement> P in ObjExNihilo)
118 Obj2[P.Key] = P.Value.AssociatedObjectValue;
119
120 Item = Obj2;
121 }
122 else if (Item is Dictionary<string, object> ObjExNihilo2)
123 {
124 GenericObject Obj2 = new GenericObject(Source.CollectionName, Source.TypeName, Guid.Empty);
125
126 foreach (KeyValuePair<string, object> P in ObjExNihilo2)
127 Obj2[P.Key] = P.Value;
128
129 Item = Obj2;
130 }
131
132 await Source.Insert(this.lazy, Item);
133 Count++;
134 }
135 }
136 else
137 throw new ScriptRuntimeException("Unexpected response from SELECT statement.", this.select);
138 }
139 finally
140 {
141 await Database.EndBulk();
142 }
143
144 return new DoubleNumber(Count);
145 }
146
154 public override bool ForAllChildNodes(ScriptNodeEventHandler Callback, object State, SearchMethod Order)
155 {
156 if (Order == SearchMethod.DepthFirst)
157 {
158 if (!(this.source?.ForAllChildNodes(Callback, State, Order) ?? true))
159 return false;
160
161 if (!(this.select?.ForAllChildNodes(Callback, State, Order) ?? true))
162 return false;
163 }
164
165 ScriptNode NewNode;
166 bool b;
167
168 if (!(this.source is null))
169 {
170 b = !Callback(this.source, out NewNode, State);
171 if (!(NewNode is null) && NewNode is SourceDefinition Source2)
172 {
173 this.source = Source2;
174 this.source.SetParent(this);
175 }
176
177 if (b || (Order == SearchMethod.TreeOrder && !this.source.ForAllChildNodes(Callback, State, Order)))
178 return false;
179 }
180
181 if (!(this.select is null))
182 {
183 b = !Callback(this.select, out NewNode, State);
184 if (!(NewNode is null) && NewNode is Select NewSelect)
185 {
186 this.select = NewSelect;
187 this.select.SetParent(this);
188 }
189
190 if (b || (Order == SearchMethod.TreeOrder && !this.select.ForAllChildNodes(Callback, State, Order)))
191 return false;
192 }
193
194 if (Order == SearchMethod.BreadthFirst)
195 {
196 if (!(this.source?.ForAllChildNodes(Callback, State, Order) ?? true))
197 return false;
198
199 if (!(this.select?.ForAllChildNodes(Callback, State, Order) ?? true))
200 return false;
201 }
202
203 return true;
204 }
205
207 public override bool Equals(object obj)
208 {
209 return (obj is InsertSelect O &&
210 AreEqual(this.source, O.source) &&
211 AreEqual(this.select, O.select) &&
212 base.Equals(obj));
213 }
214
216 public override int GetHashCode()
217 {
218 int Result = base.GetHashCode();
219 Result ^= Result << 5 ^ GetHashCode(this.source);
220 Result ^= Result << 5 ^ GetHashCode(this.select);
221 return Result;
222 }
223
224 }
225}
Static interface for database persistence. In order to work, a database provider has to be assigned t...
Definition: Database.cs:19
static Task EndBulk()
Ends bulk-processing of data. Must be called once for every call to StartBulk.
Definition: Database.cs:1494
static Task StartBulk()
Starts bulk-proccessing of data. Must be followed by a call to EndBulk.
Definition: Database.cs:1486
Generic object. Contains a sequence of properties.
Class managing a script expression.
Definition: Expression.cs:39
Base class for all nodes in a parsed script tree.
Definition: ScriptNode.cs:69
int Length
Length of expression covered by node.
Definition: ScriptNode.cs:101
static bool AreEqual(ScriptNode S1, ScriptNode S2)
Compares if two script nodes are equal.
Definition: ScriptNode.cs:275
int Start
Start position in script expression.
Definition: ScriptNode.cs:92
void SetParent(ScriptNode Parent)
Sets the parent node. Can only be used when expression is being parsed.
Definition: ScriptNode.cs:132
ToMatrix(ScriptNode Operand, bool NullCheck, int Start, int Length, Expression Expression)
To-Matrix operator.
Definition: ToMatrix.cs:22
Executes an INSERT SELECT statement against the object database.
Definition: InsertSelect.cs:19
override IElement Evaluate(Variables Variables)
Evaluates the node, using the variables provided in the Variables collection.
Definition: InsertSelect.cs:56
override async Task< IElement > EvaluateAsync(Variables Variables)
Evaluates the node asynchronously, using the variables provided in the Variables collection.
Definition: InsertSelect.cs:67
override bool ForAllChildNodes(ScriptNodeEventHandler Callback, object State, SearchMethod Order)
Calls the callback method for all child nodes.
override bool IsAsynchronous
If the node (or its decendants) include asynchronous evaluation. Asynchronous nodes should be evaluat...
Definition: InsertSelect.cs:49
InsertSelect(SourceDefinition Source, Select Select, bool Lazy, int Start, int Length, Expression Expression)
Executes an INSERT SELECT statement against the object database.
Definition: InsertSelect.cs:33
Executes a SELECT statement against the object database.
Definition: Select.cs:20
override async Task< IElement > EvaluateAsync(Variables Variables)
Evaluates the node asynchronously, using the variables provided in the Variables collection.
Definition: Select.cs:165
Abstract base class for source definitions
abstract Task< IDataSource > GetSource(Variables Variables)
Gets the actual data source, from its definition.
Collection of variables.
Definition: Variables.cs:25
Basic interface for all types of elements.
Definition: IElement.cs:20
object AssociatedObjectValue
Associated object value.
Definition: IElement.cs:33
ICollection< IElement > ChildElements
An enumeration of child elements. If the element is a scalar, this property will return null.
Definition: IElement.cs:49
Basic interface for vectors.
Definition: IVector.cs:9
Interface for script nodes with asynchronous evaluation
Interface for objects that can be converted into matrices.
Definition: IToMatrix.cs:9
Interface for data sources that can be used in SQL statements.
Definition: IDataSource.cs:13
string TypeName
Name of corresponding type.
Definition: IDataSource.cs:68
string CollectionName
Name of corresponding collection.
Definition: IDataSource.cs:60
Task Insert(bool Lazy, object Object)
Inserts an object.
delegate bool ScriptNodeEventHandler(ScriptNode Node, out ScriptNode NewNode, object State)
Delegate for ScriptNode callback methods.
SearchMethod
Method to traverse the expression structure
Definition: ScriptNode.cs:38