OdbcConnectionStringBuilder.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. [DefaultProperty ("Driver")]
  41. [TypeConverter ("System.Data.Odbc.OdbcConnectionStringBuilder+OdbcConnectionStringBuilderConverter, " + Consts.AssemblySystem_Data)]
  42. public sealed class OdbcConnectionStringBuilder : DbConnectionStringBuilder
  43. {
  44. #region Fields
  45. string driver;
  46. string dsn;
  47. bool dsnFlag = false;
  48. bool driverFlag = false;
  49. bool driverBracketFlag = false;
  50. #endregion //Fields
  51. #region Constructors
  52. public OdbcConnectionStringBuilder ()
  53. {
  54. }
  55. // FIXME : Key-Value pairs returned does not match with the one returned in MS .Net
  56. public OdbcConnectionStringBuilder (string connectionString)
  57. {
  58. string key = "", val = "";
  59. if (connectionString == null) {
  60. base.ConnectionString = "";
  61. }
  62. else {
  63. string [] parameters = connectionString.Split (new char [] { ';' });
  64. foreach (string args in parameters) {
  65. if (parameters.Length == 1 && args.Trim () == "") {
  66. ConnectionString = "";
  67. } else {
  68. string [] arg = args.Split (new char [] { '=' }, 2);
  69. if (arg.Length == 2) {
  70. key = arg [0].Trim ();
  71. val = arg [1].Trim ();
  72. if (key == "")
  73. throw new ArgumentException ("Invalid value specified", key);
  74. if (val != "") {
  75. if (key == "Driver") {
  76. val = "{" + val + "}";
  77. driverBracketFlag = true;
  78. driverFlag = true;
  79. } else if (key == "Dsn")
  80. dsnFlag = true;
  81. base.Add (key.Trim (), val.Trim ());
  82. }
  83. }
  84. }
  85. }
  86. }
  87. }
  88. #endregion // Constructors
  89. #region Properties
  90. public override Object this [string keyword]
  91. {
  92. get {
  93. if (keyword == null || keyword.Trim () == "") {
  94. throw new ArgumentNullException ("Keyword should not be emtpy");
  95. }
  96. if (keyword == "Driver") {
  97. if (base ["Driver"].ToString ().EndsWith ("}"))
  98. base ["Driver"] = base ["Driver"].ToString ().Remove (base ["Driver"].ToString ().Length - 1, 1);
  99. if (base ["Driver"].ToString ().StartsWith ("{"))
  100. base ["Driver"] = base ["Driver"].ToString ().Remove (0, 1);
  101. driverBracketFlag = false;
  102. }
  103. return base [keyword];
  104. }
  105. set {
  106. if (keyword == null || keyword.Trim () == "") {
  107. throw new ArgumentNullException ("Keyword should not be emtpy");
  108. }
  109. if (keyword == "Driver") {
  110. value = "{" + value + "}";
  111. driverFlag = true;
  112. driverBracketFlag = true;
  113. } else if (keyword == "Dsn") {
  114. if (value == null)
  115. value = "";
  116. dsnFlag = true;
  117. } else if (value != null && value.ToString ().IndexOf (';') != -1) {
  118. value = "{" + value + "}";
  119. }
  120. base.Add (keyword, value);
  121. }
  122. }
  123. public override ICollection Keys
  124. {
  125. get {
  126. return base.Keys;
  127. }
  128. }
  129. [DisplayName ("Driver")]
  130. [RefreshProperties (RefreshProperties.All)]
  131. public string Driver {
  132. get {
  133. if (ContainsKey ("Driver")) {
  134. driver = base ["Driver"].ToString ();
  135. if ((driverBracketFlag == true) && (driver.EndsWith ("}")))
  136. driver = driver.Remove (base ["Driver"].ToString ().Length - 1, 1);
  137. if ((driverBracketFlag == true) && (driver.StartsWith ("{")))
  138. driver = driver.Remove (0, 1);
  139. driverBracketFlag = false;
  140. }
  141. return driver;
  142. }
  143. set {
  144. if (value == null)
  145. throw new ArgumentNullException ("Driver");
  146. driver = value ;
  147. base ["Driver"] = "{" + driver + "}";
  148. driverBracketFlag = true;
  149. driverFlag = true;
  150. }
  151. }
  152. [DisplayName ("Dsn")]
  153. [RefreshProperties (RefreshProperties.All)]
  154. public string Dsn {
  155. get {
  156. if (ContainsKey ("Dsn"))
  157. return base ["Dsn"].ToString ();
  158. else
  159. return dsn;
  160. }
  161. set {
  162. if (value == null)
  163. throw new ArgumentNullException ("Dsn");
  164. dsn = value;
  165. base ["Dsn"] = dsn;
  166. dsnFlag = true;
  167. }
  168. }
  169. #endregion // Properties
  170. #region Methods
  171. public override bool ContainsKey (string keyword)
  172. {
  173. if (keyword == null || keyword.Trim () == "") {
  174. throw new ArgumentNullException ("Keyword should not be emtpy");
  175. }
  176. if ((keyword == "Driver" && driverFlag == false) || (keyword == "Dsn" && dsnFlag == false))
  177. return true;
  178. else
  179. return base.ContainsKey (keyword);
  180. }
  181. public override bool Remove (string keyword)
  182. {
  183. if (keyword == null || keyword.Trim () == "") {
  184. throw new ArgumentNullException ("Keyword should not be emtpy");
  185. }
  186. return base.Remove (keyword);
  187. }
  188. public override void Clear ()
  189. {
  190. base.Clear ();
  191. }
  192. public override bool TryGetValue (string keyword, out Object value)
  193. {
  194. if (keyword == null || keyword.Trim () == "") {
  195. throw new ArgumentNullException ("Keyword should not be emtpy");
  196. }
  197. return base.TryGetValue (keyword, out value);
  198. }
  199. #endregion // Methods
  200. }
  201. }
  202. #endif // NET_2_0 using