File: SmiMetaDataProperty.cs

package info (click to toggle)
mono 6.14.1%2Bds2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,282,732 kB
  • sloc: cs: 11,182,461; xml: 2,850,281; ansic: 699,123; cpp: 122,919; perl: 58,604; javascript: 30,841; asm: 21,845; makefile: 19,602; sh: 10,973; python: 4,772; pascal: 925; sql: 859; sed: 16; php: 1
file content (261 lines) | stat: -rw-r--r-- 9,245 bytes parent folder | download | duplicates (6)
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
//------------------------------------------------------------------------------
// <copyright file="SmiMetaData.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
// <owner current="true" primary="true">Microsoft</owner>
// <owner current="true" primary="false">Microsoft</owner>
//------------------------------------------------------------------------------

namespace Microsoft.SqlServer.Server {

    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using System.Data.SqlClient;
    using System.Diagnostics;
    using System.Globalization;

    // SmiMetaDataProperty defines an extended, optional property to be used on the SmiMetaData class
    //  This approach to adding properties is added combat the growing number of sparsely-used properties 
    //  that are specially handled on the base classes

    internal enum SmiPropertySelector {
        DefaultFields = 0x0,
        SortOrder = 0x1,
        UniqueKey = 0x2,
    }

    // Simple collection for properties.  Could extend to IDictionary support if needed in future.
    internal class SmiMetaDataPropertyCollection {
        private const int               SelectorCount = 3;  // number of elements in SmiPropertySelector

        private SmiMetaDataProperty[]   _properties;
        private bool                    _isReadOnly;

        internal static readonly SmiMetaDataPropertyCollection EmptyInstance;

        // Singleton empty instances to ensure each property is always non-null
        private static readonly SmiDefaultFieldsProperty    __emptyDefaultFields = new SmiDefaultFieldsProperty(new List<bool>());
        private static readonly SmiOrderProperty            __emptySortOrder = new SmiOrderProperty(new List<SmiOrderProperty.SmiColumnOrder>());
        private static readonly SmiUniqueKeyProperty        __emptyUniqueKey = new SmiUniqueKeyProperty(new List<bool>());

        static SmiMetaDataPropertyCollection() {
            EmptyInstance = new SmiMetaDataPropertyCollection();
            EmptyInstance.SetReadOnly();
        }

        internal SmiMetaDataPropertyCollection() {
            _properties = new SmiMetaDataProperty[SelectorCount];
            _isReadOnly = false;
            _properties[(int)SmiPropertySelector.DefaultFields]  = __emptyDefaultFields;
            _properties[(int)SmiPropertySelector.SortOrder]      = __emptySortOrder;
            _properties[(int)SmiPropertySelector.UniqueKey]      = __emptyUniqueKey;
        }

        internal SmiMetaDataProperty this[SmiPropertySelector key] {
            get {
                return _properties[(int)key];
            }
            set {
                if (null == value) {
                    throw ADP.InternalError(ADP.InternalErrorCode.InvalidSmiCall);
                }
                EnsureWritable();
                _properties[(int)key] = value;
            }
        }

        internal bool IsReadOnly {
            get {
                return _isReadOnly;
            }
        }

        internal IEnumerable<SmiMetaDataProperty> Values {
            get {
                return new List<SmiMetaDataProperty>(_properties);
            }
        }

        // Allow switching to read only, but not back.
        internal void SetReadOnly() {
            _isReadOnly = true;
        }

        private void EnsureWritable() {
            if (IsReadOnly) {
                throw System.Data.Common.ADP.InternalError(System.Data.Common.ADP.InternalErrorCode.InvalidSmiCall);
            }
        }
    }

    // Base class for properties
    internal abstract class SmiMetaDataProperty {
        internal abstract string TraceString();
    }

    // Property defining a list of column ordinals that define a unique key
    internal class SmiUniqueKeyProperty : SmiMetaDataProperty {
        private IList<bool> _columns;

        internal SmiUniqueKeyProperty(IList<bool> columnIsKey) {
            _columns = new List<bool>(columnIsKey).AsReadOnly();
        }

        // indexed by column ordinal indicating for each column whether it is key or not
        internal bool this[int ordinal] {
            get {
                if (_columns.Count <= ordinal) {
                    return false;
                }
                else {
                    return _columns[ordinal];
                }
            }
        }

