* DataRelationCollection.cs (BinarySerialize): New. Carved out of ...
[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.Collections;
32 using System.ComponentModel;
33 using System.Collections.Generic;
34 using System.Data;
35 using System.Data.Common;
36 using System.Data.Odbc;
37 using System.Reflection;
38 using System.Text;
39
40 namespace System.Data.Odbc
41 {
42         [DefaultProperty ("Driver")]
43         [TypeConverter ("System.Data.Odbc.OdbcConnectionStringBuilder+OdbcConnectionStringBuilderConverter, " + Consts.AssemblySystem_Data)]
44         public sealed class OdbcConnectionStringBuilder : DbConnectionStringBuilder
45         {
46                 #region Fields
47                 string driver;
48                 string dsn;
49                 #endregion //Fields
50
51                 #region Constructors
52
53                 public OdbcConnectionStringBuilder () : base (true)
54                 {
55                 }
56
57                 public OdbcConnectionStringBuilder (string connectionString) : base (true)
58                 {
59                         if (connectionString == null) {
60                                 base.ConnectionString = string.Empty;
61                                 return;
62                         }
63
64                         base.ConnectionString = connectionString;
65                 }
66
67                 #endregion // Constructors
68
69                 #region Properties
70
71                 public override Object this [string keyword] {
72                         get {
73                                 if (keyword == null)
74                                         throw new ArgumentNullException ("keyword");
75                                 if (string.Compare (keyword, "Driver", StringComparison.InvariantCultureIgnoreCase) == 0)
76                                         return Driver;
77                                 if (string.Compare (keyword, "Dsn", StringComparison.InvariantCultureIgnoreCase) == 0)
78                                         return Dsn;
79                                 return base [keyword];
80                         }
81                         set {
82                                 if (value == null) {
83                                         Remove (keyword);
84                                         return;
85                                 }
86
87                                 if (keyword == null)
88                                         throw new ArgumentNullException ("keyword");
89
90                                 string text_value = value.ToString ();
91
92                                 if (string.Compare (keyword, "Driver", StringComparison.InvariantCultureIgnoreCase) == 0) {
93                                         Driver = text_value;
94                                         return;
95                                 } else if (string.Compare (keyword, "Dsn", StringComparison.InvariantCultureIgnoreCase) == 0) {
96                                         dsn = text_value;
97                                 } else if (value.ToString ().IndexOf (';') != -1) {
98                                         text_value = "{" + text_value + "}";
99                                 }
100                                 base [keyword] = value;
101                         }
102                 }
103
104                 public override ICollection Keys {
105                         get {
106                                 List<string> keys = new List<string> ();
107                                 keys.Add ("Dsn");
108                                 keys.Add ("Driver");
109
110                                 ICollection base_keys = base.Keys;
111                                 foreach (string keyword in base_keys) {
112                                         if (string.Compare (keyword, "Driver", StringComparison.InvariantCultureIgnoreCase) == 0)
113                                                 continue;
114                                         if (string.Compare (keyword, "Dsn", StringComparison.InvariantCultureIgnoreCase) == 0)
115                                                 continue;
116                                         keys.Add (keyword);
117                                 }
118
119                                 string [] final = new string [keys.Count];
120                                 keys.CopyTo (final);
121                                 return final;
122                         }
123                 }
124
125                 [DisplayName ("Driver")]
126                 [RefreshProperties (RefreshProperties.All)]
127                 public string Driver {
128                         get {
129                                 if (driver == null)
130                                         return string.Empty;
131                                 return driver;
132                         }
133                         set {
134                                 if (value == null)
135                                         throw new ArgumentNullException ("Driver");
136                                 driver = value;
137
138                                 if (value.Length > 0) {
139                                         int startBrace = value.IndexOf ('{');
140                                         int endBrace = value.IndexOf ('}');
141                                         if (startBrace == -1 || endBrace == -1)
142                                                 value = "{" + value + "}";
143                                         else if (startBrace > 0 || endBrace < (value.Length - 1))
144                                                 value = "{" + value + "}";
145                                 }
146                                 base ["Driver"] = value;
147                         }
148                 }
149                 
150                 [DisplayName ("Dsn")]
151                 [RefreshProperties (RefreshProperties.All)]
152                 public string Dsn {
153                         get {
154                                 if (dsn == null)
155                                         return string.Empty;
156                                 return dsn;
157                         }
158                         set {
159                                 if (value == null)
160                                         throw new ArgumentNullException ("Dsn");
161                                 dsn = value;
162                                 base ["Dsn"] = dsn;
163                         }
164                 }
165
166                 #endregion // Properties
167
168                 #region Methods
169
170                 public override bool ContainsKey (string keyword)
171                 {
172                         if (keyword == null)
173                                 throw new ArgumentNullException ("keyword");
174                         if (string.Compare (keyword, "Driver", StringComparison.InvariantCultureIgnoreCase) == 0)
175                                 return true;
176                         if (string.Compare (keyword, "Dsn", StringComparison.InvariantCultureIgnoreCase) == 0)
177                                 return true;
178                         return base.ContainsKey (keyword);
179                 }
180
181                 public override bool Remove (string keyword)
182                 {
183                         if (keyword == null)
184                                 throw new ArgumentNullException ("keyword");
185                         if (string.Compare (keyword, "Driver", StringComparison.InvariantCultureIgnoreCase) == 0)
186                                 driver = string.Empty;
187                         else if (string.Compare (keyword, "Dsn", StringComparison.InvariantCultureIgnoreCase) == 0)
188                                 dsn = string.Empty;
189                         return base.Remove (keyword);
190                 }
191
192                 public override void Clear ()
193                 {
194                         driver = null;
195                         dsn = null;
196                         base.Clear ();
197                 }
198
199                 public override bool TryGetValue (string keyword, out Object value)
200                 {
201                         if (keyword == null )
202                                 throw new ArgumentNullException ("keyword");
203                         bool found = base.TryGetValue (keyword, out value);
204                         if (found)
205                                 return found;
206                         if (string.Compare (keyword, "Driver", StringComparison.InvariantCultureIgnoreCase) == 0) {
207                                 value = string.Empty;
208                                 return true;
209                         } else if (string.Compare (keyword, "Dsn", StringComparison.InvariantCultureIgnoreCase) == 0) {
210                                 value = string.Empty;
211                                 return true;
212                         }
213                         return false;
214                 }
215
216                 #endregion // Methods
217         }
218 }
219 #endif // NET_2_0 using