Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
OdbcStoredProcedure.cs
1using System;
2using System.Data.Common;
3using System.Data.Odbc;
4using System.Threading.Tasks;
9
11{
15 public class OdbcStoredProcedure : ILambdaExpression, IDisposable
16 {
17 private readonly MultiReadSingleWriteObject synchObj;
18 private readonly OdbcCommand command;
19 private readonly int nrParameters;
20 private readonly string[] parameterNames;
21 private readonly ArgumentType[] parameterTypes;
22
23 internal OdbcStoredProcedure(OdbcCommand Command)
24 {
25 this.synchObj = new MultiReadSingleWriteObject(this);
26 this.command = Command;
27 this.nrParameters = this.command.Parameters.Count;
28 this.parameterNames = new string[this.nrParameters];
29 this.parameterTypes = new ArgumentType[this.nrParameters];
30
31 for (int i = 0; i < this.nrParameters; i++)
32 {
33 this.parameterNames[i] = this.command.Parameters[i].ParameterName;
34 this.parameterTypes[i] = ArgumentType.Normal;
35 }
36 }
37
41 public int NrArguments => this.nrParameters;
42
46 public string[] ArgumentNames => this.parameterNames;
47
51 public ArgumentType[] ArgumentTypes => this.parameterTypes;
52
57 public bool IsAsynchronous => true;
58
62 public void Dispose()
63 {
64 this.command?.Dispose();
65 }
66
74 {
75 return this.EvaluateAsync(Arguments, Variables).Result;
76 }
77
84 public async Task<IElement> EvaluateAsync(IElement[] Arguments, Variables Variables)
85 {
86 int i;
87
88 await this.synchObj.BeginWrite();
89 try
90 {
91 for (i = 0; i < this.nrParameters; i++)
92 {
93 OdbcParameter Parameter = this.command.Parameters[i];
94 object Value = Arguments[i].AssociatedObjectValue;
95
96 switch (Parameter.OdbcType)
97 {
98 case OdbcType.Binary:
99 case OdbcType.Bit:
100 case OdbcType.Char:
101 case OdbcType.DateTime:
102 case OdbcType.Double:
103 case OdbcType.Image:
104 case OdbcType.NChar:
105 case OdbcType.NText:
106 case OdbcType.NVarChar:
107 case OdbcType.UniqueIdentifier:
108 case OdbcType.SmallDateTime:
109 case OdbcType.Text:
110 case OdbcType.Timestamp:
111 case OdbcType.VarBinary:
112 case OdbcType.VarChar:
113 case OdbcType.Date:
114 case OdbcType.Time:
115 default:
116 Parameter.Value = Value;
117 break;
118
119 case OdbcType.Decimal:
120 case OdbcType.Numeric:
121 if (Value is decimal Decimal)
122 Parameter.Value = Decimal;
123 else if (Value is double d)
124 Parameter.Value = (decimal)d;
125 else
126 Parameter.Value = Convert.ToDecimal(Value);
127 break;
128
129 case OdbcType.Real:
130 if (Value is float Single)
131 Parameter.Value = Single;
132 else if (Value is double d)
133 Parameter.Value = (float)d;
134 else
135 Parameter.Value = Convert.ToSingle(Value);
136 break;
137
138 case OdbcType.SmallInt:
139 if (Value is Int16 Int16)
140 Parameter.Value = Int16;
141 else if (Value is double d)
142 Parameter.Value = (Int16)d;
143 else
144 Parameter.Value = Convert.ToInt16(Value);
145 break;
146
147 case OdbcType.Int:
148 if (Value is Int32 Int32)
149 Parameter.Value = Int32;
150 else if (Value is double d)
151 Parameter.Value = (Int32)d;
152 else
153 Parameter.Value = Convert.ToInt32(Value);
154 break;
155
156 case OdbcType.BigInt:
157 if (Value is Int64 Int64)
158 Parameter.Value = Int64;
159 else if (Value is double d)
160 Parameter.Value = (Int64)d;
161 else
162 Parameter.Value = Convert.ToInt64(Value);
163 break;
164
165 case OdbcType.TinyInt:
166 if (Value is byte UI8)
167 Parameter.Value = UI8;
168 else if (Value is double d)
169 Parameter.Value = (byte)d;
170 else
171 Parameter.Value = Convert.ToByte(Value);
172 break;
173 }
174 }
175
176 DbDataReader Reader = await this.command.ExecuteReaderAsync();
177
178 return await Reader.ParseAndClose();
179 }
180 finally
181 {
182 await this.synchObj.EndWrite();
183 }
184 }
185
187 public override string ToString()
188 {
189 return LambdaDefinition.ToString(this);
190 }
191 }
192}
Represents an object that allows single concurrent writers but multiple concurrent readers....
Represents a stored precedure in an ODBC Database.
ArgumentType[] ArgumentTypes
Argument types.
async Task< IElement > EvaluateAsync(IElement[] Arguments, Variables Variables)
Evaluates the lambda expression.
IElement Evaluate(IElement[] Arguments, Variables Variables)
Evaluates the lambda expression.
bool IsAsynchronous
If the node (or its decendants) include asynchronous evaluation. Asynchronous nodes should be evaluat...
Collection of variables.
Definition: Variables.cs:25
Basic interface for all types of elements.
Definition: IElement.cs:20
object AssociatedObjectValue
Associated object value.
Definition: IElement.cs:33
Base interface for lambda expressions.
ArgumentType
Type of parameter used in a function definition or a lambda definition.
Definition: IFunction.cs:9