Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
InsertValues.cs
1using System;
2using System.Threading.Tasks;
9
11{
16 {
17 private SourceDefinition source;
18 private ElementList columns;
19 private ElementList values;
20 private readonly int nrFields;
21 private readonly bool lazy;
22
33 public InsertValues(SourceDefinition Source, ElementList Columns, ElementList Values, bool Lazy, int Start, int Length, Expression Expression)
34 : base(Start, Length, Expression)
35 {
36 if ((this.nrFields = Columns.Elements.Length) != Values.Elements.Length)
37 throw new ArgumentException("Column and Value lists must have the same lengths.", nameof(Values));
38
39 this.source = Source;
40 this.source?.SetParent(this);
41
42 this.columns = Columns;
43 this.columns?.SetParent(this);
44
45 this.values = Values;
46 this.values?.SetParent(this);
47
48 this.lazy = Lazy;
49 }
50
55 public override bool IsAsynchronous => true;
56
63 {
64 return this.EvaluateAsync(Variables).Result;
65 }
66
73 public override async Task<IElement> EvaluateAsync(Variables Variables)
74 {
75 IDataSource Source = await this.source.GetSource(Variables);
76 GenericObject Obj = new GenericObject(Source.CollectionName, Source.TypeName, Guid.Empty);
77 ScriptNode Node;
78 IElement E;
79 string Column;
80 int i;
81
82 for (i = 0; i < this.nrFields; i++)
83 {
84 Column = await GetNameAsync(this.columns.Elements[i], Variables);
85
86 Node = this.values.Elements[i];
87 E = await Node.EvaluateAsync(Variables);
88
89 Obj[Column] = E.AssociatedObjectValue;
90 }
91
92 await Source.Insert(this.lazy, Obj);
93
94 return new ObjectValue(Obj);
95 }
96
104 public static async Task<string> GetNameAsync(ScriptNode Node, Variables Variables)
105 {
106 if (Node is VariableReference Ref)
107 return Ref.VariableName;
108 else
109 {
110 IElement E = await Node.EvaluateAsync(Variables);
111 if (E.AssociatedObjectValue is string s)
112 return s;
113 else
114 throw new ScriptRuntimeException("Exepected variable reference or string value.", Node);
115 }
116 }
117
125 public override bool ForAllChildNodes(ScriptNodeEventHandler Callback, object State, SearchMethod Order)
126 {
127 if (Order == SearchMethod.DepthFirst)
128 {
129 if (!(this.source?.ForAllChildNodes(Callback, State, Order) ?? true))
130 return false;
131
132 if (!(this.columns?.ForAllChildNodes(Callback, State, Order) ?? true))
133 return false;
134
135 if (!(this.values?.ForAllChildNodes(Callback, State, Order) ?? true))
136 return false;
137 }
138
139 ScriptNode NewNode;
140 bool b;
141
142 if (!(this.source is null))
143 {
144 b = !Callback(this.source, out NewNode, State);
145 if (!(NewNode is null) && NewNode is SourceDefinition Source2)
146 {
147 this.source = Source2;
148 this.source.SetParent(this);
149 }
150
151 if (b || (Order == SearchMethod.TreeOrder && !this.source.ForAllChildNodes(Callback, State, Order)))
152 return false;
153 }
154
155 if (!(this.columns is null))
156 {
157 b = !Callback(this.columns, out NewNode, State);
158 if (!(NewNode is null) && NewNode is ElementList NewColumns)
159 {
160 this.columns = NewColumns;
161 this.columns.SetParent(this);
162 }
163
164 if (b || (Order == SearchMethod.TreeOrder && !this.columns.ForAllChildNodes(Callback, State, Order)))
165 return false;
166 }
167
168 if (!(this.values is null))
169 {
170 b = !Callback(this.values, out NewNode, State);
171 if (!(NewNode is null) && NewNode is ElementList NewValues)
172 {
173 this.values = NewValues;
174 this.values.SetParent(this);
175 }
176
177 if (b || (Order == SearchMethod.TreeOrder && !this.values.ForAllChildNodes(Callback, State, Order)))
178 return false;
179 }
180
181 if (Order == SearchMethod.BreadthFirst)
182 {
183 if (!(this.source?.ForAllChildNodes(Callback, State, Order) ?? true))
184 return false;
185
186 if (!(this.columns?.ForAllChildNodes(Callback, State, Order) ?? true))
187 return false;
188
189 if (!(this.values?.ForAllChildNodes(Callback, State, Order) ?? true))
190 return false;
191 }
192
193 return true;
194 }
195
197 public override bool Equals(object obj)
198 {
199 return (obj is InsertValues O &&
200 AreEqual(this.source, O.source) &&
201 AreEqual(this.columns, O.columns) &&
202 AreEqual(this.values, O.values) &&
203 base.Equals(obj));
204 }
205
207 public override int GetHashCode()
208 {
209 int Result = base.GetHashCode();
210 Result ^= Result << 5 ^ GetHashCode(this.source);
211 Result ^= Result << 5 ^ GetHashCode(this.columns);
212 Result ^= Result << 5 ^ GetHashCode(this.values);
213 return Result;
214 }
215
216 }
217}
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
virtual Task< IElement > EvaluateAsync(Variables Variables)
Evaluates the node, using the variables provided in the Variables collection. This method should be ...
Definition: ScriptNode.cs:158
Represents a variable reference.
Represents a list of elements.
Definition: ElementList.cs:15
ScriptNode[] Elements
Elements.
Definition: ElementList.cs:59
Executes an INSERT ... VALUES ... statement against the object database.
Definition: InsertValues.cs:16
override IElement Evaluate(Variables Variables)
Evaluates the node, using the variables provided in the Variables collection.
Definition: InsertValues.cs:62
override bool ForAllChildNodes(ScriptNodeEventHandler Callback, object State, SearchMethod Order)
Calls the callback method for all child nodes.
static async Task< string > GetNameAsync(ScriptNode Node, Variables Variables)
Gets a name from a script node, either by using the name of a variable reference, or evaluating the n...
override bool IsAsynchronous
If the node (or its decendants) include asynchronous evaluation. Asynchronous nodes should be evaluat...
Definition: InsertValues.cs:55
InsertValues(SourceDefinition Source, ElementList Columns, ElementList Values, bool Lazy, int Start, int Length, Expression Expression)
Executes an INSERT ... VALUES ... statement against the object database.
Definition: InsertValues.cs:33
override async Task< IElement > EvaluateAsync(Variables Variables)
Evaluates the node asynchronously, using the variables provided in the Variables collection.
Definition: InsertValues.cs:73
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
Interface for script nodes with asynchronous evaluation
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