File: FunctionDetailsReader.cs

package info (click to toggle)
mono 4.6.2.7%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 778,148 kB
  • ctags: 914,052
  • sloc: cs: 5,779,509; xml: 2,773,713; ansic: 432,645; sh: 14,749; makefile: 12,361; perl: 2,488; python: 1,434; cpp: 849; asm: 531; sql: 95; sed: 16; php: 1
file content (553 lines) | stat: -rw-r--r-- 19,108 bytes parent folder | download
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
//---------------------------------------------------------------------
// <copyright file="FunctionDetailsReader.cs" company="Microsoft">
//      Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//
// @owner       [....]
// @backupOwner [....]
//---------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.Common;
using System.Data.EntityClient;
using System.Diagnostics;
using System.Data.Metadata.Edm;

namespace System.Data.Entity.Design.SsdlGenerator
{
    /// <summary>
    /// The purpose of this class is to give us strongly typed access to the results of the reader.
    /// NOTE: this class will dispose of the command when the reader is disposed.
    /// </summary>
    internal abstract class FunctionDetailsReader : IDisposable
    {
        private DbDataReader _reader;
        private EntityCommand _command;
        private EntityConnection _connection;
        private object[] _currentRow;

        public static FunctionDetailsReader Create(EntityConnection connection, IEnumerable<EntityStoreSchemaFilterEntry> filters, Version storeSchemaModelVersion)
        {
            Debug.Assert(connection != null, "the parameter connection is null");
            Debug.Assert(connection.State == ConnectionState.Open, "the connection is not Open");

            if (storeSchemaModelVersion >= EntityFrameworkVersions.Version3)
            {
                return new FunctionDetailsReaderV3(connection, filters);
            }
            else
            {
                return new FunctionDetailsReaderV1(connection, filters);
            }
        }

        protected void InitializeReader(EntityConnection connection, IEnumerable<EntityStoreSchemaFilterEntry> filters)
        {
            _connection = connection;

            _command = EntityStoreSchemaGeneratorDatabaseSchemaLoader.CreateFilteredCommand(
                        _connection,
                        FunctionDetailSql,
                        FunctionOrderByClause,
                        EntityStoreSchemaFilterObjectTypes.Function,
                        new List<EntityStoreSchemaFilterEntry>(filters),
                        new string[] { FunctionDetailAlias });
            _reader = _command.ExecuteReader(CommandBehavior.SequentialAccess);
        }

        internal bool Read()
        {
            Debug.Assert(_reader != null, "don't Read() when it is created from a memento");
            bool haveRow = _reader.Read();
            if (haveRow)
            {
                if (_currentRow == null)
                {
                    _currentRow = new object[ColumnCount];
                }
                _reader.GetValues(_currentRow);
            }
            else
            {
                _currentRow = null;
            }
            return haveRow;
        }

        public void Dispose()
        {
            // Technically, calling GC.SuppressFinalize is not required because the class does not
            // have a finalizer, but it does no harm, protects against the case where a finalizer is added
            // in the future, and prevents an FxCop warning.
            GC.SuppressFinalize(this);
            Debug.Assert(_reader != null, "don't Dispose() when it is created from a memento");
            _reader.Dispose();
            _command.Dispose();
        }

        internal abstract int ColumnCount { get; }

        public abstract string Catalog { get; }

        public abstract string Schema { get; }

        public abstract string ProcedureName { get; }

        public abstract string ReturnType { get; }

        public abstract bool IsIsAggregate { get; }

        public abstract bool IsBuiltIn { get; }

        public abstract bool IsComposable { get; }

        public abstract bool IsNiladic { get; }

        public abstract bool IsTvf { get; }

        public abstract string ParameterName { get; }

        public abstract bool IsParameterNameNull { get; }

        public abstract string ParameterType { get; }

        public abstract bool IsParameterTypeNull { get; }

        public abstract string ProcParameterMode { get; }

        public abstract bool IsParameterModeNull { get; }

        public bool TryGetParameterMode(out ParameterMode mode)
        {
            if (IsParameterModeNull)
            {
                mode = (ParameterMode)(-1);
                return false;
            }

            switch (ProcParameterMode)
            {
                case "IN":
                    mode = ParameterMode.In;
                    return true;
                case "OUT":
                    mode = ParameterMode.Out;
                    return true;
                case "INOUT":
                    mode = ParameterMode.InOut;
                    return true;
                default:
                    mode = (ParameterMode)(-1);
                    return false;
            }
        }

