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;
2using System.Collections.Generic;
3using System.Reflection;
4using System.Threading.Tasks;
15
17{
22 {
23 private readonly Dictionary<Type, bool> types = new Dictionary<Type, bool>();
24 private readonly Dictionary<string, bool> isLabel = new Dictionary<string, bool>();
25 private readonly IVector vector;
26 private readonly ScriptNode node;
27 private readonly string name;
28 private readonly string alias;
29
37 public VectorSource(string Name, string Alias, IVector Vector, ScriptNode Node)
38 {
39 this.vector = Vector;
40 this.node = Node;
41 this.name = Name;
42 this.alias = Alias;
43
44 Type LastType = null;
45 Type T;
46
47 foreach (IElement E in Vector.ChildElements)
48 {
49 T = E.AssociatedObjectValue?.GetType();
50 if (T is null || T == LastType)
51 continue;
52
53 LastType = T;
54 this.types[T] = true;
55 }
56 }
57
61 public IVector Vector => this.vector;
62
74 public Task<IResultSetEnumerator> Find(int Offset, int Top, bool Generic, ScriptNode Where, Variables Variables,
75 KeyValuePair<VariableReference, bool>[] Order, ScriptNode Node)
76 {
77 return Find(this.vector, Offset, Top, Generic, Where, Variables, Order, Node);
78 }
79
80 internal static async Task<IResultSetEnumerator> Find(IVector Vector, int Offset, int Top, bool Generic, ScriptNode Where, Variables Variables,
81 KeyValuePair<VariableReference, bool>[] Order, ScriptNode Node)
82 {
84 int i, c;
85
86 if (!(Where is null))
87 e = new ConditionalEnumerator(e, Variables, Where);
88
89 if ((c = Order?.Length ?? 0) > 0)
90 {
91 List<IElement> Items = new List<IElement>();
92
93 while (await e.MoveNextAsync())
94 {
95 if (!(e.Current is IElement E))
96 E = Expression.Encapsulate(e.Current);
97
98 Items.Add(Generic ? await Generalize.EvaluateAsync(E) : E);
99 }
100
101 IComparer<IElement> Order2;
102
103 if (c == 1)
104 Order2 = ToPropertyOrder(Node, Order[0]);
105 else
106 {
107 IComparer<IElement>[] Orders = new IComparer<IElement>[c];
108
109 for (i = 0; i < c; i++)
110 Orders[i] = ToPropertyOrder(Node, Order[i]);
111
112 Order2 = new CompoundOrder(Orders);
113 }
114
115 Items.Sort(Order2);
116
117 e = new SynchEnumerator(Items.GetEnumerator());
118 }
119
120 if (Offset > 0)
121 e = new OffsetEnumerator(e, Offset);
122
123 if (Top != int.MaxValue)
124 e = new MaxCountEnumerator(e, Top);
125
126 return e;
127 }
128
129 private static PropertyOrder ToPropertyOrder(ScriptNode Node, KeyValuePair<VariableReference, bool> Order)
130 {
131 return new PropertyOrder(Node, Order.Key.VariableName, Order.Value ? 1 : -1);
132 }
133
139 public Task Update(bool Lazy, IEnumerable<object> Objects)
140 {
141 return Task.CompletedTask; // Do nothing.
142 }
143
144 private Exception InvalidOperation()
145 {
146 return new ScriptRuntimeException("Operation not permitted on joined sources.", this.node);
147 }
148
160 public Task<int?> FindDelete(bool Lazy, int Offset, int Top, ScriptNode Where, Variables Variables,
161 KeyValuePair<VariableReference, bool>[] Order, ScriptNode Node)
162 {
163 throw this.InvalidOperation();
164 }
165
171 public Task Insert(bool Lazy, object Object)
172 {
173 throw this.InvalidOperation();
174 }
175
179 public string CollectionName
180 {
181 get { throw new ScriptRuntimeException("Collection not defined.", this.node); }
182 }
183
187 public string TypeName
188 {
189 get { throw new ScriptRuntimeException("Type not defined.", this.node); }
190 }
191
195 public string Name
196 {
197 get => string.IsNullOrEmpty(this.alias) ? this.name : this.alias;
198 }
199
205 public bool IsSource(string Name)
206 {
207 return
208 string.Compare(this.name, Name, true) == 0 ||
209 string.Compare(this.alias, Name, true) == 0;
210 }
211
217 public Task<bool> IsLabel(string Label)
218 {
219 lock (this.isLabel)
220 {
221 if (!this.isLabel.TryGetValue(Label, out bool Result))
222 {
223 Result = false;
224
225 foreach (Type T in this.types.Keys)
226 {
227 PropertyInfo PI = T.GetRuntimeProperty(Label);
228
229 if (PI is null)
230 {
231 FieldInfo FI = T.GetRuntimeField(Label);
232
233 if (!(FI is null))
234 {
235 Result = FI.IsPublic;
236 break;
237 }
238 }
239 else
240 {
241 Result = PI.CanRead && PI.GetMethod.IsPublic;
242 break;
243 }
244 }
245
246 this.isLabel[Label] = Result;
247 }
248
249 return Task.FromResult<bool>(Result);
250 }
251 }
252
258 public Task CreateIndex(string Name, string[] Fields)
259 {
260 throw this.InvalidOperation();
261 }
262
268 public Task<bool> DropIndex(string Name)
269 {
270 throw InvalidOperation();
271 }
272
276 public Task DropCollection()
277 {
278 throw InvalidOperation();
279 }
280
288 {
289 if (!ResultSet.HasColumnNames)
290 throw new ArgumentException("Result Set lacks named columns.", nameof(ResultSet));
291
292 int Rows = ResultSet.Rows;
293 int Columns = ResultSet.Columns;
294 string[] Names = ResultSet.ColumnNames;
295 IElement[] Objects = new IElement[Rows];
296 Dictionary<string, object> Object;
297 int x, y;
298
299 for (y = 0; y < Rows; y++)
300 {
301 Object = new Dictionary<string, object>();
302 Objects[y] = new ObjectValue(Object);
303
304 for (x = 0; x < Columns; x++)
305 Object[Names[x]] = ResultSet.GetElement(x, y).AssociatedObjectValue;
306 }
307
308 return new ObjectVector(Objects);
309 }
310
311 }
312}
Class managing a script expression.
Definition: Expression.cs:39
static IElement Encapsulate(object Value)
Encapsulates an object.
Definition: Expression.cs:4955
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:37
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.
Task CreateIndex(string Name, string[] Fields)
Creates an index in the source.
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:74
bool IsSource(string Name)
Checks if the name refers to the source.
Task DropCollection()
Drops the collection from the source.
string CollectionName
Name of corresponding collection.
Task< int?> FindDelete(bool Lazy, int Offset, int Top, ScriptNode Where, Variables Variables, KeyValuePair< VariableReference, bool >[] Order, ScriptNode Node)
Finds and Deletes a set of objects.
Collection of variables.
Definition: Variables.cs:25
Task< bool > MoveNextAsync()
Advances the enumerator to the next element of the collection.
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
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:13