2003-01-15 Rodrigo Moya <rodrigo@ximian.com>
[mono.git] / mcs / class / Mono.Data.DB2Client / Mono.Data.Db2Client / DB2ClientConnection.cs
1 #region Licence\r
2         /// DB2DriverCS - A DB2 driver for .Net\r
3         /// Copyright 2003 By Christopher Bockner\r
4         /// Released under the terms of the MIT/X11 Licence\r
5         /// Please refer to the Licence.txt file that should be distributed with this package\r
6         /// This software requires that DB2 client software be installed correctly on the machine\r
7         /// (or instance) on which the driver is running.  \r
8 #endregion\r
9 using System;\r
10 using System.Data;\r
11 using System.Runtime.InteropServices;\r
12 \r
13 namespace DB2ClientCS\r
14 {\r
15         /// <summary>   /// This class is IDbConnection compliant.  Refer to MSDN documentation for reference.\r
16         /// </summary>\r
17         \r
18         public class DB2ClientConnection : IDbConnection\r
19         {\r
20                 private string connectionString = null;\r
21                 private string dbName = null;\r
22                 private int connectionTimeout;\r
23 \r
24                 unsafe internal long dbHandle;\r
25                 private bool disposed = false;\r
26                 public DB2ClientConnection()\r
27                 {\r
28                         //\r
29                         // TODO: Add constructor logic here\r
30                         //\r
31                 }\r
32                 public DB2ClientConnection(string conString)\r
33                 {\r
34                         this.connectionString = conString;\r
35                 }\r
36                 #region ConnectionString property\r
37                 /// \r
38                 ///Accessor for the connectionString property\r
39                 public string ConnectionString \r
40                 {\r
41                         get\r
42                         {\r
43                                 return connectionString;\r
44                         }\r
45                         set\r
46                         {\r
47                                 connectionString = value;\r
48                         }\r
49                 }\r
50                 #endregion\r
51                 #region ConnectionTimeout property\r
52                 public int ConnectionTimeout\r
53                 {\r
54                         get\r
55                         {\r
56                                 return connectionTimeout;\r
57                         }\r
58                         set\r
59                         {\r
60                                 connectionTimeout = value;\r
61                         }\r
62                 }\r
63                 #endregion\r
64                 #region Database property\r
65                 public string Database\r
66                 {\r
67                         get\r
68                         {\r
69                                 return dbName;\r
70                         }\r
71                         set\r
72                         {\r
73                                 dbName = value;\r
74                         }\r
75                 }\r
76                 #endregion\r
77                 #region State property\r
78                 /// <summary>\r
79                 /// The Connection State property, open or closed. \r
80                 /// NOTE:  IBM's docs on SqlFreeHandle do not state what is done when a handle is freed\r
81                 ///     i.e. if the handle is set to SQL_NULL_HANDLE.\r
82                 /// </summary>\r
83 \r
84                 unsafe public ConnectionState State\r
85                 {\r
86                         get\r
87                         {   \r
88                                 if (dbHandle == DB2ClientConstants.SQL_NULL_HANDLE)\r
89                                         return ConnectionState.Closed;\r
90                                 else\r
91                                         return ConnectionState.Open;\r
92                         }\r
93                 }\r
94                 #endregion\r
95                 #region Handle\r
96                 ///\r
97                 /// Handle Returns an IntPtr of the dbm handle\r
98                 /// \r
99                 public IntPtr Handle\r
100                 {\r
101                         get\r
102                         {\r
103                                 return new IntPtr(dbHandle);\r
104                         }\r
105                 }\r
106                 #endregion\r
107 \r
108                 #region BeginTransaction Method\r
109                 /// <summary>\r
110                 /// Opens a transaction against the database at the default isolation level, which will be \r
111                 /// that which the packages were bound at, unless overriden in the connection string, and if nothing was specified at that point\r
112                 /// then I believe the default level is Cursor Stability (don't quote me on that, I haven't\r
113                 /// found the appropriate reference yet), ODBC equivalent is SQL_TXN_READ_COMMITTED\r
114                 /// </summary>\r
115                 /// <returns></returns>\r
116                 public IDbTransaction BeginTransaction()\r
117                 {\r
118                         return null;\r
119                 }\r
120                 #endregion\r
121                 #region BeginTransaction (IsolationLevel) Method\r
122                 /// <summary>\r
123                 /// BeginTransaction again overloadded to let us set the transaction level for the statement\r
124                 /// </summary>\r
125                 /// <param name="isolationL"></param>\r
126                 /// <returns></returns>\r
127                 public IDbTransaction BeginTransaction(IsolationLevel isolationL)\r
128                 {\r
129                         return null;\r
130                 }\r
131                 #endregion\r
132                 #region ChangeDatabase\r
133                 unsafe public void ChangeDatabase(string newDBName)\r
134                 {\r
135                 }\r
136                 #endregion\r
137                 #region Close\r
138                 ///Close, per MSDN documentation\r
139                 ///\r
140                 unsafe public void Close()\r
141                 {\r
142                         DB2ClientPrototypes.SQLDisconnect(dbHandle);\r
143                         dbHandle = DB2ClientConstants.SQL_NULL_HANDLE;\r
144                 }\r
145                 #endregion\r
146                 #region CreateCommand\r
147                 /// <summary>\r
148                 /// CreateCommand per MSDN\r
149                 /// </summary>\r
150                 /// <returns></returns>\r
151                 public IDbCommand CreateCommand()\r
152                 {\r
153                         CheckState();\r
154                         return new DB2ClientCommand();\r
155                 }\r
156                 #endregion\r
157                 #region Open\r
158                 /// <summary>\r
159                 /// Open, per MSDN\r
160                 /// </summary>\r
161                 unsafe public void Open()\r
162                 {\r
163                         IntPtr pdbHandle = Marshal.AllocHGlobal(4);\r
164                         IntPtr penvHandle = Marshal.AllocHGlobal(4);\r
165                         short sqlReturn;\r
166                         try\r
167                         {\r
168                         IntPtr tempInt = IntPtr.Zero;\r
169                         sqlReturn = DB2ClientPrototypes.SQLAllocHandle(DB2ClientConstants.SQL_HANDLE_ENV, tempInt, ref penvHandle);\r
170                         sqlReturn = DB2ClientPrototypes.SQLAllocHandle(DB2ClientConstants.SQL_HANDLE_DBC, penvHandle, ref pdbHandle);\r
171 \r
172                         if (sqlReturn == DB2ClientConstants.SQL_ERROR) \r
173                         {\r
174                                 throw new DB2ClientException(DB2ClientConstants.SQL_HANDLE_ENV, penvHandle, "Alloc Env Handle: ");\r
175                         }\r
176                         sqlReturn = DB2ClientPrototypes.SQLConnect(pdbHandle, "sample", 6, "Administrator", 13, "tirpitz", 7);\r
177                         if (sqlReturn == DB2ClientConstants.SQL_ERROR) \r
178                         {\r
179                                 throw new DB2ClientException(DB2ClientConstants.SQL_HANDLE_DBC, pdbHandle, "Error connecting to DB: ");\r
180                         }\r
181 \r
182                         if(IntPtr.Size == 4)\r
183                           dbHandle = pdbHandle.ToInt32();\r
184                         else\r
185                           dbHandle = pdbHandle.ToInt64();\r
186                 }\r
187                 catch (DB2ClientException DB2E)\r
188                 {\r
189                         Console.WriteLine(DB2E.Message);\r
190                         Marshal.FreeHGlobal(pdbHandle);\r
191                         Marshal.FreeHGlobal(penvHandle);\r
192                         return;\r
193                 }\r
194                 Marshal.FreeHGlobal(pdbHandle);\r
195                 Marshal.FreeHGlobal(penvHandle);\r
196         }\r
197                 #endregion\r
198                 #region Dispose\r
199                 /// <summary>\r
200                 /// Dispose\r
201                 /// </summary>\r
202                 unsafe public void Dispose()\r
203                 {\r
204                         if(!disposed) \r
205                         {\r
206                                 this.Close();\r
207                                 disposed = true;\r
208                         } else \r
209                                 return;\r
210                 }\r
211 \r
212                 #endregion\r
213 \r
214                 /// <summary>\r
215                 /// Called after the connection string is set, this parses the it and saves the connection attributes. \r
216                 /// </summary>\r
217                 protected void ParseConnectionString()\r
218                 {\r
219                 \r
220                 }\r
221                 private void CheckState()\r
222                 {\r
223                         if (ConnectionState.Closed == State)\r
224                                 throw new DB2ClientException ("Connection is currently closed.");\r
225                 }\r
226 \r
227 \r
228         }\r
229 }