TARGET_JVM: making public
[mono.git] / mcs / class / System.Data / System.Data.Common / DbConnectionOptions.cs
1 //
2 // System.Data.Common.DbConnectionOptions
3 //      adapted from older (pre beta1) DbConnectionString
4 //
5 // Authors:
6 //      Tim Coleman (tim@timcoleman.com)
7 //      Sebastien Pouliot  <sebastien@ximian.com>
8 //
9 // Copyright (C) Tim Coleman, 2003
10 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31
32 #if NET_2_0
33
34 using System.Collections;
35 using System.Collections.Specialized;
36 using System.Security;
37 using System.Text;
38
39 namespace System.Data.Common {
40
41         public class DbConnectionOptions {
42
43                 #region Fields
44
45                 internal NameValueCollection options;
46                 internal string normalizedConnectionString;
47
48                 #endregion // Fields
49
50                 #region Constructors
51
52                 internal DbConnectionOptions ()
53                 {
54                 }
55
56                 [MonoTODO]
57                 protected internal DbConnectionOptions (DbConnectionOptions connectionOptions)
58                 {
59                         options = connectionOptions.options;
60                 }
61
62                 [MonoTODO]
63                 public DbConnectionOptions (string connectionString)
64                 {
65                         options = new NameValueCollection ();
66                         ParseConnectionString (connectionString);
67                 }
68                 
69                 [MonoTODO]
70                 public DbConnectionOptions (string connectionString, Hashtable synonyms, bool useFirstKeyValuePair)
71                         : this (connectionString)
72                 {
73                 }
74
75                 #endregion // Constructors
76
77                 #region Properties
78
79                 [MonoTODO]
80                 public bool IsEmpty {
81                         get { throw new NotImplementedException (); }
82                 }
83
84                 public string this [string keyword] {
85                         get { return options [keyword]; }
86                 }
87
88                 public ICollection Keys {
89                         get { return options.Keys; }
90                 }
91
92                 #endregion // Properties
93
94                 #region Methods
95
96                 [MonoTODO]
97                 protected void BuildConnectionString (StringBuilder builder, string[] withoutOptions, string insertValue)
98                 {
99                         throw new NotImplementedException ();
100                 }
101
102                 public bool ContainsKey (string keyword)
103                 {
104                         return (options.Get (keyword) != null);
105                 }
106
107                 public bool ConvertValueToBoolean (string keyname, bool defaultvalue)
108                 {
109                         if (ContainsKey (keyname))
110                                 return Boolean.Parse (this [keyname].Trim ());
111                         return defaultvalue;
112                 }
113
114                 public int ConvertValueToInt32 (string keyname, int defaultvalue)
115                 {
116                         if (ContainsKey (keyname))
117                                 return Int32.Parse (this [keyname].Trim ());
118                         return defaultvalue;
119                 }
120
121                 [MonoTODO]
122                 public bool ConvertValueToIntegratedSecurity ()
123                 {
124                         throw new NotImplementedException ();
125                 }
126
127                 public string ConvertValueToString (string keyname, string defaultValue)
128                 {
129                         if (ContainsKey (keyname))
130                                 return this [keyname];
131                         return defaultValue;
132                 }
133
134                 [MonoTODO]
135                 protected internal virtual PermissionSet CreatePermissionSet ()
136                 {
137                         throw new NotImplementedException ();
138                 }
139
140                 [MonoTODO]
141                 protected internal virtual string Expand ()
142                 {
143                         throw new NotImplementedException ();
144                 }
145
146                 [MonoTODO]
147                 public static string RemoveKeyValuePairs (string connectionString, string[] keynames)
148                 {
149                         throw new NotImplementedException ();
150                 }
151
152                 [MonoTODO]
153                 public string UsersConnectionString (bool hisPasswordPwd)
154                 {
155                         throw new NotImplementedException ();
156                 }
157
158                 internal void ParseConnectionString (string connectionString)
159                 {
160                         if (connectionString.Length == 0)
161                                 return;
162
163                         connectionString += ";";
164
165                         bool inQuote = false;
166                         bool inDQuote = false;
167                         bool inName = true;
168
169                         string name = String.Empty;
170                         string value = String.Empty;
171                         StringBuilder sb = new StringBuilder ();
172
173                         for (int i = 0; i < connectionString.Length; i += 1) {
174                                 char c = connectionString [i];
175                                 char peek;
176                                 if (i == connectionString.Length - 1)
177                                         peek = '\0';
178                                 else 
179                                         peek = connectionString [i + 1];
180
181                                 switch (c) {
182                                 case '\'':
183                                         if (inDQuote) 
184                                                 sb.Append (c);
185                                         else if (peek.Equals (c)) {
186                                                 sb.Append (c);
187                                                 i += 1;
188                                         }
189                                         else 
190                                                 inQuote = !inQuote;
191                                         break;
192                                 case '"':
193                                         if (inQuote) 
194                                                 sb.Append (c);
195                                         else if (peek.Equals (c)) {
196                                                 sb.Append (c);
197                                                 i += 1;
198                                         }
199                                         else 
200                                                 inDQuote = !inDQuote;
201                                         break;
202                                 case ';':
203                                         if (inDQuote || inQuote)
204                                                 sb.Append (c);
205                                         else {
206                                                 if (name != String.Empty && name != null) {
207                                                         value = sb.ToString ();
208                                                         // FIXME - KeywordLookup is an NOP
209                                                         // options [KeywordLookup (name.Trim ())] = value;
210                                                         options [name.Trim ()] = value;
211                                                 }
212                                                 inName = true;
213                                                 name = String.Empty;
214                                                 value = String.Empty;
215                                                 sb = new StringBuilder ();
216                                         }
217                                         break;
218                                 case '=':
219                                         if (inDQuote || inQuote || !inName)
220                                                 sb.Append (c);
221                                         else if (peek.Equals (c)) {
222                                                 sb.Append (c);
223                                                 i += 1;
224                                         } 
225                                         else {
226                                                 name = sb.ToString ();
227                                                 sb = new StringBuilder ();
228                                                 inName = false;
229                                         }
230                                         break;
231                                 case ' ':
232                                         if (inQuote || inDQuote)
233                                                 sb.Append (c);
234                                         else if (sb.Length > 0 && !peek.Equals (';'))
235                                                 sb.Append (c);
236                                         break;
237                                 default:
238                                         sb.Append (c);
239                                         break;
240                                 }
241                         }       
242                         
243                         StringBuilder normalized = new StringBuilder ();
244                         ArrayList keys = new ArrayList ();
245                         keys.AddRange (Keys);
246                         keys.Sort ();
247                         foreach (string key in keys)
248                         {
249                                 string entry = String.Format ("{0}=\"{1}\";", key, this [key].Replace ("\"", "\"\""));
250                                 normalized.Append (entry);
251                         }
252                         normalizedConnectionString = normalized.ToString ();
253                 }
254
255                 #endregion // Methods
256         }
257 }
258
259 #endif