2 * Firebird ADO.NET Data provider for .NET and Mono
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
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.
15 * Copyright (c) 2004 Carlos Guzman Alvarez
16 * All Rights Reserved.
20 using System.Collections;
22 using System.Globalization;
25 namespace FirebirdSql.Data.Firebird
27 /// <include file='Doc/en_EN/FbConnectionStringBuilder.xml' path='doc/class[@name="FbConnectionStringBuilder"]/overview/*'/>
28 public sealed class FbConnectionStringBuilder
30 #region Private fields
32 private Hashtable options;
38 /// <include file='Doc/en_EN/FbConnectionStringBuilder.xml' path='doc/class[@name="FbConnectionStringBuilder"]/property[@name="ConnectionString"]/*'/>
39 public string ConnectionString
41 get { return this.ToString(); }
42 set { this.Load(value); }
45 /// <include file='Doc/en_EN/FbConnectionStringBuilder.xml' path='doc/class[@name="FbConnectionStringBuilder"]/property[@name="UserID"]/*'/>
48 get { return this.GetString("user id"); }
49 set { this.SetValue("user id", value); }
52 /// <include file='Doc/en_EN/FbConnectionStringBuilder.xml' path='doc/class[@name="FbConnectionStringBuilder"]/property[@name="Password"]/*'/>
53 public string Password
55 get { return this.GetString("password"); }
56 set { this.SetValue("password", value); }
59 /// <include file='Doc/en_EN/FbConnectionStringBuilder.xml' path='doc/class[@name="FbConnectionStringBuilder"]/property[@name="DataSource"]/*'/>
60 public string DataSource
62 get { return this.GetString("data source"); }
63 set { this.SetValue("data source", value); }
66 /// <include file='Doc/en_EN/FbConnectionStringBuilder.xml' path='doc/class[@name="FbConnectionStringBuilder"]/property[@name="Port"]/*'/>
69 get { return this.GetInt32("port number"); }
70 set { this.SetValue("port number", value); }
73 /// <include file='Doc/en_EN/FbConnectionStringBuilder.xml' path='doc/class[@name="FbConnectionStringBuilder"]/property[@name="Database"]/*'/>
74 public string Database
76 get { return this.GetString("database"); }
77 set { this.SetValue("database", value); }
80 /// <include file='Doc/en_EN/FbConnectionStringBuilder.xml' path='doc/class[@name="FbConnectionStringBuilder"]/property[@name="PacketSize"]/*'/>
81 public short PacketSize
83 get { return this.GetInt16("packet size"); }
84 set { this.SetValue("packet size", value); }
87 /// <include file='Doc/en_EN/FbConnectionStringBuilder.xml' path='doc/class[@name="FbConnectionStringBuilder"]/property[@name="Role"]/*'/>
90 get { return this.GetString("role name"); }
97 this.SetValue("role name", value);
101 /// <include file='Doc/en_EN/FbConnectionStringBuilder.xml' path='doc/class[@name="FbConnectionStringBuilder"]/property[@name="Dialect"]/*'/>
104 get { return this.GetByte("dialect"); }
105 set { this.SetValue("dialect", value); }
108 /// <include file='Doc/en_EN/FbConnectionStringBuilder.xml' path='doc/class[@name="FbConnectionStringBuilder"]/property[@name="Charset"]/*'/>
109 public string Charset
111 get { return this.GetString("charset"); }
112 set { this.SetValue("charset", value); }
115 /// <include file='Doc/en_EN/FbConnectionStringBuilder.xml' path='doc/class[@name="FbConnectionStringBuilder"]/property[@name="ConnectionTimeout"]/*'/>
116 public int ConnectionTimeout
118 get { return this.GetInt32("connection timeout"); }
119 set { this.SetValue("connection timeout", value); }
122 /// <include file='Doc/en_EN/FbConnectionStringBuilder.xml' path='doc/class[@name="FbConnectionStringBuilder"]/property[@name="Pooling"]/*'/>
125 get { return this.GetBoolean("pooling"); }
126 set { this.SetValue("pooling", value); }
129 /// <include file='Doc/en_EN/FbConnectionStringBuilder.xml' path='doc/class[@name="FbConnectionStringBuilder"]/property[@name="ConnectionLifeTime"]/*'/>
130 public long ConnectionLifeTime
132 get { return this.GetInt64("connection lifetime"); }
133 set { this.SetValue("connection lifetime", value); }
136 /// <include file='Doc/en_EN/FbConnectionStringBuilder.xml' path='doc/class[@name="FbConnectionStringBuilder"]/property[@name="MinPoolSize"]/*'/>
137 public int MinPoolSize
139 get { return this.GetInt32("min pool size"); }
140 set { this.SetValue("min pool size", value); }
143 /// <include file='Doc/en_EN/FbConnectionStringBuilder.xml' path='doc/class[@name="FbConnectionStringBuilder"]/property[@name="MaxPoolSize"]/*'/>
144 public int MaxPoolSize
146 get { return this.GetInt32("max pool size"); }
147 set { this.SetValue("max pool size", value); }
150 /// <include file='Doc/en_EN/FbConnectionStringBuilder.xml' path='doc/class[@name="FbConnectionStringBuilder"]/property[@name="FetchSize"]/*'/>
153 get { return this.GetInt32("fetch size"); }
154 set { this.SetValue("fetch size", value); }
157 /// <include file='Doc/en_EN/FbConnectionStringBuilder.xml' path='doc/class[@name="FbConnectionStringBuilder"]/property[@name="ServerType"]/*'/>
158 public int ServerType
160 get { return this.GetInt32("server type"); }
161 set { this.SetValue("server type", value); }
164 /// <include file='Doc/en_EN/FbConnectionStringBuilder.xml' path='doc/class[@name="FbConnectionStringBuilder"]/property[@name="IsolationLevel"]/*'/>
165 public IsolationLevel IsolationLevel
167 get { return this.GetIsolationLevel("isolation level"); }
168 set { this.SetValue("isolation level", value.ToString()); }
175 /// <include file='Doc/en_EN/FbConnectionStringBuilder.xml' path='doc/class[@name="FbConnectionStringBuilder"]/constructor[@name="ctor"]/*'/>
176 public FbConnectionStringBuilder() : this(null)
180 /// <include file='Doc/en_EN/FbConnectionStringBuilder.xml' path='doc/class[@name="FbConnectionStringBuilder"]/constructor[@name="ctor(System.String)"]/*'/>
181 public FbConnectionStringBuilder(string connectionString)
183 this.options = new Hashtable();
185 if (connectionString != null && connectionString.Length > 0)
187 this.Load(connectionString);
193 #region Overriden methods
196 /// Overrided method, returns the Firebird connection string.
198 /// <returns>The Firebird connection string.</returns>
199 public override string ToString()
201 StringBuilder cs = new StringBuilder();
203 IDictionaryEnumerator e = this.options.GetEnumerator();
212 string key = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(e.Key.ToString());
213 cs.AppendFormat(CultureInfo.CurrentCulture, "{0}={1}", key, e.Value);
217 return cs.ToString();
222 #region Private Methods
224 private void Load(string connectionString)
226 string[] keyPairs = connectionString.Split(';');
227 Hashtable synonyms = FbConnectionString.Synonyms;
229 if (this.options != null)
231 this.options.Clear();
234 foreach (string keyPair in keyPairs)
236 string[] values = keyPair.Split('=');
238 if (values.Length == 2 &&
239 values[0] != null && values[0].Length > 0 &&
240 values[1] != null && values[1].Length > 0)
242 values[0] = values[0].ToLower(CultureInfo.CurrentCulture).Trim();
244 if (synonyms.Contains(values[0]))
246 string key = (string)synonyms[values[0]];
247 this.options[key] = values[1].Trim();
253 private string GetString(string key)
255 if (this.options.Contains(key))
257 return (string)this.options[key];
262 private bool GetBoolean(string key)
264 if (this.options.Contains(key))
266 return Boolean.Parse(this.options[key].ToString());
271 private byte GetByte(string key)
273 if (this.options.Contains(key))
275 return Convert.ToByte(this.options[key], CultureInfo.InvariantCulture);
280 private short GetInt16(string key)
282 if (this.options.Contains(key))
284 return Convert.ToInt16(this.options[key], CultureInfo.InvariantCulture);
289 private int GetInt32(string key)
291 if (this.options.Contains(key))
293 return Convert.ToInt32(this.options[key], CultureInfo.InvariantCulture);
298 private long GetInt64(string key)
300 if (this.options.Contains(key))
302 return Convert.ToInt64(this.options[key], CultureInfo.InvariantCulture);
307 private void SetValue(string key, object value)
309 if (this.options.Contains(key))
311 this.options[key] = value;
315 this.options.Add(key, value);
319 private IsolationLevel GetIsolationLevel(string key)
321 if (this.options.Contains(key))
323 string il = this.options[key].ToString().ToLower(CultureInfo.CurrentCulture);
326 case "readcommitted":
327 return IsolationLevel.ReadCommitted;
329 case "readuncommitted":
330 return IsolationLevel.ReadUncommitted;
332 case "repeatableread":
333 return IsolationLevel.RepeatableRead;
336 return IsolationLevel.Serializable;
339 return IsolationLevel.Chaos;
342 return IsolationLevel.Unspecified;
345 return IsolationLevel.ReadCommitted;