Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
CreateIndex.cs
1using System;
2using System.Threading.Tasks;
6
8{
13 {
14 private ScriptNode name;
15 private SourceDefinition source;
16 private readonly ScriptNode[] columns;
17 private readonly bool[] ascending;
18 private readonly int nrColumns;
19
30 public CreateIndex(ScriptNode Name, SourceDefinition Source, ScriptNode[] Columns, bool[] Ascending,
32 : base(Start, Length, Expression)
33 {
34 this.nrColumns = Columns.Length;
35 if (Ascending.Length != this.nrColumns)
36 throw new ArgumentException("Number of columns does not match number of ascending argument values.", nameof(Ascending));
37
38 this.name = Name;
39 this.name?.SetParent(this);
40
41 this.source = Source;
42 this.source?.SetParent(this);
43
44 this.columns = Columns;
45 this.columns?.SetParent(this);
46
47 this.ascending = Ascending;
48 }
49
54 public override bool IsAsynchronous => true;
55
62 {
63 return this.EvaluateAsync(Variables).Result;
64 }
65
72 public override async Task<IElement> EvaluateAsync(Variables Variables)
73 {
74 IDataSource Source = await this.source.GetSource(Variables);
75 string Name = await InsertValues.GetNameAsync(this.name, Variables);
76 string[] Fields = new string[this.nrColumns];
77 int i;
78
79 for (i = 0; i < this.nrColumns; i++)
80 {
81 string s = await InsertValues.GetNameAsync(this.columns[i], Variables);
82 Fields[i] = this.ascending[i] ? s : "-" + s;
83 }
84
85 await Source.CreateIndex(Name, Fields);
86
87 return new StringValue(Name);
88 }
89
97 public override bool ForAllChildNodes(ScriptNodeEventHandler Callback, object State, SearchMethod Order)
98 {
99 if (Order == SearchMethod.DepthFirst)
100 {
101 if (!(this.name?.ForAllChildNodes(Callback, State, Order) ?? true))
102 return false;
103
104 if (!(this.source?.ForAllChildNodes(Callback, State, Order) ?? true))
105 return false;
106
107 if (!this.columns.ForAllChildNodes(Callback, State, Order))
108 return false;
109 }
110
111 ScriptNode NewNode;
112 bool b;
113
114 if (!(this.name is null))
115 {
116 b = !Callback(this.name, out NewNode, State);
117 if (!(NewNode is null))
118 {
119 this.name = NewNode;
120 this.name.SetParent(this);
121 }
122
123 if (b || (Order == SearchMethod.TreeOrder && !this.name.ForAllChildNodes(Callback, State, Order)))
124 return false;
125 }
126
127 if (!(this.source is null))
128 {
129 b = !Callback(this.source, out NewNode, State);
130 if (!(NewNode is null) && NewNode is SourceDefinition Source2)
131 {
132 this.source = Source2;
133 this.source.SetParent(this);
134 }
135
136 if (b || (Order == SearchMethod.TreeOrder && !this.source.ForAllChildNodes(Callback, State, Order)))
137 return false;
138 }
139
140 int i;
141
142 for (i = 0; i < this.nrColumns; i++)
143 {
144 ScriptNode Node = this.columns[i];
145 if (!(Node is null))
146 {
147 b = !Callback(Node, out NewNode, State);
148 if (!(NewNode is null))
149 {
150 this.columns[i] = NewNode;
151 NewNode.SetParent(this);
152 Node = NewNode;
153 }
154
155 if (b || (Order == SearchMethod.TreeOrder && !Node.ForAllChildNodes(Callback, State, Order)))
156 return false;
157 }
158 }
159
160 if (Order == SearchMethod.BreadthFirst)
161 {
162 if (!(this.name?.ForAllChildNodes(Callback, State, Order) ?? true))
163 return false;
164
165 if (!(this.source?.ForAllChildNodes(Callback, State, Order) ?? true))
166 return false;
167
168 if (!this.columns.ForAllChildNodes(Callback, State, Order))
169 return false;
170 }
171
172 return true;
173 }
174
176 public override bool Equals(object obj)
177 {
178 return (obj is CreateIndex O &&
179 AreEqual(this.name, O.name) &&
180 AreEqual(this.source, O.source) &&
181 AreEqual(this.columns, O.columns) &&
182 AreEqual(this.ascending, O.ascending) &&
183 base.Equals(obj));
184 }
185
187 public override int GetHashCode()
188 {
189 int Result = base.GetHashCode();
190 Result ^= Result << 5 ^ GetHashCode(this.name);
191 Result ^= Result << 5 ^ GetHashCode(this.source);
192 Result ^= Result << 5 ^ GetHashCode(this.columns);
193 Result ^= Result << 5 ^ GetHashCode(this.ascending);
194 return Result;
195 }
196
197 }
198}
Class managing a script expression.
Definition: Expression.cs:39
Base class for all nodes in a parsed script tree.
Definition: ScriptNode.cs:69
bool ForAllChildNodes(ScriptNodeEventHandler Callback, object State, bool DepthFirst)
Calls the callback method for all child nodes.
Definition: ScriptNode.cs:243
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.
Definition: ScriptNode.cs:132
Executes an CREATE INDEX ... ON ... (...) statement against the object database.
Definition: CreateIndex.cs:13
CreateIndex(ScriptNode Name, SourceDefinition Source, ScriptNode[] Columns, bool[] Ascending, int Start, int Length, Expression Expression)
Executes an CREATE INDEX ... ON ... (...) statement against the object database.
Definition: CreateIndex.cs:30
override IElement Evaluate(Variables Variables)
Evaluates the node, using the variables provided in the Variables collection.
Definition: CreateIndex.cs:61
override bool ForAllChildNodes(ScriptNodeEventHandler Callback, object State, SearchMethod Order)
Calls the callback method for all child nodes.
Definition: CreateIndex.cs:97
override bool IsAsynchronous
If the node (or its decendants) include asynchronous evaluation. Asynchronous nodes should be evaluat...
Definition: CreateIndex.cs:54
override bool Equals(object obj)
Definition: CreateIndex.cs:176
override async Task< IElement > EvaluateAsync(Variables Variables)
Evaluates the node asynchronously, using the variables provided in the Variables collection.
Definition: CreateIndex.cs:72
Executes an INSERT ... VALUES ... statement against the object database.
Definition: InsertValues.cs:16
static async Task< string > GetNameAsync(ScriptNode Node, Variables Variables)
Gets a name from a script node, either by using the name of a variable reference, or evaluating the n...
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:20
Interface for script nodes with asynchronous evaluation
Interface for data sources that can be used in SQL statements.
Definition: IDataSource.cs:13
Task CreateIndex(string Name, string[] Fields)
Creates an index in the source.
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