2006-11-27 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[mono.git] / mcs / class / FirebirdSql.Data.Firebird / FirebirdSql.Data.Firebird / FbConnectionStringBuilder.cs
1 /*
2  *      Firebird ADO.NET Data provider for .NET and Mono 
3  * 
4  *         The contents of this file are subject to the Initial 
5  *         Developer's Public License Version 1.0 (the "License"); 
6  *         you may not use this file except in compliance with the 
7  *         License. You may obtain a copy of the License at 
8  *         http://www.firebirdsql.org/index.php?op=doc&id=idpl
9  *
10  *         Software distributed under the License is distributed on 
11  *         an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either 
12  *         express or implied. See the License for the specific 
13  *         language governing rights and limitations under the License.
14  * 
15  *      Copyright (c) 2004 Carlos Guzman Alvarez
16  *      All Rights Reserved.
17  */
18
19 using System;
20 using System.Collections;
21 using System.Data;
22 using System.Globalization;
23 using System.Text;
24
25 namespace FirebirdSql.Data.Firebird
26 {
27         /// <include file='Doc/en_EN/FbConnectionStringBuilder.xml'     path='doc/class[@name="FbConnectionStringBuilder"]/overview/*'/>
28         public sealed class FbConnectionStringBuilder
29         {
30                 #region Private fields
31
32                 private Hashtable options;
33
34                 #endregion
35
36                 #region Properties
37
38                 /// <include file='Doc/en_EN/FbConnectionStringBuilder.xml'     path='doc/class[@name="FbConnectionStringBuilder"]/property[@name="ConnectionString"]/*'/>
39                 public string ConnectionString
40                 {
41                         get { return this.ToString(); }
42                         set { this.Load(value); }
43                 }
44
45                 /// <include file='Doc/en_EN/FbConnectionStringBuilder.xml'     path='doc/class[@name="FbConnectionStringBuilder"]/property[@name="UserID"]/*'/>
46                 public string UserID
47                 {
48                         get { return this.GetString("user id"); }
49                         set { this.SetValue("user id", value); }
50                 }
51
52                 /// <include file='Doc/en_EN/FbConnectionStringBuilder.xml'     path='doc/class[@name="FbConnectionStringBuilder"]/property[@name="Password"]/*'/>
53                 public string Password
54                 {
55                         get { return this.GetString("password"); }
56                         set { this.SetValue("password", value); }
57                 }
58
59                 /// <include file='Doc/en_EN/FbConnectionStringBuilder.xml'     path='doc/class[@name="FbConnectionStringBuilder"]/property[@name="DataSource"]/*'/>
60                 public string DataSource
61                 {
62                         get { return this.GetString("data source"); }
63                         set { this.SetValue("data source", value); }
64                 }
65
66                 /// <include file='Doc/en_EN/FbConnectionStringBuilder.xml'     path='doc/class[@name="FbConnectionStringBuilder"]/property[@name="Port"]/*'/>
67                 public int Port
68                 {
69                         get { return this.GetInt32("port number"); }
70                         set { this.SetValue("port number", value); }
71                 }
72
73                 /// <include file='Doc/en_EN/FbConnectionStringBuilder.xml'     path='doc/class[@name="FbConnectionStringBuilder"]/property[@name="Database"]/*'/>
74                 public string Database
75                 {
76                         get { return this.GetString("database"); }
77                         set { this.SetValue("database", value); }
78                 }
79
80                 /// <include file='Doc/en_EN/FbConnectionStringBuilder.xml'     path='doc/class[@name="FbConnectionStringBuilder"]/property[@name="PacketSize"]/*'/>
81                 public short PacketSize
82                 {
83                         get { return this.GetInt16("packet size"); }
84                         set { this.SetValue("packet size", value); }
85                 }
86
87                 /// <include file='Doc/en_EN/FbConnectionStringBuilder.xml'     path='doc/class[@name="FbConnectionStringBuilder"]/property[@name="Role"]/*'/>
88                 public string Role
89                 {
90                         get { return this.GetString("role name"); }
91                         set
92                         {
93                                 if (value == null)
94                                 {
95                                         value = String.Empty;
96                                 }
97                                 this.SetValue("role name", value);
98                         }
99                 }
100
101                 /// <include file='Doc/en_EN/FbConnectionStringBuilder.xml'     path='doc/class[@name="FbConnectionStringBuilder"]/property[@name="Dialect"]/*'/>
102                 public byte Dialect
103                 {
104                         get { return this.GetByte("dialect"); }
105                         set { this.SetValue("dialect", value); }
106                 }
107
108                 /// <include file='Doc/en_EN/FbConnectionStringBuilder.xml'     path='doc/class[@name="FbConnectionStringBuilder"]/property[@name="Charset"]/*'/>
109                 public string Charset
110                 {
111                         get { return this.GetString("charset"); }
112                         set { this.SetValue("charset", value); }
113                 }
114
115                 /// <include file='Doc/en_EN/FbConnectionStringBuilder.xml'     path='doc/class[@name="FbConnectionStringBuilder"]/property[@name="ConnectionTimeout"]/*'/>
116                 public int ConnectionTimeout
117                 {
118                         get { return this.GetInt32("connection timeout"); }
119                         set { this.SetValue("connection timeout", value); }
120                 }
121
122                 /// <include file='Doc/en_EN/FbConnectionStringBuilder.xml'     path='doc/class[@name="FbConnectionStringBuilder"]/property[@name="Pooling"]/*'/>
123                 public bool Pooling
124                 {
125                         get { return this.GetBoolean("pooling"); }
126                         set { this.SetValue("pooling", value); }
127                 }
128
129                 /// <include file='Doc/en_EN/FbConnectionStringBuilder.xml'     path='doc/class[@name="FbConnectionStringBuilder"]/property[@name="ConnectionLifeTime"]/*'/>
130                 public long ConnectionLifeTime
131                 {
132                         get { return this.GetInt64("connection lifetime"); }
133                         set { this.SetValue("connection lifetime", value); }
134                 }
135
136                 /// <include file='Doc/en_EN/FbConnectionStringBuilder.xml'     path='doc/class[@name="FbConnectionStringBuilder"]/property[@name="MinPoolSize"]/*'/>
137                 public int MinPoolSize
138                 {
139                         get { return this.GetInt32("min pool size"); }
140                         set { this.SetValue("min pool size", value); }
141                 }
142
143                 /// <include file='Doc/en_EN/FbConnectionStringBuilder.xml'     path='doc/class[@name="FbConnectionStringBuilder"]/property[@name="MaxPoolSize"]/*'/>
144                 public int MaxPoolSize
145                 {
146                         get { return this.GetInt32("max pool size"); }
147                         set { this.SetValue("max pool size", value); }
148                 }
149
150                 /// <include file='Doc/en_EN/FbConnectionStringBuilder.xml'     path='doc/class[@name="FbConnectionStringBuilder"]/property[@name="FetchSize"]/*'/>
151                 public int FetchSize
152                 {
153                         get { return this.GetInt32("fetch size"); }
154                         set { this.SetValue("fetch size", value); }
155                 }
156
157                 /// <include file='Doc/en_EN/FbConnectionStringBuilder.xml'     path='doc/class[@name="FbConnectionStringBuilder"]/property[@name="ServerType"]/*'/>
158                 public int ServerType
159                 {
160                         get { return this.GetInt32("server type"); }
161                         set { this.SetValue("server type", value); }
162                 }
163
164                 /// <include file='Doc/en_EN/FbConnectionStringBuilder.xml'     path='doc/class[@name="FbConnectionStringBuilder"]/property[@name="IsolationLevel"]/*'/>
165                 public IsolationLevel IsolationLevel
166                 {
167                         get { return this.GetIsolationLevel("isolation level"); }
168                         set { this.SetValue("isolation level", value.ToString()); }
169                 }
170
171                 #endregion
172
173                 #region Constructors
174
175                 /// <include file='Doc/en_EN/FbConnectionStringBuilder.xml'     path='doc/class[@name="FbConnectionStringBuilder"]/constructor[@name="ctor"]/*'/>
176                 public FbConnectionStringBuilder() : this(null)
177                 {
178                 }
179
180                 /// <include file='Doc/en_EN/FbConnectionStringBuilder.xml'     path='doc/class[@name="FbConnectionStringBuilder"]/constructor[@name="ctor(System.String)"]/*'/>
181                 public FbConnectionStringBuilder(string connectionString)
182                 {
183                         this.options = new Hashtable();
184
185                         if (connectionString != null && connectionString.Length > 0)
186                         {
187                                 this.Load(connectionString);
188                         }
189                 }
190
191                 #endregion
192
193                 #region Overriden methods
194
195                 /// <summary>
196                 /// Overrided method, returns the Firebird connection string.
197                 /// </summary>
198                 /// <returns>The Firebird connection string.</returns>
199                 public override string ToString()
200                 {
201                         StringBuilder cs = new StringBuilder();
202
203                         IDictionaryEnumerator e = this.options.GetEnumerator();
204                         while (e.MoveNext())
205                         {
206                                 if (e.Value != null)
207                                 {
208                                         if (cs.Length > 0)
209                                         {
210                                                 cs.Append(";");
211                                         }
212                                         string key = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(e.Key.ToString());
213                                         cs.AppendFormat(CultureInfo.CurrentCulture, "{0}={1}", key, e.Value);
214                                 }
215                         }
216
217                         return cs.ToString();
218                 }
219
220                 #endregion
221
222                 #region Private Methods
223
224                 private void Load(string connectionString)
225                 {
226                         string[]        keyPairs = connectionString.Split(';');
227                         Hashtable       synonyms = FbConnectionString.Synonyms;
228
229                         if (this.options != null)
230                         {
231                                 this.options.Clear();
232                         }
233
234                         foreach (string keyPair in keyPairs)
235                         {
236                                 string[] values = keyPair.Split('=');
237
238                                 if (values.Length == 2 &&
239                                         values[0] != null && values[0].Length > 0 &&
240                                         values[1] != null && values[1].Length > 0)
241                                 {
242                                         values[0] = values[0].ToLower(CultureInfo.CurrentCulture).Trim();
243
244                                         if (synonyms.Contains(values[0]))
245                                         {
246                                                 string key = (string)synonyms[values[0]];
247                                                 this.options[key] = values[1].Trim();
248                                         }
249                                 }
250                         }
251                 }
252
253                 private string GetString(string key)
254                 {
255                         if (this.options.Contains(key))
256                         {
257                                 return (string)this.options[key];
258                         }
259                         return null;
260                 }
261
262                 private bool GetBoolean(string key)
263                 {
264                         if (this.options.Contains(key))
265                         {
266                                 return Boolean.Parse(this.options[key].ToString());
267                         }
268                         return false;
269                 }
270
271                 private byte GetByte(string key)
272                 {
273                         if (this.options.Contains(key))
274                         {
275                                 return Convert.ToByte(this.options[key], CultureInfo.InvariantCulture);
276                         }
277                         return 0;
278                 }
279
280                 private short GetInt16(string key)
281                 {
282                         if (this.options.Contains(key))
283                         {
284                                 return Convert.ToInt16(this.options[key], CultureInfo.InvariantCulture);
285                         }
286                         return 0;
287                 }
288
289                 private int GetInt32(string key)
290                 {
291                         if (this.options.Contains(key))
292                         {
293                                 return Convert.ToInt32(this.options[key], CultureInfo.InvariantCulture);
294                         }
295                         return 0;
296                 }
297
298                 private long GetInt64(string key)
299                 {
300                         if (this.options.Contains(key))
301                         {
302                                 return Convert.ToInt64(this.options[key], CultureInfo.InvariantCulture);
303                         }
304                         return 0;
305                 }
306
307                 private void SetValue(string key, object value)
308                 {
309                         if (this.options.Contains(key))
310                         {
311                                 this.options[key] = value;
312                         }
313                         else
314                         {
315                                 this.options.Add(key, value);
316                         }
317                 }
318
319                 private IsolationLevel GetIsolationLevel(string key)
320                 {
321                         if (this.options.Contains(key))
322                         {
323                                 string il = this.options[key].ToString().ToLower(CultureInfo.CurrentCulture);
324                                 switch (il)
325                                 {
326                                         case "readcommitted":
327                                                 return IsolationLevel.ReadCommitted;
328
329                                         case "readuncommitted":
330                                                 return IsolationLevel.ReadUncommitted;
331
332                                         case "repeatableread":
333                                                 return IsolationLevel.RepeatableRead;
334
335                                         case "serializable":
336                                                 return IsolationLevel.Serializable;
337
338                                         case "chaos":
339                                                 return IsolationLevel.Chaos;
340
341                                         case "unspecified":
342                                                 return IsolationLevel.Unspecified;
343                                 }
344                         }
345                         return IsolationLevel.ReadCommitted;
346                 }
347
348                 #endregion
349         }
350 }