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
|
//
// EngineConfig.cs - Holds information on the capabilities and behavior of an
// RDBMS engine.
//
// Author:
// Gert Driesen (drieseng@users.sourceforge.net
//
// Copyright (c) 2008 Gert Driesen
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Configuration;
using System.Globalization;
using System.Xml;
namespace MonoTests.System.Data
{
internal sealed class EngineConfig
{
private string name;
private string quoteCharacter;
private bool removesTrailingSpaces;
private bool emptyBinaryAsNull;
private bool supportsMicroseconds;
private bool supportsUniqueIdentifier;
private bool supportsDate;
private bool supportsTime;
private bool supportsTimestamp;
private EngineType type;
private int clientVersion;
private EngineConfig ()
{
}
public string Name {
get { return name; }
}
/// <summary>
/// Returns the character(s) for quoting identifiers.
/// </summary>
public string QuoteCharacter {
get { return quoteCharacter; }
}
public EngineType Type {
get { return type; }
}
public bool RemovesTrailingSpaces {
get { return removesTrailingSpaces; }
}
public bool EmptyBinaryAsNull {
get { return emptyBinaryAsNull; }
}
public bool SupportsMicroseconds {
get { return supportsMicroseconds; }
}
public bool SupportsUniqueIdentifier {
get { return supportsUniqueIdentifier; }
}
public bool SupportsDate {
get { return supportsDate; }
}
public bool SupportsTime {
get { return supportsTime; }
}
public bool SupportsTimestamp {
get { return supportsTimestamp; }
}
public int ClientVersion {
get { return clientVersion; }
}
public static EngineConfig FromXml (XmlNode config)
{
EngineConfig engine = new EngineConfig ();
engine.name = GetAttribValue (config, "name", true);
engine.quoteCharacter = GetAttribValue (config, "quoteCharacter", true);
engine.removesTrailingSpaces = ParseBoolean (config, "removesTrailingSpaces", false, true);
engine.emptyBinaryAsNull = ParseBoolean (config, "emptyBinaryAsNull", false, true);
engine.supportsMicroseconds = ParseBoolean (config, "supportsMicroseconds", false, true);
engine.supportsUniqueIdentifier = ParseBoolean (config, "supportsUniqueIdentifier", false, true);
engine.supportsDate = ParseBoolean (config, "supportsDate", false, true);
engine.supportsTime = ParseBoolean (config, "supportsTime", false, true);
engine.supportsTimestamp = ParseBoolean (config, "supportsTimestamp", false, true);
engine.type = ParseEngineType (config, "type");
engine.clientVersion = ParseClientVersion (config, "clientversion");
return engine;
}
static string GetAttribValue (XmlNode node, string name, bool required)
{
XmlAttribute attr = node.Attributes [name];
if (attr == null) {
if (required)
throw CreateAttributeMissingException (name, node);
return null;
}
return attr.Value;
}
static bool ParseBoolean (XmlNode config, string attrName, bool required, bool defaultValue)
{
XmlAttribute attr = config.Attributes [attrName];
if (attr == null) {
if (required)
throw CreateAttributeMissingException (attrName, config);
return defaultValue;
}
string value = attr.Value;
try {
return bool.Parse (value);
} catch (Exception ex) {
throw CreateInvalidValueException (attrName,
value, attr, ex);
}
}
static EngineType ParseEngineType (XmlNode config, string attrName)
{
XmlAttribute attr = config.Attributes [attrName];
if (attr == null)
throw CreateAttributeMissingException (attrName, config);
string value = attr.Value;
try {
return (EngineType) Enum.Parse (typeof (EngineType), value);
} catch (Exception ex) {
throw CreateInvalidValueException (attrName,
value, attr, ex);
}
}
static int ParseClientVersion (XmlNode config, string attrName)
{
XmlAttribute attr = config.Attributes [attrName];
if (attr == null)
return -1;
string value = attr.Value;
try {
return Int32.Parse (value);
} catch (Exception ex) {
throw CreateInvalidValueException (attrName,
value, attr, ex);
}
}
static Exception CreateInvalidValueException (string name, string value, XmlNode node, Exception cause)
{
string msg = string.Format (CultureInfo.InvariantCulture,
"Invalid value '{0}' for attribute {1}.",
value, name);
throw new ConfigurationErrorsException (msg, cause, node);
}
static Exception CreateAttributeMissingException (string name, XmlNode node)
{
string msg = string.Format (CultureInfo.InvariantCulture,
"Missing '{0}' attribute.", name);
throw new ConfigurationErrorsException (msg, node);
}
}
}
|