EngineConfig.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. //
  2. // EngineConfig.cs - Holds information on the capabilities and behavior of an
  3. // RDBMS engine.
  4. //
  5. // Author:
  6. // Gert Driesen ([email protected]
  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. using System;
  30. using System.Configuration;
  31. using System.Globalization;
  32. using System.Xml;
  33. namespace MonoTests.System.Data
  34. {
  35. internal sealed class EngineConfig
  36. {
  37. private string name;
  38. private string quoteCharacter;
  39. private bool removesTrailingSpaces;
  40. private bool emptyBinaryAsNull;
  41. private bool supportsMicroseconds;
  42. private bool supportsUniqueIdentifier;
  43. private bool supportsDate;
  44. private bool supportsTime;
  45. private bool supportsTimestamp;
  46. private EngineType type;
  47. private int clientVersion;
  48. private EngineConfig ()
  49. {
  50. }
  51. public string Name {
  52. get { return name; }
  53. }
  54. /// <summary>
  55. /// Returns the character(s) for quoting identifiers.
  56. /// </summary>
  57. public string QuoteCharacter {
  58. get { return quoteCharacter; }
  59. }
  60. public EngineType Type {
  61. get { return type; }
  62. }
  63. public bool RemovesTrailingSpaces {
  64. get { return removesTrailingSpaces; }
  65. }
  66. public bool EmptyBinaryAsNull {
  67. get { return emptyBinaryAsNull; }
  68. }
  69. public bool SupportsMicroseconds {
  70. get { return supportsMicroseconds; }
  71. }
  72. public bool SupportsUniqueIdentifier {
  73. get { return supportsUniqueIdentifier; }
  74. }
  75. public bool SupportsDate {
  76. get { return supportsDate; }
  77. }
  78. public bool SupportsTime {
  79. get { return supportsTime; }
  80. }
  81. public bool SupportsTimestamp {
  82. get { return supportsTimestamp; }
  83. }
  84. public int ClientVersion {
  85. get { return clientVersion; }
  86. }
  87. public static EngineConfig FromXml (XmlNode config)
  88. {
  89. EngineConfig engine = new EngineConfig ();
  90. engine.name = GetAttribValue (config, "name", true);
  91. engine.quoteCharacter = GetAttribValue (config, "quoteCharacter", true);
  92. engine.removesTrailingSpaces = ParseBoolean (config, "removesTrailingSpaces", false, true);
  93. engine.emptyBinaryAsNull = ParseBoolean (config, "emptyBinaryAsNull", false, true);
  94. engine.supportsMicroseconds = ParseBoolean (config, "supportsMicroseconds", false, true);
  95. engine.supportsUniqueIdentifier = ParseBoolean (config, "supportsUniqueIdentifier", false, true);
  96. engine.supportsDate = ParseBoolean (config, "supportsDate", false, true);
  97. engine.supportsTime = ParseBoolean (config, "supportsTime", false, true);
  98. engine.supportsTimestamp = ParseBoolean (config, "supportsTimestamp", false, true);
  99. engine.type = ParseEngineType (config, "type");
  100. engine.clientVersion = ParseClientVersion (config, "clientversion");
  101. return engine;
  102. }
  103. static string GetAttribValue (XmlNode node, string name, bool required)
  104. {
  105. XmlAttribute attr = node.Attributes [name];
  106. if (attr == null) {
  107. if (required)
  108. throw CreateAttributeMissingException (name, node);
  109. return null;
  110. }
  111. return attr.Value;
  112. }
  113. static bool ParseBoolean (XmlNode config, string attrName, bool required, bool defaultValue)
  114. {
  115. XmlAttribute attr = config.Attributes [attrName];
  116. if (attr == null) {
  117. if (required)
  118. throw CreateAttributeMissingException (attrName, config);
  119. return defaultValue;
  120. }
  121. string value = attr.Value;
  122. try {
  123. return bool.Parse (value);
  124. } catch (Exception ex) {
  125. throw CreateInvalidValueException (attrName,
  126. value, attr, ex);
  127. }
  128. }
  129. static EngineType ParseEngineType (XmlNode config, string attrName)
  130. {
  131. XmlAttribute attr = config.Attributes [attrName];
  132. if (attr == null)
  133. throw CreateAttributeMissingException (attrName, config);
  134. string value = attr.Value;
  135. try {
  136. return (EngineType) Enum.Parse (typeof (EngineType), value);
  137. } catch (Exception ex) {
  138. throw CreateInvalidValueException (attrName,
  139. value, attr, ex);
  140. }
  141. }
  142. static int ParseClientVersion (XmlNode config, string attrName)
  143. {
  144. XmlAttribute attr = config.Attributes [attrName];
  145. if (attr == null)
  146. return -1;
  147. string value = attr.Value;
  148. try {
  149. return Int32.Parse (value);
  150. } catch (Exception ex) {
  151. throw CreateInvalidValueException (attrName,
  152. value, attr, ex);
  153. }
  154. }
  155. static Exception CreateInvalidValueException (string name, string value, XmlNode node, Exception cause)
  156. {
  157. string msg = string.Format (CultureInfo.InvariantCulture,
  158. "Invalid value '{0}' for attribute {1}.",
  159. value, name);
  160. #if NET_2_0
  161. throw new ConfigurationErrorsException (msg, cause, node);
  162. #else
  163. throw new ConfigurationException (msg, cause, node);
  164. #endif
  165. }
  166. static Exception CreateAttributeMissingException (string name, XmlNode node)
  167. {
  168. string msg = string.Format (CultureInfo.InvariantCulture,
  169. "Missing '{0}' attribute.", name);
  170. #if NET_2_0
  171. throw new ConfigurationErrorsException (msg, node);
  172. #else
  173. throw new ConfigurationException (msg, node);
  174. #endif
  175. }
  176. }
  177. }