EngineConfig.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 EngineConfig ()
  48. {
  49. }
  50. public string Name {
  51. get { return name; }
  52. }
  53. /// <summary>
  54. /// Returns the character(s) for quoting identifiers.
  55. /// </summary>
  56. public string QuoteCharacter {
  57. get { return quoteCharacter; }
  58. }
  59. public EngineType Type {
  60. get { return type; }
  61. }
  62. public bool RemovesTrailingSpaces {
  63. get { return removesTrailingSpaces; }
  64. }
  65. public bool EmptyBinaryAsNull {
  66. get { return emptyBinaryAsNull; }
  67. }
  68. public bool SupportsMicroseconds {
  69. get { return supportsMicroseconds; }
  70. }
  71. public bool SupportsUniqueIdentifier {
  72. get { return supportsUniqueIdentifier; }
  73. }
  74. public bool SupportsDate {
  75. get { return supportsDate; }
  76. }
  77. public bool SupportsTime {
  78. get { return supportsTime; }
  79. }
  80. public bool SupportsTimestamp {
  81. get { return supportsTimestamp; }
  82. }
  83. public static EngineConfig FromXml (XmlNode config)
  84. {
  85. EngineConfig engine = new EngineConfig ();
  86. engine.name = GetAttribValue (config, "name", true);
  87. engine.quoteCharacter = GetAttribValue (config, "quoteCharacter", true);
  88. engine.removesTrailingSpaces = ParseBoolean (config, "removesTrailingSpaces", false, true);
  89. engine.emptyBinaryAsNull = ParseBoolean (config, "emptyBinaryAsNull", false, true);
  90. engine.supportsMicroseconds = ParseBoolean (config, "supportsMicroseconds", false, true);
  91. engine.supportsUniqueIdentifier = ParseBoolean (config, "supportsUniqueIdentifier", false, true);
  92. engine.supportsDate = ParseBoolean (config, "supportsDate", false, true);
  93. engine.supportsTime = ParseBoolean (config, "supportsTime", false, true);
  94. engine.supportsTimestamp = ParseBoolean (config, "supportsTimestamp", false, true);
  95. engine.type = ParseEngineType (config, "type");
  96. return engine;
  97. }
  98. static string GetAttribValue (XmlNode node, string name, bool required)
  99. {
  100. XmlAttribute attr = node.Attributes [name];
  101. if (attr == null) {
  102. if (required)
  103. throw CreateAttributeMissingException (name, node);
  104. return null;
  105. }
  106. return attr.Value;
  107. }
  108. static bool ParseBoolean (XmlNode config, string attrName, bool required, bool defaultValue)
  109. {
  110. XmlAttribute attr = config.Attributes [attrName];
  111. if (attr == null) {
  112. if (required)
  113. throw CreateAttributeMissingException (attrName, config);
  114. return defaultValue;
  115. }
  116. string value = attr.Value;
  117. try {
  118. return bool.Parse (value);
  119. } catch (Exception ex) {
  120. throw CreateInvalidValueException (attrName,
  121. value, attr, ex);
  122. }
  123. }
  124. static EngineType ParseEngineType (XmlNode config, string attrName)
  125. {
  126. XmlAttribute attr = config.Attributes [attrName];
  127. if (attr == null)
  128. throw CreateAttributeMissingException (attrName, config);
  129. string value = attr.Value;
  130. try {
  131. return (EngineType) Enum.Parse (typeof (EngineType), value);
  132. } catch (Exception ex) {
  133. throw CreateInvalidValueException (attrName,
  134. value, attr, ex);
  135. }
  136. }
  137. static Exception CreateInvalidValueException (string name, string value, XmlNode node, Exception cause)
  138. {
  139. string msg = string.Format (CultureInfo.InvariantCulture,
  140. "Invalid value '{0}' for attribute {1}.",
  141. value, name);
  142. #if NET_2_0
  143. throw new ConfigurationErrorsException (msg, cause, node);
  144. #else
  145. throw new ConfigurationException (msg, cause, node);
  146. #endif
  147. }
  148. static Exception CreateAttributeMissingException (string name, XmlNode node)
  149. {
  150. string msg = string.Format (CultureInfo.InvariantCulture,
  151. "Missing '{0}' attribute.", name);
  152. #if NET_2_0
  153. throw new ConfigurationErrorsException (msg, node);
  154. #else
  155. throw new ConfigurationException (msg, node);
  156. #endif
  157. }
  158. }
  159. }