Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
InsertObjects.cs
1using System;
2using System.Collections.Generic;
3using System.Threading.Tasks;
12
14{
19 {
20 private SourceDefinition source;
21 private ElementList objects;
22 private readonly bool lazy;
23
33 public InsertObjects(SourceDefinition Source, ElementList Objects, bool Lazy, int Start, int Length, Expression Expression)
34 : base(Start, Length, Expression)
35 {
36 this.source = Source;
37 this.source?.SetParent(this);
38
39 this.objects = Objects;
40 this.objects?.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 List<IElement> Result = new List<IElement>();
71 IEnumerable<IElement> Objects;
72 IElement E;
73 long Count = 0;
74 object Item;
75
76 await Database.StartBulk();
77 try
78 {
79 foreach (ScriptNode Object in this.objects.Elements)
80 {
81 E = await Object.EvaluateAsync(Variables);
82 if (E is IVector V)
83 Objects = V.ChildElements;
84 else if (E is ISet S)
85 Objects = S.ChildElements;
86 else
87 Objects = new IElement[] { E };
88
89 foreach (IElement E2 in Objects)
90 {
91 Item = E2.AssociatedObjectValue;
92
93 if (Item is Dictionary<string, IElement> ObjExNihilo)
94 {
95 GenericObject Obj2 = new GenericObject(Source.CollectionName, Source.TypeName, Guid.Empty);
96
97 foreach (KeyValuePair<string, IElement> P in ObjExNihilo)
98 Obj2[P.Key] = P.Value.AssociatedObjectValue;
99
100 Item = Obj2;
101 }
102 else if (Item is Dictionary<string, object> ObjExNihilo2)
103 {
104 GenericObject Obj2 = new GenericObject(Source.CollectionName, Source.TypeName, Guid.Empty);
105
106 foreach (KeyValuePair<string, object> P in ObjExNihilo2)
107 Obj2[P.Key] = P.Value;
108
109 Item = Obj2;
110 }
111
112 await Source.Insert(this.lazy, Item);
113 Count++;
114
115 Result.Add(new ObjectValue(Item));
116 }
117 }
118 }
119 finally
120 {
121 await Database.EndBulk();
122 }
123
124 if (Result.Count == 1)
125 return Result[0];
126 else
127 return new ObjectVector(Result.ToArray());
128 }
129
137 public override bool ForAllChildNodes(ScriptNodeEventHandler Callback, object State, SearchMethod Order)
138 {
139 if (Order == SearchMethod.DepthFirst)
140 {
141 if (!(this.source?.ForAllChildNodes(Callback, State, Order) ?? true))
142 return false;
143
144 if (!(this.objects?.ForAllChildNodes(Callback, State, Order) ?? true))
145 return false;
146 }
147
148 ScriptNode NewNode;
149 bool b;
150
151 if (!(this.source is null))
152 {
153 b = !Callback(this.source, out NewNode, State);
154 if (!(NewNode is null) && NewNode is SourceDefinition Source2)
155 {
156 this.source = Source2;
157 this.source.SetParent(this);
158 }
159
160 if (b || (Order == SearchMethod.TreeOrder && !this.source.ForAllChildNodes(Callback, State, Order)))
161 return false;
162 }
163
164 if (!(this.objects is null))
165 {
166 b = !Callback(this.objects, out NewNode, State);
167 if (!(NewNode is null) && NewNode is ElementList NewObjects)
168 {
169 this.objects = NewObjects;
170 this.objects.SetParent(this);
171 }
172
173 if (b || (Order == SearchMethod.TreeOrder && !this.objects.ForAllChildNodes(Callback, State, Order)))
174 return false;
175 }
176
177 if (Order == SearchMethod.BreadthFirst)
178 {
179 if (!(this.source?.ForAllChildNodes(Callback, State, Order) ?? true))
180 return false;
181
182 if (!(this.objects?.ForAllChildNodes(Callback, State, Order) ?? true))
183 return false;
184 }
185
186 return true;
187 }
188
190 public override bool Equals(object obj)
191 {
192 return (obj is InsertObjects O &&
193 AreEqual(this.source, O.source) &&
194 AreEqual(this.objects, O.objects) &&
195 base.Equals(obj));
196 }
197
199 public override int GetHashCode()
200 {
201 int Result = base.GetHashCode();
202 Result ^= Result << 5 ^ GetHashCode(this.source);
203 Result ^= Result << 5 ^ GetHashCode(this.objects);
204 return Result;
205 }
206
207 }
208}
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
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 list of elements.
Definition: ElementList.cs:15
ScriptNode[] Elements
Elements.
Definition: ElementList.cs:59
Executes an INSERT ... OBJECT[S] ... statement against the object database.
override IElement Evaluate(Variables Variables)
Evaluates the node, using the variables provided in the Variables collection.
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...
override async Task< IElement > EvaluateAsync(Variables Variables)
Evaluates the node asynchronously, using the variables provided in the Variables collection.
InsertObjects(SourceDefinition Source, ElementList Objects, bool Lazy, int Start, int Length, Expression Expression)
Executes an INSERT ... OBJECT[S] ... statement against the object database.
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
Basic interface for vectors.
Definition: IVector.cs:9
Basic interface for all types of sets.
Definition: ISet.cs:10
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