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;
3using System.Threading.Tasks;
13
15{
20 {
21 private SourceDefinition source;
22 private ElementList objects;
23 private readonly bool lazy;
24
34 public InsertObjects(SourceDefinition Source, ElementList Objects, bool Lazy, int Start, int Length, Expression Expression)
35 : base(Start, Length, Expression)
36 {
37 this.source = Source;
38 this.source?.SetParent(this);
39
40 this.objects = Objects;
41 this.objects?.SetParent(this);
42
43 this.lazy = Lazy;
44 }
45
50 public override bool IsAsynchronous => true;
51
58 {
59 return this.EvaluateAsync(Variables).Result;
60 }
61
68 public override async Task<IElement> EvaluateAsync(Variables Variables)
69 {
70 IDataSource Source = await this.source.GetSource(Variables);
72 IEnumerable<IElement> Objects;
73 IElement E;
74 long Count = 0;
75 object Item;
76
77 await Database.StartBulk();
78 try
79 {
80 foreach (ScriptNode Object in this.objects.Elements)
81 {
82 E = await Object.EvaluateAsync(Variables);
83 if (E is IVector V)
84 Objects = V.ChildElements;
85 else if (E is ISet S)
86 Objects = S.ChildElements;
87 else
88 Objects = new IElement[] { E };
89
90 foreach (IElement E2 in Objects)
91 {
92 Item = E2.AssociatedObjectValue;
93
94 if (Item is Dictionary<string, IElement> ObjExNihilo)
95 {
96 GenericObject Obj2 = new GenericObject(Source.CollectionName, Source.TypeName, Guid.Empty);
97
98 foreach (KeyValuePair<string, IElement> P in ObjExNihilo)
99 Obj2[P.Key] = P.Value.AssociatedObjectValue;
100
101 Item = Obj2;
102 }
103 else if (Item is Dictionary<string, object> ObjExNihilo2)
104 {
105 GenericObject Obj2 = new GenericObject(Source.CollectionName, Source.TypeName, Guid.Empty);
106
107 foreach (KeyValuePair<string, object> P in ObjExNihilo2)
108 Obj2[P.Key] = P.Value;
109
110 Item = Obj2;
111 }
112
113 await Source.Insert(this.lazy, Item);
114 Count++;
115
116 Result.Add(new ObjectValue(Item));
117 }
118 }
119 }
120 finally
121 {
122 await Database.EndBulk();
123 }
124
125 if (Result.Count == 1)
126 return Result[0];
127 else
128 return new ObjectVector(Result.ToArray());
129 }
130
138 public override bool ForAllChildNodes(ScriptNodeEventHandler Callback, object State, SearchMethod Order)
139 {
140 if (Order == SearchMethod.DepthFirst)
141 {
142 if (!(this.source?.ForAllChildNodes(Callback, State, Order) ?? true))
143 return false;
144
145 if (!(this.objects?.ForAllChildNodes(Callback, State, Order) ?? true))
146 return false;
147 }
148
149 ScriptNode NewNode;
150 bool b;
151
152 if (!(this.source is null))
153 {
154 b = !Callback(this.source, out NewNode, State);
155 if (!(NewNode is null) && NewNode is SourceDefinition Source2)
156 {
157 this.source = Source2;
158 this.source.SetParent(this);
159 }
160
161 if (b || (Order == SearchMethod.TreeOrder && !this.source.ForAllChildNodes(Callback, State, Order)))
162 return false;
163 }
164
165 if (!(this.objects is null))
166 {
167 b = !Callback(this.objects, out NewNode, State);
168 if (!(NewNode is null) && NewNode is ElementList NewObjects)
169 {
170 this.objects = NewObjects;
171 this.objects.SetParent(this);
172 }
173
174 if (b || (Order == SearchMethod.TreeOrder && !this.objects.ForAllChildNodes(Callback, State, Order)))
175 return false;
176 }
177
178 if (Order == SearchMethod.BreadthFirst)
179 {
180 if (!(this.source?.ForAllChildNodes(Callback, State, Order) ?? true))
181 return false;
182
183 if (!(this.objects?.ForAllChildNodes(Callback, State, Order) ?? true))
184 return false;
185 }
186
187 return true;
188 }
189
191 public override bool Equals(object obj)
192 {
193 return (obj is InsertObjects O &&
194 AreEqual(this.source, O.source) &&
195 AreEqual(this.objects, O.objects) &&
196 base.Equals(obj));
197 }
198
200 public override int GetHashCode()
201 {
202 int Result = base.GetHashCode();
203 Result ^= Result << 5 ^ GetHashCode(this.source);
204 Result ^= Result << 5 ^ GetHashCode(this.objects);
205 return Result;
206 }
207
208 }
209}
Static interface for database persistence. In order to work, a database provider has to be assigned t...
Definition: Database.cs:21
static Task EndBulk()
Ends bulk-processing of data. Must be called once for every call to StartBulk.
Definition: Database.cs:2259
static Task StartBulk()
Starts bulk-proccessing of data. Must be followed by a call to EndBulk.
Definition: Database.cs:2251
Generic object. Contains a sequence of properties.
A chunked list is a linked list of chunks of objects of type T .
Definition: ChunkedList.cs:54
int Count
Number of elements in collection.
Definition: ChunkedList.cs:68
void Add(T Item)
Adds an item to the collection.
Definition: ChunkedList.cs:272
T[] ToArray()
Returns an array containing all elements of the collection.
Class managing a script expression.
Definition: Expression.cs:41
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 or created.
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:21
object AssociatedObjectValue
Associated object value.
Definition: IElement.cs:34
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:14
string TypeName
Name of corresponding type.
Definition: IDataSource.cs:86
string CollectionName
Name of corresponding collection.
Definition: IDataSource.cs:78
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