synced with ByteFX (0.74) 11-30-2003
[mono-project.git] / mcs / class / ByteFX.Data / Common / DBConnectionString.cs
blob2fe01655ad4b95bd068335326089e3d0678970dd
1 // ByteFX.Data data access components for .Net
2 // Copyright (C) 2002-2003 ByteFX, Inc.
3 //
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License, or (at your option) any later version.
8 //
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 using System;
19 using System.Collections;
20 using System.Text;
22 namespace ByteFX.Data.Common
24 /// <summary>
25 /// Summary description for Utility.
26 /// </summary>
27 internal class DBConnectionString
29 protected Hashtable keyValues = new Hashtable();
30 protected string connectString;
31 protected string host;
32 protected string username;
33 protected string password;
34 protected string database;
35 protected int connectTimeout;
36 protected int port;
37 protected int maxPoolSize;
38 protected int minPoolSize;
39 protected int connectLifetime;
40 protected bool pooling;
41 protected bool persistSecurityInfo;
43 public DBConnectionString()
45 persistSecurityInfo = false;
48 public DBConnectionString(string connectString) : this()
50 this.connectString = connectString;
53 #region Properties
54 public string Host
56 get { return host; }
59 public string Username
61 get { return username; }
64 public string Password
66 get { return password; }
69 public int ConnectTimeout
71 get { return connectTimeout; }
74 public string Database
76 get { return database; }
79 public int Port
81 get { return port; }
84 public int MaxPoolSize
86 get { return maxPoolSize; }
89 public int MinPoolSize
91 get { return minPoolSize; }
94 public bool Pooling
96 get { return pooling; }
99 public int ConnectionLifetime
101 get { return connectLifetime; }
104 public string ConnectString
106 get { return GetConnectionString(); }
107 set { connectString = value; Parse(); }
110 #endregion
112 private string GetConnectionString()
114 StringBuilder str = new StringBuilder();
116 foreach ( string key in keyValues.Keys)
118 if ((key.ToLower() == "pwd" || key.ToLower() == "password") &&
119 !persistSecurityInfo) continue;
120 str.AppendFormat("{0}={1};", key, keyValues[key]);
123 if (str.Length > 0)
124 str.Remove( str.Length-1, 1 );
126 return str.ToString();
129 protected virtual void ConnectionParameterParsed(string key, string value)
131 switch (key.ToLower())
133 case "persist security info":
134 if (value.ToLower() == "no" || value.ToLower() == "false")
135 persistSecurityInfo = false;
136 else
137 persistSecurityInfo = true;
138 break;
140 case "uid":
141 case "username":
142 case "user id":
143 case "user name":
144 case "userid":
145 username = value;
146 break;
148 case "password":
149 case "pwd":
150 password = value;
151 break;
153 case "host":
154 case "server":
155 case "data source":
156 case "datasource":
157 case "address":
158 case "addr":
159 case "network address":
160 host = value;
161 break;
163 case "initial catalog":
164 case "database":
165 database = value;
166 break;
168 case "connection timeout":
169 case "connect timeout":
170 connectTimeout = Int32.Parse( value );
171 break;
173 case "port":
174 port = Int32.Parse( value );
175 break;
177 case "pooling":
178 if (value.ToLower() == "no" || value.ToLower() == "false")
179 pooling = false;
180 else
181 pooling = true;
182 break;
184 case "min pool size":
185 minPoolSize = Int32.Parse(value);
186 break;
188 case "max pool size":
189 maxPoolSize = Int32.Parse(value);
190 break;
192 case "connection lifetime":
193 connectLifetime = Int32.Parse(value);
194 break;
198 protected void Parse()
200 String[] keyvalues = connectString.Split( ';' );
201 String[] newkeyvalues = new String[keyvalues.Length];
202 int x = 0;
204 // first run through the array and check for any keys that
205 // have ; in their value
206 foreach (String keyvalue in keyvalues)
208 // check for trailing ; at the end of the connection string
209 if (keyvalue.Length == 0) continue;
211 // this value has an '=' sign so we are ok
212 if (keyvalue.IndexOf('=') >= 0)
214 newkeyvalues[x++] = keyvalue;
216 else
218 newkeyvalues[x-1] += ";";
219 newkeyvalues[x-1] += keyvalue;
223 keyValues.Clear();
225 // now we run through our normalized key-values, splitting on equals
226 for (int y=0; y < x; y++)
228 String[] parts = newkeyvalues[y].Split( '=' );
230 // first trim off any space and lowercase the key
231 parts[0] = parts[0].Trim().ToLower();
232 parts[1] = parts[1].Trim();
234 // we also want to clear off any quotes
235 parts[0] = parts[0].Trim('\'', '"');
236 parts[1] = parts[1].Trim('\'', '"');
238 ConnectionParameterParsed( parts[0], parts[1] );
239 keyValues.Add( parts[0], parts[1] );