File: SqlParameterizer.cs

package info (click to toggle)
mono 6.12.0.199%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 1,296,836 kB
  • sloc: cs: 11,181,803; xml: 2,850,076; ansic: 699,709; cpp: 123,344; perl: 59,361; javascript: 30,841; asm: 21,853; makefile: 20,405; sh: 15,009; python: 4,839; pascal: 925; sql: 859; sed: 16; php: 1
file content (305 lines) | stat: -rw-r--r-- 12,860 bytes parent folder | download | duplicates (5)
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data.Linq.Mapping;
using System.Data.Linq.Provider;
using System.Linq.Expressions;
using System.Diagnostics.CodeAnalysis;

namespace System.Data.Linq.SqlClient {

    internal enum SqlParameterType {
        Value,
        UserArgument,
        PreviousResult
    }

    internal class SqlParameterInfo {
        SqlParameter parameter;
        object value;
        Delegate accessor;
        internal SqlParameterInfo(SqlParameter parameter, Delegate accessor) {
            this.parameter = parameter;
            this.accessor = accessor;
        }
        internal SqlParameterInfo(SqlParameter parameter, object value) {
            this.parameter = parameter;
            this.value = value;
        }
        internal SqlParameterInfo(SqlParameter parameter) {
            this.parameter = parameter;
        }
        internal SqlParameterType Type {
            get {
                if (this.accessor != null) {
                    return SqlParameterType.UserArgument;
                }
                else if (this.parameter.Name == "@ROWCOUNT") {
                    return SqlParameterType.PreviousResult;
                }
                else {
                    return SqlParameterType.Value;
                }
            }
        }
        internal SqlParameter Parameter {
            get { return this.parameter; }
        }
        internal Delegate Accessor {
            get { return this.accessor; }
        }
        internal object Value {
            get { return this.value; }
        }
    }

    internal class SqlParameterizer {
        TypeSystemProvider typeProvider;
        SqlNodeAnnotations annotations;
        int index;

        internal SqlParameterizer(TypeSystemProvider typeProvider, SqlNodeAnnotations annotations) {
            this.typeProvider = typeProvider;
            this.annotations = annotations;
        }

        internal ReadOnlyCollection<SqlParameterInfo> Parameterize(SqlNode node) {
            return this.ParameterizeInternal(node).AsReadOnly();
        }

        private List<SqlParameterInfo> ParameterizeInternal(SqlNode node) {
            Visitor v = new Visitor(this);
            v.Visit(node);
            return new List<SqlParameterInfo>(v.currentParams);
        }

        internal ReadOnlyCollection<ReadOnlyCollection<SqlParameterInfo>> ParameterizeBlock(SqlBlock block) {
            SqlParameterInfo rowStatus =
                new SqlParameterInfo(
                    new SqlParameter(typeof(int), typeProvider.From(typeof(int)), "@ROWCOUNT", block.SourceExpression)
                    );
            List<ReadOnlyCollection<SqlParameterInfo>> list = new List<ReadOnlyCollection<SqlParameterInfo>>();
            for (int i = 0, n = block.Statements.Count; i < n; i++) {
                SqlNode statement = block.Statements[i];
                List<SqlParameterInfo> parameters = this.ParameterizeInternal(statement);
                if (i > 0) {
                    parameters.Add(rowStatus);
                }
                list.Add(parameters.AsReadOnly());
            }
            return list.AsReadOnly();
        }

        internal virtual string CreateParameterName() {
            return "@p" + this.index++;
        }

        class Visitor : SqlVisitor {
            private SqlParameterizer parameterizer;
            internal Dictionary<object, SqlParameterInfo> map;
            internal List<SqlParameterInfo> currentParams;
            private bool topLevel;
            private ProviderType timeProviderType;  // for special case handling of DateTime parameters

