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