2006-02-29 Carlos Alberto Cortez <calberto.cortez@gmail.com>
[mono.git] / mcs / class / ByteFX.Data / Common / DBConnectionString.cs
1 // ByteFX.Data data access components for .Net\r
2 // Copyright (C) 2002-2003  ByteFX, Inc.\r
3 //\r
4 // This library is free software; you can redistribute it and/or\r
5 // modify it under the terms of the GNU Lesser General Public\r
6 // License as published by the Free Software Foundation; either\r
7 // version 2.1 of the License, or (at your option) any later version.\r
8 // \r
9 // This library is distributed in the hope that it will be useful,\r
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of\r
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\r
12 // Lesser General Public License for more details.\r
13 // \r
14 // You should have received a copy of the GNU Lesser General Public\r
15 // License along with this library; if not, write to the Free Software\r
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
17 \r
18 using System;\r
19 using System.Collections;\r
20 using System.Text;\r
21 \r
22 namespace ByteFX.Data.Common\r
23 {\r
24         /// <summary>\r
25         /// Summary description for Utility.\r
26         /// </summary>\r
27         internal abstract class DBConnectionString\r
28         {\r
29                 protected Hashtable     keyValues = new Hashtable();\r
30                 protected string        connectionName = String.Empty;\r
31                 protected string        connectString;\r
32 \r
33                 public DBConnectionString()\r
34                 {       \r
35                         keyValues = GetDefaultValues();\r
36                 }\r
37 \r
38                 public void SetConnectionString(string value)\r
39                 {\r
40                         Hashtable ht = Parse( value );                  \r
41                         connectString = value;\r
42                         keyValues = ht;\r
43                 }\r
44 \r
45                 protected string GetString(string name) \r
46                 {\r
47                         if (! keyValues.ContainsKey(name)) return String.Empty;\r
48                         return (keyValues[name] as string);\r
49                 }\r
50 \r
51                 protected int GetInt( string name ) \r
52                 {\r
53                         return Convert.ToInt32(keyValues[name]);\r
54                 }\r
55 \r
56                 protected bool GetBool( string name ) \r
57                 {\r
58                         return Convert.ToBoolean(keyValues[name]);\r
59                 }\r
60 \r
61                 protected virtual bool ConnectionParameterParsed(Hashtable hash, string key, string value)\r
62                 {\r
63                         switch (key.ToLower()) \r
64                         {\r
65                                 case "persist security info":\r
66                                         hash["persist security info"] = \r
67                                                 value.ToLower() == "yes" || value.ToLower() == "true";\r
68                                         return true;\r
69 \r
70                                 case "uid":\r
71                                 case "username":\r
72                                 case "user id":\r
73                                 case "user name": \r
74                                 case "userid":\r
75                                         hash["user id"] = value;\r
76                                         return true;\r
77 \r
78                                 case "password": \r
79                                 case "pwd":\r
80                                         hash["password"] = value;\r
81                                         return true;\r
82 \r
83                                 case "host":\r
84                                 case "server":\r
85                                 case "data source":\r
86                                 case "datasource":\r
87                                 case "address":\r
88                                 case "addr":\r
89                                 case "network address":\r
90                                         hash["host"] = value;\r
91                                         return true;\r
92                                 \r
93                                 case "initial catalog":\r
94                                 case "database":\r
95                                         hash["database"] = value;\r
96                                         return true;\r
97 \r
98                                 case "connection timeout":\r
99                                 case "connect timeout":\r
100                                         hash["connect timeout"] = Int32.Parse( value );\r
101                                         return true;\r
102 \r
103                                 case "port":\r
104                                         hash["port"] = Int32.Parse( value );\r
105                                         return true;\r
106 \r
107                                 case "pooling":\r
108                                         hash["pooling"] = \r
109                                                 value.ToLower() == "yes" || value.ToLower() == "true";\r
110                                         return true;\r
111 \r
112                                 case "min pool size":\r
113                                         hash["min pool size"] = Int32.Parse(value);\r
114                                         return true;\r
115 \r
116                                 case "max pool size":\r
117                                         hash["max pool size"] = Int32.Parse(value);\r
118                                         return true;\r
119 \r
120                                 case "connection lifetime":\r
121                                         hash["connect lifetime"] = Int32.Parse(value);\r
122                                         return true;\r
123                         }\r
124                         return false;\r
125                 }\r
126 \r
127                 protected virtual Hashtable GetDefaultValues()\r
128                 {\r
129                         return null;\r
130                 }\r
131 \r
132                 protected Hashtable ParseKeyValuePairs( string src )\r
133                 {\r
134                         String[] keyvalues = src.Split( ';' );\r
135                         String[] newkeyvalues = new String[keyvalues.Length];\r
136                         int              x = 0;\r
137 \r
138                         // first run through the array and check for any keys that\r
139                         // have ; in their value\r
140                         foreach (String keyvalue in keyvalues) \r
141                         {\r
142                                 // check for trailing ; at the end of the connection string\r
143                                 if (keyvalue.Length == 0) continue;\r
144 \r
145                                 // this value has an '=' sign so we are ok\r
146                                 if (keyvalue.IndexOf('=') >= 0) \r
147                                 {\r
148                                         newkeyvalues[x++] = keyvalue;\r
149                                 }\r
150                                 else \r
151                                 {\r
152                                         newkeyvalues[x-1] += ";";\r
153                                         newkeyvalues[x-1] += keyvalue;\r
154                                 }\r
155                         }\r
156 \r
157                         Hashtable hash = new Hashtable();\r
158 \r
159                         // now we run through our normalized key-values, splitting on equals\r
160                         for (int y=0; y < x; y++) \r
161                         {\r
162                                 String[] parts = newkeyvalues[y].Split( '=' );\r
163 \r
164                                 // first trim off any space and lowercase the key\r
165                                 parts[0] = parts[0].Trim().ToLower();\r
166                                 parts[1] = parts[1].Trim();\r
167 \r
168                                 // we also want to clear off any quotes\r
169                                 parts[0] = parts[0].Trim('\'', '"');\r
170                                 parts[1] = parts[1].Trim('\'', '"');\r
171 \r
172                                 hash.Add( parts[0], parts[1] );\r
173                         }\r
174                         return hash;\r
175                 }\r
176 \r
177                 protected virtual Hashtable Parse(string newConnectString) \r
178                 {\r
179                         Hashtable hash = ParseKeyValuePairs( newConnectString );\r
180                         Hashtable newHash = GetDefaultValues();\r
181 \r
182                         foreach (object key in hash.Keys)\r
183                                 ConnectionParameterParsed( newHash, (string)key, (string)hash[key] );\r
184                         return newHash;\r
185                 }\r
186 \r
187 \r
188         }\r
189 }\r