OdbcConnectionStringBuilder.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. //
  2. // System.Data.Odbc.OdbcConnectionStringBuilder
  3. //
  4. // Authors:
  5. // Nidhi Rawal ([email protected])
  6. //
  7. // Copyright (C) 2007 Novell, Inc (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. #if NET_2_0
  29. using System;
  30. using System.Text;
  31. using System.Reflection;
  32. using System.Collections;
  33. using System.ComponentModel;
  34. using System.Collections.Generic;
  35. using System.Data;
  36. using System.Data.Common;
  37. using System.Data.Odbc;
  38. namespace System.Data.Odbc
  39. {
  40. public sealed class OdbcConnectionStringBuilder : DbConnectionStringBuilder
  41. {
  42. #region Fields
  43. string driver;
  44. string dsn;
  45. bool dsnFlag = false;
  46. bool driverFlag = false;
  47. bool driverBracketFlag = false;
  48. #endregion //Fields
  49. #region Constructors
  50. public OdbcConnectionStringBuilder ()
  51. {
  52. }
  53. // FIXME : Key-Value pairs returned does not match with the one returned in MS .Net
  54. public OdbcConnectionStringBuilder (string connectionString)
  55. {
  56. string key = "", val = "";
  57. if (connectionString == null) {
  58. base.ConnectionString = "";
  59. }
  60. else {
  61. string [] parameters = connectionString.Split (new char [] { ';' });
  62. foreach (string args in parameters) {
  63. if (parameters.Length == 1 && args.Trim () == "") {
  64. ConnectionString = "";
  65. } else {
  66. string [] arg = args.Split (new char [] { '=' }, 2);
  67. if (arg.Length == 2) {
  68. key = arg [0].Trim ();
  69. val = arg [1].Trim ();
  70. if (key == "")
  71. throw new ArgumentException ("Invalid value specified", key);
  72. if (val != "") {
  73. if (key == "Driver") {
  74. val = "{" + val + "}";
  75. driverBracketFlag = true;
  76. driverFlag = true;
  77. } else if (key == "Dsn")
  78. dsnFlag = true;
  79. base.Add (key.Trim (), val.Trim ());
  80. }
  81. }
  82. }
  83. }
  84. }
  85. }
  86. #endregion // Constructors
  87. #region Properties
  88. public override Object this [string keyword]
  89. {
  90. get {
  91. if (keyword == null || keyword.Trim () == "") {
  92. throw new ArgumentNullException ("Keyword should not be emtpy");
  93. }
  94. if (keyword == "Driver") {
  95. if (base ["Driver"].ToString ().EndsWith ("}"))
  96. base ["Driver"] = base ["Driver"].ToString ().Remove (base ["Driver"].ToString ().Length - 1, 1);
  97. if (base ["Driver"].ToString ().StartsWith ("{"))
  98. base ["Driver"] = base ["Driver"].ToString ().Remove (0, 1);
  99. driverBracketFlag = false;
  100. }
  101. return base [keyword];
  102. }
  103. set {
  104. if (keyword == null || keyword.Trim () == "") {
  105. throw new ArgumentNullException ("Keyword should not be emtpy");
  106. }
  107. if (keyword == "Driver") {
  108. value = "{" + value + "}";
  109. driverFlag = true;
  110. driverBracketFlag = true;
  111. } else if (keyword == "Dsn") {
  112. if (value == null)
  113. value = "";
  114. dsnFlag = true;
  115. } else if (value != null && value.ToString ().IndexOf (';') != -1) {
  116. value = "{" + value + "}";
  117. }
  118. base.Add (keyword, value);
  119. }
  120. }
  121. public override ICollection Keys
  122. {
  123. get {
  124. return base.Keys;
  125. }
  126. }
  127. [DisplayName ("Driver")]
  128. [RefreshProperties (RefreshProperties.All)]
  129. public string Driver {
  130. get {
  131. if (ContainsKey ("Driver")) {
  132. driver = base ["Driver"].ToString ();
  133. if ((driverBracketFlag == true) && (driver.EndsWith ("}")))
  134. driver = driver.Remove (base ["Driver"].ToString ().Length - 1, 1);
  135. if ((driverBracketFlag == true) && (driver.StartsWith ("{")))
  136. driver = driver.Remove (0, 1);
  137. driverBracketFlag = false;
  138. }
  139. return driver;
  140. }
  141. set {
  142. if (value == null)
  143. throw new ArgumentNullException ("Driver");
  144. driver = value ;
  145. base ["Driver"] = "{" + driver + "}";
  146. driverBracketFlag = true;
  147. driverFlag = true;
  148. }
  149. }
  150. [DisplayName ("Dsn")]
  151. [RefreshProperties (RefreshProperties.All)]
  152. public string Dsn {
  153. get {
  154. if (ContainsKey ("Dsn"))
  155. return base ["Dsn"].ToString ();
  156. else
  157. return dsn;
  158. }
  159. set {
  160. if (value == null)
  161. throw new ArgumentNullException ("Dsn");
  162. dsn = value;
  163. base ["Dsn"] = dsn;
  164. dsnFlag = true;
  165. }
  166. }
  167. #endregion // Properties
  168. #region Methods
  169. public override bool ContainsKey (string keyword)
  170. {
  171. if (keyword == null || keyword.Trim () == "") {
  172. throw new ArgumentNullException ("Keyword should not be emtpy");
  173. }
  174. if ((keyword == "Driver" && driverFlag == false) || (keyword == "Dsn" && dsnFlag == false))
  175. return true;
  176. else
  177. return base.ContainsKey (keyword);
  178. }
  179. public override bool Remove (string keyword)
  180. {
  181. if (keyword == null || keyword.Trim () == "") {
  182. throw new ArgumentNullException ("Keyword should not be emtpy");
  183. }
  184. return base.Remove (keyword);
  185. }
  186. public override void Clear ()
  187. {
  188. base.Clear ();
  189. }
  190. public override bool TryGetValue (string keyword, out Object value)
  191. {
  192. if (keyword == null || keyword.Trim () == "") {
  193. throw new ArgumentNullException ("Keyword should not be emtpy");
  194. }
  195. return base.TryGetValue (keyword, out value);
  196. }
  197. #endregion // Methods
  198. }
  199. }
  200. #endif // NET_2_0 using