Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
DataExpression.cs
Go to the documentation of this file.
5
6namespace System.Data;
7
8internal sealed class DataExpression : IFilter
9{
10 internal string _originalExpression;
11
12 private readonly bool _parsed;
13
14 private bool _bound;
15
17
19
20 private readonly StorageType _storageType;
21
22 private readonly Type _dataType;
23
24 private DataColumn[] _dependency = Array.Empty<DataColumn>();
25
26 internal string Expression
27 {
28 get
29 {
30 if (_originalExpression == null)
31 {
32 return "";
33 }
35 }
36 }
37
39
40 internal bool HasValue => _expr != null;
41
42 [RequiresUnreferencedCode("Members of types used in the expression might be trimmed")]
43 internal DataExpression(DataTable table, string expression)
44 : this(table, expression, null)
45 {
46 }
47
48 [RequiresUnreferencedCode("Members of types used in the expression might be trimmed")]
49 internal DataExpression(DataTable table, string expression, Type type)
50 {
52 expressionParser.LoadExpression(expression);
54 _expr = null;
55 if (expression != null)
56 {
58 if (_storageType == StorageType.BigInteger)
59 {
61 }
63 _expr = expressionParser.Parse();
64 _parsed = true;
65 if (_expr != null && table != null)
66 {
67 Bind(table);
68 }
69 else
70 {
71 _bound = false;
72 }
73 }
74 }
75
76 internal void Bind(DataTable table)
77 {
78 _table = table;
79 if (table != null && _expr != null)
80 {
82 _expr.Bind(table, list);
84 _table = table;
85 _bound = true;
86 _dependency = list.ToArray();
87 }
88 }
89
91 {
92 if (_expr != null)
93 {
94 return _expr.DependsOn(column);
95 }
96 return false;
97 }
98
99 internal object Evaluate()
100 {
101 return Evaluate((DataRow)null, DataRowVersion.Default);
102 }
103
104 [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode", Justification = "Constructors taking expression are marked as unsafe")]
105 internal object Evaluate(DataRow row, DataRowVersion version)
106 {
107 if (!_bound)
108 {
109 Bind(_table);
110 }
111 object obj;
112 if (_expr != null)
113 {
114 obj = _expr.Eval(row, version);
115 if (obj != DBNull.Value || StorageType.Uri < _storageType)
116 {
117 try
118 {
119 if (StorageType.Object != _storageType)
120 {
122 }
123 }
125 {
128 }
129 }
130 }
131 else
132 {
133 obj = null;
134 }
135 return obj;
136 }
137
138 internal object Evaluate(DataRow[] rows)
139 {
140 return Evaluate(rows, DataRowVersion.Default);
141 }
142
143 [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode", Justification = "Constructors taking expression are marked as unsafe")]
144 internal object Evaluate(DataRow[] rows, DataRowVersion version)
145 {
146 if (!_bound)
147 {
148 Bind(_table);
149 }
150 if (_expr != null)
151 {
152 List<int> list = new List<int>();
153 foreach (DataRow dataRow in rows)
154 {
155 if (dataRow.RowState != DataRowState.Deleted && (version != DataRowVersion.Original || dataRow._oldRecord != -1))
156 {
157 list.Add(dataRow.GetRecordFromVersion(version));
158 }
159 }
160 int[] recordNos = list.ToArray();
161 return _expr.Eval(recordNos);
162 }
163 return DBNull.Value;
164 }
165
166 [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode", Justification = "Constructors taking expression are marked as unsafe")]
167 public bool Invoke(DataRow row, DataRowVersion version)
168 {
169 if (_expr == null)
170 {
171 return true;
172 }
173 if (row == null)
174 {
176 }
177 object value = _expr.Eval(row, version);
178 try
179 {
180 return ToBoolean(value);
181 }
182 catch (EvaluateException)
183 {
185 }
186 }
187
189 {
190 return _dependency;
191 }
192
193 internal bool IsTableAggregate()
194 {
195 if (_expr != null)
196 {
197 return _expr.IsTableConstant();
198 }
199 return false;
200 }
201
202 internal static bool IsUnknown(object value)
203 {
205 }
206
207 internal bool HasLocalAggregate()
208 {
209 if (_expr != null)
210 {
211 return _expr.HasLocalAggregate();
212 }
213 return false;
214 }
215
216 internal bool HasRemoteAggregate()
217 {
218 if (_expr != null)
219 {
220 return _expr.HasRemoteAggregate();
221 }
222 return false;
223 }
224
225 internal static bool ToBoolean(object value)
226 {
227 if (IsUnknown(value))
228 {
229 return false;
230 }
231 if (value is bool)
232 {
233 return (bool)value;
234 }
236 {
237 return sqlBoolean.IsTrue;
238 }
239 if (value is string)
240 {
241 try
242 {
243 return bool.Parse((string)value);
244 }
246 {
249 }
250 }
251 throw ExprException.DatavalueConvertion(value, typeof(bool), null);
252 }
253}
static readonly DBNull Value
Definition DBNull.cs:8
static bool IsCatchableExceptionType(Exception e)
Definition ADP.cs:790
static bool IsObjectNull(object value)
static StorageType GetStorageType(Type dataType)
static object ChangeType2(object value, StorageType stype, Type type, IFormatProvider formatProvider)
bool Invoke(DataRow row, DataRowVersion version)
DataExpression(DataTable table, string expression, Type type)
DataExpression(DataTable table, string expression)
object Evaluate(DataRow[] rows)
object Evaluate(DataRow[] rows, DataRowVersion version)
void Bind(DataTable table)
static bool ToBoolean(object value)
readonly StorageType _storageType
object Evaluate(DataRow row, DataRowVersion version)
static bool IsUnknown(object value)
bool DependsOn(DataColumn column)
IFormatProvider FormatProvider
Definition DataTable.cs:435
static Exception TraceExceptionForCapture(Exception e)
static Exception FilterConvertion(string expr)
static Exception InvokeArgument()
static Exception UnsupportedDataType(Type type)
static Exception DatavalueConvertion(object value, Type type, Exception innerException)
void Bind(DataTable table, List< DataColumn > list)
virtual bool DependsOn(DataColumn column)
ExpressionNode Optimize()