            internal Visitor(SqlParameterizer parameterizer) {
                this.parameterizer = parameterizer;
                this.topLevel = true;
                this.map = new Dictionary<object, SqlParameterInfo>();
                this.currentParams = new List<SqlParameterInfo>();
            }

            private SqlParameter InsertLookup(SqlValue cp) {
                SqlParameterInfo pi = null;
                if (!this.map.TryGetValue(cp, out pi)) {
                    SqlParameter p;
                    if (this.timeProviderType == null) {
                        p = new SqlParameter(cp.ClrType, cp.SqlType, this.parameterizer.CreateParameterName(), cp.SourceExpression);
                        pi = new SqlParameterInfo(p, cp.Value);
                    }
                    else {
                        p = new SqlParameter(cp.ClrType, this.timeProviderType, this.parameterizer.CreateParameterName(), cp.SourceExpression);
                        pi = new SqlParameterInfo(p, ((DateTime)cp.Value).TimeOfDay);
                    }
                    this.map.Add(cp, pi);
                    this.currentParams.Add(pi);
                }
                return pi.Parameter;
            }

            internal override SqlExpression VisitBinaryOperator(SqlBinary bo)
            {
                //
                // Special case to allow DateTime CLR type to be passed as a paramater where
                // a SQL type TIME is expected. We do this only for the equality/inequality
                // comparisons.
                //
                switch (bo.NodeType) {
                    case SqlNodeType.EQ:
                    case SqlNodeType.EQ2V:
                    case SqlNodeType.NE:
                    case SqlNodeType.NE2V: {
                        SqlDbType leftSqlDbType = ((SqlTypeSystem.SqlType)(bo.Left.SqlType)).SqlDbType;
                        SqlDbType rightSqlDbType = ((SqlTypeSystem.SqlType)(bo.Right.SqlType)).SqlDbType;
                        if (leftSqlDbType == rightSqlDbType)
                            break;

                        bool isLeftColRef = bo.Left is SqlColumnRef;
                        bool isRightColRef = bo.Right is SqlColumnRef;
                        if (isLeftColRef == isRightColRef)
                            break;

                        if (isLeftColRef && leftSqlDbType == SqlDbType.Time && bo.Right.ClrType == typeof(DateTime))
                            this.timeProviderType = bo.Left.SqlType;
                        else if (isRightColRef && rightSqlDbType == SqlDbType.Time && bo.Left.ClrType == typeof(DateTime))
                            this.timeProviderType = bo.Left.SqlType;
                        break;
                    }
                }
                base.VisitBinaryOperator(bo);
                return bo;
            }

            internal override SqlSelect VisitSelect(SqlSelect select) {
                bool saveTop = this.topLevel;
                this.topLevel = false;
                select = this.VisitSelectCore(select);
                this.topLevel = saveTop;
                select.Selection = this.VisitExpression(select.Selection);
                return select;
            }

            internal override SqlUserQuery VisitUserQuery(SqlUserQuery suq) {
                bool saveTop = this.topLevel;
                this.topLevel = false;
                for (int i = 0, n = suq.Arguments.Count; i < n; i++) {
                    suq.Arguments[i] = this.VisitParameter(suq.Arguments[i]);
                }
                this.topLevel = saveTop;
                suq.Projection = this.VisitExpression(suq.Projection);
                return suq;
            }

            [SuppressMessage("Microsoft.Design", "CA1061:DoNotHideBaseClassMethods", Justification="Unknown reason.")]
            internal SqlExpression VisitParameter(SqlExpression expr) {
                SqlExpression result = this.VisitExpression(expr);
                switch (result.NodeType) {
                    case SqlNodeType.Parameter:
                        return (SqlParameter)result;
                    case SqlNodeType.Value:
                        // force even literal values to become parameters
                        return this.InsertLookup((SqlValue)result);
                    default:
                        System.Diagnostics.Debug.Assert(false);
                        return result;
                }
            }

