[bcl] Enable tests for the monodroid profile.
[mono.git] / mcs / class / System.Data / Test / ProviderTests / Common / EngineConfig.cs
1 //
2 // EngineConfig.cs  - Holds information on the capabilities and behavior of an
3 // RDBMS engine.
4 //
5 // Author:
6 //      Gert Driesen (drieseng@users.sourceforge.net
7 //
8 // Copyright (c) 2008 Gert Driesen
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 //
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 //
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 using System;
31 using System.Configuration;
32 using System.Globalization;
33 using System.Xml;
34
35 namespace MonoTests.System.Data
36 {
37         internal sealed class EngineConfig
38         {
39                 private string name;
40                 private string quoteCharacter;
41                 private bool removesTrailingSpaces;
42                 private bool emptyBinaryAsNull;
43                 private bool supportsMicroseconds;
44                 private bool supportsUniqueIdentifier;
45                 private bool supportsDate;
46                 private bool supportsTime;
47                 private bool supportsTimestamp;
48                 private EngineType type;
49                 private int clientVersion;
50
51                 private EngineConfig ()
52                 {
53                 }
54
55                 public string Name {
56                         get { return name; }
57                 }
58
59                 /// <summary>
60                 /// Returns the character(s) for quoting identifiers.
61                 /// </summary>
62                 public string QuoteCharacter {
63                         get { return quoteCharacter; }
64                 }
65
66                 public EngineType Type {
67                         get { return type; }
68                 }
69
70                 public bool RemovesTrailingSpaces {
71                         get { return removesTrailingSpaces; }
72                 }
73
74                 public bool EmptyBinaryAsNull {
75                         get { return emptyBinaryAsNull; }
76                 }
77
78                 public bool SupportsMicroseconds {
79                         get { return supportsMicroseconds; }
80                 }
81
82                 public bool SupportsUniqueIdentifier {
83                         get { return supportsUniqueIdentifier; }
84                 }
85
86                 public bool SupportsDate {
87                         get { return supportsDate; }
88                 }
89
90                 public bool SupportsTime {
91                         get { return supportsTime; }
92                 }
93
94                 public bool SupportsTimestamp {
95                         get { return supportsTimestamp; }
96                 }
97
98                 public int ClientVersion {
99                        get { return clientVersion; }
100                 }
101
102                 public static EngineConfig FromXml (XmlNode config)
103                 {
104                         EngineConfig engine = new EngineConfig ();
105                         engine.name = GetAttribValue (config, "name", true);
106                         engine.quoteCharacter = GetAttribValue (config, "quoteCharacter", true);
107                         engine.removesTrailingSpaces = ParseBoolean (config, "removesTrailingSpaces", false, true);
108                         engine.emptyBinaryAsNull = ParseBoolean (config, "emptyBinaryAsNull", false, true);
109                         engine.supportsMicroseconds = ParseBoolean (config, "supportsMicroseconds", false, true);
110                         engine.supportsUniqueIdentifier = ParseBoolean (config, "supportsUniqueIdentifier", false, true);
111                         engine.supportsDate = ParseBoolean (config, "supportsDate", false, true);
112                         engine.supportsTime = ParseBoolean (config, "supportsTime", false, true);
113                         engine.supportsTimestamp = ParseBoolean (config, "supportsTimestamp", false, true);
114                         engine.type = ParseEngineType (config, "type");
115                         engine.clientVersion = ParseClientVersion (config, "clientversion");
116                         return engine;
117                 }
118
119                 static string GetAttribValue (XmlNode node, string name, bool required)
120                 {
121                         XmlAttribute attr = node.Attributes [name];
122                         if (attr == null) {
123                                 if (required)
124                                         throw CreateAttributeMissingException (name, node);
125                                 return null;
126                         }
127                         return attr.Value;
128                 }
129
130                 static bool ParseBoolean (XmlNode config, string attrName, bool required, bool defaultValue)
131                 {
132                         XmlAttribute attr = config.Attributes [attrName];
133                         if (attr == null) {
134                                 if (required)
135                                         throw CreateAttributeMissingException (attrName, config);
136                                 return defaultValue;
137                         }
138
139                         string value = attr.Value;
140
141                         try {
142                                 return bool.Parse (value);
143                         } catch (Exception ex) {
144                                 throw CreateInvalidValueException (attrName,
145                                         value, attr, ex);
146                         }
147                 }
148
149                 static EngineType ParseEngineType (XmlNode config, string attrName)
150                 {
151                         XmlAttribute attr = config.Attributes [attrName];
152                         if (attr == null)
153                                 throw CreateAttributeMissingException (attrName, config);
154
155                         string value = attr.Value;
156
157                         try {
158                                 return (EngineType) Enum.Parse (typeof (EngineType), value);
159                         } catch (Exception ex) {
160                                 throw CreateInvalidValueException (attrName,
161                                         value, attr, ex);
162                         }
163                 }
164
165                 static int ParseClientVersion (XmlNode config, string attrName)
166                 {
167                         XmlAttribute attr = config.Attributes [attrName];
168                         if (attr == null)
169                                 return -1;
170
171                         string value = attr.Value;
172
173                         try {
174                                 return Int32.Parse (value);
175                         } catch (Exception ex) {
176                                 throw CreateInvalidValueException (attrName,
177                                         value, attr, ex);
178                         }                       
179                 }
180
181                 static Exception CreateInvalidValueException (string name, string value, XmlNode node, Exception cause)
182                 {
183                         string msg = string.Format (CultureInfo.InvariantCulture,
184                                         "Invalid value '{0}' for attribute {1}.",
185                                         value, name);
186                         throw new ConfigurationErrorsException (msg, cause, node);
187                 }
188
189                 static Exception CreateAttributeMissingException (string name, XmlNode node)
190                 {
191                         string msg = string.Format (CultureInfo.InvariantCulture,
192                                 "Missing '{0}' attribute.", name);
193                         throw new ConfigurationErrorsException (msg, node);
194                 }
195         }
196 }