        internal EntityStoreSchemaGenerator.DbObjectKey CreateDbObjectKey()
        {
            Debug.Assert(_currentRow != null, "don't call this method when you not reading");
            return new EntityStoreSchemaGenerator.DbObjectKey(this.Catalog, this.Schema, this.ProcedureName, EntityStoreSchemaGenerator.DbObjectType.Function);
        }

        protected static T ConvertDBNull<T>(object value)
        {
            return Convert.IsDBNull(value) ? default(T) : (T)value;
        }

        public abstract void Attach(Memento memento);

        public abstract Memento CreateMemento();

        private static readonly string FunctionDetailAlias = "sp";
        protected abstract string FunctionDetailSql { get; }
        private static readonly string FunctionOrderByClause = @" 
            ORDER BY
                sp.SchemaName
            ,   sp.Name
            ,   sp.Ordinal
            ";

        internal sealed class FunctionDetailsReaderV1 : FunctionDetailsReader
        {
            internal FunctionDetailsReaderV1(MementoV1 memento)
            {
                _currentRow = memento.Values;
            }

            public FunctionDetailsReaderV1(EntityConnection connection, IEnumerable<EntityStoreSchemaFilterEntry> filters)
            {
                InitializeReader(connection, filters);
            }

            public override void Attach(Memento memento)
            {
                Debug.Assert(memento != null, "the parameter memento is null");
                Debug.Assert(memento.Values != null, "the values in the memento are null");
                Debug.Assert(memento is MementoV1, "the memento is for a different version");
                Debug.Assert(_reader == null, "don't attach to a real reader");
                _currentRow = memento.Values;
            }

            public override Memento CreateMemento()
            {
                Debug.Assert(_currentRow != null, "don't call this method when you not reading");
                return new MementoV1((object[])_currentRow.Clone());
            }

            const int PROC_SCHEMA_INDEX = 0;
            const int PROC_NAME_INDEX = 1;
            const int PROC_RET_TYPE_INDEX = 2;
            const int PROC_ISAGGREGATE_INDEX = 3;
            const int PROC_ISCOMPOSABLE_INDEX = 4;
            const int PROC_ISBUILTIN_INDEX = 5;
            const int PROC_ISNILADIC_INDEX = 6;
            const int PARAM_NAME_INDEX = 7;
            const int PARAM_TYPE_INDEX = 8;
            const int PARAM_DIRECTION_INDEX = 9;

            internal override int ColumnCount { get { return 10; } }

            public override string Catalog { get { return null; } }

            public override string Schema
            {
                get { return ConvertDBNull<string>(_currentRow[PROC_SCHEMA_INDEX]); }
            }

            public override string ProcedureName
            {
                get { return ConvertDBNull<string>(_currentRow[PROC_NAME_INDEX]); }
            }

            public override string ReturnType
            {
                get { return ConvertDBNull<string>(_currentRow[PROC_RET_TYPE_INDEX]); }
            }

            public override bool IsIsAggregate
            {
                get { return ConvertDBNull<bool>(_currentRow[PROC_ISAGGREGATE_INDEX]); }
            }

            public override bool IsBuiltIn
            {
                get { return ConvertDBNull<bool>(_currentRow[PROC_ISBUILTIN_INDEX]); }
            }

            public override bool IsComposable
            {
                get { return ConvertDBNull<bool>(_currentRow[PROC_ISCOMPOSABLE_INDEX]); }
            }

            public override bool IsNiladic
            {
                get { return ConvertDBNull<bool>(_currentRow[PROC_ISNILADIC_INDEX]); }
            }

            public override bool IsTvf { get { return false; } }

            public override string ParameterName
            {
                get { return (string)_currentRow[PARAM_NAME_INDEX]; }
            }

            public override bool IsParameterNameNull
            {
                get { return Convert.IsDBNull(_currentRow[PARAM_NAME_INDEX]); }
            }

            public override string ParameterType
            {
                get { return (string)_currentRow[PARAM_TYPE_INDEX]; }
            }

            public override bool IsParameterTypeNull
            {
                get { return Convert.IsDBNull(_currentRow[PARAM_TYPE_INDEX]); }
            }

