OdbcConnectionStringBuilder.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. public string Driver {
  128. get {
  129. if (ContainsKey ("Driver")) {
  130. driver = base ["Driver"].ToString ();
  131. if ((driverBracketFlag == true) && (driver.EndsWith ("}")))
  132. driver = driver.Remove (base ["Driver"].ToString ().Length - 1, 1);
  133. if ((driverBracketFlag == true) && (driver.StartsWith ("{")))
  134. driver = driver.Remove (0, 1);
  135. driverBracketFlag = false;
  136. }
  137. return driver;
  138. }
  139. set {
  140. if (value == null)
  141. throw new ArgumentNullException ("Driver");
  142. driver = value ;
  143. base ["Driver"] = "{" + driver + "}";
  144. driverBracketFlag = true;
  145. driverFlag = true;
  146. }
  147. }
  148. public string Dsn {
  149. get {
  150. if (ContainsKey ("Dsn"))
  151. return base ["Dsn"].ToString ();
  152. else
  153. return dsn;
  154. }
  155. set {
  156. if (value == null)
  157. throw new ArgumentNullException ("Dsn");
  158. dsn = value;
  159. base ["Dsn"] = dsn;
  160. dsnFlag = true;
  161. }
  162. }
  163. #endregion // Properties
  164. #region Methods
  165. public override bool ContainsKey (string keyword)
  166. {
  167. if (keyword == null || keyword.Trim () == "") {
  168. throw new ArgumentNullException ("Keyword should not be emtpy");
  169. }
  170. if ((keyword == "Driver" && driverFlag == false) || (keyword == "Dsn" && dsnFlag == false))
  171. return true;
  172. else
  173. return base.ContainsKey (keyword);
  174. }
  175. public override bool Remove (string keyword)
  176. {
  177. if (keyword == null || keyword.Trim () == "") {
  178. throw new ArgumentNullException ("Keyword should not be emtpy");
  179. }
  180. return base.Remove (keyword);
  181. }
  182. public override void Clear ()
  183. {
  184. base.Clear ();
  185. }
  186. public override bool TryGetValue (string keyword, out Object value)
  187. {
  188. if (keyword == null || keyword.Trim () == "") {
  189. throw new ArgumentNullException ("Keyword should not be emtpy");
  190. }
  191. return base.TryGetValue (keyword, out value);
  192. }
  193. #endregion // Methods
  194. }
  195. }
  196. #endif // NET_2_0 using