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;
2using System.Collections.Generic;
3using System.Threading.Tasks;
4using System.Xml;
12
14{
18 public class XmlSource : IDataSource
19 {
20 private readonly XmlDocument xmlDocument;
21 private readonly XmlNode xmlNode;
22 private readonly ScriptNode node;
23 private readonly string name;
24 private readonly string alias;
25
33 public XmlSource(string Name, string Alias, XmlDocument Xml, ScriptNode Node)
34 {
35 this.xmlDocument = Xml;
36 this.xmlNode = null;
37 this.node = Node;
38 this.name = Name;
39 this.alias = Alias;
40 }
41
49 public XmlSource(string Name, string Alias, XmlNode Xml, ScriptNode Node)
50 {
51 this.xmlDocument = null;
52 this.xmlNode = Xml;
53 this.node = Node;
54 this.name = Name;
55 this.alias = Alias;
56 }
57
69 public Task<IResultSetEnumerator> Find(int Offset, int Top, bool Generic, ScriptNode Where, Variables Variables,
70 KeyValuePair<VariableReference, bool>[] Order, ScriptNode Node)
71 {
72 XPath WhereXPath = Where as XPath;
73 if (WhereXPath is null && !(Where is null))
74 throw new ScriptRuntimeException("WHERE clause must use an XPATH expression when selecting nodes from XML.", Where);
75
76 IElement Obj;
77
78 if (WhereXPath is null)
79 Obj = new ObjectValue(this.xmlDocument ?? this.xmlNode);
80 else
81 {
82 ObjectProperties P = new ObjectProperties(this.xmlDocument ?? this.xmlNode, Variables);
83 Obj = WhereXPath.Evaluate(P);
84 }
85
86 if (!(Obj is IVector Vector))
87 Vector = Operators.Vectors.VectorDefinition.Encapsulate(new IElement[] { Obj }, false, Node);
88
89 return VectorSource.Find(Vector, Offset, Top, Generic, null, Variables, Order, Node);
90 }
91
97 public Task Update(bool Lazy, IEnumerable<object> Objects)
98 {
99 return Task.CompletedTask; // Do nothing.
100 }
101
102 private Exception InvalidOperation()
103 {
104 return new ScriptRuntimeException("Operation not permitted on joined sources.", this.node);
105 }
106
118 public Task<int?> FindDelete(bool Lazy, int Offset, int Top, ScriptNode Where, Variables Variables,
119 KeyValuePair<VariableReference, bool>[] Order, ScriptNode Node)
120 {
121 throw this.InvalidOperation();
122 }
123
129 public Task Insert(bool Lazy, object Object)
130 {
131 throw this.InvalidOperation();
132 }
133
137 public string CollectionName
138 {
139 get { throw new ScriptRuntimeException("Collection not defined.", this.node); }
140 }
141
145 public string TypeName
146 {
147 get { throw new ScriptRuntimeException("Type not defined.", this.node); }
148 }
149
153 public string Name
154 {
155 get => string.IsNullOrEmpty(this.alias) ? this.name : this.alias;
156 }
157
163 public bool IsSource(string Name)
164 {
165 return
166 string.Compare(this.name, Name, true) == 0 ||
167 string.Compare(this.alias, Name, true) == 0;
168 }
169
175 public Task<bool> IsLabel(string Label)
176 {
177 return Task.FromResult(false);
178 }
179
185 public Task CreateIndex(string Name, string[] Fields)
186 {
187 throw this.InvalidOperation();
188 }
189
195 public Task<bool> DropIndex(string Name)
196 {
197 throw InvalidOperation();
198 }
199
203 public Task DropCollection()
204 {
205 throw InvalidOperation();
206 }
207
208 }
209}
Base class for all nodes in a parsed script tree.
Definition: ScriptNode.cs:69
Node repesenting an XPath expression
Definition: XPath.cs:16
override IElement Evaluate(Variables Variables)
Evaluates the node, using the variables provided in the Variables collection.
Definition: XPath.cs:73
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
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:69
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.
Definition: XmlSource.cs:118
string Name
Collection name or alias.
Definition: XmlSource.cs:154
Task< bool > DropIndex(string Name)
Drops an index from the source.
Definition: XmlSource.cs:195
Task Update(bool Lazy, IEnumerable< object > Objects)
Updates a set of objects.
Definition: XmlSource.cs:97
string TypeName
Name of corresponding type.
Definition: XmlSource.cs:146
Task Insert(bool Lazy, object Object)
Inserts an object.
Definition: XmlSource.cs:129
Task DropCollection()
Drops the collection from the source.
Definition: XmlSource.cs:203
Task< bool > IsLabel(string Label)
Checks if the label is a label in the source.
Definition: XmlSource.cs:175
XmlSource(string Name, string Alias, XmlDocument Xml, ScriptNode Node)
Data Source defined by XML.
Definition: XmlSource.cs:33
Task CreateIndex(string Name, string[] Fields)
Creates an index in the source.
Definition: XmlSource.cs:185
bool IsSource(string Name)
Checks if the name refers to the source.
Definition: XmlSource.cs:163
string CollectionName
Name of corresponding collection.
Definition: XmlSource.cs:138
XmlSource(string Name, string Alias, XmlNode Xml, ScriptNode Node)
Data Source defined by XML.
Definition: XmlSource.cs:49
Collection of variables.
Definition: Variables.cs:25
Basic interface for all types of elements.
Definition: IElement.cs:20
IElement Encapsulate(ICollection< 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:13