        [Conditional("DEBUG")]
        internal void CheckCount(int countToMatch) {
            Debug.Assert(0 == _columns.Count || countToMatch == _columns.Count, 
                    "SmiDefaultFieldsProperty.CheckCount: DefaultFieldsProperty size (" + _columns.Count +
                    ") not equal to checked size (" + countToMatch + ")" );
        }

        internal override string TraceString() {
            string returnValue = "UniqueKey(";
            bool delimit = false;
            for (int columnOrd = 0; columnOrd < _columns.Count; columnOrd++) {
                if (delimit) {
                    returnValue += ",";
                }
                else {
                    delimit = true;
                }
                if (_columns[columnOrd]) {
                    returnValue += columnOrd.ToString(CultureInfo.InvariantCulture);
                }
            }
            returnValue += ")";

            return returnValue;
        }
    }

    // Property defining a sort order for a set of columns (by ordinal and ASC/DESC).
    internal class SmiOrderProperty : SmiMetaDataProperty {
        internal struct SmiColumnOrder {
            internal int SortOrdinal;
            internal SortOrder Order;

            internal string TraceString() {
                return String.Format(CultureInfo.InvariantCulture, "{0} {1}", SortOrdinal, Order);
            }
        }

        private IList<SmiColumnOrder> _columns;

        internal SmiOrderProperty(IList<SmiColumnOrder> columnOrders) {
            _columns = new List<SmiColumnOrder>(columnOrders).AsReadOnly();
        }

        // Readonly list of the columnorder instances making up the sort order
        //  order in list indicates precedence
        internal SmiColumnOrder this[int ordinal] {
            get {
                if (_columns.Count <= ordinal) {
                    SmiColumnOrder order = new SmiColumnOrder();
                    order.Order = SortOrder.Unspecified;
                    order.SortOrdinal = -1;
                    return order;
                }
                else {
                    return _columns[ordinal];
                }
            }
        }


        [Conditional("DEBUG")]
        internal void CheckCount(int countToMatch) {
            Debug.Assert(0 == _columns.Count || countToMatch == _columns.Count, 
                    "SmiDefaultFieldsProperty.CheckCount: DefaultFieldsProperty size (" + _columns.Count +
                    ") not equal to checked size (" + countToMatch + ")" );
        }

        internal override string TraceString() {
            string returnValue = "SortOrder(";
            bool delimit = false;
            foreach(SmiColumnOrder columnOrd in _columns) {
                if (delimit) {
                    returnValue += ",";
                }
                else {
                    delimit = true;
                }

                if (System.Data.SqlClient.SortOrder.Unspecified != columnOrd.Order) {
                    returnValue += columnOrd.TraceString();
                }
            }
            returnValue += ")";

            return returnValue;
         }
    }

    // property defining inheritance relationship(s)
    internal class SmiDefaultFieldsProperty : SmiMetaDataProperty {
        #region private fields

        private IList<bool>     _defaults;

        #endregion

        #region internal interface

        internal SmiDefaultFieldsProperty(IList<bool> defaultFields) {
            _defaults = new List<bool>(defaultFields).AsReadOnly();
        }

        internal bool this[int ordinal] {
            get {
                if (_defaults.Count <= ordinal) {
                    return false;
                }
                else {
                    return _defaults[ordinal];
                }
            }
        }

        [Conditional("DEBUG")]
        internal void CheckCount(int countToMatch) {
            Debug.Assert(0 == _defaults.Count || countToMatch == _defaults.Count, 
                    "SmiDefaultFieldsProperty.CheckCount: DefaultFieldsProperty size (" + _defaults.Count +
                    ") not equal to checked size (" + countToMatch + ")" );
        }

        internal override string TraceString() {
            string returnValue = "DefaultFields(";
            bool delimit = false;
            for(int columnOrd = 0; columnOrd < _defaults.Count; columnOrd++) {
                if (delimit) {
                    returnValue += ",";
                }
                else {
                    delimit = true;
                }

                if (_defaults[columnOrd]) {
                    returnValue += columnOrd;
                }
            }
            returnValue += ")";

            return returnValue;
         }

        #endregion
    }
}