File: sqlmetadatafactory.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 (288 lines) | stat: -rw-r--r-- 12,764 bytes parent folder | download | duplicates (2)
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
//------------------------------------------------------------------------------
// <copyright file="SqlMetaDataFactory.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//
// <owner current="true" primary="true">Microsoft</owner>
// <owner current="true" primary="false">Mugunm</owner>
//
//------------------------------------------------------------------------------

namespace System.Data.SqlClient{

    using System;
    using System.Data;
    using System.IO;
    using System.Collections;        
    using System.Data.ProviderBase;
    using System.Data.Common;
    using System.Data.SqlClient;
    using System.Diagnostics;
    using System.Globalization;
    using System.Text;
    using System.Xml;
    using System.Xml.Schema;

    
    internal sealed class SqlMetaDataFactory : DbMetaDataFactory{ // V1.2.3300

        private const string _serverVersionNormalized90 = "09.00.0000";
        private const string _serverVersionNormalized90782 = "09.00.0782";
        private const string _serverVersionNormalized10 = "10.00.0000";


        public SqlMetaDataFactory(Stream XMLStream, 
                                    string serverVersion, 
                                    string serverVersionNormalized): 
                base(XMLStream, serverVersion, serverVersionNormalized) {
  
           
        }

        private void addUDTsToDataTypesTable(DataTable dataTypesTable, SqlConnection connection , String  ServerVersion) {

            const string sqlCommand = 
                "select " + 
                    "assemblies.name, " +
                    "types.assembly_class, " +
                    "ASSEMBLYPROPERTY(assemblies.name, 'VersionMajor') as version_major, " +
                    "ASSEMBLYPROPERTY(assemblies.name, 'VersionMinor') as version_minor, " +
                    "ASSEMBLYPROPERTY(assemblies.name, 'VersionBuild') as version_build, " +
                    "ASSEMBLYPROPERTY(assemblies.name, 'VersionRevision') as version_revision, " +
                    "ASSEMBLYPROPERTY(assemblies.name, 'CultureInfo') as culture_info, " +
                    "ASSEMBLYPROPERTY(assemblies.name, 'PublicKey') as public_key, " +
                    "is_nullable, " +
                    "is_fixed_length, " +
                    "max_length " +
                "from sys.assemblies as assemblies  join sys.assembly_types as types " +
                "on assemblies.assembly_id = types.assembly_id ";

            // pre 9.0/Yukon servers do not have UDTs
            if (0 > string.Compare(ServerVersion,  _serverVersionNormalized90, StringComparison.OrdinalIgnoreCase)){
                return;
            }
                     
            
            // Execute the SELECT statement
            SqlCommand command = connection.CreateCommand();
            command.CommandText = sqlCommand;
            DataRow newRow = null;
            DataColumn providerDbtype = dataTypesTable.Columns[DbMetaDataColumnNames.ProviderDbType];
            DataColumn columnSize = dataTypesTable.Columns[DbMetaDataColumnNames.ColumnSize];
            DataColumn isFixedLength = dataTypesTable.Columns[DbMetaDataColumnNames.IsFixedLength];
            DataColumn isSearchable = dataTypesTable.Columns[DbMetaDataColumnNames.IsSearchable];
            DataColumn isLiteralSupported = dataTypesTable.Columns[DbMetaDataColumnNames.IsLiteralSupported];
            DataColumn typeName = dataTypesTable.Columns[DbMetaDataColumnNames.TypeName];
            DataColumn isNullable = dataTypesTable.Columns[DbMetaDataColumnNames.IsNullable];

            if ((providerDbtype == null) ||
                (columnSize == null) || 
                (isFixedLength == null) ||
                (isSearchable == null) || 
                (isLiteralSupported == null) || 
                (typeName == null) || 
                (isNullable == null)) {
                throw ADP.InvalidXml();
            }
                    
            const int columnSizeIndex = 10;
            const int isFixedLengthIndex = 9;
            const int isNullableIndex = 8;
            const int assemblyNameIndex = 0;
            const int assemblyClassIndex = 1;
            const int versionMajorIndex = 2;
            const int versionMinorIndex = 3;
            const int versionBuildIndex = 4;
            const int versionRevisionIndex = 5;
            const int cultureInfoIndex = 6;
            const int publicKeyIndex = 7;
                    
                    
            using (IDataReader reader = command.ExecuteReader()) {
                
                object[] values = new object[11];
                while (reader.Read()) {
                    
                    reader.GetValues(values);
                    newRow = dataTypesTable.NewRow();

                    newRow[providerDbtype] = SqlDbType.Udt;

                    if (values[columnSizeIndex] != DBNull.Value) {
                        newRow[columnSize] = values[columnSizeIndex];
                    }

                    if (values[isFixedLengthIndex] != DBNull.Value) {
                        newRow[isFixedLength] = values[isFixedLengthIndex];
                    }

                    newRow[isSearchable] = true;
                    newRow[isLiteralSupported] = false;
                    if (values[isNullableIndex] != DBNull.Value) {
                        newRow[isNullable] = values[isNullableIndex];
                    }
                           
                    if ((values[assemblyNameIndex] != DBNull.Value) &&
                        (values[assemblyClassIndex] != DBNull.Value) &&
                        (values[versionMajorIndex] != DBNull.Value) &&
                        (values[versionMinorIndex] != DBNull.Value) &&
                        (values[versionBuildIndex] != DBNull.Value) &&
                        (values[versionRevisionIndex] != DBNull.Value)) {
                        
                        StringBuilder nameString = new StringBuilder();
                        nameString.Append(values[assemblyClassIndex].ToString());
                        nameString.Append(", ");
                        nameString.Append(values[assemblyNameIndex].ToString());
                        nameString.Append(", Version=");

                        nameString.Append(values[versionMajorIndex].ToString());
                        nameString.Append(".");
                        nameString.Append(values[versionMinorIndex].ToString());
                        nameString.Append(".");
                        nameString.Append(values[versionBuildIndex].ToString());
                        nameString.Append(".");
                        nameString.Append(values[versionRevisionIndex].ToString());

                        if (values[cultureInfoIndex] != DBNull.Value) {
                            nameString.Append(", Culture=");
                            nameString.Append(values[cultureInfoIndex].ToString());
                        }

                        if (values[publicKeyIndex] != DBNull.Value) {
                        
                            nameString.Append(", PublicKeyToken=");
                        
                            StringBuilder resultString = new StringBuilder();
                            Byte[] byteArrayValue = (Byte[])values[publicKeyIndex];
                            foreach (byte b in byteArrayValue) {
                                resultString.Append(String.Format((IFormatProvider)null, "{0,-2:x2}", b));
                            }
                            nameString.Append(resultString.ToString());
                        }
                                               
                        newRow[typeName] = nameString.ToString();
                        dataTypesTable.Rows.Add(newRow);
                        newRow.AcceptChanges();
                    } // if assembly name
                     
                }//end while
            } // end using
        }

