Merge pull request #5714 from alexischr/update_bockbuild
[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.Globalization;
32 using System.Xml;
33
34 namespace MonoTests.System.Data.Connected
35 {
36         public sealed class EngineConfig
37         {
38                 private string name;
39                 private string quoteCharacter;
40                 private bool removesTrailingSpaces;
41                 private bool emptyBinaryAsNull;
42                 private bool supportsMicroseconds;
43                 private bool supportsUniqueIdentifier;
44                 private bool supportsDate;
45                 private bool supportsTime;
46                 private bool supportsTimestamp;
47                 private EngineType type;
48                 private int clientVersion;
49
50                 public EngineConfig ()
51                 {
52                 }
53
54                 public string Name {
55                         get { return name; }
56                 }
57
58                 /// <summary>
59                 /// Returns the character(s) for quoting identifiers.
60                 /// </summary>
61                 public string QuoteCharacter {
62                         get { return quoteCharacter; }
63                         set { quoteCharacter = value; }
64                 }
65
66                 public EngineType Type {
67                         get { return type; }
68                         set { type = value; }
69                 }
70
71                 public bool RemovesTrailingSpaces {
72                         get { return removesTrailingSpaces; }
73                         set { removesTrailingSpaces = value; }
74                 }
75
76                 public bool EmptyBinaryAsNull {
77                         get { return emptyBinaryAsNull; }
78                         set { emptyBinaryAsNull = value; }
79                 }
80
81                 public bool SupportsMicroseconds {
82                         get { return supportsMicroseconds; }
83                         set { supportsMicroseconds = value; }
84                 }
85
86                 public bool SupportsUniqueIdentifier {
87                         get { return supportsUniqueIdentifier; }
88                         set { supportsUniqueIdentifier = value; }
89                 }
90
91                 public bool SupportsDate {
92                         get { return supportsDate; }
93                         set { supportsDate = value; }
94                 }
95
96                 public bool SupportsTime {
97                         get { return supportsTime; }
98                         set { supportsTime = value; }
99                 }
100
101                 public bool SupportsTimestamp {
102                         get { return supportsTimestamp; }
103                         set { supportsTimestamp = value; }
104                 }
105
106                 public int ClientVersion {
107                        get { return clientVersion; }
108                        set { clientVersion = value; }
109                 }
110
111                 public static EngineConfig FromXml (XmlNode config)
112                 {
113                         EngineConfig engine = new EngineConfig ();
114                         engine.name = GetAttribValue (config, "name", true);
115                         engine.quoteCharacter = GetAttribValue (config, "quoteCharacter", true);
116                         engine.removesTrailingSpaces = ParseBoolean (config, "removesTrailingSpaces", false, true);
117                         engine.emptyBinaryAsNull = ParseBoolean (config, "emptyBinaryAsNull", false, true);
118                         engine.supportsMicroseconds = ParseBoolean (config, "supportsMicroseconds", false, true);
119                         engine.supportsUniqueIdentifier = ParseBoolean (config, "supportsUniqueIdentifier", false, true);
120                         engine.supportsDate = ParseBoolean (config, "supportsDate", false, true);
121                         engine.supportsTime = ParseBoolean (config, "supportsTime", false, true);
122                         engine.supportsTimestamp = ParseBoolean (config, "supportsTimestamp", false, true);
123                         engine.type = ParseEngineType (config, "type");
124                         engine.clientVersion = ParseClientVersion (config, "clientversion");
125                         return engine;
126                 }
127
128                 static string GetAttribValue (XmlNode node, string name, bool required)
129                 {
130                         XmlAttribute attr = node.Attributes [name];
131                         if (attr == null) {
132                                 if (required)
133                                         throw CreateAttributeMissingException (name, node);
134                                 return null;
135                         }
136                         return attr.Value;
137                 }
138
139                 static bool ParseBoolean (XmlNode config, string attrName, bool required, bool defaultValue)
140                 {
141                         XmlAttribute attr = config.Attributes [attrName];
142                         if (attr == null) {
143                                 if (required)
144                                         throw CreateAttributeMissingException (attrName, config);
145                                 return defaultValue;
146                         }
147
148                         string value = attr.Value;
149
150                         try {
151                                 return bool.Parse (value);
152                         } catch (Exception ex) {
153                                 throw CreateInvalidValueException (attrName,
154                                         value, attr, ex);
155                         }
156                 }
157
158                 static EngineType ParseEngineType (XmlNode config, string attrName)
159                 {
160                         XmlAttribute attr = config.Attributes [attrName];
161                         if (attr == null)
162                                 throw CreateAttributeMissingException (attrName, config);
163
164                         string value = attr.Value;
165
166                         try {
167                                 return (EngineType) Enum.Parse (typeof (EngineType), value);
168                         } catch (Exception ex) {
169                                 throw CreateInvalidValueException (attrName,
170                                         value, attr, ex);
171                         }
172                 }
173
174                 static int ParseClientVersion (XmlNode config, string attrName)
175                 {
176                         XmlAttribute attr = config.Attributes [attrName];
177                         if (attr == null)
178                                 return -1;
179
180                         string value = attr.Value;
181
182                         try {
183                                 return Int32.Parse (value);
184                         } catch (Exception ex) {
185                                 throw CreateInvalidValueException (attrName,
186                                         value, attr, ex);
187                         }                       
188                 }
189
190                 static Exception CreateInvalidValueException (string name, string value, XmlNode node, Exception cause)
191                 {
192                         string msg = string.Format (CultureInfo.InvariantCulture,
193                                         "Invalid value '{0}' for attribute {1}.",
194                                         value, name);
195                         throw new ArgumentOutOfRangeException (msg, cause);
196                 }
197
198                 static Exception CreateAttributeMissingException (string name, XmlNode node)
199                 {
200                         string msg = string.Format (CultureInfo.InvariantCulture,
201                                 "Missing '{0}' attribute.", name);
202                         throw new ArgumentException (msg);
203                 }
204         }
205 }