* System.Data_test.dll.sources: Added DbDataAdapterTest.cs,
[mono.git] / mcs / class / System.Data / System.Data.Common / DbProviderFactoriesConfigurationHandler.cs
1 //
2 // System.Data.Common.DbProviderFactoriesConfigurationHandler.cs
3 //
4 // Author:
5 //   Sureshkumar T (tsureshkumar@novell.com)
6 //   Tim Coleman (tim@timcoleman.com)
7 //
8 // Copyright (C) Tim Coleman, 2003
9 //
10
11 //
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
13 //
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
21 // 
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 // 
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 //
33
34 #if NET_2_0
35
36 using System.IO;
37 using System.Xml;
38 using System.Globalization;
39 using System.Configuration;
40
41 namespace System.Data.Common
42 {
43         public class DbProviderFactoriesConfigurationHandler : IConfigurationSectionHandler
44         {
45                 #region Constructors
46
47                 public DbProviderFactoriesConfigurationHandler ()
48                 {
49                 }
50
51                 #endregion // Constructors
52
53                 #region Methods
54
55                 public virtual object Create (object parent, object configContext, XmlNode section)
56                 {
57                         DataSet ds = new DataSet (DbProviderFactories.CONFIG_SECTION_NAME);
58                         CreateDataTables (ds, section);
59                         return ds;
60                 }
61
62                 internal virtual void CreateDataTables (DataSet ds, XmlNode section)
63                 {
64                         DataTable dt = ds.Tables.Add (DbProviderFactories.CONFIG_SEC_TABLE_NAME);
65                         DataColumn [] columns = new DataColumn [4];
66                         columns [0] = new DataColumn ("Name", typeof (string));
67                         columns [1] = new DataColumn ("Description", typeof (string));
68                         columns [2] = new DataColumn ("InvariantName", typeof (string));
69                         columns [3] = new DataColumn ("AssemblyQualifiedName", typeof (string));
70                         dt.Columns.AddRange (columns);
71                         dt.PrimaryKey = new DataColumn [] { columns [2] };
72
73                         foreach (XmlNode node in section.ChildNodes) {
74                                 if (node.NodeType != XmlNodeType.Element)
75                                         continue;
76
77                                 if (node.Name == DbProviderFactories.CONFIG_SEC_TABLE_NAME) {
78                                         foreach (XmlNode factoryNode in node.ChildNodes) {
79                                                 if (factoryNode.NodeType != XmlNodeType.Element)
80                                                         continue;
81
82                                                 switch (factoryNode.Name) {
83                                                 case "add":
84                                                         AddRow (dt, factoryNode);
85                                                         break;
86                                                 case "clear":
87                                                         dt.Rows.Clear ();
88                                                         break;
89                                                 case "remove":
90                                                         RemoveRow (dt, factoryNode);
91                                                         break;
92                                                 default:
93                                                         throw new ConfigurationErrorsException (
94                                                                 "Unrecognized element.", factoryNode);
95                                                 }
96                                         }
97                                 }
98                         }
99                 }
100
101                 string GetAttributeValue (XmlNode node, string name, bool required)
102                 {
103                         XmlAttribute attr = node.Attributes [name];
104                         if (attr == null) {
105                                 if (!required)
106                                         return null;
107                                 throw new ConfigurationErrorsException ("Required Attribute '" 
108                                         + name + "' is  missing!", node);
109                         }
110                         string value = attr.Value;
111                         if (value == "")
112                                 throw new ConfigurationException ("Attribute '" + name 
113                                         + "' cannot be empty!", node);
114                         return value;
115                 }
116
117                 void AddRow (DataTable dt, XmlNode addNode)
118                 {
119                         string name = GetAttributeValue (addNode, "name", true);
120                         string description = GetAttributeValue (addNode, "description", true);
121                         string invariant = GetAttributeValue (addNode, "invariant", true);
122                         string type = GetAttributeValue (addNode, "type", true);
123
124                         // FIXME: throw ConfigurationErrorsException for unrecognized
125                         // attributes. Consider "supports" valid although we're not using
126                         // it
127
128                         DataRow row = dt.NewRow ();
129                         row [0] = name;
130                         row [1] = description;
131                         row [2] = invariant;
132                         row [3] = type;
133
134                         dt.Rows.Add (row);
135                 }
136
137                 void RemoveRow (DataTable dt, XmlNode removeNode)
138                 {
139                         // FIXME: throw ConfigurationErrorsException for unrecognized
140                         // attributes.
141
142                         string invariant = GetAttributeValue (removeNode, "invariant", true);
143                         DataRow row = dt.Rows.Find (invariant);
144                         if (row != null)
145                                 row.Delete ();
146                 }
147
148                 #endregion // Methods
149         }
150 }
151
152 #endif // NET_2_0