            internal override SqlStoredProcedureCall VisitStoredProcedureCall(SqlStoredProcedureCall spc) {
                this.VisitUserQuery(spc);

                for (int i = 0, n = spc.Function.Parameters.Count; i < n; i++) {
                    MetaParameter mp = spc.Function.Parameters[i];
                    SqlParameter arg = spc.Arguments[i] as SqlParameter;
                    if (arg != null) {
                        arg.Direction = this.GetParameterDirection(mp);
                        if (arg.Direction == ParameterDirection.InputOutput ||
                            arg.Direction == ParameterDirection.Output) {
                            // Text, NText and Image parameters cannot be used as output parameters
                            // so we retype them if necessary.
                            RetypeOutParameter(arg);
                        }
                    }
                }

                // add default return value 
                SqlParameter p = new SqlParameter(typeof(int?), this.parameterizer.typeProvider.From(typeof(int)), "@RETURN_VALUE", spc.SourceExpression);
                p.Direction = System.Data.ParameterDirection.Output;
                this.currentParams.Add(new SqlParameterInfo(p));

                return spc;
            }

            private bool RetypeOutParameter(SqlParameter node) {
                if (!node.SqlType.IsLargeType) {
                    return false;
                }
                ProviderType newType = this.parameterizer.typeProvider.GetBestLargeType(node.SqlType);
                if (node.SqlType != newType) {
                    node.SetSqlType(newType);
                    return true;
                }
                // Since we are dealing with a long out parameter that hasn't been
                // retyped, we need to annotate
                this.parameterizer.annotations.Add(
                    node, 
                    new SqlServerCompatibilityAnnotation(
#if (MONO)
                        Strings.MaxSizeNotSupported(node.SourceExpression), SqlProvider.ProviderMode.Sql2000));
#else
                        SqlClient.Strings.MaxSizeNotSupported(node.SourceExpression), SqlProvider.ProviderMode.Sql2000));
#endif
                return false;
            }

            [SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic", Justification="Unknown reason.")]
            private System.Data.ParameterDirection GetParameterDirection(MetaParameter p) {
                if (p.Parameter.IsRetval) {
                    return System.Data.ParameterDirection.ReturnValue;
                }
                else if (p.Parameter.IsOut) {
                    return System.Data.ParameterDirection.Output;
                }
                else if (p.Parameter.ParameterType.IsByRef) {
                    return System.Data.ParameterDirection.InputOutput;
                }
                else {
                    return System.Data.ParameterDirection.Input;
                }
            }

            internal override SqlStatement VisitInsert(SqlInsert sin) {
                bool saveTop = this.topLevel;
                this.topLevel = false;
                base.VisitInsert(sin);
                this.topLevel = saveTop;
                return sin;
            }

            internal override SqlStatement VisitUpdate(SqlUpdate sup) {
                bool saveTop = this.topLevel;
                this.topLevel = false;
                base.VisitUpdate(sup);
                this.topLevel = saveTop;
                return sup;
            }

            internal override SqlStatement VisitDelete(SqlDelete sd) {
                bool saveTop = this.topLevel;
                this.topLevel = false;
                base.VisitDelete(sd);
                this.topLevel = saveTop;
                return sd;
            }

            internal override SqlExpression VisitValue(SqlValue value) {
                if (this.topLevel || !value.IsClientSpecified || !value.SqlType.CanBeParameter) {
                    return value;
                }
                else {
                    return this.InsertLookup(value);
                }
            }

            internal override SqlExpression VisitClientParameter(SqlClientParameter cp) {
                if (cp.SqlType.CanBeParameter) {
                    SqlParameter p = new SqlParameter(cp.ClrType, cp.SqlType, this.parameterizer.CreateParameterName(), cp.SourceExpression);
                    this.currentParams.Add(new SqlParameterInfo(p, cp.Accessor.Compile()));
                    return p;
                }
                else {
                    return cp;
                }
            }
        }
    }
}