Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
XmlSource.cs
1using System;
3using System.Threading.Tasks;
4using System.Xml;
13
15{
19 public class XmlSource : IDataSource
20 {
21 private readonly XmlDocument xmlDocument;
22 private readonly XmlNode xmlNode;
23 private readonly ScriptNode node;
24 private readonly string name;
25 private readonly string alias;
26
34 public XmlSource(string Name, string Alias, XmlDocument Xml, ScriptNode Node)
35 {
36 this.xmlDocument = Xml;
37 this.xmlNode = null;
38 this.node = Node;
39 this.name = Name;
40 this.alias = Alias;
41 }
42
50 public XmlSource(string Name, string Alias, XmlNode Xml, ScriptNode Node)
51 {
52 this.xmlDocument = null;
53 this.xmlNode = Xml;
54 this.node = Node;
55 this.name = Name;
56 this.alias = Alias;
57 }
58
70 public async Task<IResultSetEnumerator> Find(int Offset, int Top, bool Generic, ScriptNode Where, Variables Variables,
71 KeyValuePair<VariableReference, bool>[] Order, ScriptNode Node)
72 {
73 XPath WhereXPath = Where as XPath;
74 if (WhereXPath is null && !(Where is null))
75 throw new ScriptRuntimeException("WHERE clause must use an XPATH expression when selecting nodes from XML.", Where);
76
77 IElement Obj;
78
79 if (WhereXPath is null)
80 Obj = new ObjectValue(this.xmlDocument ?? this.xmlNode);
81 else
82 {
83 ObjectProperties P = new ObjectProperties(this.xmlDocument ?? this.xmlNode, Variables);
84
85 if (WhereXPath.IsAsynchronous)
86 Obj = await WhereXPath.EvaluateAsync(P);
87 else
88 Obj = WhereXPath.Evaluate(P);
89 }
90
91 if (!(Obj is IVector Vector))
92 Vector = Operators.Vectors.VectorDefinition.Encapsulate(new IElement[] { Obj }, false, Node);
93
94 return await VectorSource.Find(Vector, Offset, Top, Generic, null, Variables, Order, Node);
95 }
96
110 public async Task<bool> Process(IProcessor<object> Processor, int Offset, int Top, bool Generic,
111 ScriptNode Where, Variables Variables, KeyValuePair<VariableReference, bool>[] Order,
112 ScriptNode Node)
113 {
114 XPath WhereXPath = Where as XPath;
115 if (WhereXPath is null && !(Where is null))
116 throw new ScriptRuntimeException("WHERE clause must use an XPATH expression when selecting nodes from XML.", Where);
117
118 IElement Obj;
119
120 if (WhereXPath is null)
121 Obj = new ObjectValue(this.xmlDocument ?? this.xmlNode);
122 else
123 {
124 ObjectProperties P = new ObjectProperties(this.xmlDocument ?? this.xmlNode, Variables);
125 Obj = WhereXPath.Evaluate(P); // TODO: Async
126 }
127
128 if (!(Obj is IVector Vector))
129 Vector = Operators.Vectors.VectorDefinition.Encapsulate(new IElement[] { Obj }, false, Node);
130
131 return await VectorSource.Process(Processor, Vector, Offset, Top, Generic, null, Variables, Order, Node);
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?> Delete(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 return Task.FromResult(false);
220 }
221
227 public Task CreateIndex(string Name, string[] Fields)
228 {
229 throw this.InvalidOperation();
230 }
231
237 public Task<bool> DropIndex(string Name)
238 {
239 throw this.InvalidOperation();
240 }
241
245 public Task DropCollection()
246 {
247 throw this.InvalidOperation();
248 }
249
250 }
251}
override bool IsAsynchronous
If the node (or its decendants) include asynchronous evaluation. Asynchronous nodes should be evaluat...
Base class for all nodes in a parsed script tree.
Definition: ScriptNode.cs:69
Node repesenting an XPath expression
Definition: XPath.cs:15
override IElement Evaluate(Variables Variables)
Evaluates the node, using the variables provided in the Variables collection.
Definition: XPath.cs:72
override Task< IElement > EvaluateAsync(Variables Variables)
Evaluates the node, using the variables provided in the Variables collection.
Definition: XPath.cs:85
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
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 .
Task< int?> Delete(bool Lazy, int Offset, int Top, ScriptNode Where, Variables Variables, KeyValuePair< VariableReference, bool >[] Order, ScriptNode Node)
Deletes a set of objects.
Definition: XmlSource.cs:160
string Name
Collection name or alias.
Definition: XmlSource.cs:196
Task< bool > DropIndex(string Name)
Drops an index from the source.
Definition: XmlSource.cs:237
Task Update(bool Lazy, IEnumerable< object > Objects)
Updates a set of objects.
Definition: XmlSource.cs:139
string TypeName
Name of corresponding type.
Definition: XmlSource.cs:188
Task Insert(bool Lazy, object Object)
Inserts an object.
Definition: XmlSource.cs:171
Task DropCollection()
Drops the collection specified by the source.
Definition: XmlSource.cs:245
Task< bool > IsLabel(string Label)
Checks if the label is a label in the source.
Definition: XmlSource.cs:217
XmlSource(string Name, string Alias, XmlDocument Xml, ScriptNode Node)
Data Source defined by XML.
Definition: XmlSource.cs:34
Task CreateIndex(string Name, string[] Fields)
Creates an index in the source.
Definition: XmlSource.cs:227
async 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: XmlSource.cs:70
bool IsSource(string Name)
Checks if the name refers to the source.
Definition: XmlSource.cs:205
string CollectionName
Name of corresponding collection.
Definition: XmlSource.cs:180
XmlSource(string Name, string Alias, XmlNode Xml, ScriptNode Node)
Data Source defined by XML.
Definition: XmlSource.cs:50
async 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 .
Definition: XmlSource.cs:110
Collection of variables.
Definition: Variables.cs:25
Interface for processors of objects.
Definition: IProcessor.cs:9
Basic interface for all types of elements.
Definition: IElement.cs:21
IElement Encapsulate(ChunkedList< IElement > Elements, ScriptNode Node)
Encapsulates a set of elements into a similar structure as that provided by the current element.
Basic interface for vectors.
Definition: IVector.cs:9
Interface for data sources that can be used in SQL statements.
Definition: IDataSource.cs:14