File: DbProviderFactoriesConfigurationHandler.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 (280 lines) | stat: -rw-r--r-- 13,081 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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
//------------------------------------------------------------------------------
// <copyright file="DbProviderFactoriesConfigurationHandler.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 System.Data.Common {

    using System;
    using System.Collections;
    using System.Configuration;
    using System.Data;
    using System.Diagnostics;
    using System.Globalization;
    using System.Xml;

    // VSTFDevDiv # 624213: System.Data.Common.DbProviderFactories.GetFactoryClasses() still gets OracleClient provider in ClientSku environment.
    // NOTES: As part of this bug fix, the decision was taken to make it consistent and to remove all the framework 
    //      providers from the list in the machine.config file. The DbProviderFactories section of the machine.config will contain only
    //      custom providers names and details.
    internal enum DbProvidersIndex : int
    {
        Odbc = 0,
        OleDb,
        OracleClient,
        SqlClient,
        DbProvidersIndexCount // As enums are 0-based index, the DbProvidersIndexCount will hold the maximum count of the enum objects;
    }

    internal class DbProviderFactoryConfigSection
    {
        Type factType;
        string name;
        string invariantName;
        string description;
        string assemblyQualifiedName;

        public DbProviderFactoryConfigSection(Type FactoryType, string FactoryName, string FactoryDescription)
        {
            try
            {
                factType = FactoryType;
                name = FactoryName;
                invariantName = factType.Namespace.ToString();
                description = FactoryDescription;
                assemblyQualifiedName = factType.AssemblyQualifiedName.ToString();
            }
            catch 
            {
                factType = null;
                name = string.Empty;
                invariantName = string.Empty;
                description = string.Empty;
                assemblyQualifiedName = string.Empty;
            }
        }

        public DbProviderFactoryConfigSection(string FactoryName, string FactoryInvariantName, string FactoryDescription, string FactoryAssemblyQualifiedName)
        {
            factType = null;
            name = FactoryName;
            invariantName = FactoryInvariantName;
            description = FactoryDescription;
            assemblyQualifiedName = FactoryAssemblyQualifiedName;
        }

        public bool IsNull()
        {
            if ((factType == null) && (invariantName == string.Empty))
                return true;
            else
                return false;
        }

        public string Name
        {
            get { return name; }
        }

        public string InvariantName
        {
            get { return invariantName; }
        }

        public string Description
        {
            get { return description; }
        }

        public string AssemblyQualifiedName
        {
            get { return assemblyQualifiedName; }
        }
    }

    // <configSections>
    //     <section name="system.data" type="System.Data.Common.DbProviderFactoriesConfigurationHandler, System.Data, Version=%ASSEMBLY_VERSION%, Culture=neutral, PublicKeyToken=%ECMA_PUBLICKEY%" />
    // </configSections>
    // <system.data>
    //     <DbProviderFactories>
    //         <add name="Odbc Data Provider"         invariant="System.Data.Odbc"         support="1BF" description=".Net Framework Data Provider for Odbc"      type="System.Data.Odbc.OdbcFactory, System.Data, Version=%ASSEMBLY_VERSION%, Culture=neutral, PublicKeyToken=%ECMA_PUBLICKEY%"/>
    //         <add name="OleDb Data Provider"        invariant="System.Data.OleDb"        support="1BF" description=".Net Framework Data Provider for OleDb"     type="System.Data.OleDb.OleDbFactory, System.Data, Version=%ASSEMBLY_VERSION%, Culture=neutral, PublicKeyToken=%ECMA_PUBLICKEY%"/>
    //         <add name="OracleClient Data Provider" invariant="System.Data.OracleClient" support="1AF" description=".Net Framework Data Provider for Oracle"    type="System.Data.OracleClient.OracleFactory, System.Data.OracleClient, Version=%ASSEMBLY_VERSION%, Culture=neutral, PublicKeyToken=%ECMA_PUBLICKEY%"/>
    //         <add name="SqlClient Data Provider"    invariant="System.Data.SqlClient"    support="1FF" description=".Net Framework Data Provider for SqlServer" type="System.Data.SqlClient.SqlClientFactory, System.Data, Version=%ASSEMBLY_VERSION%, Culture=neutral, PublicKeyToken=%ECMA_PUBLICKEY%"/>
    //     </DbProviderFactories>
    // </system.data>
    // this class is delayed created, use ConfigurationSettings.GetSection("system.data") to obtain
    public class DbProviderFactoriesConfigurationHandler : IConfigurationSectionHandler { // V1.2.3300
        internal const string sectionName = "system.data";
        internal const string providerGroup = "DbProviderFactories";

        // NOTES: Framework-Based DbProviderFactories Details
        internal const string odbcProviderName = "Odbc Data Provider";
        internal const string odbcProviderDescription = ".Net Framework Data Provider for Odbc";

        internal const string oledbProviderName = "OleDb Data Provider";
        internal const string oledbProviderDescription = ".Net Framework Data Provider for OleDb";

        internal const string oracleclientProviderName = "OracleClient Data Provider";
        internal const string oracleclientProviderNamespace = "System.Data.OracleClient";
        internal const string oracleclientProviderDescription = ".Net Framework Data Provider for Oracle";

        internal const string sqlclientProviderName = "SqlClient Data Provider";
        internal const string sqlclientProviderDescription = ".Net Framework Data Provider for SqlServer";

        internal const string sqlclientPartialAssemblyQualifiedName = "System.Data.SqlClient.SqlClientFactory, System.Data,";
        internal const string oracleclientPartialAssemblyQualifiedName = "System.Data.OracleClient.OracleClientFactory, System.Data.OracleClient,";

        public DbProviderFactoriesConfigurationHandler() { // V1.2.3300
        }

        virtual public object Create(object parent, object configContext, XmlNode section) { // V1.2.3300
#if DEBUG
            try {
#endif
                return CreateStatic(parent, configContext, section);
#if DEBUG
            }
            catch(Exception e) {
                ADP.TraceExceptionWithoutRethrow(e); // it will be rethrown
                throw;
            }
#endif
        }

        static internal object CreateStatic(object parent, object configContext, XmlNode section) {            
            object config = parent;
            if (null != section) {
                config = HandlerBase.CloneParent(parent as DataSet, false);
                bool foundFactories = false;

                HandlerBase.CheckForUnrecognizedAttributes(section);
                foreach (XmlNode child in section.ChildNodes) {
                    if (HandlerBase.IsIgnorableAlsoCheckForNonElement(child)) {
                        continue;
                    }
                    string sectionGroup = child.Name;
                    switch(sectionGroup) {
                    case DbProviderFactoriesConfigurationHandler.providerGroup:
                        if (foundFactories) {
                            throw ADP.ConfigSectionsUnique(DbProviderFactoriesConfigurationHandler.providerGroup);
                        }
                        foundFactories = true;
                        HandleProviders(config as DataSet, configContext, child, sectionGroup);
                        break;
                    default:
                        throw ADP.ConfigUnrecognizedElement(child);
                    }
                }
            }
            return config;
        }

        // sectionName - i.e. "providerconfiguration"
        private static void HandleProviders(DataSet config, object configContext, XmlNode section, string sectionName) {
            DataTableCollection tables = config.Tables;
            DataTable dataTable = tables[sectionName];
            bool tableExisted = (null != dataTable);
            dataTable = DbProviderDictionarySectionHandler.CreateStatic(dataTable, configContext, section);
            if (!tableExisted) {
                tables.Add(dataTable);
            }
        }

        // based off of DictionarySectionHandler
        private static class DbProviderDictionarySectionHandler/* : IConfigurationSectionHandler*/ {
            /*
            internal DbProviderDictionarySectionHandler() {
            }

            public object Create(Object parent, Object context, XmlNode section) {
                return CreateStatic(parent, context, section);
            }
            */

            static internal DataTable CreateStatic(DataTable config, Object context, XmlNode section) {
                if (null != section) {
                    HandlerBase.CheckForUnrecognizedAttributes(section);
                    
                    if (null == config) {
                        config = DbProviderFactoriesConfigurationHandler.CreateProviderDataTable();
                    }
                    // else already copied via DataSet.Copy

                    foreach (XmlNode child in section.ChildNodes) {
                        if (HandlerBase.IsIgnorableAlsoCheckForNonElement(child)) {
                            continue;
                        }
                        switch(child.Name) {
                        case "add":
                            HandleAdd(child, config);
                            break;
                        case "remove":
                            HandleRemove(child, config);
                            break;
                        case "clear":
                            HandleClear(child, config);
                            break;
                        default:
                            throw ADP.ConfigUnrecognizedElement(child);
                        }
                    }
                    config.AcceptChanges();
                }
                return config;
            }
            static private void HandleAdd(XmlNode child, DataTable config) {
                HandlerBase.CheckForChildNodes(child);
                DataRow values = config.NewRow();
                values[0] = HandlerBase.RemoveAttribute(child, "name", true, false);
                values[1] = HandlerBase.RemoveAttribute(child, "description", true, false);
                values[2] = HandlerBase.RemoveAttribute(child, "invariant", true, false);
                values[3] = HandlerBase.RemoveAttribute(child, "type", true, false);

                // because beta shipped recognizing "support=hex#", need to give
                // more time for other providers to remove it from the .config files
                HandlerBase.RemoveAttribute(child, "support", false, false);
                
                HandlerBase.CheckForUnrecognizedAttributes(child);
                config.Rows.Add(values);
            }
            static private void HandleRemove(XmlNode child, DataTable config) {
                HandlerBase.CheckForChildNodes(child);                
                String invr = HandlerBase.RemoveAttribute(child, "invariant", true, false);                
                HandlerBase.CheckForUnrecognizedAttributes(child);
                DataRow row = config.Rows.Find(invr);
                if (null != row) { // ignore invariants that don't exist
                    row.Delete();
                }
            }
            static private void HandleClear(XmlNode child, DataTable config) {
                HandlerBase.CheckForChildNodes(child);
                HandlerBase.CheckForUnrecognizedAttributes(child);
                config.Clear();
            }
        }
        
        internal static DataTable CreateProviderDataTable() {
            DataColumn frme = new DataColumn("Name", typeof(string));
            frme.ReadOnly = true;
            DataColumn desc = new DataColumn("Description", typeof(string));
            desc.ReadOnly = true;
            DataColumn invr = new DataColumn("InvariantName", typeof(string));
            invr.ReadOnly = true;
            DataColumn qual = new DataColumn("AssemblyQualifiedName", typeof(string));
            qual.ReadOnly = true;

            DataColumn[] primaryKey = new DataColumn[] { invr };
            DataColumn[] columns = new DataColumn[] {frme, desc, invr, qual };
            DataTable table = new DataTable(DbProviderFactoriesConfigurationHandler.providerGroup);
            table.Locale = CultureInfo.InvariantCulture;
            table.Columns.AddRange(columns);
            table.PrimaryKey = primaryKey;
            return table;
        }
    }
}