Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
MsSqlStoredProcedure.cs
1using System;
2using System.Data;
3using System.Data.SqlClient;
4using System.Threading.Tasks;
9
11{
15 public class MsSqlStoredProcedure : ILambdaExpression, IDisposable
16 {
17 private readonly MultiReadSingleWriteObject synchObj;
18 private readonly SqlCommand command;
19 private readonly int nrParameters;
20 private readonly string[] parameterNames;
21 private readonly ArgumentType[] parameterTypes;
22
23 internal MsSqlStoredProcedure(SqlCommand 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 SqlParameter Parameter = this.command.Parameters[i];
94 object Value = Arguments[i].AssociatedObjectValue;
95
96 switch (Parameter.SqlDbType)
97 {
98 case SqlDbType.Binary:
99 case SqlDbType.Bit:
100 case SqlDbType.Char:
101 case SqlDbType.DateTime:
102 case SqlDbType.Float:
103 case SqlDbType.Image:
104 case SqlDbType.NChar:
105 case SqlDbType.NText:
106 case SqlDbType.NVarChar:
107 case SqlDbType.UniqueIdentifier:
108 case SqlDbType.SmallDateTime:
109 case SqlDbType.Text:
110 case SqlDbType.Timestamp:
111 case SqlDbType.VarBinary:
112 case SqlDbType.VarChar:
113 case SqlDbType.Variant:
114 case SqlDbType.Xml:
115 case SqlDbType.Udt:
116 case SqlDbType.Structured:
117 case SqlDbType.Date:
118 case SqlDbType.Time:
119 case SqlDbType.DateTime2:
120 case SqlDbType.DateTimeOffset:
121 default:
122 Parameter.Value = Value;
123 break;
124
125 case SqlDbType.Decimal:
126 case SqlDbType.SmallMoney:
127 case SqlDbType.Money:
128 if (Value is decimal Decimal)
129 Parameter.Value = Decimal;
130 else if (Value is double d)
131 Parameter.Value = (decimal)d;
132 else
133 Parameter.Value = Convert.ToDecimal(Value);
134 break;
135
136 case SqlDbType.Real:
137 if (Value is float Single)
138 Parameter.Value = Single;
139 else if (Value is double d)
140 Parameter.Value = (float)d;
141 else
142 Parameter.Value = Convert.ToSingle(Value);
143 break;
144
145 case SqlDbType.SmallInt:
146 if (Value is Int16 Int16)
147 Parameter.Value = Int16;
148 else if (Value is double d)
149 Parameter.Value = (Int16)d;
150 else
151 Parameter.Value = Convert.ToInt16(Value);
152 break;
153
154 case SqlDbType.Int:
155 if (Value is Int32 Int32)
156 Parameter.Value = Int32;
157 else if (Value is double d)
158 Parameter.Value = (Int32)d;
159 else
160 Parameter.Value = Convert.ToInt32(Value);
161 break;
162
163 case SqlDbType.BigInt:
164 if (Value is Int64 Int64)
165 Parameter.Value = Int64;
166 else if (Value is double d)
167 Parameter.Value = (Int64)d;
168 else
169 Parameter.Value = Convert.ToInt64(Value);
170 break;
171
172 case SqlDbType.TinyInt:
173 if (Value is byte UI8)
174 Parameter.Value = UI8;
175 else if (Value is double d)
176 Parameter.Value = (byte)d;
177 else
178 Parameter.Value = Convert.ToByte(Value);
179 break;
180 }
181 }
182
183 SqlDataReader Reader = await this.command.ExecuteReaderAsync();
184
185 return await Reader.ParseAndClose();
186 }
187 finally
188 {
189 await this.synchObj.EndWrite();
190 }
191 }
192
194 public override string ToString()
195 {
196 return LambdaDefinition.ToString(this);
197 }
198 }
199}
Represents an object that allows single concurrent writers but multiple concurrent readers....
Represents a stored precedure in a MS SQL Database.
IElement Evaluate(IElement[] Arguments, Variables Variables)
Evaluates the lambda expression.
async Task< IElement > EvaluateAsync(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