2004-04-22 Martin Baulig <martin@ximian.com>
[mono.git] / mcs / class / System.Data / System.Data.Common / DbConnectionString.cs
1 //
2 // System.Data.Common.DbConnectionString
3 //
4 // Author:
5 //   Tim Coleman (tim@timcoleman.com)
6 //
7 // Copyright (C) Tim Coleman, 2003
8 //
9
10 #if NET_2_0
11
12 using System.Collections;
13 using System.Collections.Specialized;
14 using System.Data;
15 using System.Runtime.Serialization;
16 using System.Text;
17
18 namespace System.Data.Common {
19         public class DbConnectionString : ISerializable
20         {
21                 #region Fields
22
23                 KeyRestrictionBehavior behavior;
24                 string normalizedConnectionString;
25                 internal NameValueCollection options;
26
27                 #endregion // Fields
28
29                 #region Constructors
30
31                 [MonoTODO]
32                 protected internal DbConnectionString (DbConnectionString constr)
33                 {
34                         options = constr.options;
35                 }
36
37                 [MonoTODO]
38                 public DbConnectionString (string connectionString)
39                 {
40                         options = new NameValueCollection ();
41                         ParseConnectionString (connectionString);
42                 }
43                 
44                 [MonoTODO]
45                 protected DbConnectionString (SerializationInfo si, StreamingContext sc)
46                 {
47                 }
48
49                 [MonoTODO]
50                 public DbConnectionString (string connectionString, string restrictions, KeyRestrictionBehavior behavior)
51                         : this (connectionString)
52                 {
53                         this.behavior = behavior;
54                 }
55
56                 #endregion // Constructors
57
58                 #region Properties
59
60                 public KeyRestrictionBehavior Behavior {
61                         get { return behavior; }
62                 }
63
64                 [MonoTODO]
65                 protected virtual string CacheConnectionString {
66                         get { throw new NotImplementedException (); }
67                 }
68
69                 [MonoTODO]
70                 public bool IsEmpty {
71                         get { throw new NotImplementedException (); }
72                 }
73
74                 public string this [string x] {
75                         get { return options [x]; }
76                 }
77
78                 public ICollection Keys {
79                         get { return options.Keys; }
80                 }
81
82                 public string NormalizedConnectionString {
83                         get { return normalizedConnectionString; }
84                 }
85
86                 [MonoTODO]
87                 public string Restrictions {
88                         get { throw new NotImplementedException (); }
89                 }
90                 
91                 #endregion // Properties
92
93                 #region Methods
94
95                 public static void AppendKeyValuePairBuilder (StringBuilder builder, string keyname, string keyvalue)
96                 {
97                         throw new NotImplementedException ();
98                 }
99
100                 protected void BuildConnectionString (StringBuilder builder, string[] withoutOptions, string insertValue)
101                 {
102                         throw new NotImplementedException ();
103                 }
104
105                 public bool ContainsKey (string keyword)
106                 {
107                         return (options.Get (keyword) != null);
108                 }
109
110                 public bool ConvertValueToBoolean (string keyname, bool defaultvalue)
111                 {
112                         if (ContainsKey (keyname))
113                                 return Boolean.Parse (this [keyname].Trim ());
114                         return defaultvalue;
115                 }
116
117                 public int ConvertValueToInt32 (string keyname, int defaultvalue)
118                 {
119                         if (ContainsKey (keyname))
120                                 return Int32.Parse (this [keyname].Trim ());
121                         return defaultvalue;
122                 }
123
124                 public bool ConvertValueToIntegratedSecurity ()
125                 {
126                         throw new NotImplementedException ();
127                 }
128
129                 public string ConvertValueToString (string keyname, string defaultValue)
130                 {
131                         if (ContainsKey (keyname))
132                                 return this [keyname];
133                         return defaultValue;
134                 }
135
136                 public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
137                 {
138                         throw new NotImplementedException ();
139                 }
140
141                 protected virtual string KeywordLookup (string keyname)
142                 {
143                         return keyname;
144                 }
145
146                 protected void ParseConnectionString (string connectionString)
147                 {
148                         if (connectionString.Length == 0)
149                                 return;
150
151                         connectionString += ";";
152
153                         bool inQuote = false;
154                         bool inDQuote = false;
155                         bool inName = true;
156
157                         string name = String.Empty;
158                         string value = String.Empty;
159                         StringBuilder sb = new StringBuilder ();
160
161                         for (int i = 0; i < connectionString.Length; i += 1) {
162                                 char c = connectionString [i];
163                                 char peek;
164                                 if (i == connectionString.Length - 1)
165                                         peek = '\0';
166                                 else 
167                                         peek = connectionString [i + 1];
168
169                                 switch (c) {
170                                 case '\'':
171                                         if (inDQuote) 
172                                                 sb.Append (c);
173                                         else if (peek.Equals (c)) {
174                                                 sb.Append (c);
175                                                 i += 1;
176                                         }
177                                         else 
178                                                 inQuote = !inQuote;
179                                         break;
180                                 case '"':
181                                         if (inQuote) 
182                                                 sb.Append (c);
183                                         else if (peek.Equals (c)) {
184                                                 sb.Append (c);
185                                                 i += 1;
186                                         }
187                                         else 
188                                                 inDQuote = !inDQuote;
189                                         break;
190                                 case ';':
191                                         if (inDQuote || inQuote)
192                                                 sb.Append (c);
193                                         else {
194                                                 if (name != String.Empty && name != null) {
195                                                         value = sb.ToString ();
196                                                         options [KeywordLookup (name.Trim ())] = value;
197                                                 }
198                                                 inName = true;
199                                                 name = String.Empty;
200                                                 value = String.Empty;
201                                                 sb = new StringBuilder ();
202                                         }
203                                         break;
204                                 case '=':
205                                         if (inDQuote || inQuote || !inName)
206                                                 sb.Append (c);
207                                         else if (peek.Equals (c)) {
208                                                 sb.Append (c);
209                                                 i += 1;
210                                         } 
211                                         else {
212                                                 name = sb.ToString ();
213                                                 sb = new StringBuilder ();
214                                                 inName = false;
215                                         }
216                                         break;
217                                 case ' ':
218                                         if (inQuote || inDQuote)
219                                                 sb.Append (c);
220                                         else if (sb.Length > 0 && !peek.Equals (';'))
221                                                 sb.Append (c);
222                                         break;
223                                 default:
224                                         sb.Append (c);
225                                         break;
226                                 }
227                         }       
228                         
229                         StringBuilder normalized = new StringBuilder ();
230                         ArrayList keys = new ArrayList ();
231                         keys.AddRange (Keys);
232                         keys.Sort ();
233                         foreach (string key in keys)
234                         {
235                                 string entry = String.Format ("{0}=\"{1}\";", key, this [key].Replace ("\"", "\"\""));
236                                 normalized.Append (entry);
237                         }
238                         normalizedConnectionString = normalized.ToString ();
239                 }
240
241                 public virtual void PermissionDemand ()
242                 {
243                         throw new NotImplementedException ();
244                 }
245
246                 public static string RemoveKeyValuePairs (string connectionString, string[] keynames)
247                 {
248                         throw new NotImplementedException ();
249                 }
250
251                 public string UsersConnectionString (bool hisPasswordPwd)
252                 {
253                         throw new NotImplementedException ();
254                 }
255
256                 #endregion // Methods
257
258         }
259 }
260
261 #endif