1 | /********************************************************************* |
2 | * |
3 | * This is based on code created by Peter Harvey, |
4 | * (pharvey@codebydesign.com). |
5 | * |
6 | * Modified and extended by Nick Gorham |
7 | * (nick@lurcher.org). |
8 | * |
9 | * Any bugs or problems should be considered the fault of Nick and not |
10 | * Peter. |
11 | * |
12 | * copyright (c) 1999 Nick Gorham |
13 | * |
14 | * This library is free software; you can redistribute it and/or |
15 | * modify it under the terms of the GNU Lesser General Public |
16 | * License as published by the Free Software Foundation; either |
17 | * version 2 of the License, or (at your option) any later version. |
18 | * |
19 | * This library is distributed in the hope that it will be useful, |
20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
22 | * Lesser General Public License for more details. |
23 | * |
24 | * You should have received a copy of the GNU Lesser General Public |
25 | * License along with this library; if not, write to the Free Software |
26 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
27 | * |
28 | ********************************************************************** |
29 | * |
30 | * $Id: SQLDisconnect.c,v 1.9 2009/02/18 17:59:08 lurcher Exp $ |
31 | * |
32 | * $Log: SQLDisconnect.c,v $ |
33 | * Revision 1.9 2009/02/18 17:59:08 lurcher |
34 | * Shift to using config.h, the compile lines were making it hard to spot warnings |
35 | * |
36 | * Revision 1.8 2004/02/18 15:47:44 lurcher |
37 | * |
38 | * Fix a leak in the iconv code |
39 | * |
40 | * Revision 1.7 2003/10/30 18:20:45 lurcher |
41 | * |
42 | * Fix broken thread protection |
43 | * Remove SQLNumResultCols after execute, lease S4/S% to driver |
44 | * Fix string overrun in SQLDriverConnect |
45 | * Add initial support for Interix |
46 | * |
47 | * Revision 1.6 2002/12/05 17:44:30 lurcher |
48 | * |
49 | * Display unknown return values in return logging |
50 | * |
51 | * Revision 1.5 2002/08/12 13:17:52 lurcher |
52 | * |
53 | * Replicate the way the MS DM handles loading of driver libs, and allocating |
54 | * handles in the driver. usage counting in the driver means that dlopen is |
55 | * only called for the first use, and dlclose for the last. AllocHandle for |
56 | * the driver environment is only called for the first time per driver |
57 | * per application environment. |
58 | * |
59 | * Revision 1.4 2002/07/24 08:49:51 lurcher |
60 | * |
61 | * Alter UNICODE support to use iconv for UNICODE-ANSI conversion |
62 | * |
63 | * Revision 1.3 2002/07/04 17:27:56 lurcher |
64 | * |
65 | * Small bug fixes |
66 | * |
67 | * Revision 1.1.1.1 2001/10/17 16:40:05 lurcher |
68 | * |
69 | * First upload to SourceForge |
70 | * |
71 | * Revision 1.8 2001/04/12 17:43:36 nick |
72 | * |
73 | * Change logging and added autotest to odbctest |
74 | * |
75 | * Revision 1.7 2001/03/21 16:12:29 nick |
76 | * |
77 | * Alter cleaning of stmt and desc handles if a SQLDisconnect fails |
78 | * |
79 | * Revision 1.6 2001/03/02 14:24:23 nick |
80 | * |
81 | * Fix thread detection for Solaris |
82 | * |
83 | * Revision 1.5 2001/02/12 11:20:22 nick |
84 | * |
85 | * Add supoort for calling SQLDriverLoad and SQLDriverUnload |
86 | * |
87 | * Revision 1.4 2000/12/18 12:53:29 nick |
88 | * |
89 | * More pooling tweeks |
90 | * |
91 | * Revision 1.3 2000/12/18 12:32:16 nick |
92 | * |
93 | * Fix missing return codes |
94 | * |
95 | * Revision 1.2 2000/12/14 18:10:19 nick |
96 | * |
97 | * Add connection pooling |
98 | * |
99 | * Revision 1.1.1.1 2000/09/04 16:42:52 nick |
100 | * Imported Sources |
101 | * |
102 | * Revision 1.13 2000/04/27 20:49:03 ngorham |
103 | * |
104 | * Fixes to work with Star Office 5.2 |
105 | * |
106 | * Revision 1.12 2001/04/27 01:33:40 ngorham |
107 | * |
108 | * Fix a problem where handles were not being free'd |
109 | * |
110 | * Revision 1.11 2001/04/05 21:15:01 ngorham |
111 | * |
112 | * Fix small memory leak in SQLDisconnect and the Postgres driver |
113 | * |
114 | * Revision 1.10 2000/02/25 00:02:00 ngorham |
115 | * |
116 | * Add a patch to support IBM DB2, and Solaris threads |
117 | * |
118 | * Revision 1.9 2000/02/22 22:14:45 ngorham |
119 | * |
120 | * Added support for solaris threads |
121 | * Added check to overcome bug in PHP4 |
122 | * Fixed bug in descriptors and ODBC 3 drivers |
123 | * |
124 | * Revision 1.8 1999/12/28 15:05:01 ngorham |
125 | * |
126 | * Fix bug that caused StarOffice to fail. A SQLConnect, SQLDisconnect, |
127 | * followed by another SQLConnect on the same DBC would fail. |
128 | * |
129 | * Revision 1.7 1999/11/13 23:40:59 ngorham |
130 | * |
131 | * Alter the way DM logging works |
132 | * Upgrade the Postgres driver to 6.4.6 |
133 | * |
134 | * Revision 1.6 1999/10/24 23:54:17 ngorham |
135 | * |
136 | * First part of the changes to the error reporting |
137 | * |
138 | * Revision 1.5 1999/09/21 22:34:24 ngorham |
139 | * |
140 | * Improve performance by removing unneeded logging calls when logging is |
141 | * disabled |
142 | * |
143 | * Revision 1.4 1999/07/10 21:10:16 ngorham |
144 | * |
145 | * Adjust error sqlstate from driver manager, depending on requested |
146 | * version (ODBC2/3) |
147 | * |
148 | * Revision 1.3 1999/07/04 21:05:07 ngorham |
149 | * |
150 | * Add LGPL Headers to code |
151 | * |
152 | * Revision 1.2 1999/06/30 23:56:54 ngorham |
153 | * |
154 | * Add initial thread safety code |
155 | * |
156 | * Revision 1.1.1.1 1999/05/29 13:41:05 sShandyb |
157 | * first go at it |
158 | * |
159 | * Revision 1.1.1.1 1999/05/27 18:23:17 pharvey |
160 | * Imported sources |
161 | * |
162 | * Revision 1.2 1999/04/30 16:22:47 nick |
163 | * Another checkpoint |
164 | * |
165 | * Revision 1.1 1999/04/25 23:06:11 nick |
166 | * Initial revision |
167 | * |
168 | * |
169 | **********************************************************************/ |
170 | |
171 | #include <config.h> |
172 | #include "drivermanager.h" |
173 | |
174 | static char const rcsid[]= "$RCSfile: SQLDisconnect.c,v $ $Revision: 1.9 $" ; |
175 | |
176 | extern int pooling_enabled; |
177 | |
178 | SQLRETURN SQLDisconnect( SQLHDBC connection_handle ) |
179 | { |
180 | DMHDBC connection = (DMHDBC)connection_handle; |
181 | SQLRETURN ret; |
182 | SQLCHAR s1[ 100 + LOG_MESSAGE_LEN ]; |
183 | |
184 | /* |
185 | * check connection |
186 | */ |
187 | |
188 | if ( !__validate_dbc( connection )) |
189 | { |
190 | dm_log_write( __FILE__, |
191 | __LINE__, |
192 | LOG_INFO, |
193 | LOG_INFO, |
194 | "Error: SQL_INVALID_HANDLE" ); |
195 | |
196 | return SQL_INVALID_HANDLE; |
197 | } |
198 | |
199 | function_entry( connection ); |
200 | |
201 | if ( log_info.log_flag ) |
202 | { |
203 | sprintf( connection -> msg, "\n\t\tEntry:\ |
204 | \n\t\t\tConnection = %p" , |
205 | connection ); |
206 | |
207 | dm_log_write( __FILE__, |
208 | __LINE__, |
209 | LOG_INFO, |
210 | LOG_INFO, |
211 | connection -> msg ); |
212 | } |
213 | |
214 | thread_protect( SQL_HANDLE_DBC, connection ); |
215 | |
216 | /* |
217 | * check states |
218 | */ |
219 | |
220 | if ( connection -> state == STATE_C6 ) |
221 | { |
222 | dm_log_write( __FILE__, |
223 | __LINE__, |
224 | LOG_INFO, |
225 | LOG_INFO, |
226 | "Error: 25000" ); |
227 | |
228 | __post_internal_error( &connection -> error, |
229 | ERROR_25000, NULL, |
230 | connection -> environment -> requested_version ); |
231 | |
232 | return function_return_nodrv( SQL_HANDLE_DBC, connection, SQL_ERROR ); |
233 | } |
234 | else if ( connection -> state == STATE_C2 ) |
235 | { |
236 | dm_log_write( __FILE__, |
237 | __LINE__, |
238 | LOG_INFO, |
239 | LOG_INFO, |
240 | "Error: 08003" ); |
241 | |
242 | __post_internal_error( &connection -> error, |
243 | ERROR_08003, NULL, |
244 | connection -> environment -> requested_version ); |
245 | |
246 | return function_return_nodrv( SQL_HANDLE_DBC, connection, SQL_ERROR ); |
247 | } |
248 | |
249 | /* |
250 | * any statements that are in SQL_NEED_DATA |
251 | */ |
252 | |
253 | if( __check_stmt_from_dbc( connection, STATE_S8 )) { |
254 | |
255 | dm_log_write( __FILE__, |
256 | __LINE__, |
257 | LOG_INFO, |
258 | LOG_INFO, |
259 | "Error: HY010" ); |
260 | |
261 | __post_internal_error( &connection -> error, |
262 | ERROR_HY010, NULL, |
263 | connection -> environment -> requested_version ); |
264 | |
265 | return function_return_nodrv( SQL_HANDLE_DBC, connection, SQL_ERROR ); |
266 | } |
267 | |
268 | if( __check_stmt_from_dbc( connection, STATE_S13 )) { |
269 | |
270 | dm_log_write( __FILE__, |
271 | __LINE__, |
272 | LOG_INFO, |
273 | LOG_INFO, |
274 | "Error: HY010" ); |
275 | |
276 | __post_internal_error( &connection -> error, |
277 | ERROR_HY010, NULL, |
278 | connection -> environment -> requested_version ); |
279 | |
280 | return function_return_nodrv( SQL_HANDLE_DBC, connection, SQL_ERROR ); |
281 | } |
282 | |
283 | /* |
284 | * is it a pooled connection, or can it go back |
285 | */ |
286 | |
287 | if ( connection -> pooled_connection ) |
288 | { |
289 | __clean_stmt_from_dbc( connection ); |
290 | __clean_desc_from_dbc( connection ); |
291 | |
292 | return_to_pool( connection ); |
293 | |
294 | if ( log_info.log_flag ) |
295 | { |
296 | sprintf( connection -> msg, |
297 | "\n\t\tExit:[%s]" , |
298 | __get_return_status( SQL_SUCCESS, s1 )); |
299 | |
300 | dm_log_write( __FILE__, |
301 | __LINE__, |
302 | LOG_INFO, |
303 | LOG_INFO, |
304 | connection -> msg ); |
305 | } |
306 | |
307 | return function_return_nodrv( SQL_HANDLE_DBC, connection, SQL_SUCCESS ); |
308 | } |
309 | else if ( pooling_enabled && connection -> pooling_timeout > 0 ) |
310 | { |
311 | __clean_stmt_from_dbc( connection ); |
312 | __clean_desc_from_dbc( connection ); |
313 | |
314 | return_to_pool( connection ); |
315 | |
316 | if ( log_info.log_flag ) |
317 | { |
318 | sprintf( connection -> msg, |
319 | "\n\t\tExit:[%s]" , |
320 | __get_return_status( SQL_SUCCESS, s1 )); |
321 | |
322 | dm_log_write( __FILE__, |
323 | __LINE__, |
324 | LOG_INFO, |
325 | LOG_INFO, |
326 | connection -> msg ); |
327 | } |
328 | |
329 | return function_return_nodrv( SQL_HANDLE_DBC, connection, SQL_SUCCESS ); |
330 | } |
331 | |
332 | /* |
333 | * disconnect from the driver |
334 | */ |
335 | |
336 | if ( !CHECK_SQLDISCONNECT( connection )) |
337 | { |
338 | dm_log_write( __FILE__, |
339 | __LINE__, |
340 | LOG_INFO, |
341 | LOG_INFO, |
342 | "Error: IM001" ); |
343 | |
344 | __post_internal_error( &connection -> error, |
345 | ERROR_IM001, NULL, |
346 | connection -> environment -> requested_version ); |
347 | |
348 | return function_return_nodrv( SQL_HANDLE_DBC, connection, SQL_ERROR ); |
349 | } |
350 | |
351 | ret = SQLDISCONNECT( connection, |
352 | connection -> driver_dbc ); |
353 | |
354 | if ( SQL_SUCCEEDED( ret )) |
355 | { |
356 | /* |
357 | * grab any errors |
358 | */ |
359 | |
360 | if ( ret == SQL_SUCCESS_WITH_INFO ) |
361 | { |
362 | function_return_ex( IGNORE_THREAD, connection, ret, TRUE ); |
363 | } |
364 | |
365 | /* |
366 | * complete disconnection from driver |
367 | */ |
368 | |
369 | __disconnect_part_three( connection ); |
370 | } |
371 | |
372 | if ( log_info.log_flag ) |
373 | { |
374 | sprintf( connection -> msg, |
375 | "\n\t\tExit:[%s]" , |
376 | __get_return_status( ret, s1 )); |
377 | |
378 | dm_log_write( __FILE__, |
379 | __LINE__, |
380 | LOG_INFO, |
381 | LOG_INFO, |
382 | connection -> msg ); |
383 | } |
384 | |
385 | return function_return( SQL_HANDLE_DBC, connection, ret ); |
386 | } |
387 | |