2010-07-25 Carlos Alberto Cortez <calberto.cortez@gmail.com>
[mono.git] / mcs / class / IBM.Data.DB2 / IBM.Data.DB2 / DB2ConnectionSettings.cs
1
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining
4 // a copy of this software and associated documentation files (the
5 // "Software"), to deal in the Software without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Software, and to
8 // permit persons to whom the Software is furnished to do so, subject to
9 // the following conditions:
10 // 
11 // The above copyright notice and this permission notice shall be
12 // included in all copies or substantial portions of the Software.
13 // 
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 //
22 using System;\r
23 using System.Runtime.InteropServices;\r
24 using System.Text;\r
25 \r
26 namespace IBM.Data.DB2\r
27 {\r
28         internal sealed class DB2ConnectionSettings\r
29         {\r
30                 private string   connectionString;\r
31                 private string   userName = "";\r
32                 private string   passWord = "";\r
33                 private string   databaseAlias = "";\r
34                 private string   server = "";\r
35                 private bool     pooling = true;\r
36                 private TimeSpan connectTimeout = new TimeSpan(0, 0, 15);\r
37                 private TimeSpan connectionLifeTime = new TimeSpan(0, 0, 15);   // 15 seconds\r
38                 private int              connectionPoolSizeMin = 0;\r
39                 private int              connectionPoolSizeMax = -1;    // no maximum\r
40 \r
41                 private DB2ConnectionPool pool;\r
42 \r
43                 private DB2ConnectionSettings(string connectionString)\r
44                 {\r
45                         this.connectionString = connectionString;\r
46                         this.Parse();\r
47                 }\r
48 \r
49                 public static DB2ConnectionSettings GetConnectionSettings(string connectionString)\r
50                 {\r
51                         DB2ConnectionPool pool = DB2ConnectionPool.FindConnectionPool(connectionString);\r
52                         if(pool != null)\r
53                         {\r
54                                 return pool.ConnectionSettings;\r
55                         }\r
56                         DB2ConnectionSettings settings = new DB2ConnectionSettings(connectionString);\r
57                         if(settings.Pooling)\r
58                         {\r
59                                 settings.pool = DB2ConnectionPool.GetConnectionPool(settings);\r
60                         }\r
61                         return settings;\r
62                 }\r
63 \r
64                 public DB2OpenConnection GetRealOpenConnection(DB2Connection connection)\r
65                 {\r
66                         if(pool != null)\r
67                         {\r
68                                 return pool.GetOpenConnection(connection);\r
69                         }\r
70                         else\r
71                         {\r
72                                 return new DB2OpenConnection(this, connection);\r
73                         }\r
74                 }\r
75 \r
76                 public DB2ConnectionPool Pool\r
77                 {\r
78                         get { return pool; }\r
79                 }\r
80 \r
81                 public string ConnectionString\r
82                 {\r
83                         get { return connectionString; }\r
84                 }\r
85 \r
86                 public string UserName\r
87                 {\r
88                         get { return userName; }\r
89                 }\r
90                 \r
91                 public string PassWord\r
92                 {\r
93                         get { return passWord; }\r
94                 }\r
95 \r
96                 /// <summary>\r
97                 /// database alias (for cataloged database)\r
98                 /// </summary>\r
99                 public string DatabaseAlias\r
100                 {\r
101                         get { return databaseAlias; }\r
102                 }\r
103 \r
104                 /// <summary>\r
105                 /// server name with optional port number for direct connection (<server name/ip address>[:<port>])\r
106                 /// </summary>\r
107                 public string Server\r
108                 {\r
109                         get { return server; }\r
110                 }\r
111 \r
112                 public TimeSpan ConnectTimeout\r
113                 {\r
114                         get { return connectTimeout; }\r
115                 }\r
116 \r
117                 /// <summary>\r
118                 /// Connection pooling yes/no\r
119                 /// </summary>\r
120                 public bool Pooling\r
121                 {\r
122                         get { return pooling; }\r
123                 }\r
124 \r
125                 public int ConnectionPoolSizeMin\r
126                 {\r
127                         get { return connectionPoolSizeMin; }\r
128                 }\r
129 \r
130                 public int ConnectionPoolSizeMax\r
131                 {\r
132                         get { return connectionPoolSizeMin; }\r
133                 }\r
134 \r
135                 public TimeSpan ConnectionLifeTime\r
136                 {\r
137                         get { return connectionLifeTime; }\r
138                 }\r
139 \r
140 \r
141                 /// <summary>\r
142                 /// parsed according to IBM DB2 .NET data provider help\r
143                 /// </summary>\r
144                 public void Parse()\r
145                 {\r
146                         string[] parts = connectionString.Split(new char[]{';'});\r
147                         foreach(string part in parts)\r
148                         {\r
149                                 string[] pairs = part.Split(new char[]{'='});\r
150                                 switch(pairs[0].ToLower())\r
151                                 {\r
152                                         case "database":\r
153                                         case "dsn":\r
154                                                 databaseAlias = pairs[1];\r
155                                                 break;\r
156                                         case "uid":\r
157                                         case "user id":\r
158                                                 userName = pairs[1];\r
159                                                 break;\r
160                                         case "pwd":\r
161                                         case "password":\r
162                                                 passWord = pairs[1];\r
163                                                 break;\r
164                                         case "server":\r
165                                                 server = pairs[1];\r
166                                                 break;\r
167                                         case "pooling":\r
168                                                 pooling = (pairs[1].ToLower() == "true") || (pairs[1]== "1");\r
169                                                 break;\r
170                                         case "connect timeout":\r
171                                         case "timeout":\r
172                                         case "connection timeout":\r
173                                                 connectTimeout = new TimeSpan(0, 0, int.Parse(pairs[1]));\r
174                                                 break;\r
175                                         case "min pool size":\r
176                                                 connectionPoolSizeMin = int.Parse(pairs[1]);\r
177                                                 break;\r
178                                         case "max pool size":\r
179                                                 connectionPoolSizeMax = int.Parse(pairs[1]);\r
180                                                 break;\r
181                                         case "connection lifetime":\r
182                                                 connectionLifeTime = new TimeSpan(0, 0, int.Parse(pairs[1]));\r
183                                                 break;\r
184                                 }\r
185                         }\r
186                         if(connectionLifeTime.Ticks <= 0)\r
187                         {\r
188                                 pooling = false;\r
189                         }\r
190                 }\r
191         \r
192                 public override int GetHashCode()\r
193                 {\r
194                         return connectionString.GetHashCode ();\r
195                 }\r
196         \r
197                 public override bool Equals(object obj)\r
198                 {\r
199                         return connectionString.Equals (obj);\r
200                 }\r
201         }\r
202 }\r