            public override string ProcParameterMode
            {
                get { return (string)_currentRow[PARAM_DIRECTION_INDEX]; }
            }

            public override bool IsParameterModeNull
            {
                get { return Convert.IsDBNull(_currentRow[PARAM_DIRECTION_INDEX]); }
            }

            protected override string FunctionDetailSql
            {
                get
                {
                    return @"
            SELECT
                  sp.SchemaName
                , sp.Name 
                , sp.ReturnTypeName
                , sp.IsAggregate
                , sp.IsComposable
                , sp.IsBuiltIn
                , sp.IsNiladic
                , sp.ParameterName
                , sp.ParameterType
                , sp.Mode
            FROM (  
            (SELECT
                  r.CatalogName as CatalogName
              ,   r.SchemaName as SchemaName
              ,   r.Name as Name
              ,   r.ReturnType.TypeName as ReturnTypeName
              ,   r.IsAggregate as IsAggregate
              ,   true as IsComposable
              ,   r.IsBuiltIn as IsBuiltIn
              ,   r.IsNiladic as IsNiladic
              ,   p.Name as ParameterName
              ,   p.ParameterType.TypeName as ParameterType
              ,   p.Mode as Mode
              ,   p.Ordinal as Ordinal
            FROM
                OfType(SchemaInformation.Functions, Store.ScalarFunction) as r 
                 OUTER APPLY
                r.Parameters as p)
            UNION ALL
            (SELECT
                  r.CatalogName as CatalogName
              ,   r.SchemaName as SchemaName
              ,   r.Name as Name
              ,   CAST(NULL as string) as ReturnTypeName
              ,   false as IsAggregate
              ,   false as IsComposable
              ,   false as IsBuiltIn
              ,   false as IsNiladic
              ,   p.Name as ParameterName
              ,   p.ParameterType.TypeName as ParameterType
              ,   p.Mode as Mode
              ,   p.Ordinal as Ordinal
            FROM
                SchemaInformation.Procedures as r 
                 OUTER APPLY
                r.Parameters as p)) as sp
            ";
                }
            }
        }

        internal sealed class FunctionDetailsReaderV3 : FunctionDetailsReader
        {
            internal FunctionDetailsReaderV3(MementoV3 memento)
            {
                _currentRow = memento.Values;
            }

            public FunctionDetailsReaderV3(EntityConnection connection, IEnumerable<EntityStoreSchemaFilterEntry> filters)
            {
                InitializeReader(connection, filters);
            }

            public override void Attach(Memento memento)
            {
                Debug.Assert(memento != null, "the parameter memento is null");
                Debug.Assert(memento.Values != null, "the values in the memento are null");
                Debug.Assert(memento is MementoV3, "the memento is for a different version");
                Debug.Assert(_reader == null, "don't attach to a real reader");
                _currentRow = memento.Values;
            }

            public override Memento CreateMemento()
            {
                Debug.Assert(_currentRow != null, "don't call this method when you not reading");
                return new MementoV3((object[])_currentRow.Clone());
            }

            const int PROC_CATALOG_INDEX = 0;
            const int PROC_SCHEMA_INDEX = 1;
            const int PROC_NAME_INDEX = 2;
            const int PROC_RET_TYPE_INDEX = 3;
            const int PROC_ISAGGREGATE_INDEX = 4;
            const int PROC_ISCOMPOSABLE_INDEX = 5;
            const int PROC_ISBUILTIN_INDEX = 6;
            const int PROC_ISNILADIC_INDEX = 7;
            const int PROC_ISTVF_INDEX = 8;
            const int PARAM_NAME_INDEX = 9;
            const int PARAM_TYPE_INDEX = 10;
            const int PARAM_DIRECTION_INDEX = 11;

            internal override int ColumnCount { get { return 12; } }

            public override string Catalog
            {
                get { return ConvertDBNull<string>(_currentRow[PROC_CATALOG_INDEX]); }
            }

            public override string Schema
            {
                get { return ConvertDBNull<string>(_currentRow[PROC_SCHEMA_INDEX]); }
            }

            public override string ProcedureName
            {
                get { return ConvertDBNull<string>(_currentRow[PROC_NAME_INDEX]); }
            }

            public override string ReturnType
            {
                get { return ConvertDBNull<string>(_currentRow[PROC_RET_TYPE_INDEX]); }
            }

