(DISTFILES): Comment out a few missing files.
[mono-project.git] / mcs / class / ByteFX.Data / Common / DBConnectionString.cs
blob06b41be27fe5456281921a858b77cbf0c23bd200
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 abstract class DBConnectionString
29 protected Hashtable keyValues = new Hashtable();
30 protected string connectionName = String.Empty;
31 protected string connectString;
33 public DBConnectionString()
35 keyValues = GetDefaultValues();
38 public void SetConnectionString(string value)
40 Hashtable ht = Parse( value );
41 connectString = value;
42 keyValues = ht;
45 protected string GetString(string name)
47 if (! keyValues.ContainsKey(name)) return String.Empty;
48 return (keyValues[name] as string);
51 protected int GetInt( string name )
53 return Convert.ToInt32(keyValues[name]);
56 protected bool GetBool( string name )
58 return Convert.ToBoolean(keyValues[name]);
61 protected virtual bool ConnectionParameterParsed(Hashtable hash, string key, string value)
63 switch (key.ToLower())
65 case "persist security info":
66 hash["persist security info"] =
67 value.ToLower() == "yes" || value.ToLower() == "true";
68 return true;
70 case "uid":
71 case "username":
72 case "user id":
73 case "user name":
74 case "userid":
75 hash["user id"] = value;
76 return true;
78 case "password":
79 case "pwd":
80 hash["password"] = value;
81 return true;
83 case "host":
84 case "server":
85 case "data source":
86 case "datasource":
87 case "address":
88 case "addr":
89 case "network address":
90 hash["host"] = value;
91 return true;
93 case "initial catalog":
94 case "database":
95 hash["database"] = value;
96 return true;
98 case "connection timeout":
99 case "connect timeout":
100 hash["connect timeout"] = Int32.Parse( value );
101 return true;
103 case "port":
104 hash["port"] = Int32.Parse( value );
105 return true;
107 case "pooling":
108 hash["pooling"] =
109 value.ToLower() == "yes" || value.ToLower() == "true";
110 return true;
112 case "min pool size":
113 hash["min pool size"] = Int32.Parse(value);
114 return true;
116 case "max pool size":
117 hash["max pool size"] = Int32.Parse(value);
118 return true;
120 case "connection lifetime":
121 hash["connect lifetime"] = Int32.Parse(value);
122 return true;
124 return false;
127 protected virtual Hashtable GetDefaultValues()
129 return null;
132 protected Hashtable ParseKeyValuePairs( string src )
134 String[] keyvalues = src.Split( ';' );
135 String[] newkeyvalues = new String[keyvalues.Length];
136 int x = 0;
138 // first run through the array and check for any keys that
139 // have ; in their value
140 foreach (String keyvalue in keyvalues)
142 // check for trailing ; at the end of the connection string
143 if (keyvalue.Length == 0) continue;
145 // this value has an '=' sign so we are ok
146 if (keyvalue.IndexOf('=') >= 0)
148 newkeyvalues[x++] = keyvalue;
150 else
152 newkeyvalues[x-1] += ";";
153 newkeyvalues[x-1] += keyvalue;
157 Hashtable hash = new Hashtable();
159 // now we run through our normalized key-values, splitting on equals
160 for (int y=0; y < x; y++)
162 String[] parts = newkeyvalues[y].Split( '=' );
164 // first trim off any space and lowercase the key
165 parts[0] = parts[0].Trim().ToLower();
166 parts[1] = parts[1].Trim();
168 // we also want to clear off any quotes
169 parts[0] = parts[0].Trim('\'', '"');
170 parts[1] = parts[1].Trim('\'', '"');
172 hash.Add( parts[0], parts[1] );
174 return hash;
177 protected virtual Hashtable Parse(string newConnectString)
179 Hashtable hash = ParseKeyValuePairs( newConnectString );
180 Hashtable newHash = GetDefaultValues();
182 foreach (object key in hash.Keys)
183 ConnectionParameterParsed( newHash, (string)key, (string)hash[key] );
184 return newHash;