        private void AddTVPsToDataTypesTable(DataTable dataTypesTable, SqlConnection connection , String  ServerVersion) {

            const string sqlCommand = 
                "select " + 
                    "name, " +
                    "is_nullable, " +
                    "max_length " +
                "from sys.types " +
                "where is_table_type = 1";

            // 

            if (0 > string.Compare(ServerVersion,  _serverVersionNormalized10, StringComparison.OrdinalIgnoreCase)){
                return;
            }
                     
            
            // Execute the SELECT statement
            SqlCommand command = connection.CreateCommand();
            command.CommandText = sqlCommand;
            DataRow newRow = null;
            DataColumn providerDbtype = dataTypesTable.Columns[DbMetaDataColumnNames.ProviderDbType];
            DataColumn columnSize = dataTypesTable.Columns[DbMetaDataColumnNames.ColumnSize];
            DataColumn isSearchable = dataTypesTable.Columns[DbMetaDataColumnNames.IsSearchable];
            DataColumn isLiteralSupported = dataTypesTable.Columns[DbMetaDataColumnNames.IsLiteralSupported];
            DataColumn typeName = dataTypesTable.Columns[DbMetaDataColumnNames.TypeName];
            DataColumn isNullable = dataTypesTable.Columns[DbMetaDataColumnNames.IsNullable];

            if ((providerDbtype == null) ||
                (columnSize == null) || 
                (isSearchable == null) || 
                (isLiteralSupported == null) || 
                (typeName == null) || 
                (isNullable == null)) {
                throw ADP.InvalidXml();
            }
                    
            const int columnSizeIndex = 2;
            const int isNullableIndex = 1;
            const int typeNameIndex = 0;
                    
            using (IDataReader reader = command.ExecuteReader()) {
                
                object[] values = new object[11];
                while (reader.Read()) {
                    
                    reader.GetValues(values);
                    newRow = dataTypesTable.NewRow();

                    newRow[providerDbtype] = SqlDbType.Structured;

                    if (values[columnSizeIndex] != DBNull.Value) {
                        newRow[columnSize] = values[columnSizeIndex];
                    }

                    newRow[isSearchable] = false;
                    newRow[isLiteralSupported] = false;
                    if (values[isNullableIndex] != DBNull.Value) {
                        newRow[isNullable] = values[isNullableIndex];
                    }
                           
                    if (values[typeNameIndex] != DBNull.Value) {
                        newRow[typeName] = values[typeNameIndex];
                        dataTypesTable.Rows.Add(newRow);
                        newRow.AcceptChanges();
                    } // if type name
                }//end while
            } // end using
        }

        private DataTable GetDataTypesTable(SqlConnection connection){
        
                                 
            // verify the existence of the table in the data set
            DataTable dataTypesTable = CollectionDataSet.Tables[DbMetaDataCollectionNames.DataTypes]; 
            if (dataTypesTable == null){
                throw ADP.UnableToBuildCollection(DbMetaDataCollectionNames.DataTypes);
            }
        
             // copy the table filtering out any rows that don't apply to tho current version of the prrovider
            dataTypesTable = CloneAndFilterCollection(DbMetaDataCollectionNames.DataTypes, null);
        
            addUDTsToDataTypesTable(dataTypesTable, connection, ServerVersionNormalized);
            AddTVPsToDataTypesTable(dataTypesTable, connection, ServerVersionNormalized);
                                    
            dataTypesTable.AcceptChanges();
            return  dataTypesTable;
                        
        }

        protected override DataTable PrepareCollection(String collectionName, String[] restrictions, DbConnection connection){

            SqlConnection sqlConnection = (SqlConnection) connection;
            DataTable resultTable = null;

            if (collectionName == DbMetaDataCollectionNames.DataTypes){
                if (ADP.IsEmptyArray(restrictions) == false) {
                    throw ADP.TooManyRestrictions(DbMetaDataCollectionNames.DataTypes);
                }
                resultTable = GetDataTypesTable(sqlConnection);
            }

            if (resultTable == null){
               throw ADP.UnableToBuildCollection(collectionName);
            }

            return resultTable;
            
        }

       
        
    }
}