2 // System.Data.Odbc.OdbcConnectionStringBuilder
5 // Nidhi Rawal (rawalnidhi_rawal@yahoo.com)
7 // Copyright (C) 2007 Novell, Inc (http://www.novell.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
32 using System.Reflection;
33 using System.Collections;
34 using System.ComponentModel;
35 using System.Collections.Generic;
38 using System.Data.Common;
39 using System.Data.Odbc;
41 namespace System.Data.Odbc
43 [DefaultProperty ("Driver")]
44 [TypeConverter ("System.Data.Odbc.OdbcConnectionStringBuilder+OdbcConnectionStringBuilderConverter, " + Consts.AssemblySystem_Data)]
45 public sealed class OdbcConnectionStringBuilder : DbConnectionStringBuilder
51 bool driverFlag = false;
52 bool driverBracketFlag = false;
56 public OdbcConnectionStringBuilder ()
60 // FIXME : Key-Value pairs returned does not match with the one returned in MS .Net
61 public OdbcConnectionStringBuilder (string connectionString)
63 string key = "", val = "";
64 if (connectionString == null) {
65 base.ConnectionString = "";
68 string [] parameters = connectionString.Split (new char [] { ';' });
69 foreach (string args in parameters) {
70 if (parameters.Length == 1 && args.Trim () == "") {
71 ConnectionString = "";
73 string [] arg = args.Split (new char [] { '=' }, 2);
74 if (arg.Length == 2) {
75 key = arg [0].Trim ();
76 val = arg [1].Trim ();
78 throw new ArgumentException ("Invalid value specified", key);
80 if (key == "Driver") {
81 val = "{" + val + "}";
82 driverBracketFlag = true;
84 } else if (key == "Dsn")
86 base.Add (key.Trim (), val.Trim ());
94 #endregion // Constructors
97 public override Object this [string keyword]
100 if (keyword == null || keyword.Trim () == "") {
101 throw new ArgumentNullException ("Keyword should not be emtpy");
103 if (keyword == "Driver") {
104 if (base ["Driver"].ToString ().EndsWith ("}"))
105 base ["Driver"] = base ["Driver"].ToString ().Remove (base ["Driver"].ToString ().Length - 1, 1);
106 if (base ["Driver"].ToString ().StartsWith ("{"))
107 base ["Driver"] = base ["Driver"].ToString ().Remove (0, 1);
108 driverBracketFlag = false;
110 return base [keyword];
113 if (keyword == null || keyword.Trim () == "") {
114 throw new ArgumentNullException ("Keyword should not be emtpy");
116 if (keyword == "Driver") {
117 value = "{" + value + "}";
119 driverBracketFlag = true;
120 } else if (keyword == "Dsn") {
124 } else if (value != null && value.ToString ().IndexOf (';') != -1) {
125 value = "{" + value + "}";
127 base.Add (keyword, value);
131 public override ICollection Keys
138 [DisplayName ("Driver")]
139 [RefreshProperties (RefreshProperties.All)]
140 public string Driver {
142 if (ContainsKey ("Driver")) {
143 driver = base ["Driver"].ToString ();
144 if ((driverBracketFlag == true) && (driver.EndsWith ("}")))
145 driver = driver.Remove (base ["Driver"].ToString ().Length - 1, 1);
146 if ((driverBracketFlag == true) && (driver.StartsWith ("{")))
147 driver = driver.Remove (0, 1);
148 driverBracketFlag = false;
154 throw new ArgumentNullException ("Driver");
156 base ["Driver"] = "{" + driver + "}";
157 driverBracketFlag = true;
162 [DisplayName ("Dsn")]
163 [RefreshProperties (RefreshProperties.All)]
166 if (ContainsKey ("Dsn"))
167 return base ["Dsn"].ToString ();
173 throw new ArgumentNullException ("Dsn");
179 #endregion // Properties
182 public override bool ContainsKey (string keyword)
184 if (keyword == null || keyword.Trim () == "") {
185 throw new ArgumentNullException ("Keyword should not be emtpy");
187 if ((keyword == "Driver" && driverFlag == false) || (keyword == "Dsn" && dsnFlag == false))
190 return base.ContainsKey (keyword);
193 public override bool Remove (string keyword)
195 if (keyword == null || keyword.Trim () == "") {
196 throw new ArgumentNullException ("Keyword should not be emtpy");
198 return base.Remove (keyword);
201 public override void Clear ()
206 public override bool TryGetValue (string keyword, out Object value)
208 if (keyword == null || keyword.Trim () == "") {
209 throw new ArgumentNullException ("Keyword should not be emtpy");
211 return base.TryGetValue (keyword, out value);
213 #endregion // Methods
216 #endif // NET_2_0 using