Welcome to mirror list, hosted at ThFree Co, Russian Federation.

DataExpression.cs « Filter « Data « System « System.Data « referencesource « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2a13366e59e4a4cd34096a6246d0432cd6c9aa91 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
//------------------------------------------------------------------------------
// <copyright file="DataExpression.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
// <owner current="true" primary="true">[....]</owner>
// <owner current="true" primary="false">[....]</owner>
// <owner current="false" primary="false">[....]</owner>
//------------------------------------------------------------------------------

namespace System.Data {
    using System;
    using System.Diagnostics;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data.SqlTypes;
    using System.Data.Common;

    internal sealed class DataExpression : IFilter {
        internal string originalExpression = null;  // original, unoptimized string

        private bool parsed = false;
        private bool bound = false;
        private ExpressionNode expr = null;
        private DataTable table = null;
        private readonly StorageType _storageType;
        private readonly Type _dataType;  // This set if the expression is part of ExpressionCoulmn
        private DataColumn[] dependency = DataTable.zeroColumns;

        internal DataExpression(DataTable table, string expression) : this(table, expression, null) {
        }

        internal DataExpression(DataTable table, string expression, Type type) {
            ExpressionParser parser = new ExpressionParser(table);
            parser.LoadExpression(expression);

            originalExpression = expression;
            expr = null;

            if (expression != null) {
                _storageType = DataStorage.GetStorageType(type);
                if (_storageType == StorageType.BigInteger)
                {
                    throw ExprException.UnsupportedDataType(type);
                }

                _dataType = type;
                expr = parser.Parse();
                parsed = true;
                if (expr != null && table != null) {
                    this.Bind(table);
                }
                else {
                    bound = false;
                }
            }
        }

        internal string Expression {
            get {
                return (originalExpression != null ? originalExpression : ""); // 
            }
        }

        internal ExpressionNode ExpressionNode {
            get {
                return expr;
            }
        }

        internal bool HasValue {
            get {
                return (null != expr);
            }
        }

        internal void Bind(DataTable table) {
            this.table = table;

            if (table == null)
                return;

            if (expr != null) {
                Debug.Assert(parsed, "Invalid calling order: Bind() before Parse()");
                List<DataColumn> list = new List<DataColumn>();
                expr.Bind(table, list);
                expr = expr.Optimize();
                this.table = table;
                bound = true;
                dependency = list.ToArray();
            }
        }

        internal bool DependsOn(DataColumn column) {
            if (expr != null) {
                return expr.DependsOn(column);
            }
            else {
                return false;
            }
        }

        internal object Evaluate() {
            return Evaluate((DataRow)null, DataRowVersion.Default);
        }

        internal object Evaluate(DataRow row, DataRowVersion version) {
            object result;

            if (!bound) {
                this.Bind(this.table);
            }
            if (expr != null) {
                result = expr.Eval(row, version);
                // if the type is a SqlType (StorageType.Uri < _storageType), convert DBNull values.
                if (result != DBNull.Value || StorageType.Uri < _storageType) {
                    // we need to convert the return value to the column.Type;
                    try {
                        if (StorageType.Object != _storageType) {
                            result = SqlConvert.ChangeType2(result, _storageType, _dataType, table.FormatProvider);
                        }
                    }
                    catch (Exception e) {
                        // 
                        if (!ADP.IsCatchableExceptionType(e)) {
                            throw;
                        }
                        ExceptionBuilder.TraceExceptionForCapture(e);

                        // 

                        throw ExprException.DatavalueConvertion(result, _dataType, e);
                    }
                }
            }
            else {
                result = null;
            }
            return result;
        }

        internal object Evaluate(DataRow[] rows) {
            return Evaluate(rows, DataRowVersion.Default);
        }


        internal object Evaluate(DataRow[] rows, DataRowVersion version) {
            if (!bound) {
                this.Bind(this.table);
            }
            if (expr != null) {
                List<int> recordList = new List<int>();
                foreach(DataRow row in rows) {
                    if (row.RowState == DataRowState.Deleted)
                        continue;
                    if (version == DataRowVersion.Original && row.oldRecord == -1)
                        continue;
                    recordList.Add(row.GetRecordFromVersion(version));
                }
                int[]  records = recordList.ToArray();
                return expr.Eval(records);
            }
            else {
                return DBNull.Value;
            }
        }

        public bool Invoke(DataRow row, DataRowVersion version) {
            if (expr == null)
                return true;

            if (row == null) {
                throw ExprException.InvokeArgument();
            }
            object val = expr.Eval(row, version);
            bool result;
            try {
                result = ToBoolean(val);
            }
            catch (EvaluateException) {
                throw ExprException.FilterConvertion(Expression);
            }
            return result;
        }

        internal DataColumn[] GetDependency() {
            Debug.Assert(dependency != null, "GetDependencies: null, we should have created an empty list");
            return dependency;
        }

        internal bool IsTableAggregate() {
            if (expr != null)
                return expr.IsTableConstant();
            else
                return false;
        }

        internal static bool IsUnknown(object value) {
            return DataStorage.IsObjectNull(value);
        }

        internal bool HasLocalAggregate() {
            if (expr != null)
                return expr.HasLocalAggregate();
            else
                return false;
        }
        
        internal bool HasRemoteAggregate() {
            if (expr != null)
                return expr.HasRemoteAggregate();
            else
                return false;
        }

        internal static bool ToBoolean(object value) {
            if (IsUnknown(value))
                return false;
            if (value is bool)
                return(bool)value;
            if (value is SqlBoolean){
                return (((SqlBoolean)value).IsTrue);
            }
//check for SqlString is not added, value for true and false should be given with String, not with SqlString
            if (value is string) {
                try {
                    return Boolean.Parse((string)value);
                }
                catch (Exception e) {
                    // 
                    if (!ADP.IsCatchableExceptionType(e)) {
                        throw;
                    }
                    ExceptionBuilder.TraceExceptionForCapture(e);
                    // 
                    throw ExprException.DatavalueConvertion(value, typeof(bool), e);
                }
            }

            throw ExprException.DatavalueConvertion(value, typeof(bool), null);
        }
    }

    
}