Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
VectorSource.cs
1using System;
3using System.Reflection;
4using System.Threading.Tasks;
16
18{
23 {
24 private readonly Dictionary<Type, bool> types = new Dictionary<Type, bool>();
25 private readonly Dictionary<string, bool> isLabel = new Dictionary<string, bool>();
26 private readonly IVector vector;
27 private readonly ScriptNode node;
28 private readonly string name;
29 private readonly string alias;
30
38 public VectorSource(string Name, string Alias, IVector Vector, ScriptNode Node)
39 {
40 this.vector = Vector;
41 this.node = Node;
42 this.name = Name;
43 this.alias = Alias;
44
45 Type LastType = null;
46 Type T;
47
48 foreach (IElement E in Vector.ChildElements)
49 {
50 T = E.AssociatedObjectValue?.GetType();
51 if (T is null || T == LastType)
52 continue;
53
54 LastType = T;
55 this.types[T] = true;
56 }
57 }
58
62 public IVector Vector => this.vector;
63
75 public Task<IResultSetEnumerator> Find(int Offset, int Top, bool Generic, ScriptNode Where, Variables Variables,
76 KeyValuePair<VariableReference, bool>[] Order, ScriptNode Node)
77 {
78 return Find(this.vector, Offset, Top, Generic, Where, Variables, Order, Node);
79 }
80
93 public static async Task<IResultSetEnumerator> Find(IVector Vector, int Offset,
94 int Top, bool Generic, ScriptNode Where, Variables Variables,
95 KeyValuePair<VariableReference, bool>[] Order, ScriptNode Node)
96 {
98 int i, c;
99
100 if (!(Where is null))
101 e = new ConditionalEnumerator(e, Variables, Where);
102
103 if ((c = Order?.Length ?? 0) > 0)
104 {
105 List<IElement> Items = new List<IElement>();
106
107 while (await e.MoveNextAsync())
108 {
109 if (!(e.Current is IElement E))
110 E = Expression.Encapsulate(e.Current);
111
112 Items.Add(Generic ? await Generalize.EvaluateAsync(E) : E);
113 }
114
115 IComparer<IElement> Order2;
116
117 if (c == 1)
118 Order2 = ToPropertyOrder(Node, Order[0]);
119 else
120 {
121 IComparer<IElement>[] Orders = new IComparer<IElement>[c];
122
123 for (i = 0; i < c; i++)
124 Orders[i] = ToPropertyOrder(Node, Order[i]);
125
126 Order2 = new CompoundOrder(Orders);
127 }
128
129 Items.Sort(Order2);
130
131 e = new SynchEnumerator(Items.GetEnumerator());
132 }
133
134 if (Offset > 0)
135 e = new OffsetEnumerator(e, Offset);
136
137 if (Top != int.MaxValue)
138 e = new MaxCountEnumerator(e, Top);
139
140 return e;
141 }
142
143 private static PropertyOrder ToPropertyOrder(ScriptNode Node, KeyValuePair<VariableReference, bool> Order)
144 {
145 return new PropertyOrder(Node, Order.Key.VariableName, Order.Value ? 1 : -1);
146 }
147
161 public Task<bool> Process(IProcessor<object> Processor, int Offset, int Top, bool Generic,
162 ScriptNode Where, Variables Variables, KeyValuePair<VariableReference, bool>[] Order,
163 ScriptNode Node)
164 {
165 return Process(Processor, this.vector, Offset, Top, Generic, Where, Variables, Order, Node);
166 }
167
168 internal static async Task<bool> Process(IProcessor<object> Processor, IVector Vector,
169 int Offset, int Top, bool Generic, ScriptNode Where, Variables Variables,
170 KeyValuePair<VariableReference, bool>[] Order, ScriptNode Node)
171 {
172 IResultSetEnumerator e = await Find(Vector, Offset, Top, Generic, Where, Variables, Order, Node);
173 bool IsAsynchronous = Processor.IsAsynchronous;
174
175 while (await e.MoveNextAsync())
176 {
177 if (IsAsynchronous)
178 {
179 if (!await Processor.ProcessAsync(e.Current))
180 return false;
181 }
182 else
183 {
184 if (!Processor.Process(e.Current))
185 return false;
186 }
187 }
188
189 return true;
190 }
191
197 public Task Update(bool Lazy, IEnumerable<object> Objects)
198 {
199 return Task.CompletedTask; // Do nothing.
200 }
201
202 private Exception InvalidOperation()
203 {
204 return new ScriptRuntimeException("Operation not permitted on joined sources.", this.node);
205 }
206
218 public Task<int?> Delete(bool Lazy, int Offset, int Top, ScriptNode Where, Variables Variables,
219 KeyValuePair<VariableReference, bool>[] Order, ScriptNode Node)
220 {
221 throw this.InvalidOperation();
222 }
223
229 public Task Insert(bool Lazy, object Object)
230 {
231 throw this.InvalidOperation();
232 }
233
237 public string CollectionName
238 {
239 get { throw new ScriptRuntimeException("Collection not defined.", this.node); }
240 }
241
245 public string TypeName
246 {
247 get { throw new ScriptRuntimeException("Type not defined.", this.node); }
248 }
249
253 public string Name
254 {
255 get => string.IsNullOrEmpty(this.alias) ? this.name : this.alias;
256 }
257
263 public bool IsSource(string Name)
264 {
265 return
266 string.Compare(this.name, Name, true) == 0 ||
267 string.Compare(this.alias, Name, true) == 0;
268 }
269
275 public Task<bool> IsLabel(string Label)
276 {
277 lock (this.isLabel)
278 {
279 if (!this.isLabel.TryGetValue(Label, out bool Result))
280 {
281 Result = false;
282
283 foreach (Type T in this.types.Keys)
284 {
285 PropertyInfo PI = T.GetRuntimeProperty(Label);
286
287 if (PI is null)
288 {
289 FieldInfo FI = T.GetRuntimeField(Label);
290
291 if (!(FI is null))
292 {
293 Result = FI.IsPublic;
294 break;
295 }
296 }
297 else
298 {
299 Result = PI.CanRead && PI.GetMethod.IsPublic;
300 break;
301 }
302 }
303
304 this.isLabel[Label] = Result;
305 }
306
307 return Task.FromResult<bool>(Result);
308 }
309 }
310
316 public Task CreateIndex(string Name, string[] Fields)
317 {
318 throw this.InvalidOperation();
319 }
320
326 public Task<bool> DropIndex(string Name)
327 {
328 throw this.InvalidOperation();
329 }
330
334 public Task DropCollection()
335 {
336 throw this.InvalidOperation();
337 }
338
346 {
347 if (!ResultSet.HasColumnNames)
348 throw new ArgumentException("Result Set lacks named columns.", nameof(ResultSet));
349
350 int Rows = ResultSet.Rows;
351 int Columns = ResultSet.Columns;
352 string[] Names = ResultSet.ColumnNames;
353 IElement[] Objects = new IElement[Rows];
354 Dictionary<string, object> Object;
355 int x, y;
356
357 for (y = 0; y < Rows; y++)
358 {
359 Object = new Dictionary<string, object>();
360 Objects[y] = new ObjectValue(Object);
361
362 for (x = 0; x < Columns; x++)
363 Object[Names[x]] = ResultSet.GetElement(x, y).AssociatedObjectValue;
364 }
365
366 return new ObjectVector(Objects);
367 }
368
369 }
370}
Class managing a script expression.
Definition: Expression.cs:41
static IElement Encapsulate(object Value)
Encapsulates an object.
Definition: Expression.cs:5241
Base class for all nodes in a parsed script tree.
Definition: ScriptNode.cs:69
IElement GetElement(int Index)
Gets an element of the vector.
bool HasColumnNames
If the matrix has column names defined.
string[] ColumnNames
Contains optional column names.
Orders elements based on a sequence of comparers.
Orders elements based on values of a given property.
Creates a generalized representation of an object.
Definition: Generalize.cs:14
override Task< IElement > EvaluateAsync(IElement Argument, Variables Variables)
Evaluates the function.
Definition: Generalize.cs:60
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.
VectorSource(string Name, string Alias, IVector Vector, ScriptNode Node)
Data Source defined by a vector.
Definition: VectorSource.cs:38
Task< bool > DropIndex(string Name)
Drops an index from the source.
Task Update(bool Lazy, IEnumerable< object > Objects)
Updates a set of objects.
Task Insert(bool Lazy, object Object)
Inserts an object.
string TypeName
Name of corresponding type.
Task< bool > IsLabel(string Label)
Checks if the label is a label in the source.
static ObjectVector ToGenericObjectVector(ObjectMatrix ResultSet)
Converts an object matrix, with named columns, to a vector of objects ex nihilo.
static async Task< IResultSetEnumerator > Find(IVector Vector, int Offset, int Top, bool Generic, ScriptNode Where, Variables Variables, KeyValuePair< VariableReference, bool >[] Order, ScriptNode Node)
Finds elements in a vector.
Definition: VectorSource.cs:93
Task CreateIndex(string Name, string[] Fields)
Creates an index in the source.
Task< int?> Delete(bool Lazy, int Offset, int Top, ScriptNode Where, Variables Variables, KeyValuePair< VariableReference, bool >[] Order, ScriptNode Node)
Deletes a set of objects.
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 .
Definition: VectorSource.cs:75
bool IsSource(string Name)
Checks if the name refers to the source.
Task DropCollection()
Drops the collection specified by the source.
string CollectionName
Name of corresponding collection.
Task< bool > Process(IProcessor< object > Processor, int Offset, int Top, bool Generic, ScriptNode Where, Variables Variables, KeyValuePair< VariableReference, bool >[] Order, ScriptNode Node)
Processes objects matching filter conditions in Where .
Collection of variables.
Definition: Variables.cs:25
Task< bool > MoveNextAsync()
Advances the enumerator to the next element of the collection.
Interface for processors of objects.
Definition: IProcessor.cs:9
bool Process(T Object)
Processes an object synchronously.
bool IsAsynchronous
If the processor operates asynchronously.
Definition: IProcessor.cs:13
Task< bool > ProcessAsync(T Object)
Processes an object asynchronously.
Basic interface for all types of elements.
Definition: IElement.cs:21
object AssociatedObjectValue
Associated object value.
Definition: IElement.cs:34
ICollection< IElement > ChildElements
An enumeration of child elements. If the element is a scalar, this property will return null.
Definition: IElement.cs:50
Basic interface for vectors.
Definition: IVector.cs:9
ICollection< IElement > VectorElements
An enumeration of vector elements.
Definition: IVector.cs:22
Interface for data sources that can be used in SQL statements.
Definition: IDataSource.cs:14