            public override bool IsIsAggregate
            {
                get { return ConvertDBNull<bool>(_currentRow[PROC_ISAGGREGATE_INDEX]); }
            }

            public override bool IsBuiltIn
            {
                get { return ConvertDBNull<bool>(_currentRow[PROC_ISBUILTIN_INDEX]); }
            }

            public override bool IsComposable
            {
                get { return ConvertDBNull<bool>(_currentRow[PROC_ISCOMPOSABLE_INDEX]); }
            }

            public override bool IsNiladic
            {
                get { return ConvertDBNull<bool>(_currentRow[PROC_ISNILADIC_INDEX]); }
            }

            public override bool IsTvf
            {
                get { return ConvertDBNull<bool>(_currentRow[PROC_ISTVF_INDEX]); }
            }

            public override string ParameterName
            {
                get { return (string)_currentRow[PARAM_NAME_INDEX]; }
            }

            public override bool IsParameterNameNull
            {
                get { return Convert.IsDBNull(_currentRow[PARAM_NAME_INDEX]); }
            }

            public override string ParameterType
            {
                get { return (string)_currentRow[PARAM_TYPE_INDEX]; }
            }

            public override bool IsParameterTypeNull
            {
                get { return Convert.IsDBNull(_currentRow[PARAM_TYPE_INDEX]); }
            }

            public override string ProcParameterMode
            {
                get { return (string)_currentRow[PARAM_DIRECTION_INDEX]; }
            }

            public override bool IsParameterModeNull
            {
                get { return Convert.IsDBNull(_currentRow[PARAM_DIRECTION_INDEX]); }
            }

            protected override string FunctionDetailSql
            {
                get
                {
                    return @"
            Function IsTvf(f Store.Function) as (f is of (Store.TableValuedFunction))
            SELECT
                  sp.CatalogName
                , sp.SchemaName
                , sp.Name 
                , sp.ReturnTypeName
                , sp.IsAggregate
                , sp.IsComposable
                , sp.IsBuiltIn
                , sp.IsNiladic
                , sp.IsTvf
                , sp.ParameterName
                , sp.ParameterType
                , sp.Mode
            FROM (  
            (SELECT
                  r.CatalogName as CatalogName
              ,   r.SchemaName as SchemaName
              ,   r.Name as Name
              ,   TREAT(r as Store.ScalarFunction).ReturnType.TypeName as ReturnTypeName
              ,   TREAT(r as Store.ScalarFunction).IsAggregate as IsAggregate
              ,   true as IsComposable
              ,   r.IsBuiltIn as IsBuiltIn
              ,   r.IsNiladic as IsNiladic
              ,   IsTvf(r) as IsTvf
              ,   p.Name as ParameterName
              ,   p.ParameterType.TypeName as ParameterType
              ,   p.Mode as Mode
              ,   p.Ordinal as Ordinal
            FROM
                SchemaInformation.Functions as r 
                 OUTER APPLY
                r.Parameters as p)
            UNION ALL
            (SELECT
                  r.CatalogName as CatalogName
              ,   r.SchemaName as SchemaName
              ,   r.Name as Name
              ,   CAST(NULL as string) as ReturnTypeName
              ,   false as IsAggregate
              ,   false as IsComposable
              ,   false as IsBuiltIn
              ,   false as IsNiladic
              ,   false as IsTvf
              ,   p.Name as ParameterName
              ,   p.ParameterType.TypeName as ParameterType
              ,   p.Mode as Mode
              ,   p.Ordinal as Ordinal
            FROM
                SchemaInformation.Procedures as r 
                 OUTER APPLY
                r.Parameters as p)) as sp
            ";
                }
            }
        }

        internal abstract class Memento
        {
            protected object[] _values;

            internal object[] Values
            {
                get { return _values; }
            }

            public abstract FunctionDetailsReader CreateReader();
        }

        internal sealed class MementoV1 : Memento
        {
            internal MementoV1(object[] values)
            {
                _values = values;
            }

            public override FunctionDetailsReader CreateReader()
            {
                return new FunctionDetailsReaderV1(this);
            }
        }

        internal sealed class MementoV3 : Memento
        {
            internal MementoV3(object[] values)
            {
                _values = values;
            }

            public override FunctionDetailsReader CreateReader()
            {
                return new FunctionDetailsReaderV3(this);
            }
        }
    }
}