xref: /PHP-7.0/ext/odbc/php_odbc.c (revision 478f119a)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 7                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1997-2017 The PHP Group                                |
6    +----------------------------------------------------------------------+
7    | This source file is subject to version 3.01 of the PHP license,      |
8    | that is bundled with this package in the file LICENSE, and is        |
9    | available through the world-wide-web at the following url:           |
10    | http://www.php.net/license/3_01.txt                                  |
11    | If you did not receive a copy of the PHP license and are unable to   |
12    | obtain it through the world-wide-web, please send a note to          |
13    | license@php.net so we can mail you a copy immediately.               |
14    +----------------------------------------------------------------------+
15    | Authors: Stig S�ther Bakken <ssb@php.net>                            |
16    |          Andreas Karajannis <Andreas.Karajannis@gmd.de>              |
17    |          Frank M. Kromann <frank@kromann.info>  Support for DB/2 CLI |
18    |          Kevin N. Shallow <kshallow@tampabay.rr.com> Birdstep Support|
19    |          Daniel R. Kalowsky <kalowsky@php.net>                       |
20    +----------------------------------------------------------------------+
21 */
22 
23 /* $Id$ */
24 
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28 
29 #include "php.h"
30 #include "php_globals.h"
31 
32 #include "ext/standard/info.h"
33 #include "ext/standard/php_string.h"
34 #include "ext/standard/php_standard.h"
35 
36 #include "php_odbc.h"
37 #include "php_odbc_includes.h"
38 #include "php_globals.h"
39 
40 #if HAVE_UODBC
41 
42 #include <fcntl.h>
43 #include "ext/standard/head.h"
44 #include "php_ini.h"
45 
46 #ifdef PHP_WIN32
47 #include <winsock2.h>
48 
49 #define ODBC_TYPE "Win32"
50 #define PHP_ODBC_TYPE ODBC_TYPE
51 
52 #endif
53 
54 /*
55  * not defined elsewhere
56  */
57 
58 #ifndef TRUE
59 #define TRUE 1
60 #define FALSE 0
61 #endif
62 
63 void odbc_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent);
64 
65 static int le_result, le_conn, le_pconn;
66 
67 #define SAFE_SQL_NTS(n) ((SQLSMALLINT) ((n)?(SQL_NTS):0))
68 
69 /* {{{ arginfo */
70 ZEND_BEGIN_ARG_INFO(arginfo_odbc_close_all, 0)
71 ZEND_END_ARG_INFO()
72 
73 ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_binmode, 0, 0, 2)
74 	ZEND_ARG_INFO(0, result_id)
75 	ZEND_ARG_INFO(0, mode)
76 ZEND_END_ARG_INFO()
77 
78 ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_longreadlen, 0, 0, 2)
79 	ZEND_ARG_INFO(0, result_id)
80 	ZEND_ARG_INFO(0, length)
81 ZEND_END_ARG_INFO()
82 
83 ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_prepare, 0, 0, 2)
84 	ZEND_ARG_INFO(0, connection_id)
85 	ZEND_ARG_INFO(0, query)
86 ZEND_END_ARG_INFO()
87 
88 ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_execute, 0, 0, 1)
89 	ZEND_ARG_INFO(0, result_id)
90 	ZEND_ARG_INFO(0, parameters_array)
91 ZEND_END_ARG_INFO()
92 
93 ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_cursor, 0, 0, 1)
94 	ZEND_ARG_INFO(0, result_id)
95 ZEND_END_ARG_INFO()
96 
97 #ifdef HAVE_SQLDATASOURCES
98 ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_data_source, 0, 0, 2)
99 	ZEND_ARG_INFO(0, connection_id)
100 	ZEND_ARG_INFO(0, fetch_type)
101 ZEND_END_ARG_INFO()
102 #endif
103 
104 ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_exec, 0, 0, 2)
105 	ZEND_ARG_INFO(0, connection_id)
106 	ZEND_ARG_INFO(0, query)
107 	ZEND_ARG_INFO(0, flags)
108 ZEND_END_ARG_INFO()
109 
110 #ifdef PHP_ODBC_HAVE_FETCH_HASH
111 ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_fetch_object, 0, 0, 1)
112 	ZEND_ARG_INFO(0, result)
113 	ZEND_ARG_INFO(0, rownumber)
114 ZEND_END_ARG_INFO()
115 
116 ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_fetch_array, 0, 0, 1)
117 	ZEND_ARG_INFO(0, result)
118 	ZEND_ARG_INFO(0, rownumber)
119 ZEND_END_ARG_INFO()
120 #endif
121 
122 ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_fetch_into, 0, 0, 2)
123 	ZEND_ARG_INFO(0, result_id)
124 	ZEND_ARG_INFO(1, result_array)
125 	ZEND_ARG_INFO(0, rownumber)
126 ZEND_END_ARG_INFO()
127 
128 ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_fetch_row, 0, 0, 1)
129 	ZEND_ARG_INFO(0, result_id)
130 	ZEND_ARG_INFO(0, row_number)
131 ZEND_END_ARG_INFO()
132 
133 ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_result, 0, 0, 2)
134 	ZEND_ARG_INFO(0, result_id)
135 	ZEND_ARG_INFO(0, field)
136 ZEND_END_ARG_INFO()
137 
138 ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_result_all, 0, 0, 1)
139 	ZEND_ARG_INFO(0, result_id)
140 	ZEND_ARG_INFO(0, format)
141 ZEND_END_ARG_INFO()
142 
143 ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_free_result, 0, 0, 1)
144 	ZEND_ARG_INFO(0, result_id)
145 ZEND_END_ARG_INFO()
146 
147 ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_connect, 0, 0, 3)
148 	ZEND_ARG_INFO(0, dsn)
149 	ZEND_ARG_INFO(0, user)
150 	ZEND_ARG_INFO(0, password)
151 	ZEND_ARG_INFO(0, cursor_option)
152 ZEND_END_ARG_INFO()
153 
154 ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_pconnect, 0, 0, 3)
155 	ZEND_ARG_INFO(0, dsn)
156 	ZEND_ARG_INFO(0, user)
157 	ZEND_ARG_INFO(0, password)
158 	ZEND_ARG_INFO(0, cursor_option)
159 ZEND_END_ARG_INFO()
160 
161 ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_close, 0, 0, 1)
162 	ZEND_ARG_INFO(0, connection_id)
163 ZEND_END_ARG_INFO()
164 
165 ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_num_rows, 0, 0, 1)
166 	ZEND_ARG_INFO(0, result_id)
167 ZEND_END_ARG_INFO()
168 
169 #if !defined(HAVE_SOLID) && !defined(HAVE_SOLID_30)
170 ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_next_result, 0, 0, 1)
171 	ZEND_ARG_INFO(0, result_id)
172 ZEND_END_ARG_INFO()
173 #endif
174 
175 ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_num_fields, 0, 0, 1)
176 	ZEND_ARG_INFO(0, result_id)
177 ZEND_END_ARG_INFO()
178 
179 ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_field_name, 0, 0, 2)
180 	ZEND_ARG_INFO(0, result_id)
181 	ZEND_ARG_INFO(0, field_number)
182 ZEND_END_ARG_INFO()
183 
184 ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_field_type, 0, 0, 2)
185 	ZEND_ARG_INFO(0, result_id)
186 	ZEND_ARG_INFO(0, field_number)
187 ZEND_END_ARG_INFO()
188 
189 ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_field_len, 0, 0, 2)
190 	ZEND_ARG_INFO(0, result_id)
191 	ZEND_ARG_INFO(0, field_number)
192 ZEND_END_ARG_INFO()
193 
194 ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_field_scale, 0, 0, 2)
195 	ZEND_ARG_INFO(0, result_id)
196 	ZEND_ARG_INFO(0, field_number)
197 ZEND_END_ARG_INFO()
198 
199 ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_field_num, 0, 0, 2)
200 	ZEND_ARG_INFO(0, result_id)
201 	ZEND_ARG_INFO(0, field_name)
202 ZEND_END_ARG_INFO()
203 
204 ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_autocommit, 0, 0, 1)
205 	ZEND_ARG_INFO(0, connection_id)
206 	ZEND_ARG_INFO(0, onoff)
207 ZEND_END_ARG_INFO()
208 
209 ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_commit, 0, 0, 1)
210 	ZEND_ARG_INFO(0, connection_id)
211 ZEND_END_ARG_INFO()
212 
213 ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_rollback, 0, 0, 1)
214 	ZEND_ARG_INFO(0, connection_id)
215 ZEND_END_ARG_INFO()
216 
217 ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_error, 0, 0, 0)
218 	ZEND_ARG_INFO(0, connection_id)
219 ZEND_END_ARG_INFO()
220 
221 ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_errormsg, 0, 0, 0)
222 	ZEND_ARG_INFO(0, connection_id)
223 ZEND_END_ARG_INFO()
224 
225 ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_setoption, 0, 0, 4)
226 	ZEND_ARG_INFO(0, conn_id)
227 	ZEND_ARG_INFO(0, which)
228 	ZEND_ARG_INFO(0, option)
229 	ZEND_ARG_INFO(0, value)
230 ZEND_END_ARG_INFO()
231 
232 ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_tables, 0, 0, 1)
233 	ZEND_ARG_INFO(0, connection_id)
234 	ZEND_ARG_INFO(0, qualifier)
235 	ZEND_ARG_INFO(0, owner)
236 	ZEND_ARG_INFO(0, name)
237 	ZEND_ARG_INFO(0, table_types)
238 ZEND_END_ARG_INFO()
239 
240 ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_columns, 0, 0, 1)
241 	ZEND_ARG_INFO(0, connection_id)
242 	ZEND_ARG_INFO(0, qualifier)
243 	ZEND_ARG_INFO(0, owner)
244 	ZEND_ARG_INFO(0, table_name)
245 	ZEND_ARG_INFO(0, column_name)
246 ZEND_END_ARG_INFO()
247 
248 ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_gettypeinfo, 0, 0, 1)
249 	ZEND_ARG_INFO(0, connection_id)
250 	ZEND_ARG_INFO(0, data_type)
251 ZEND_END_ARG_INFO()
252 
253 ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_primarykeys, 0, 0, 4)
254 	ZEND_ARG_INFO(0, connection_id)
255 	ZEND_ARG_INFO(0, qualifier)
256 	ZEND_ARG_INFO(0, owner)
257 	ZEND_ARG_INFO(0, table)
258 ZEND_END_ARG_INFO()
259 
260 #if !defined(HAVE_SOLID) && !defined(HAVE_SOLID_30) && !defined(HAVE_SOLID_35)
261 #if !defined(HAVE_BIRDSTEP)
262 ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_procedurecolumns, 0, 0, 1)
263 	ZEND_ARG_INFO(0, connection_id)
264 	ZEND_ARG_INFO(0, qualifier)
265 	ZEND_ARG_INFO(0, owner)
266 	ZEND_ARG_INFO(0, proc)
267 	ZEND_ARG_INFO(0, column)
268 ZEND_END_ARG_INFO()
269 #endif
270 
271 ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_procedures, 0, 0, 1)
272 	ZEND_ARG_INFO(0, connection_id)
273 	ZEND_ARG_INFO(0, qualifier)
274 	ZEND_ARG_INFO(0, owner)
275 	ZEND_ARG_INFO(0, name)
276 ZEND_END_ARG_INFO()
277 
278 ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_foreignkeys, 0, 0, 7)
279 	ZEND_ARG_INFO(0, connection_id)
280 	ZEND_ARG_INFO(0, pk_qualifier)
281 	ZEND_ARG_INFO(0, pk_owner)
282 	ZEND_ARG_INFO(0, pk_table)
283 	ZEND_ARG_INFO(0, fk_qualifier)
284 	ZEND_ARG_INFO(0, fk_owner)
285 	ZEND_ARG_INFO(0, fk_table)
286 ZEND_END_ARG_INFO()
287 #endif
288 
289 ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_specialcolumns, 0, 0, 7)
290 	ZEND_ARG_INFO(0, connection_id)
291 	ZEND_ARG_INFO(0, type)
292 	ZEND_ARG_INFO(0, qualifier)
293 	ZEND_ARG_INFO(0, owner)
294 	ZEND_ARG_INFO(0, table)
295 	ZEND_ARG_INFO(0, scope)
296 	ZEND_ARG_INFO(0, nullable)
297 ZEND_END_ARG_INFO()
298 
299 ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_statistics, 0, 0, 6)
300 	ZEND_ARG_INFO(0, connection_id)
301 	ZEND_ARG_INFO(0, qualifier)
302 	ZEND_ARG_INFO(0, owner)
303 	ZEND_ARG_INFO(0, name)
304 	ZEND_ARG_INFO(0, unique)
305 	ZEND_ARG_INFO(0, accuracy)
306 ZEND_END_ARG_INFO()
307 
308 #if !defined(HAVE_DBMAKER) && !defined(HAVE_SOLID) && !defined(HAVE_SOLID_30) &&!defined(HAVE_SOLID_35) && !defined(HAVE_BIRDSTEP)
309 ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_tableprivileges, 0, 0, 4)
310 	ZEND_ARG_INFO(0, connection_id)
311 	ZEND_ARG_INFO(0, qualifier)
312 	ZEND_ARG_INFO(0, owner)
313 	ZEND_ARG_INFO(0, name)
314 ZEND_END_ARG_INFO()
315 
316 ZEND_BEGIN_ARG_INFO_EX(arginfo_odbc_columnprivileges, 0, 0, 5)
317 	ZEND_ARG_INFO(0, connection_id)
318 	ZEND_ARG_INFO(0, catalog)
319 	ZEND_ARG_INFO(0, schema)
320 	ZEND_ARG_INFO(0, table)
321 	ZEND_ARG_INFO(0, column)
322 ZEND_END_ARG_INFO()
323 #endif
324 /* }}} */
325 
326 /* {{{ odbc_functions[]
327  */
328 const zend_function_entry odbc_functions[] = {
329 	PHP_FE(odbc_autocommit, arginfo_odbc_autocommit)
330 	PHP_FE(odbc_binmode, arginfo_odbc_binmode)
331 	PHP_FE(odbc_close, arginfo_odbc_close)
332 	PHP_FE(odbc_close_all, arginfo_odbc_close_all)
333 	PHP_FE(odbc_columns, arginfo_odbc_columns)
334 	PHP_FE(odbc_commit, arginfo_odbc_commit)
335 	PHP_FE(odbc_connect, arginfo_odbc_connect)
336 	PHP_FE(odbc_cursor, arginfo_odbc_cursor)
337 #ifdef HAVE_SQLDATASOURCES
338 	PHP_FE(odbc_data_source, arginfo_odbc_data_source)
339 #endif
340 	PHP_FE(odbc_execute, arginfo_odbc_execute)
341 	PHP_FE(odbc_error, arginfo_odbc_error)
342 	PHP_FE(odbc_errormsg, arginfo_odbc_errormsg)
343 	PHP_FE(odbc_exec, arginfo_odbc_exec)
344 #ifdef PHP_ODBC_HAVE_FETCH_HASH
345 	PHP_FE(odbc_fetch_array, arginfo_odbc_fetch_array)
346 	PHP_FE(odbc_fetch_object, arginfo_odbc_fetch_object)
347 #endif
348 	PHP_FE(odbc_fetch_row, arginfo_odbc_fetch_row)
349 	PHP_FE(odbc_fetch_into, arginfo_odbc_fetch_into)
350 	PHP_FE(odbc_field_len, arginfo_odbc_field_len)
351 	PHP_FE(odbc_field_scale, arginfo_odbc_field_scale)
352 	PHP_FE(odbc_field_name, arginfo_odbc_field_name)
353 	PHP_FE(odbc_field_type, arginfo_odbc_field_type)
354 	PHP_FE(odbc_field_num, arginfo_odbc_field_num)
355 	PHP_FE(odbc_free_result, arginfo_odbc_free_result)
356 	PHP_FE(odbc_gettypeinfo, arginfo_odbc_gettypeinfo)
357 	PHP_FE(odbc_longreadlen, arginfo_odbc_longreadlen)
358 #if !defined(HAVE_SOLID) && !defined(HAVE_SOLID_30)
359 	PHP_FE(odbc_next_result, arginfo_odbc_next_result)
360 #endif
361 	PHP_FE(odbc_num_fields, arginfo_odbc_num_fields)
362 	PHP_FE(odbc_num_rows, arginfo_odbc_num_rows)
363 	PHP_FE(odbc_pconnect, arginfo_odbc_pconnect)
364 	PHP_FE(odbc_prepare, arginfo_odbc_prepare)
365 	PHP_FE(odbc_result, arginfo_odbc_result)
366 	PHP_FE(odbc_result_all, arginfo_odbc_result_all)
367 	PHP_FE(odbc_rollback, arginfo_odbc_rollback)
368 	PHP_FE(odbc_setoption, arginfo_odbc_setoption)
369 	PHP_FE(odbc_specialcolumns, arginfo_odbc_specialcolumns)
370 	PHP_FE(odbc_statistics, arginfo_odbc_statistics)
371 	PHP_FE(odbc_tables, arginfo_odbc_tables)
372 	PHP_FE(odbc_primarykeys, arginfo_odbc_primarykeys)
373 #if !defined(HAVE_DBMAKER) && !defined(HAVE_SOLID) && !defined(HAVE_SOLID_30) &&!defined(HAVE_SOLID_35) && !defined(HAVE_BIRDSTEP)    /* not supported now */
374 	PHP_FE(odbc_columnprivileges, arginfo_odbc_columnprivileges)
375 	PHP_FE(odbc_tableprivileges, arginfo_odbc_tableprivileges)
376 #endif
377 #if !defined(HAVE_SOLID) && !defined(HAVE_SOLID_30) && !defined(HAVE_SOLID_35) /* not supported */
378 	PHP_FE(odbc_foreignkeys, arginfo_odbc_foreignkeys)
379 	PHP_FE(odbc_procedures, arginfo_odbc_procedures)
380 #if !defined(HAVE_BIRDSTEP)
381 	PHP_FE(odbc_procedurecolumns, arginfo_odbc_procedurecolumns)
382 #endif
383 #endif
384 	PHP_FALIAS(odbc_do, odbc_exec, arginfo_odbc_exec)
385 	PHP_FALIAS(odbc_field_precision, odbc_field_len, arginfo_odbc_field_len)
386 	PHP_FE_END
387 };
388 /* }}} */
389 
390 PHP_ODBC_API ZEND_DECLARE_MODULE_GLOBALS(odbc)
391 static PHP_GINIT_FUNCTION(odbc);
392 
393 /* {{{ odbc_module_entry
394  */
395 zend_module_entry odbc_module_entry = {
396 	STANDARD_MODULE_HEADER,
397 	"odbc",
398 	odbc_functions,
399 	PHP_MINIT(odbc),
400 	PHP_MSHUTDOWN(odbc),
401 	PHP_RINIT(odbc),
402 	PHP_RSHUTDOWN(odbc),
403 	PHP_MINFO(odbc),
404 	PHP_ODBC_VERSION,
405 	PHP_MODULE_GLOBALS(odbc),
406 	PHP_GINIT(odbc),
407 	NULL,
408 	NULL,
409 	STANDARD_MODULE_PROPERTIES_EX
410 };
411 /* }}} */
412 
413 #ifdef COMPILE_DL_ODBC
414 #ifdef ZTS
415 ZEND_TSRMLS_CACHE_DEFINE()
416 #endif
ZEND_GET_MODULE(odbc)417 ZEND_GET_MODULE(odbc)
418 #endif
419 
420 /* {{{ _free_odbc_result
421  */
422 static void _free_odbc_result(zend_resource *rsrc)
423 {
424 	odbc_result *res = (odbc_result *)rsrc->ptr;
425 	int i;
426 	RETCODE rc;
427 
428 	if (res) {
429 		if (res->values) {
430 			for(i = 0; i < res->numcols; i++) {
431 				if (res->values[i].value)
432 					efree(res->values[i].value);
433 			}
434 			efree(res->values);
435 			res->values = NULL;
436 		}
437 		/* If aborted via timer expiration, don't try to call any unixODBC function */
438 		if (res->stmt && !(PG(connection_status) & PHP_CONNECTION_TIMEOUT)) {
439 #if defined(HAVE_SOLID) || defined(HAVE_SOLID_30) || defined(HAVE_SOLID_35)
440 			SQLTransact(res->conn_ptr->henv, res->conn_ptr->hdbc,
441 						(SQLUSMALLINT) SQL_COMMIT);
442 #endif
443 			rc = SQLFreeStmt(res->stmt,SQL_DROP);
444 			/* We don't want the connection to be closed after the last statement has been closed
445 			 * Connections will be closed on shutdown
446 			 * zend_list_delete(res->conn_ptr->id);
447 			 */
448 		}
449 		if (res->param_info) {
450 			efree(res->param_info);
451 		}
452 		efree(res);
453 	}
454 }
455 /* }}} */
456 
457 /* {{{ safe_odbc_disconnect
458  * disconnect, and if it fails, then issue a rollback for any pending transaction (lurcher)
459  */
safe_odbc_disconnect(void * handle)460 static void safe_odbc_disconnect( void *handle )
461 {
462 	int ret;
463 
464 	ret = SQLDisconnect( handle );
465 	if ( ret == SQL_ERROR )
466 	{
467 		SQLTransact( NULL, handle, SQL_ROLLBACK );
468 		SQLDisconnect( handle );
469 	}
470 }
471 /* }}} */
472 
473 /* {{{ _close_odbc_conn
474  */
_close_odbc_conn(zend_resource * rsrc)475 static void _close_odbc_conn(zend_resource *rsrc)
476 {
477 	zend_resource *p;
478 	odbc_result *res;
479 
480 	odbc_connection *conn = (odbc_connection *)rsrc->ptr;
481 
482 	ZEND_HASH_FOREACH_PTR(&EG(regular_list), p) {
483 		if (p->ptr && (p->type == le_result)) {
484 			res = (odbc_result *)p->ptr;
485 			if (res->conn_ptr == conn) {
486 				zend_list_close(p);
487 			}
488 		}
489 	} ZEND_HASH_FOREACH_END();
490 
491 	/* If aborted via timer expiration, don't try to call any unixODBC function */
492 	if (!(PG(connection_status) & PHP_CONNECTION_TIMEOUT)) {
493 		safe_odbc_disconnect(conn->hdbc);
494 		SQLFreeConnect(conn->hdbc);
495 		SQLFreeEnv(conn->henv);
496 	}
497 	efree(conn);
498 	ODBCG(num_links)--;
499 }
500 /* }}} */
501 
502 /* {{{ void _close_odbc_pconn
503  */
_close_odbc_pconn(zend_resource * rsrc)504 static void _close_odbc_pconn(zend_resource *rsrc)
505 {
506 	zend_resource *p;
507 	odbc_result *res;
508 	odbc_connection *conn = (odbc_connection *)rsrc->ptr;
509 
510 	ZEND_HASH_FOREACH_PTR(&EG(regular_list), p) {
511 		if (p->ptr && (p->type == le_result)) {
512 			res = (odbc_result *)p->ptr;
513 			if (res->conn_ptr == conn) {
514 				zend_list_close(p);
515 			}
516 		}
517 	} ZEND_HASH_FOREACH_END();
518 
519 	/* If aborted via timer expiration, don't try to call any unixODBC function */
520 	if (!(PG(connection_status) & PHP_CONNECTION_TIMEOUT)) {
521 		safe_odbc_disconnect(conn->hdbc);
522 		SQLFreeConnect(conn->hdbc);
523 		SQLFreeEnv(conn->henv);
524 	}
525 	free(conn);
526 
527 	ODBCG(num_links)--;
528 	ODBCG(num_persistent)--;
529 }
530 /* }}} */
531 
532 /* {{{ PHP_INI_DISP(display_link_nums)
533  */
PHP_INI_DISP(display_link_nums)534 static PHP_INI_DISP(display_link_nums)
535 {
536 	char *value;
537 
538 	if (type == PHP_INI_DISPLAY_ORIG && ini_entry->modified) {
539 		value = ZSTR_VAL(ini_entry->orig_value);
540 	} else if (ini_entry->value) {
541 		value = ZSTR_VAL(ini_entry->value);
542 	} else {
543 		value = NULL;
544 	}
545 
546 	if (value) {
547 		if (atoi(value) == -1) {
548 			PUTS("Unlimited");
549 		} else {
550 			php_printf("%s", value);
551 		}
552 	}
553 }
554 /* }}} */
555 
556 /* {{{ PHP_INI_DISP(display_defPW)
557  */
PHP_INI_DISP(display_defPW)558 static PHP_INI_DISP(display_defPW)
559 {
560 	char *value;
561 
562 	if (type == PHP_INI_DISPLAY_ORIG && ini_entry->modified) {
563 		value = ZSTR_VAL(ini_entry->orig_value);
564 	} else if (ini_entry->value) {
565 		value = ZSTR_VAL(ini_entry->value);
566 	} else {
567 		value = NULL;
568 	}
569 
570 	if (value) {
571 #if PHP_DEBUG
572 		php_printf("%s", value);
573 #else
574 		PUTS("********");
575 #endif
576 	} else {
577 		if (PG(html_errors)) {
578 			PUTS("<i>no value</i>");
579 		} else {
580 			PUTS("no value");
581 		}
582 	}
583 }
584 /* }}} */
585 
586 /* {{{ PHP_INI_DISP(display_binmode)
587  */
PHP_INI_DISP(display_binmode)588 static PHP_INI_DISP(display_binmode)
589 {
590 	char *value;
591 
592 	if (type == PHP_INI_DISPLAY_ORIG && ini_entry->modified) {
593 		value = ZSTR_VAL(ini_entry->orig_value);
594 	} else if (ini_entry->value) {
595 		value = ZSTR_VAL(ini_entry->value);
596 	} else {
597 		value = NULL;
598 	}
599 
600 	if (value) {
601 		switch(atoi(value)) {
602 			case 0:
603 				PUTS("passthru");
604 				break;
605 			case 1:
606 				PUTS("return as is");
607 				break;
608 			case 2:
609 				PUTS("return as char");
610 				break;
611 		}
612 	}
613 }
614 /* }}} */
615 
616 /* {{{ PHP_INI_DISP(display_lrl)
617  */
PHP_INI_DISP(display_lrl)618 static PHP_INI_DISP(display_lrl)
619 {
620 	char *value;
621 
622 	if (type == PHP_INI_DISPLAY_ORIG && ini_entry->modified) {
623 		value = ZSTR_VAL(ini_entry->orig_value);
624 	} else if (ini_entry->value) {
625 		value = ZSTR_VAL(ini_entry->value);
626 	} else {
627 		value = NULL;
628 	}
629 
630 	if (value) {
631 		if (atoi(value) <= 0) {
632 			PUTS("Passthru");
633 		} else {
634 			php_printf("return up to %s bytes", value);
635 		}
636 	}
637 }
638 /* }}} */
639 
640 
641 /* {{{ PHP_INI_DISP(display_cursortype)
642  */
PHP_INI_DISP(display_cursortype)643 static PHP_INI_DISP(display_cursortype)
644 {
645 	char *value;
646 
647 	if (type == PHP_INI_DISPLAY_ORIG && ini_entry->modified) {
648 		value = ZSTR_VAL(ini_entry->orig_value);
649 	} else if (ini_entry->value) {
650 		value = ZSTR_VAL(ini_entry->value);
651 	} else {
652 		value = NULL;
653 	}
654 
655 	if (value) {
656 		switch (atoi (value))
657 		  {
658 		    case SQL_CURSOR_FORWARD_ONLY:
659 				PUTS ("Forward Only cursor");
660 				break;
661 
662 			case SQL_CURSOR_STATIC:
663 			    PUTS ("Static cursor");
664 				break;
665 
666 			case SQL_CURSOR_KEYSET_DRIVEN:
667 				PUTS ("Keyset driven cursor");
668 				break;
669 
670 			case SQL_CURSOR_DYNAMIC:
671 				PUTS ("Dynamic cursor");
672 				break;
673 
674 			default:
675 				php_printf("Unknown cursor model %s", value);
676 				break;
677 		  }
678 	}
679 }
680 
681 /* }}} */
682 
683 /* {{{ PHP_INI_BEGIN
684  */
685 PHP_INI_BEGIN()
686 	STD_PHP_INI_BOOLEAN("odbc.allow_persistent", "1", PHP_INI_SYSTEM, OnUpdateLong,
687 			allow_persistent, zend_odbc_globals, odbc_globals)
688 	STD_PHP_INI_ENTRY_EX("odbc.max_persistent",  "-1", PHP_INI_SYSTEM, OnUpdateLong,
689 			max_persistent, zend_odbc_globals, odbc_globals, display_link_nums)
690 	STD_PHP_INI_ENTRY_EX("odbc.max_links", "-1", PHP_INI_SYSTEM, OnUpdateLong,
691 			max_links, zend_odbc_globals, odbc_globals, display_link_nums)
692 	STD_PHP_INI_ENTRY("odbc.default_db", NULL, PHP_INI_ALL, OnUpdateString,
693 			defDB, zend_odbc_globals, odbc_globals)
694 	STD_PHP_INI_ENTRY("odbc.default_user", NULL, PHP_INI_ALL, OnUpdateString,
695 			defUser, zend_odbc_globals, odbc_globals)
696 	STD_PHP_INI_ENTRY_EX("odbc.default_pw", NULL, PHP_INI_ALL, OnUpdateString,
697 			defPW, zend_odbc_globals, odbc_globals, display_defPW)
698 	STD_PHP_INI_ENTRY_EX("odbc.defaultlrl", "4096", PHP_INI_ALL, OnUpdateLong,
699 			defaultlrl, zend_odbc_globals, odbc_globals, display_lrl)
700 	STD_PHP_INI_ENTRY_EX("odbc.defaultbinmode", "1", PHP_INI_ALL, OnUpdateLong,
701 			defaultbinmode, zend_odbc_globals, odbc_globals, display_binmode)
702 	STD_PHP_INI_BOOLEAN("odbc.check_persistent", "1", PHP_INI_SYSTEM, OnUpdateLong,
703 			check_persistent, zend_odbc_globals, odbc_globals)
704 	STD_PHP_INI_ENTRY_EX("odbc.default_cursortype", "3", PHP_INI_ALL, OnUpdateLong,
705 			default_cursortype, zend_odbc_globals, odbc_globals, display_cursortype)
PHP_INI_END()706 PHP_INI_END()
707 /* }}} */
708 
709 static PHP_GINIT_FUNCTION(odbc)
710 {
711 #if defined(COMPILE_DL_ODBC) && defined(ZTS)
712 	ZEND_TSRMLS_CACHE_UPDATE();
713 #endif
714 	odbc_globals->num_persistent = 0;
715 }
716 
717 /* {{{ PHP_MINIT_FUNCTION */
PHP_MINIT_FUNCTION(odbc)718 PHP_MINIT_FUNCTION(odbc)
719 {
720 #ifdef SQLANY_BUG
721 	ODBC_SQL_CONN_T foobar;
722 	RETCODE rc;
723 #endif
724 
725 	REGISTER_INI_ENTRIES();
726 	le_result = zend_register_list_destructors_ex(_free_odbc_result, NULL, "odbc result", module_number);
727 	le_conn = zend_register_list_destructors_ex(_close_odbc_conn, NULL, "odbc link", module_number);
728 	le_pconn = zend_register_list_destructors_ex(NULL, _close_odbc_pconn, "odbc link persistent", module_number);
729 	odbc_module_entry.type = type;
730 
731 	REGISTER_STRING_CONSTANT("ODBC_TYPE", PHP_ODBC_TYPE, CONST_CS | CONST_PERSISTENT);
732 	REGISTER_LONG_CONSTANT("ODBC_BINMODE_PASSTHRU", 0, CONST_CS | CONST_PERSISTENT);
733 	REGISTER_LONG_CONSTANT("ODBC_BINMODE_RETURN", 1, CONST_CS | CONST_PERSISTENT);
734 	REGISTER_LONG_CONSTANT("ODBC_BINMODE_CONVERT", 2, CONST_CS | CONST_PERSISTENT);
735 	/* Define Constants for options
736 	   these Constants are defined in <sqlext.h>
737 	*/
738 	REGISTER_LONG_CONSTANT("SQL_ODBC_CURSORS", SQL_ODBC_CURSORS, CONST_PERSISTENT | CONST_CS);
739 	REGISTER_LONG_CONSTANT("SQL_CUR_USE_DRIVER", SQL_CUR_USE_DRIVER, CONST_PERSISTENT | CONST_CS);
740 	REGISTER_LONG_CONSTANT("SQL_CUR_USE_IF_NEEDED", SQL_CUR_USE_IF_NEEDED, CONST_PERSISTENT | CONST_CS);
741 	REGISTER_LONG_CONSTANT("SQL_CUR_USE_ODBC", SQL_CUR_USE_ODBC, CONST_PERSISTENT | CONST_CS);
742 
743 
744 	REGISTER_LONG_CONSTANT("SQL_CONCURRENCY", SQL_CONCURRENCY, CONST_PERSISTENT | CONST_CS);
745 	REGISTER_LONG_CONSTANT("SQL_CONCUR_READ_ONLY", SQL_CONCUR_READ_ONLY, CONST_PERSISTENT | CONST_CS);
746 	REGISTER_LONG_CONSTANT("SQL_CONCUR_LOCK", SQL_CONCUR_LOCK, CONST_PERSISTENT | CONST_CS);
747 	REGISTER_LONG_CONSTANT("SQL_CONCUR_ROWVER", SQL_CONCUR_ROWVER, CONST_PERSISTENT | CONST_CS);
748 	REGISTER_LONG_CONSTANT("SQL_CONCUR_VALUES", SQL_CONCUR_VALUES, CONST_PERSISTENT | CONST_CS);
749 
750 	REGISTER_LONG_CONSTANT("SQL_CURSOR_TYPE", SQL_CURSOR_TYPE, CONST_PERSISTENT | CONST_CS);
751 	REGISTER_LONG_CONSTANT("SQL_CURSOR_FORWARD_ONLY", SQL_CURSOR_FORWARD_ONLY, CONST_PERSISTENT | CONST_CS);
752 	REGISTER_LONG_CONSTANT("SQL_CURSOR_KEYSET_DRIVEN", SQL_CURSOR_KEYSET_DRIVEN, CONST_PERSISTENT | CONST_CS);
753 	REGISTER_LONG_CONSTANT("SQL_CURSOR_DYNAMIC", SQL_CURSOR_DYNAMIC, CONST_PERSISTENT | CONST_CS);
754 	REGISTER_LONG_CONSTANT("SQL_CURSOR_STATIC", SQL_CURSOR_STATIC, CONST_PERSISTENT | CONST_CS);
755 
756 	REGISTER_LONG_CONSTANT("SQL_KEYSET_SIZE", SQL_KEYSET_SIZE, CONST_PERSISTENT | CONST_CS);
757 
758 	/* these are for the Data Source type */
759 	REGISTER_LONG_CONSTANT("SQL_FETCH_FIRST", SQL_FETCH_FIRST, CONST_PERSISTENT | CONST_CS);
760 	REGISTER_LONG_CONSTANT("SQL_FETCH_NEXT", SQL_FETCH_NEXT, CONST_PERSISTENT | CONST_CS);
761 
762 	/*
763 	 * register the standard data types
764 	 */
765 	REGISTER_LONG_CONSTANT("SQL_CHAR", SQL_CHAR, CONST_PERSISTENT | CONST_CS);
766 	REGISTER_LONG_CONSTANT("SQL_VARCHAR", SQL_VARCHAR, CONST_PERSISTENT | CONST_CS);
767 	REGISTER_LONG_CONSTANT("SQL_LONGVARCHAR", SQL_LONGVARCHAR, CONST_PERSISTENT | CONST_CS);
768 	REGISTER_LONG_CONSTANT("SQL_DECIMAL", SQL_DECIMAL, CONST_PERSISTENT | CONST_CS);
769 	REGISTER_LONG_CONSTANT("SQL_NUMERIC", SQL_NUMERIC, CONST_PERSISTENT | CONST_CS);
770 	REGISTER_LONG_CONSTANT("SQL_BIT", SQL_BIT, CONST_PERSISTENT | CONST_CS);
771 	REGISTER_LONG_CONSTANT("SQL_TINYINT", SQL_TINYINT, CONST_PERSISTENT | CONST_CS);
772 	REGISTER_LONG_CONSTANT("SQL_SMALLINT", SQL_SMALLINT, CONST_PERSISTENT | CONST_CS);
773 	REGISTER_LONG_CONSTANT("SQL_INTEGER", SQL_INTEGER, CONST_PERSISTENT | CONST_CS);
774 	REGISTER_LONG_CONSTANT("SQL_BIGINT", SQL_BIGINT, CONST_PERSISTENT | CONST_CS);
775 	REGISTER_LONG_CONSTANT("SQL_REAL", SQL_REAL, CONST_PERSISTENT | CONST_CS);
776 	REGISTER_LONG_CONSTANT("SQL_FLOAT", SQL_FLOAT, CONST_PERSISTENT | CONST_CS);
777 	REGISTER_LONG_CONSTANT("SQL_DOUBLE", SQL_DOUBLE, CONST_PERSISTENT | CONST_CS);
778 	REGISTER_LONG_CONSTANT("SQL_BINARY", SQL_BINARY, CONST_PERSISTENT | CONST_CS);
779 	REGISTER_LONG_CONSTANT("SQL_VARBINARY", SQL_VARBINARY, CONST_PERSISTENT | CONST_CS);
780 	REGISTER_LONG_CONSTANT("SQL_LONGVARBINARY", SQL_LONGVARBINARY, CONST_PERSISTENT | CONST_CS);
781 	REGISTER_LONG_CONSTANT("SQL_DATE", SQL_DATE, CONST_PERSISTENT | CONST_CS);
782 	REGISTER_LONG_CONSTANT("SQL_TIME", SQL_TIME, CONST_PERSISTENT | CONST_CS);
783 	REGISTER_LONG_CONSTANT("SQL_TIMESTAMP", SQL_TIMESTAMP, CONST_PERSISTENT | CONST_CS);
784 #if defined(ODBCVER) && (ODBCVER >= 0x0300)
785 	REGISTER_LONG_CONSTANT("SQL_TYPE_DATE", SQL_TYPE_DATE, CONST_PERSISTENT | CONST_CS);
786 	REGISTER_LONG_CONSTANT("SQL_TYPE_TIME", SQL_TYPE_TIME, CONST_PERSISTENT | CONST_CS);
787 	REGISTER_LONG_CONSTANT("SQL_TYPE_TIMESTAMP", SQL_TYPE_TIMESTAMP, CONST_PERSISTENT | CONST_CS);
788 	REGISTER_LONG_CONSTANT("SQL_WCHAR", SQL_WCHAR, CONST_PERSISTENT | CONST_CS);
789 	REGISTER_LONG_CONSTANT("SQL_WVARCHAR", SQL_WVARCHAR, CONST_PERSISTENT | CONST_CS);
790 	REGISTER_LONG_CONSTANT("SQL_WLONGVARCHAR", SQL_WLONGVARCHAR, CONST_PERSISTENT | CONST_CS);
791 
792 	/*
793 	 * SQLSpecialColumns values
794 	 */
795 	REGISTER_LONG_CONSTANT("SQL_BEST_ROWID", SQL_BEST_ROWID, CONST_PERSISTENT | CONST_CS);
796 	REGISTER_LONG_CONSTANT("SQL_ROWVER", SQL_ROWVER, CONST_PERSISTENT | CONST_CS);
797 	REGISTER_LONG_CONSTANT("SQL_SCOPE_CURROW", SQL_SCOPE_CURROW, CONST_PERSISTENT | CONST_CS);
798 	REGISTER_LONG_CONSTANT("SQL_SCOPE_TRANSACTION", SQL_SCOPE_TRANSACTION, CONST_PERSISTENT | CONST_CS);
799 	REGISTER_LONG_CONSTANT("SQL_SCOPE_SESSION", SQL_SCOPE_SESSION, CONST_PERSISTENT | CONST_CS);
800 	REGISTER_LONG_CONSTANT("SQL_NO_NULLS", SQL_NO_NULLS, CONST_PERSISTENT | CONST_CS);
801 	REGISTER_LONG_CONSTANT("SQL_NULLABLE", SQL_NULLABLE, CONST_PERSISTENT | CONST_CS);
802 
803 	/*
804 	 * SQLStatistics values
805 	 */
806 	REGISTER_LONG_CONSTANT("SQL_INDEX_UNIQUE", SQL_INDEX_UNIQUE, CONST_PERSISTENT | CONST_CS);
807 	REGISTER_LONG_CONSTANT("SQL_INDEX_ALL", SQL_INDEX_ALL, CONST_PERSISTENT | CONST_CS);
808 	REGISTER_LONG_CONSTANT("SQL_ENSURE", SQL_ENSURE, CONST_PERSISTENT | CONST_CS);
809 	REGISTER_LONG_CONSTANT("SQL_QUICK", SQL_QUICK, CONST_PERSISTENT | CONST_CS);
810 #endif
811 
812 #if defined(HAVE_IBMDB2) && defined(_AIX)
813 	/* atexit() handler in the DB2/AIX library segfaults in PHP CLI */
814 	/* DB2NOEXITLIST env variable prevents DB2 from invoking atexit() */
815 	putenv("DB2NOEXITLIST=TRUE");
816 #endif
817 
818 	return SUCCESS;
819 }
820 /* }}} */
821 
822 /* {{{ PHP_RINIT_FUNCTION */
PHP_RINIT_FUNCTION(odbc)823 PHP_RINIT_FUNCTION(odbc)
824 {
825 	ODBCG(defConn) = -1;
826 	ODBCG(num_links) = ODBCG(num_persistent);
827 	memset(ODBCG(laststate), '\0', 6);
828 	memset(ODBCG(lasterrormsg), '\0', SQL_MAX_MESSAGE_LENGTH);
829 	return SUCCESS;
830 }
831 /* }}} */
832 
833 /* {{{ PHP_RSHUTDOWN_FUNCTION */
PHP_RSHUTDOWN_FUNCTION(odbc)834 PHP_RSHUTDOWN_FUNCTION(odbc)
835 {
836 	return SUCCESS;
837 }
838 /* }}} */
839 
840 /* {{{ PHP_MSHUTDOWN_FUNCTION */
PHP_MSHUTDOWN_FUNCTION(odbc)841 PHP_MSHUTDOWN_FUNCTION(odbc)
842 {
843 	UNREGISTER_INI_ENTRIES();
844 	return SUCCESS;
845 }
846 /* }}} */
847 
848 /* {{{ PHP_MINFO_FUNCTION */
PHP_MINFO_FUNCTION(odbc)849 PHP_MINFO_FUNCTION(odbc)
850 {
851 	char buf[32];
852 
853 	php_info_print_table_start();
854 	php_info_print_table_header(2, "ODBC Support", "enabled");
855 	snprintf(buf, sizeof(buf), ZEND_LONG_FMT, ODBCG(num_persistent));
856 	php_info_print_table_row(2, "Active Persistent Links", buf);
857 	snprintf(buf, sizeof(buf), ZEND_LONG_FMT, ODBCG(num_links));
858 	php_info_print_table_row(2, "Active Links", buf);
859 	php_info_print_table_row(2, "ODBC library", PHP_ODBC_TYPE);
860 #ifdef ODBCVER
861 	snprintf(buf, sizeof(buf), "0x%0.4x", ODBCVER);
862 	php_info_print_table_row(2, "ODBCVER", buf);
863 #endif
864 #ifndef PHP_WIN32
865 	php_info_print_table_row(2, "ODBC_INCLUDE", PHP_ODBC_INCLUDE);
866 	php_info_print_table_row(2, "ODBC_LFLAGS", PHP_ODBC_LFLAGS);
867 	php_info_print_table_row(2, "ODBC_LIBS", PHP_ODBC_LIBS);
868 #endif
869 	php_info_print_table_end();
870 
871 	DISPLAY_INI_ENTRIES();
872 
873 }
874 /* }}} */
875 
876 /* {{{ odbc_sql_error */
odbc_sql_error(ODBC_SQL_ERROR_PARAMS)877 void odbc_sql_error(ODBC_SQL_ERROR_PARAMS)
878 {
879 	SQLINTEGER	error;        /* Not used */
880 	SQLSMALLINT	errormsgsize; /* Not used */
881 	RETCODE rc;
882 	ODBC_SQL_ENV_T henv;
883 	ODBC_SQL_CONN_T conn;
884 
885 	if (conn_resource) {
886 		henv = conn_resource->henv;
887 		conn = conn_resource->hdbc;
888 	} else {
889 		henv = SQL_NULL_HENV;
890 		conn = SQL_NULL_HDBC;
891 	}
892 
893 	/* This leads to an endless loop in many drivers!
894 	 *
895 	   while(henv != SQL_NULL_HENV){
896 		do {
897 	 */
898 	rc = SQLError(henv, conn, stmt, ODBCG(laststate), &error, ODBCG(lasterrormsg), sizeof(ODBCG(lasterrormsg))-1, &errormsgsize);
899 	if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
900 		snprintf(ODBCG(laststate), sizeof(ODBCG(laststate)), "HY000");
901 		snprintf(ODBCG(lasterrormsg), sizeof(ODBCG(lasterrormsg)), "Failed to fetch error message");
902 	}
903 	if (conn_resource) {
904 		memcpy(conn_resource->laststate, ODBCG(laststate), sizeof(ODBCG(laststate)));
905 		memcpy(conn_resource->lasterrormsg, ODBCG(lasterrormsg), sizeof(ODBCG(lasterrormsg)));
906 	}
907 	if (func) {
908 		php_error_docref(NULL, E_WARNING, "SQL error: %s, SQL state %s in %s", ODBCG(lasterrormsg), ODBCG(laststate), func);
909 	} else {
910 		php_error_docref(NULL, E_WARNING, "SQL error: %s, SQL state %s", ODBCG(lasterrormsg), ODBCG(laststate));
911 	}
912 	/*
913 		} while (SQL_SUCCEEDED(rc));
914 	}
915 	*/
916 }
917 /* }}} */
918 
919 /* {{{ php_odbc_fetch_attribs */
php_odbc_fetch_attribs(INTERNAL_FUNCTION_PARAMETERS,int mode)920 void php_odbc_fetch_attribs(INTERNAL_FUNCTION_PARAMETERS, int mode)
921 {
922 	odbc_result *result;
923 	zval *pv_res;
924 	zend_long flag;
925 
926 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rl", &pv_res, &flag) == FAILURE) {
927 		return;
928 	}
929 
930 	if ((result = (odbc_result *)zend_fetch_resource(Z_RES_P(pv_res), "ODBC result", le_result)) == NULL) {
931 		RETURN_FALSE;
932 	}
933 
934 	if (mode) {
935 		result->longreadlen = flag;
936 	} else {
937 		result->binmode = flag;
938 	}
939 
940 	RETURN_TRUE;
941 }
942 /* }}} */
943 
944 /* {{{ odbc_bindcols */
odbc_bindcols(odbc_result * result)945 int odbc_bindcols(odbc_result *result)
946 {
947 	RETCODE rc;
948 	int i;
949 	SQLSMALLINT 	colnamelen; /* Not used */
950 	SQLLEN      	displaysize;
951 	SQLUSMALLINT	colfieldid;
952 	int		charextraalloc;
953 
954 	result->values = (odbc_result_value *) safe_emalloc(sizeof(odbc_result_value), result->numcols, 0);
955 
956 	result->longreadlen = ODBCG(defaultlrl);
957 	result->binmode = ODBCG(defaultbinmode);
958 
959 	for(i = 0; i < result->numcols; i++) {
960 		charextraalloc = 0;
961 		colfieldid = SQL_COLUMN_DISPLAY_SIZE;
962 
963 		rc = PHP_ODBC_SQLCOLATTRIBUTE(result->stmt, (SQLUSMALLINT)(i+1), PHP_ODBC_SQL_DESC_NAME,
964 				result->values[i].name, sizeof(result->values[i].name), &colnamelen, 0);
965 		rc = PHP_ODBC_SQLCOLATTRIBUTE(result->stmt, (SQLUSMALLINT)(i+1), SQL_COLUMN_TYPE,
966 				NULL, 0, NULL, &result->values[i].coltype);
967 
968 		/* Don't bind LONG / BINARY columns, so that fetch behaviour can
969 		 * be controlled by odbc_binmode() / odbc_longreadlen()
970 		 */
971 
972 		switch(result->values[i].coltype) {
973 			case SQL_BINARY:
974 			case SQL_VARBINARY:
975 			case SQL_LONGVARBINARY:
976 			case SQL_LONGVARCHAR:
977 #if defined(ODBCVER) && (ODBCVER >= 0x0300)
978 			case SQL_WLONGVARCHAR:
979 #endif
980 				result->values[i].value = NULL;
981 				break;
982 
983 #ifdef HAVE_ADABAS
984 			case SQL_TIMESTAMP:
985 				result->values[i].value = (char *)emalloc(27);
986 				SQLBindCol(result->stmt, (SQLUSMALLINT)(i+1), SQL_C_CHAR, result->values[i].value,
987 							27, &result->values[i].vallen);
988 				break;
989 #endif /* HAVE_ADABAS */
990 			case SQL_CHAR:
991 			case SQL_VARCHAR:
992 #if defined(ODBCVER) && (ODBCVER >= 0x0300)
993 			case SQL_WCHAR:
994 			case SQL_WVARCHAR:
995 				colfieldid = SQL_DESC_OCTET_LENGTH;
996 #else
997 				charextraalloc = 1;
998 #endif
999 			default:
1000 				rc = PHP_ODBC_SQLCOLATTRIBUTE(result->stmt, (SQLUSMALLINT)(i+1), colfieldid,
1001 								NULL, 0, NULL, &displaysize);
1002 #if defined(ODBCVER) && (ODBCVER >= 0x0300)
1003 				if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO && colfieldid == SQL_DESC_OCTET_LENGTH) {
1004 					SQLINTEGER err;
1005 					SQLCHAR errtxt[128];
1006 					SQLCHAR state[6];
1007 
1008 					memset(errtxt, '\0', 128);
1009 					memset(state, '\0', 6);
1010 
1011 					if (SQL_SUCCESS == SQLGetDiagRec(SQL_HANDLE_STMT, result->stmt, 1, state, &err, errtxt, 128, NULL)) {
1012 						errtxt[127] = '\0';
1013 						state[5] = '\0';
1014 						php_error_docref(NULL, E_WARNING, "SQLColAttribute can't handle SQL_DESC_OCTET_LENGTH: [%s] %s", state, errtxt);
1015 					}
1016 					 /* This is  a quirk for ODBC 2.0 compatibility for broken driver implementations.
1017 					  */
1018 					charextraalloc = 1;
1019 					rc = SQLColAttributes(result->stmt, (SQLUSMALLINT)(i+1), SQL_COLUMN_DISPLAY_SIZE,
1020 								NULL, 0, NULL, &displaysize);
1021 				}
1022 
1023 				/* Workaround for drivers that report NVARCHAR(MAX) columns as SQL_WVARCHAR with size 0 (bug #69975) */
1024 				if (result->values[i].coltype == SQL_WVARCHAR && displaysize == 0) {
1025 					result->values[i].coltype = SQL_WLONGVARCHAR;
1026 					result->values[i].value = NULL;
1027 					break;
1028 				}
1029 #endif
1030 				/* Workaround for Oracle ODBC Driver bug (#50162) when fetching TIMESTAMP column */
1031 				if (result->values[i].coltype == SQL_TIMESTAMP) {
1032 					displaysize += 3;
1033 				}
1034 
1035 				if (charextraalloc) {
1036 					/* Since we don't know the exact # of bytes, allocate extra */
1037 					displaysize *= 4;
1038 				}
1039 				result->values[i].value = (char *)emalloc(displaysize + 1);
1040 				rc = SQLBindCol(result->stmt, (SQLUSMALLINT)(i+1), SQL_C_CHAR, result->values[i].value,
1041 							displaysize + 1, &result->values[i].vallen);
1042 				break;
1043 		}
1044 	}
1045 	return 1;
1046 }
1047 /* }}} */
1048 
1049 /* {{{ odbc_transact */
odbc_transact(INTERNAL_FUNCTION_PARAMETERS,int type)1050 void odbc_transact(INTERNAL_FUNCTION_PARAMETERS, int type)
1051 {
1052 	odbc_connection *conn;
1053 	RETCODE rc;
1054 	zval *pv_conn;
1055 
1056 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &pv_conn) == FAILURE) {
1057 		return;
1058 	}
1059 
1060 	if (!(conn = (odbc_connection *)zend_fetch_resource2(Z_RES_P(pv_conn), "ODBC-Link", le_conn, le_pconn))) {
1061 		RETURN_FALSE;
1062 	}
1063 
1064 	rc = SQLTransact(conn->henv, conn->hdbc, (SQLUSMALLINT)((type)?SQL_COMMIT:SQL_ROLLBACK));
1065 	if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
1066 		odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLTransact");
1067 		RETURN_FALSE;
1068 	}
1069 
1070 	RETURN_TRUE;
1071 }
1072 /* }}} */
1073 
1074 /* {{{ _close_pconn_with_res */
_close_pconn_with_res(zend_resource * le,zend_resource * res)1075 static int _close_pconn_with_res(zend_resource *le, zend_resource *res)
1076 {
1077 	if (le->type == le_pconn && (((odbc_connection *)(le->ptr))->res == res)){
1078 		return 1;
1079 	}else{
1080 		return 0;
1081 	}
1082 }
1083 /* }}} */
1084 
1085 /* {{{ odbc_column_lengths */
odbc_column_lengths(INTERNAL_FUNCTION_PARAMETERS,int type)1086 void odbc_column_lengths(INTERNAL_FUNCTION_PARAMETERS, int type)
1087 {
1088 	odbc_result *result;
1089 #if defined(HAVE_SOLID) || defined(HAVE_SOLID_30)
1090 	/* this seems to be necessary for Solid2.3 ( tested by
1091 	 * tammy@synchronis.com) and Solid 3.0 (tested by eric@terra.telemediair.nl)
1092 	 * Solid does not seem to declare a SQLINTEGER, but it does declare a
1093 	 * SQL_INTEGER which does not work (despite being the same type as a SDWORD.
1094 	 * Solid 3.5 does not have this issue.
1095 	 */
1096 	SDWORD len;
1097 #else
1098 	SQLLEN len;
1099 #endif
1100 	zval *pv_res;
1101 	zend_long pv_num;
1102 
1103 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rl", &pv_res, &pv_num) == FAILURE) {
1104 		return;
1105 	}
1106 
1107 	if ((result = (odbc_result *)zend_fetch_resource(Z_RES_P(pv_res), "ODBC result", le_result)) == NULL) {
1108 		RETURN_FALSE;
1109 	}
1110 
1111 	if (result->numcols == 0) {
1112 		php_error_docref(NULL, E_WARNING, "No tuples available at this result index");
1113 		RETURN_FALSE;
1114 	}
1115 
1116 	if (pv_num > result->numcols) {
1117 		php_error_docref(NULL, E_WARNING, "Field index larger than number of fields");
1118 		RETURN_FALSE;
1119 	}
1120 
1121 	if (pv_num < 1) {
1122 		php_error_docref(NULL, E_WARNING, "Field numbering starts at 1");
1123 		RETURN_FALSE;
1124 	}
1125 
1126 	PHP_ODBC_SQLCOLATTRIBUTE(result->stmt, (SQLUSMALLINT)pv_num, (SQLUSMALLINT) (type?SQL_COLUMN_SCALE:SQL_COLUMN_PRECISION), NULL, 0, NULL, &len);
1127 
1128 	RETURN_LONG(len);
1129 }
1130 /* }}} */
1131 
1132 /* Main User Functions */
1133 
1134 /* {{{ proto void odbc_close_all(void)
1135    Close all ODBC connections */
PHP_FUNCTION(odbc_close_all)1136 PHP_FUNCTION(odbc_close_all)
1137 {
1138 	zend_resource *p;
1139 
1140 	if (zend_parse_parameters_none() == FAILURE) {
1141 		return;
1142 	}
1143 
1144 	/* Loop through list and close all statements */
1145 	ZEND_HASH_FOREACH_PTR(&EG(regular_list), p) {
1146 		if (p->ptr && (p->type == le_result)) {
1147 			zend_list_close(p);
1148 		}
1149 	} ZEND_HASH_FOREACH_END();
1150 
1151 	/* Second loop through list, now close all connections */
1152 	ZEND_HASH_FOREACH_PTR(&EG(regular_list), p) {
1153 		if (p->ptr) {
1154 			if (p->type == le_conn){
1155 				zend_list_close(p);
1156 			} else if (p->type == le_pconn){
1157 				zend_list_close(p);
1158 				/* Delete the persistent connection */
1159 				zend_hash_apply_with_argument(&EG(persistent_list),
1160 					(apply_func_arg_t) _close_pconn_with_res, (void *)p);
1161 			}
1162 		}
1163 	} ZEND_HASH_FOREACH_END();
1164 }
1165 /* }}} */
1166 
1167 /* {{{ proto bool odbc_binmode(int result_id, int mode)
1168    Handle binary column data */
PHP_FUNCTION(odbc_binmode)1169 PHP_FUNCTION(odbc_binmode)
1170 {
1171 	php_odbc_fetch_attribs(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
1172 }
1173 /* }}} */
1174 
1175 /* {{{ proto bool odbc_longreadlen(int result_id, int length)
1176    Handle LONG columns */
PHP_FUNCTION(odbc_longreadlen)1177 PHP_FUNCTION(odbc_longreadlen)
1178 {
1179 	php_odbc_fetch_attribs(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
1180 }
1181 /* }}} */
1182 
1183 /* {{{ proto resource odbc_prepare(resource connection_id, string query)
1184    Prepares a statement for execution */
PHP_FUNCTION(odbc_prepare)1185 PHP_FUNCTION(odbc_prepare)
1186 {
1187 	zval *pv_conn;
1188 	char *query;
1189 	size_t query_len;
1190 	odbc_result *result = NULL;
1191 	odbc_connection *conn;
1192 	RETCODE rc;
1193 	int i;
1194 #ifdef HAVE_SQL_EXTENDED_FETCH
1195 	SQLUINTEGER      scrollopts;
1196 #endif
1197 
1198 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rs", &pv_conn, &query, &query_len) == FAILURE) {
1199 		return;
1200 	}
1201 
1202 	if (!(conn = (odbc_connection *)zend_fetch_resource2(Z_RES_P(pv_conn), "ODBC-Link", le_conn, le_pconn))) {
1203 		RETURN_FALSE;
1204 	}
1205 
1206 	result = (odbc_result *)ecalloc(1, sizeof(odbc_result));
1207 
1208 	result->numparams = 0;
1209 	result->param_info = NULL;
1210 
1211 	rc = PHP_ODBC_SQLALLOCSTMT(conn->hdbc, &(result->stmt));
1212 	if (rc == SQL_INVALID_HANDLE) {
1213 		efree(result);
1214 		php_error_docref(NULL, E_WARNING, "SQLAllocStmt error 'Invalid Handle'");
1215 		RETURN_FALSE;
1216 	}
1217 
1218 	if (rc == SQL_ERROR) {
1219 		odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLAllocStmt");
1220 		efree(result);
1221 		RETURN_FALSE;
1222 	}
1223 
1224 #ifdef HAVE_SQL_EXTENDED_FETCH
1225 	/* Solid doesn't have ExtendedFetch, if DriverManager is used, get Info,
1226 	   whether Driver supports ExtendedFetch */
1227 	rc = SQLGetInfo(conn->hdbc, SQL_FETCH_DIRECTION, (void *) &scrollopts, sizeof(scrollopts), NULL);
1228 	if (rc == SQL_SUCCESS) {
1229 		if ((result->fetch_abs = (scrollopts & SQL_FD_FETCH_ABSOLUTE))) {
1230 			/* Try to set CURSOR_TYPE to dynamic. Driver will replace this with other
1231 			   type if not possible.
1232 			*/
1233 			SQLSetStmtOption(result->stmt, SQL_CURSOR_TYPE, ODBCG(default_cursortype));
1234 		}
1235 	} else {
1236 		result->fetch_abs = 0;
1237 	}
1238 #endif
1239 
1240 	rc = SQLPrepare(result->stmt, query, SQL_NTS);
1241 	switch (rc) {
1242 		case SQL_SUCCESS:
1243 			break;
1244 		case SQL_SUCCESS_WITH_INFO:
1245 			odbc_sql_error(conn, result->stmt, "SQLPrepare");
1246 			break;
1247 		default:
1248 			odbc_sql_error(conn, result->stmt, "SQLPrepare");
1249 			RETURN_FALSE;
1250 	}
1251 
1252 	SQLNumParams(result->stmt, &(result->numparams));
1253 	SQLNumResultCols(result->stmt, &(result->numcols));
1254 
1255 	if (result->numcols > 0) {
1256 		if (!odbc_bindcols(result)) {
1257 			efree(result);
1258 			RETURN_FALSE;
1259 		}
1260 	} else {
1261 		result->values = NULL;
1262 	}
1263 	Z_ADDREF_P(pv_conn);
1264 	result->conn_ptr = conn;
1265 	result->fetched = 0;
1266 
1267 	result->param_info = (odbc_param_info *) safe_emalloc(sizeof(odbc_param_info), result->numparams, 0);
1268 	for (i=0;i<result->numparams;i++) {
1269 		rc = SQLDescribeParam(result->stmt, (SQLUSMALLINT)(i+1), &result->param_info[i].sqltype, &result->param_info[i].precision,
1270 													&result->param_info[i].scale, &result->param_info[i].nullable);
1271 		if (rc == SQL_ERROR) {
1272 			odbc_sql_error(result->conn_ptr, result->stmt, "SQLDescribeParameter");
1273 			SQLFreeStmt(result->stmt, SQL_RESET_PARAMS);
1274 			efree(result->param_info);
1275 			efree(result);
1276 			RETURN_FALSE;
1277 		}
1278 	}
1279 
1280 	RETURN_RES(zend_register_resource(result, le_result));
1281 }
1282 /* }}} */
1283 
1284 /*
1285  * Execute prepared SQL statement. Supports only input parameters.
1286  */
1287 
1288 /* {{{ proto bool odbc_execute(resource result_id [, array parameters_array])
1289    Execute a prepared statement */
PHP_FUNCTION(odbc_execute)1290 PHP_FUNCTION(odbc_execute)
1291 {
1292 	zval *pv_res, *pv_param_arr, *tmp;
1293 	typedef struct params_t {
1294 		SQLLEN vallen;
1295 		int fp;
1296 	} params_t;
1297 	params_t *params = NULL;
1298 	char *filename;
1299 	unsigned char otype;
1300 	SQLSMALLINT ctype;
1301    	odbc_result *result;
1302 	int numArgs, i, ne;
1303 	RETCODE rc;
1304 
1305 	numArgs = ZEND_NUM_ARGS();
1306 
1307 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "r|a", &pv_res, &pv_param_arr) == FAILURE) {
1308 		return;
1309 	}
1310 
1311 	if ((result = (odbc_result *)zend_fetch_resource(Z_RES_P(pv_res), "ODBC result", le_result)) == NULL) {
1312 		RETURN_FALSE;
1313 	}
1314 
1315 	/* XXX check for already bound parameters*/
1316 	if (result->numparams > 0 && numArgs == 1) {
1317 		php_error_docref(NULL, E_WARNING, "No parameters to SQL statement given");
1318 		RETURN_FALSE;
1319 	}
1320 
1321 	if (result->numparams > 0) {
1322 		if ((ne = zend_hash_num_elements(Z_ARRVAL_P(pv_param_arr))) < result->numparams) {
1323 			php_error_docref(NULL, E_WARNING,"Not enough parameters (%d should be %d) given", ne, result->numparams);
1324 			RETURN_FALSE;
1325 		}
1326 
1327 		zend_hash_internal_pointer_reset(Z_ARRVAL_P(pv_param_arr));
1328 		params = (params_t *)safe_emalloc(sizeof(params_t), result->numparams, 0);
1329 		for(i = 0; i < result->numparams; i++) {
1330 			params[i].fp = -1;
1331 		}
1332 
1333 		for(i = 1; i <= result->numparams; i++) {
1334 			if ((tmp = zend_hash_get_current_data(Z_ARRVAL_P(pv_param_arr))) == NULL) {
1335 				php_error_docref(NULL, E_WARNING,"Error getting parameter");
1336 				SQLFreeStmt(result->stmt,SQL_RESET_PARAMS);
1337 				for (i = 0; i < result->numparams; i++) {
1338 					if (params[i].fp != -1) {
1339 						close(params[i].fp);
1340 					}
1341 				}
1342 				efree(params);
1343 				RETURN_FALSE;
1344 			}
1345 
1346 			otype = Z_TYPE_P(tmp);
1347 			convert_to_string_ex(tmp);
1348 			if (Z_TYPE_P(tmp) != IS_STRING) {
1349 				php_error_docref(NULL, E_WARNING,"Error converting parameter");
1350 				SQLFreeStmt(result->stmt, SQL_RESET_PARAMS);
1351 				for (i = 0; i < result->numparams; i++) {
1352 					if (params[i].fp != -1) {
1353 						close(params[i].fp);
1354 					}
1355 				}
1356 				efree(params);
1357 				RETURN_FALSE;
1358 			}
1359 
1360 			params[i-1].vallen = Z_STRLEN_P(tmp);
1361 			params[i-1].fp = -1;
1362 
1363 			if (IS_SQL_BINARY(result->param_info[i-1].sqltype)) {
1364 				ctype = SQL_C_BINARY;
1365 			} else {
1366 				ctype = SQL_C_CHAR;
1367 			}
1368 
1369 			if (Z_STRLEN_P(tmp) > 2 &&
1370 				Z_STRVAL_P(tmp)[0] == '\'' &&
1371 				Z_STRVAL_P(tmp)[Z_STRLEN_P(tmp) - 1] == '\'') {
1372 
1373 				if (CHECK_ZVAL_NULL_PATH(tmp)) {
1374 					RETURN_FALSE;
1375 				}
1376 				filename = estrndup(&Z_STRVAL_P(tmp)[1], Z_STRLEN_P(tmp) - 2);
1377 				filename[strlen(filename)] = '\0';
1378 
1379 				/* Check the basedir */
1380 				if (php_check_open_basedir(filename)) {
1381 					efree(filename);
1382 					SQLFreeStmt(result->stmt, SQL_RESET_PARAMS);
1383 					for (i = 0; i < result->numparams; i++) {
1384 						if (params[i].fp != -1) {
1385 							close(params[i].fp);
1386 						}
1387 					}
1388 					efree(params);
1389 					RETURN_FALSE;
1390 				}
1391 
1392 				if ((params[i-1].fp = open(filename,O_RDONLY)) == -1) {
1393 					php_error_docref(NULL, E_WARNING,"Can't open file %s", filename);
1394 					SQLFreeStmt(result->stmt, SQL_RESET_PARAMS);
1395 					for (i = 0; i < result->numparams; i++) {
1396 						if (params[i].fp != -1) {
1397 							close(params[i].fp);
1398 						}
1399 					}
1400 					efree(params);
1401 					efree(filename);
1402 					RETURN_FALSE;
1403 				}
1404 
1405 				efree(filename);
1406 
1407 				params[i-1].vallen = SQL_LEN_DATA_AT_EXEC(0);
1408 
1409 				rc = SQLBindParameter(result->stmt, (SQLUSMALLINT)i, SQL_PARAM_INPUT,
1410 									  ctype, result->param_info[i-1].sqltype, result->param_info[i-1].precision, result->param_info[i-1].scale,
1411 									  (void *)(intptr_t)params[i-1].fp, 0,
1412 									  &params[i-1].vallen);
1413 			} else {
1414 #ifdef HAVE_DBMAKER
1415 				precision = params[i-1].vallen;
1416 #endif
1417 				if (otype == IS_NULL) {
1418 					params[i-1].vallen = SQL_NULL_DATA;
1419 				}
1420 
1421 				rc = SQLBindParameter(result->stmt, (SQLUSMALLINT)i, SQL_PARAM_INPUT,
1422 									  ctype, result->param_info[i-1].sqltype, result->param_info[i-1].precision, result->param_info[i-1].scale,
1423 									  Z_STRVAL_P(tmp), 0,
1424 									  &params[i-1].vallen);
1425 			}
1426 			if (rc == SQL_ERROR) {
1427 				odbc_sql_error(result->conn_ptr, result->stmt, "SQLBindParameter");
1428 				SQLFreeStmt(result->stmt, SQL_RESET_PARAMS);
1429 				for (i = 0; i < result->numparams; i++) {
1430 					if (params[i].fp != -1) {
1431 						close(params[i].fp);
1432 					}
1433 				}
1434 				efree(params);
1435 				RETURN_FALSE;
1436 			}
1437 			zend_hash_move_forward(Z_ARRVAL_P(pv_param_arr));
1438 		}
1439 	}
1440 	/* Close cursor, needed for doing multiple selects */
1441 	rc = SQLFreeStmt(result->stmt, SQL_CLOSE);
1442 
1443 	if (rc == SQL_ERROR) {
1444 		odbc_sql_error(result->conn_ptr, result->stmt, "SQLFreeStmt");
1445 	}
1446 
1447 	rc = SQLExecute(result->stmt);
1448 
1449 	result->fetched = 0;
1450 	if (rc == SQL_NEED_DATA) {
1451 		char buf[4096];
1452 		int fp, nbytes;
1453 		while (rc == SQL_NEED_DATA) {
1454 			rc = SQLParamData(result->stmt, (void*)&fp);
1455 			if (rc == SQL_NEED_DATA) {
1456 				while ((nbytes = read(fp, &buf, 4096)) > 0) {
1457 					SQLPutData(result->stmt, (void*)&buf, nbytes);
1458 				}
1459 			}
1460 		}
1461 	} else {
1462 		switch (rc) {
1463 			case SQL_SUCCESS:
1464 				break;
1465 			case SQL_NO_DATA_FOUND:
1466 			case SQL_SUCCESS_WITH_INFO:
1467 				odbc_sql_error(result->conn_ptr, result->stmt, "SQLExecute");
1468 				break;
1469 			default:
1470 				odbc_sql_error(result->conn_ptr, result->stmt, "SQLExecute");
1471 				RETVAL_FALSE;
1472 		}
1473 	}
1474 
1475 	if (result->numparams > 0) {
1476 		SQLFreeStmt(result->stmt, SQL_RESET_PARAMS);
1477 		for(i = 0; i < result->numparams; i++) {
1478 			if (params[i].fp != -1) {
1479 				close(params[i].fp);
1480 			}
1481 		}
1482 		efree(params);
1483 	}
1484 
1485 	if (rc == SQL_SUCCESS || rc == SQL_SUCCESS_WITH_INFO || rc == SQL_NO_DATA_FOUND) {
1486 		RETVAL_TRUE;
1487 	}
1488 
1489 	if (result->numcols == 0) {
1490 		SQLNumResultCols(result->stmt, &(result->numcols));
1491 
1492 		if (result->numcols > 0) {
1493 			if (!odbc_bindcols(result)) {
1494 				efree(result);
1495 				RETVAL_FALSE;
1496 			}
1497 		} else {
1498 			result->values = NULL;
1499 		}
1500 	}
1501 }
1502 /* }}} */
1503 
1504 /* {{{ proto string odbc_cursor(resource result_id)
1505    Get cursor name */
PHP_FUNCTION(odbc_cursor)1506 PHP_FUNCTION(odbc_cursor)
1507 {
1508 	zval *pv_res;
1509 	SQLUSMALLINT max_len;
1510 	SQLSMALLINT len;
1511 	char *cursorname;
1512    	odbc_result *result;
1513 	RETCODE rc;
1514 
1515 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &pv_res) == FAILURE) {
1516 		return;
1517 	}
1518 
1519 	if ((result = (odbc_result *)zend_fetch_resource(Z_RES_P(pv_res), "ODBC result", le_result)) == NULL) {
1520 		RETURN_FALSE;
1521 	}
1522 
1523 	rc = SQLGetInfo(result->conn_ptr->hdbc,SQL_MAX_CURSOR_NAME_LEN, (void *)&max_len,sizeof(max_len),&len);
1524 	if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
1525 		RETURN_FALSE;
1526 	}
1527 
1528 	if (max_len > 0) {
1529 		cursorname = emalloc(max_len + 1);
1530 		rc = SQLGetCursorName(result->stmt,cursorname,(SQLSMALLINT)max_len,&len);
1531 		if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
1532 			char        state[6];     /* Not used */
1533 	 		SQLINTEGER  error;        /* Not used */
1534 			char        errormsg[SQL_MAX_MESSAGE_LENGTH];
1535 			SQLSMALLINT errormsgsize; /* Not used */
1536 
1537 			SQLError( result->conn_ptr->henv, result->conn_ptr->hdbc,
1538 						result->stmt, state, &error, errormsg,
1539 						sizeof(errormsg)-1, &errormsgsize);
1540 			if (!strncmp(state,"S1015",5)) {
1541 				snprintf(cursorname, max_len+1, "php_curs_" ZEND_ULONG_FMT, (zend_ulong)result->stmt);
1542 				if (SQLSetCursorName(result->stmt,cursorname,SQL_NTS) != SQL_SUCCESS) {
1543 					odbc_sql_error(result->conn_ptr, result->stmt, "SQLSetCursorName");
1544 					RETVAL_FALSE;
1545 				} else {
1546 					RETVAL_STRING(cursorname);
1547 				}
1548 			} else {
1549 				php_error_docref(NULL, E_WARNING, "SQL error: %s, SQL state %s", errormsg, state);
1550 				RETVAL_FALSE;
1551 			}
1552 		} else {
1553 			RETVAL_STRING(cursorname);
1554 		}
1555 		efree(cursorname);
1556 	} else {
1557 		RETVAL_FALSE;
1558 	}
1559 }
1560 /* }}} */
1561 
1562 #ifdef HAVE_SQLDATASOURCES
1563 /* {{{ proto array odbc_data_source(resource connection_id, int fetch_type)
1564    Return information about the currently connected data source */
PHP_FUNCTION(odbc_data_source)1565 PHP_FUNCTION(odbc_data_source)
1566 {
1567 	zval *zv_conn;
1568 	zend_long zv_fetch_type;
1569 	RETCODE rc = 0; /* assume all is good */
1570 	odbc_connection *conn;
1571 	UCHAR server_name[100], desc[200];
1572 	SQLSMALLINT len1=0, len2=0, fetch_type;
1573 
1574 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rl", &zv_conn, &zv_fetch_type) == FAILURE) {
1575 		return;
1576 	}
1577 
1578 	fetch_type = (SQLSMALLINT) zv_fetch_type;
1579 
1580 	if (!(fetch_type == SQL_FETCH_FIRST || fetch_type == SQL_FETCH_NEXT)) {
1581 		php_error_docref(NULL, E_WARNING, "Invalid fetch type (%d)", fetch_type);
1582 		RETURN_FALSE;
1583 	}
1584 
1585 	if (!(conn = (odbc_connection *)zend_fetch_resource2(Z_RES_P(zv_conn), "ODBC-Link", le_conn, le_pconn))) {
1586 		RETURN_FALSE;
1587 	}
1588 
1589 	/* now we have the "connection" lets call the DataSource object */
1590 	rc = SQLDataSources(conn->henv,
1591 			fetch_type,
1592 			server_name,
1593 			(SQLSMALLINT)sizeof(server_name),
1594 			&len1,
1595 			desc,
1596 			(SQLSMALLINT)sizeof(desc),
1597 			&len2);
1598 
1599 	if (rc != SQL_SUCCESS) {
1600 		/* ummm.... he did it */
1601 		odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLDataSources");
1602 		RETURN_FALSE;
1603 	}
1604 
1605 	if (len1 == 0 || len2 == 0) {
1606 		/* we have a non-valid entry... so stop the looping */
1607 		RETURN_FALSE;
1608 	}
1609 
1610 	array_init(return_value);
1611 
1612 	add_assoc_string_ex(return_value, "server", sizeof("server")-1, server_name);
1613 	add_assoc_string_ex(return_value, "description", sizeof("description")-1, desc);
1614 
1615 }
1616 /* }}} */
1617 #endif /* HAVE_SQLDATASOURCES */
1618 
1619 /* {{{ proto resource odbc_exec(resource connection_id, string query [, int flags])
1620    Prepare and execute an SQL statement */
1621 /* XXX Use flags */
PHP_FUNCTION(odbc_exec)1622 PHP_FUNCTION(odbc_exec)
1623 {
1624 	zval *pv_conn;
1625 	zend_long pv_flags;
1626 	int numArgs;
1627 	char *query;
1628 	size_t query_len;
1629 	odbc_result *result = NULL;
1630 	odbc_connection *conn;
1631 	RETCODE rc;
1632 #ifdef HAVE_SQL_EXTENDED_FETCH
1633 	SQLUINTEGER      scrollopts;
1634 #endif
1635 
1636 	numArgs = ZEND_NUM_ARGS();
1637 
1638 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rs|l", &pv_conn, &query, &query_len, &pv_flags) == FAILURE) {
1639 		return;
1640 	}
1641 
1642 	if (!(conn = (odbc_connection *)zend_fetch_resource2(Z_RES_P(pv_conn), "ODBC-Link", le_conn, le_pconn))) {
1643 		RETURN_FALSE;
1644 	}
1645 
1646 	result = (odbc_result *)ecalloc(1, sizeof(odbc_result));
1647 
1648 	rc = PHP_ODBC_SQLALLOCSTMT(conn->hdbc, &(result->stmt));
1649 	if (rc == SQL_INVALID_HANDLE) {
1650 		php_error_docref(NULL, E_WARNING, "SQLAllocStmt error 'Invalid Handle'");
1651 		efree(result);
1652 		RETURN_FALSE;
1653 	}
1654 
1655 	if (rc == SQL_ERROR) {
1656 		odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLAllocStmt");
1657 		efree(result);
1658 		RETURN_FALSE;
1659 	}
1660 
1661 #ifdef HAVE_SQL_EXTENDED_FETCH
1662 	/* Solid doesn't have ExtendedFetch, if DriverManager is used, get Info,
1663 	   whether Driver supports ExtendedFetch */
1664 	rc = SQLGetInfo(conn->hdbc, SQL_FETCH_DIRECTION, (void *) &scrollopts, sizeof(scrollopts), NULL);
1665 	if (rc == SQL_SUCCESS) {
1666 		if ((result->fetch_abs = (scrollopts & SQL_FD_FETCH_ABSOLUTE))) {
1667 			/* Try to set CURSOR_TYPE to dynamic. Driver will replace this with other
1668 			   type if not possible.
1669 			 */
1670 			SQLSetStmtOption(result->stmt, SQL_CURSOR_TYPE, ODBCG(default_cursortype));
1671 		}
1672 	} else {
1673 		result->fetch_abs = 0;
1674 	}
1675 #endif
1676 
1677 	rc = SQLExecDirect(result->stmt, query, SQL_NTS);
1678 	if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO && rc != SQL_NO_DATA_FOUND) {
1679 		/* XXX FIXME we should really check out SQLSTATE with SQLError
1680 		 * in case rc is SQL_SUCCESS_WITH_INFO here.
1681 		 */
1682 		odbc_sql_error(conn, result->stmt, "SQLExecDirect");
1683 		SQLFreeStmt(result->stmt, SQL_DROP);
1684 		efree(result);
1685 		RETURN_FALSE;
1686 	}
1687 
1688 	SQLNumResultCols(result->stmt, &(result->numcols));
1689 
1690 	/* For insert, update etc. cols == 0 */
1691 	if (result->numcols > 0) {
1692 		if (!odbc_bindcols(result)) {
1693 			efree(result);
1694 			RETURN_FALSE;
1695 		}
1696 	} else {
1697 		result->values = NULL;
1698 	}
1699 	Z_ADDREF_P(pv_conn);
1700 	result->conn_ptr = conn;
1701 	result->fetched = 0;
1702 	RETURN_RES(zend_register_resource(result, le_result));
1703 }
1704 /* }}} */
1705 
1706 #ifdef PHP_ODBC_HAVE_FETCH_HASH
1707 #define ODBC_NUM  1
1708 #define ODBC_OBJECT  2
1709 
1710 /* {{{ php_odbc_fetch_hash */
php_odbc_fetch_hash(INTERNAL_FUNCTION_PARAMETERS,int result_type)1711 static void php_odbc_fetch_hash(INTERNAL_FUNCTION_PARAMETERS, int result_type)
1712 {
1713 	int i;
1714 	odbc_result *result;
1715 	RETCODE rc;
1716 	SQLSMALLINT sql_c_type;
1717 	char *buf = NULL;
1718 #ifdef HAVE_SQL_EXTENDED_FETCH
1719 	SQLULEN crow;
1720 	SQLUSMALLINT RowStatus[1];
1721 	SQLLEN rownum;
1722 	zval *pv_res, tmp;
1723 	zend_long pv_row = -1;
1724 
1725 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "r|l", &pv_res, &pv_row) == FAILURE) {
1726 		return;
1727 	}
1728 
1729 	rownum = pv_row;
1730 #else
1731 	zval *pv_res, tmp;
1732 
1733 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &pv_res) == FAILURE) {
1734 		return;
1735 	}
1736 #endif
1737 
1738 	if ((result = (odbc_result *)zend_fetch_resource(Z_RES_P(pv_res), "ODBC result", le_result)) == NULL) {
1739 		RETURN_FALSE;
1740 	}
1741 
1742 	if (result->numcols == 0) {
1743 		php_error_docref(NULL, E_WARNING, "No tuples available at this result index");
1744 		RETURN_FALSE;
1745 	}
1746 
1747 #ifdef HAVE_SQL_EXTENDED_FETCH
1748 	if (result->fetch_abs) {
1749 		if (rownum > 0) {
1750 			rc = SQLExtendedFetch(result->stmt,SQL_FETCH_ABSOLUTE,rownum,&crow,RowStatus);
1751 		} else {
1752 			rc = SQLExtendedFetch(result->stmt,SQL_FETCH_NEXT,1,&crow,RowStatus);
1753 		}
1754 	} else
1755 #endif
1756 	rc = SQLFetch(result->stmt);
1757 
1758 	if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
1759 		RETURN_FALSE;
1760 	}
1761 
1762 	array_init(return_value);
1763 
1764 #ifdef HAVE_SQL_EXTENDED_FETCH
1765 	if (rownum > 0 && result->fetch_abs)
1766 		result->fetched = rownum;
1767 	else
1768 #endif
1769 		result->fetched++;
1770 
1771 	for(i = 0; i < result->numcols; i++) {
1772 		sql_c_type = SQL_C_CHAR;
1773 
1774 		switch(result->values[i].coltype) {
1775 			case SQL_BINARY:
1776 			case SQL_VARBINARY:
1777 			case SQL_LONGVARBINARY:
1778 				if (result->binmode <= 0) {
1779 					ZVAL_EMPTY_STRING(&tmp);
1780 					break;
1781 				}
1782 				if (result->binmode == 1) {
1783 					sql_c_type = SQL_C_BINARY;
1784 				}
1785 			case SQL_LONGVARCHAR:
1786 #if defined(ODBCVER) && (ODBCVER >= 0x0300)
1787 			case SQL_WLONGVARCHAR:
1788 #endif
1789 				if (IS_SQL_LONG(result->values[i].coltype) && result->longreadlen <= 0) {
1790 					ZVAL_EMPTY_STRING(&tmp);
1791 					break;
1792 				}
1793 				if (buf == NULL) {
1794 					buf = emalloc(result->longreadlen + 1);
1795 				}
1796 
1797 				rc = SQLGetData(result->stmt, (SQLUSMALLINT)(i + 1), sql_c_type, buf, result->longreadlen + 1, &result->values[i].vallen);
1798 
1799 				if (rc == SQL_ERROR) {
1800 					odbc_sql_error(result->conn_ptr, result->stmt, "SQLGetData");
1801 					efree(buf);
1802 					RETURN_FALSE;
1803 				}
1804 
1805 				if (rc == SQL_SUCCESS_WITH_INFO) {
1806 					ZVAL_STRINGL(&tmp, buf, result->longreadlen);
1807 				} else if (result->values[i].vallen == SQL_NULL_DATA) {
1808 					ZVAL_NULL(&tmp);
1809 					break;
1810 				} else {
1811 					ZVAL_STRINGL(&tmp, buf, result->values[i].vallen);
1812 				}
1813 				break;
1814 
1815 			default:
1816 				if (result->values[i].vallen == SQL_NULL_DATA) {
1817 					ZVAL_NULL(&tmp);
1818 					break;
1819 				}
1820 				ZVAL_STRINGL(&tmp, result->values[i].value, result->values[i].vallen);
1821 				break;
1822 		}
1823 
1824 		if (result_type & ODBC_NUM) {
1825 			zend_hash_index_update(Z_ARRVAL_P(return_value), i, &tmp);
1826 		} else {
1827 			if (!*(result->values[i].name) && Z_TYPE(tmp) == IS_STRING) {
1828 				zend_hash_update(Z_ARRVAL_P(return_value), Z_STR(tmp), &tmp);
1829 			} else {
1830 				zend_hash_str_update(Z_ARRVAL_P(return_value), result->values[i].name, strlen(result->values[i].name), &tmp);
1831 			}
1832 		}
1833 	}
1834 	if (buf) {
1835 		efree(buf);
1836 	}
1837 }
1838 /* }}} */
1839 
1840 
1841 /* {{{ proto object odbc_fetch_object(int result [, int rownumber])
1842    Fetch a result row as an object */
PHP_FUNCTION(odbc_fetch_object)1843 PHP_FUNCTION(odbc_fetch_object)
1844 {
1845 	php_odbc_fetch_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, ODBC_OBJECT);
1846 	if (Z_TYPE_P(return_value) == IS_ARRAY) {
1847 		object_and_properties_init(return_value, ZEND_STANDARD_CLASS_DEF_PTR, Z_ARRVAL_P(return_value));
1848 	}
1849 }
1850 /* }}} */
1851 
1852 /* {{{ proto array odbc_fetch_array(int result [, int rownumber])
1853    Fetch a result row as an associative array */
PHP_FUNCTION(odbc_fetch_array)1854 PHP_FUNCTION(odbc_fetch_array)
1855 {
1856 	php_odbc_fetch_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, ODBC_OBJECT);
1857 }
1858 /* }}} */
1859 #endif
1860 
1861 /* {{{ proto int odbc_fetch_into(resource result_id, array &result_array [, int rownumber])
1862    Fetch one result row into an array */
PHP_FUNCTION(odbc_fetch_into)1863 PHP_FUNCTION(odbc_fetch_into)
1864 {
1865 	int i;
1866 	odbc_result *result;
1867 	RETCODE rc;
1868 	SQLSMALLINT sql_c_type;
1869 	char *buf = NULL;
1870 	zval *pv_res, *pv_res_arr, tmp;
1871 #ifdef HAVE_SQL_EXTENDED_FETCH
1872 	zend_long pv_row = 0;
1873 	SQLULEN crow;
1874 	SQLUSMALLINT RowStatus[1];
1875 	SQLLEN rownum = -1;
1876 #endif /* HAVE_SQL_EXTENDED_FETCH */
1877 
1878 #ifdef HAVE_SQL_EXTENDED_FETCH
1879 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rz/|l", &pv_res, &pv_res_arr, &pv_row) == FAILURE) {
1880 		return;
1881 	}
1882 
1883 	rownum = pv_row;
1884 #else
1885 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rz/", &pv_res, &pv_res_arr) == FAILURE) {
1886 		return;
1887 	}
1888 #endif /* HAVE_SQL_EXTENDED_FETCH */
1889 
1890 	if ((result = (odbc_result *)zend_fetch_resource(Z_RES_P(pv_res), "ODBC result", le_result)) == NULL) {
1891 		RETURN_FALSE;
1892 	}
1893 
1894 	if (result->numcols == 0) {
1895 		php_error_docref(NULL, E_WARNING, "No tuples available at this result index");
1896 		RETURN_FALSE;
1897 	}
1898 
1899 	if (Z_TYPE_P(pv_res_arr) != IS_ARRAY) {
1900 		array_init(pv_res_arr);
1901 	}
1902 
1903 #ifdef HAVE_SQL_EXTENDED_FETCH
1904 	if (result->fetch_abs) {
1905 		if (rownum > 0) {
1906 			rc = SQLExtendedFetch(result->stmt,SQL_FETCH_ABSOLUTE,rownum,&crow,RowStatus);
1907 		} else {
1908 			rc = SQLExtendedFetch(result->stmt,SQL_FETCH_NEXT,1,&crow,RowStatus);
1909 		}
1910 	} else
1911 #endif
1912 		rc = SQLFetch(result->stmt);
1913 
1914 	if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
1915 		RETURN_FALSE;
1916 	}
1917 
1918 #ifdef HAVE_SQL_EXTENDED_FETCH
1919 	if (rownum > 0 && result->fetch_abs)
1920 		result->fetched = rownum;
1921 	else
1922 #endif
1923 		result->fetched++;
1924 
1925 	for(i = 0; i < result->numcols; i++) {
1926 		sql_c_type = SQL_C_CHAR;
1927 
1928 		switch(result->values[i].coltype) {
1929 			case SQL_BINARY:
1930 			case SQL_VARBINARY:
1931 			case SQL_LONGVARBINARY:
1932 				if (result->binmode <= 0) {
1933 					ZVAL_EMPTY_STRING(&tmp);
1934 					break;
1935 				}
1936 				if (result->binmode == 1) sql_c_type = SQL_C_BINARY;
1937 
1938 			case SQL_LONGVARCHAR:
1939 #if defined(ODBCVER) && (ODBCVER >= 0x0300)
1940 			case SQL_WLONGVARCHAR:
1941 #endif
1942 				if (IS_SQL_LONG(result->values[i].coltype) && result->longreadlen <= 0) {
1943 					ZVAL_EMPTY_STRING(&tmp);
1944 					break;
1945 				}
1946 
1947 				if (buf == NULL) {
1948 					buf = emalloc(result->longreadlen + 1);
1949 				}
1950 				rc = SQLGetData(result->stmt, (SQLUSMALLINT)(i + 1),sql_c_type, buf, result->longreadlen + 1, &result->values[i].vallen);
1951 
1952 				if (rc == SQL_ERROR) {
1953 					odbc_sql_error(result->conn_ptr, result->stmt, "SQLGetData");
1954 					efree(buf);
1955 					RETURN_FALSE;
1956 				}
1957 				if (rc == SQL_SUCCESS_WITH_INFO) {
1958 					ZVAL_STRINGL(&tmp, buf, result->longreadlen);
1959 				} else if (result->values[i].vallen == SQL_NULL_DATA) {
1960 					ZVAL_NULL(&tmp);
1961 					break;
1962 				} else {
1963 					ZVAL_STRINGL(&tmp, buf, result->values[i].vallen);
1964 				}
1965 				break;
1966 
1967 			default:
1968 				if (result->values[i].vallen == SQL_NULL_DATA) {
1969 					ZVAL_NULL(&tmp);
1970 					break;
1971 				}
1972 				ZVAL_STRINGL(&tmp, result->values[i].value, result->values[i].vallen);
1973 				break;
1974 		}
1975 		zend_hash_index_update(Z_ARRVAL_P(pv_res_arr), i, &tmp);
1976 	}
1977 	if (buf) efree(buf);
1978 	RETURN_LONG(result->numcols);
1979 }
1980 /* }}} */
1981 
1982 /* {{{ proto bool solid_fetch_prev(resource result_id)
1983    */
1984 #if defined(HAVE_SOLID) || defined(HAVE_SOLID_30) || defined(HAVE_SOLID_35)
PHP_FUNCTION(solid_fetch_prev)1985 PHP_FUNCTION(solid_fetch_prev)
1986 {
1987 	odbc_result *result;
1988 	RETCODE rc;
1989 	zval *pv_res;
1990 
1991 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &pv_res) == FAILURE) {
1992 		return;
1993 	}
1994 
1995 	if ((result = (odbc_result *)zend_fetch_resource(Z_RES_P(pv_res), "ODBC result", le_result)) == NULL) {
1996 		RETURN_FALSE;
1997 	}
1998 	if (result->numcols == 0) {
1999 		php_error_docref(NULL, E_WARNING, "No tuples available at this result index");
2000 		RETURN_FALSE;
2001 	}
2002 	rc = SQLFetchPrev(result->stmt);
2003 
2004 	if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
2005 		RETURN_FALSE;
2006 	}
2007 
2008 	if (result->fetched > 1) {
2009 		result->fetched--;
2010 	}
2011 
2012 	RETURN_TRUE;
2013 }
2014 #endif
2015 /* }}} */
2016 
2017 /* {{{ proto bool odbc_fetch_row(resource result_id [, int row_number])
2018    Fetch a row */
PHP_FUNCTION(odbc_fetch_row)2019 PHP_FUNCTION(odbc_fetch_row)
2020 {
2021 	SQLLEN rownum;
2022 	odbc_result *result;
2023 	RETCODE rc;
2024 	zval *pv_res;
2025 	zend_long pv_row = 1;
2026 #ifdef HAVE_SQL_EXTENDED_FETCH
2027 	SQLULEN crow;
2028 	SQLUSMALLINT RowStatus[1];
2029 #endif
2030 
2031 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "r|l", &pv_res, &pv_row) == FAILURE) {
2032 		return;
2033 	}
2034 
2035 	rownum = pv_row;
2036 
2037 	if ((result = (odbc_result *)zend_fetch_resource(Z_RES_P(pv_res), "ODBC result", le_result)) == NULL) {
2038 		RETURN_FALSE;
2039 	}
2040 
2041 	if (result->numcols == 0) {
2042 		php_error_docref(NULL, E_WARNING, "No tuples available at this result index");
2043 		RETURN_FALSE;
2044 	}
2045 
2046 #ifdef HAVE_SQL_EXTENDED_FETCH
2047 	if (result->fetch_abs) {
2048 		if (ZEND_NUM_ARGS() > 1) {
2049 			rc = SQLExtendedFetch(result->stmt,SQL_FETCH_ABSOLUTE,rownum,&crow,RowStatus);
2050 		} else {
2051 			rc = SQLExtendedFetch(result->stmt,SQL_FETCH_NEXT,1,&crow,RowStatus);
2052 		}
2053 	} else
2054 #endif
2055 		rc = SQLFetch(result->stmt);
2056 
2057 	if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
2058 		RETURN_FALSE;
2059 	}
2060 
2061 	if (ZEND_NUM_ARGS() > 1) {
2062 		result->fetched = rownum;
2063 	} else {
2064 		result->fetched++;
2065 	}
2066 
2067 	RETURN_TRUE;
2068 }
2069 /* }}} */
2070 
2071 /* {{{ proto mixed odbc_result(resource result_id, mixed field)
2072    Get result data */
PHP_FUNCTION(odbc_result)2073 PHP_FUNCTION(odbc_result)
2074 {
2075 	char *field;
2076 	zend_string *field_str;
2077 	int field_ind;
2078 	SQLSMALLINT sql_c_type = SQL_C_CHAR;
2079 	odbc_result *result;
2080 	int i = 0;
2081 	RETCODE rc;
2082 	SQLLEN	fieldsize;
2083 	zval *pv_res, *pv_field;
2084 #ifdef HAVE_SQL_EXTENDED_FETCH
2085 	SQLULEN crow;
2086 	SQLUSMALLINT RowStatus[1];
2087 #endif
2088 
2089 	field_ind = -1;
2090 	field = NULL;
2091 
2092 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rz", &pv_res, &pv_field) == FAILURE) {
2093 		return;
2094 	}
2095 
2096 	if (Z_TYPE_P(pv_field) == IS_STRING) {
2097 		field = Z_STRVAL_P(pv_field);
2098 	} else {
2099 		convert_to_long_ex(pv_field);
2100 		field_ind = Z_LVAL_P(pv_field) - 1;
2101 	}
2102 
2103 	if ((result = (odbc_result *)zend_fetch_resource(Z_RES_P(pv_res), "ODBC result", le_result)) == NULL) {
2104 		RETURN_FALSE;
2105 	}
2106 
2107 	if ((result->numcols == 0)) {
2108 		php_error_docref(NULL, E_WARNING, "No tuples available at this result index");
2109 		RETURN_FALSE;
2110 	}
2111 
2112 	/* get field index if the field parameter was a string */
2113 	if (field != NULL) {
2114 		if (result->values == NULL) {
2115 			php_error_docref(NULL, E_WARNING, "Result set contains no data");
2116 			RETURN_FALSE;
2117 		}
2118 
2119 		for(i = 0; i < result->numcols; i++) {
2120 			if (!strcasecmp(result->values[i].name, field)) {
2121 				field_ind = i;
2122 				break;
2123 			}
2124 		}
2125 
2126 		if (field_ind < 0) {
2127 			php_error_docref(NULL, E_WARNING, "Field %s not found", field);
2128 			RETURN_FALSE;
2129 		}
2130 	} else {
2131 		/* check for limits of field_ind if the field parameter was an int */
2132 		if (field_ind >= result->numcols || field_ind < 0) {
2133 			php_error_docref(NULL, E_WARNING, "Field index is larger than the number of fields");
2134 			RETURN_FALSE;
2135 		}
2136 	}
2137 
2138 	if (result->fetched == 0) {
2139 		/* User forgot to call odbc_fetch_row(), or wants to reload the results, do it now */
2140 #ifdef HAVE_SQL_EXTENDED_FETCH
2141 		if (result->fetch_abs)
2142 			rc = SQLExtendedFetch(result->stmt, SQL_FETCH_NEXT, 1, &crow,RowStatus);
2143 		else
2144 #endif
2145 			rc = SQLFetch(result->stmt);
2146 
2147 		if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
2148 			RETURN_FALSE;
2149 		}
2150 
2151 		result->fetched++;
2152 	}
2153 
2154 	switch(result->values[field_ind].coltype) {
2155 		case SQL_BINARY:
2156 		case SQL_VARBINARY:
2157 		case SQL_LONGVARBINARY:
2158 			if (result->binmode <= 1) {
2159 				sql_c_type = SQL_C_BINARY;
2160 			}
2161 			if (result->binmode <= 0) {
2162 				break;
2163 			}
2164 		case SQL_LONGVARCHAR:
2165 #if defined(ODBCVER) && (ODBCVER >= 0x0300)
2166 		case SQL_WLONGVARCHAR:
2167 #endif
2168 			if (IS_SQL_LONG(result->values[field_ind].coltype)) {
2169 				if (result->longreadlen <= 0) {
2170 				   break;
2171 				} else {
2172 				   fieldsize = result->longreadlen;
2173 				}
2174 			} else {
2175 			   PHP_ODBC_SQLCOLATTRIBUTE(result->stmt, (SQLUSMALLINT)(field_ind + 1),
2176 					   			(SQLUSMALLINT)((sql_c_type == SQL_C_BINARY) ? SQL_COLUMN_LENGTH :
2177 					   			SQL_COLUMN_DISPLAY_SIZE),
2178 					   			NULL, 0, NULL, &fieldsize);
2179 			}
2180 			/* For char data, the length of the returned string will be longreadlen - 1 */
2181 			fieldsize = (result->longreadlen <= 0) ? 4096 : result->longreadlen;
2182 			field_str = zend_string_alloc(fieldsize, 0);
2183 
2184 		/* SQLGetData will truncate CHAR data to fieldsize - 1 bytes and append \0.
2185 		 * For binary data it is truncated to fieldsize bytes.
2186 		 */
2187 			rc = SQLGetData(result->stmt, (SQLUSMALLINT)(field_ind + 1), sql_c_type,
2188 							ZSTR_VAL(field_str), fieldsize, &result->values[field_ind].vallen);
2189 
2190 			if (rc == SQL_ERROR) {
2191 				odbc_sql_error(result->conn_ptr, result->stmt, "SQLGetData");
2192 				zend_string_free(field_str);
2193 				RETURN_FALSE;
2194 			}
2195 
2196 			if (result->values[field_ind].vallen == SQL_NULL_DATA) {
2197 				zend_string_free(field_str);
2198 				RETURN_NULL();
2199 			} else if (rc == SQL_NO_DATA_FOUND) {
2200 				zend_string_free(field_str);
2201 				RETURN_FALSE;
2202 			}
2203 			/* Reduce fieldlen by 1 if we have char data. One day we might
2204 			   have binary strings... */
2205 			if ((result->values[field_ind].coltype == SQL_LONGVARCHAR)
2206 #if defined(ODBCVER) && (ODBCVER >= 0x0300)
2207 			    || (result->values[field_ind].coltype == SQL_WLONGVARCHAR)
2208 #endif
2209 			) {
2210 				fieldsize -= 1;
2211 			}
2212 			/* Don't duplicate result, saves one emalloc.
2213 			   For SQL_SUCCESS, the length is in vallen.
2214 			 */
2215 			if (rc != SQL_SUCCESS_WITH_INFO) {
2216 				field_str = zend_string_truncate(field_str, result->values[field_ind].vallen, 0);
2217 			}
2218 			RETURN_NEW_STR(field_str);
2219 			break;
2220 
2221 		default:
2222 			if (result->values[field_ind].vallen == SQL_NULL_DATA) {
2223 				RETURN_NULL();
2224 			} else {
2225 				RETURN_STRINGL(result->values[field_ind].value, result->values[field_ind].vallen);
2226 			}
2227 			break;
2228 	}
2229 
2230 /* If we come here, output unbound LONG and/or BINARY column data to the client */
2231 
2232 	/* We emalloc 1 byte more for SQL_C_CHAR (trailing \0) */
2233 	fieldsize = (sql_c_type == SQL_C_CHAR) ? 4096 : 4095;
2234 	field = emalloc(fieldsize);
2235 
2236 	/* Call SQLGetData() until SQL_SUCCESS is returned */
2237 	while(1) {
2238 		rc = SQLGetData(result->stmt, (SQLUSMALLINT)(field_ind + 1),sql_c_type, field, fieldsize, &result->values[field_ind].vallen);
2239 
2240 		if (rc == SQL_ERROR) {
2241 			odbc_sql_error(result->conn_ptr, result->stmt, "SQLGetData");
2242 			efree(field);
2243 			RETURN_FALSE;
2244 		}
2245 
2246 		if (result->values[field_ind].vallen == SQL_NULL_DATA) {
2247 			efree(field);
2248 			RETURN_NULL();
2249 		}
2250 		/* chop the trailing \0 by outputing only 4095 bytes */
2251 		PHPWRITE(field,(rc == SQL_SUCCESS_WITH_INFO) ? 4095 : result->values[field_ind].vallen);
2252 
2253 		if (rc == SQL_SUCCESS) { /* no more data avail */
2254 			efree(field);
2255 			RETURN_TRUE;
2256 		}
2257 	}
2258 	RETURN_TRUE;
2259 }
2260 /* }}} */
2261 
2262 /* {{{ proto int odbc_result_all(resource result_id [, string format])
2263    Print result as HTML table */
PHP_FUNCTION(odbc_result_all)2264 PHP_FUNCTION(odbc_result_all)
2265 {
2266 	char *buf = NULL;
2267 	odbc_result *result;
2268 	RETCODE rc;
2269 	zval *pv_res;
2270 	char *pv_format = NULL;
2271 	size_t i, pv_format_len = 0;
2272 	SQLSMALLINT sql_c_type;
2273 #ifdef HAVE_SQL_EXTENDED_FETCH
2274 	SQLULEN crow;
2275 	SQLUSMALLINT RowStatus[1];
2276 #endif
2277 
2278 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "r|s", &pv_res, &pv_format, &pv_format_len) == FAILURE) {
2279 		return;
2280 	}
2281 
2282 	if ((result = (odbc_result *)zend_fetch_resource(Z_RES_P(pv_res), "ODBC result", le_result)) == NULL) {
2283 		RETURN_FALSE;
2284 	}
2285 
2286 	if (result->numcols == 0) {
2287 		php_error_docref(NULL, E_WARNING, "No tuples available at this result index");
2288 		RETURN_FALSE;
2289 	}
2290 #ifdef HAVE_SQL_EXTENDED_FETCH
2291 	if (result->fetch_abs)
2292 		rc = SQLExtendedFetch(result->stmt,SQL_FETCH_NEXT,1,&crow,RowStatus);
2293 	else
2294 #endif
2295 		rc = SQLFetch(result->stmt);
2296 
2297 	if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
2298 		php_printf("<h2>No rows found</h2>\n");
2299 		RETURN_LONG(0);
2300 	}
2301 
2302 	/* Start table tag */
2303 	if (ZEND_NUM_ARGS() == 1) {
2304 		php_printf("<table><tr>");
2305 	} else {
2306 		php_printf("<table %s ><tr>", pv_format);
2307 	}
2308 
2309 	for (i = 0; i < result->numcols; i++) {
2310 		php_printf("<th>%s</th>", result->values[i].name);
2311 	}
2312 
2313 	php_printf("</tr>\n");
2314 
2315 	while(rc == SQL_SUCCESS || rc == SQL_SUCCESS_WITH_INFO) {
2316 		result->fetched++;
2317 		php_printf("<tr>");
2318 		for(i = 0; i < result->numcols; i++) {
2319 			sql_c_type = SQL_C_CHAR;
2320 			switch(result->values[i].coltype) {
2321 				case SQL_BINARY:
2322 				case SQL_VARBINARY:
2323 				case SQL_LONGVARBINARY:
2324 					if (result->binmode <= 0) {
2325 						php_printf("<td>Not printable</td>");
2326 						break;
2327 					}
2328 					if (result->binmode <= 1) sql_c_type = SQL_C_BINARY;
2329 				case SQL_LONGVARCHAR:
2330 #if defined(ODBCVER) && (ODBCVER >= 0x0300)
2331 				case SQL_WLONGVARCHAR:
2332 #endif
2333 					if (IS_SQL_LONG(result->values[i].coltype) &&
2334 						result->longreadlen <= 0) {
2335 						php_printf("<td>Not printable</td>");
2336 						break;
2337 					}
2338 
2339 					if (buf == NULL) {
2340 						buf = emalloc(result->longreadlen);
2341 					}
2342 
2343 					rc = SQLGetData(result->stmt, (SQLUSMALLINT)(i + 1),sql_c_type, buf, result->longreadlen, &result->values[i].vallen);
2344 
2345 					php_printf("<td>");
2346 
2347 					if (rc == SQL_ERROR) {
2348 						odbc_sql_error(result->conn_ptr, result->stmt, "SQLGetData");
2349 						php_printf("</td></tr></table>");
2350 						efree(buf);
2351 						RETURN_FALSE;
2352 					}
2353 					if (rc == SQL_SUCCESS_WITH_INFO) {
2354 						PHPWRITE(buf, result->longreadlen);
2355 					} else if (result->values[i].vallen == SQL_NULL_DATA) {
2356 						php_printf("<td>NULL</td>");
2357 						break;
2358 					} else {
2359 						PHPWRITE(buf, result->values[i].vallen);
2360 					}
2361 					php_printf("</td>");
2362 					break;
2363 				default:
2364 					if (result->values[i].vallen == SQL_NULL_DATA) {
2365 						php_printf("<td>NULL</td>");
2366 					} else {
2367 						php_printf("<td>%s</td>", result->values[i].value);
2368 					}
2369 					break;
2370 			}
2371 		}
2372    		php_printf("</tr>\n");
2373 
2374 #ifdef HAVE_SQL_EXTENDED_FETCH
2375 		if (result->fetch_abs)
2376 			rc = SQLExtendedFetch(result->stmt,SQL_FETCH_NEXT,1,&crow,RowStatus);
2377 		else
2378 #endif
2379 			rc = SQLFetch(result->stmt);
2380 	}
2381 	php_printf("</table>\n");
2382 	if (buf) efree(buf);
2383 	RETURN_LONG(result->fetched);
2384 }
2385 /* }}} */
2386 
2387 /* {{{ proto bool odbc_free_result(resource result_id)
2388    Free resources associated with a result */
PHP_FUNCTION(odbc_free_result)2389 PHP_FUNCTION(odbc_free_result)
2390 {
2391 	zval *pv_res;
2392 	odbc_result *result;
2393 	int i;
2394 
2395 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &pv_res) == FAILURE) {
2396 		return;
2397 	}
2398 
2399 	if ((result = (odbc_result *)zend_fetch_resource(Z_RES_P(pv_res), "ODBC result", le_result)) == NULL) {
2400 		RETURN_FALSE;
2401 	}
2402 
2403 	if (result->values) {
2404 		for (i = 0; i < result->numcols; i++) {
2405 			if (result->values[i].value) {
2406 				efree(result->values[i].value);
2407 			}
2408 		}
2409 		efree(result->values);
2410 		result->values = NULL;
2411 	}
2412 
2413 	zend_list_close(Z_RES_P(pv_res));
2414 
2415 	RETURN_TRUE;
2416 }
2417 /* }}} */
2418 
2419 /* {{{ proto resource odbc_connect(string DSN, string user, string password [, int cursor_option])
2420    Connect to a datasource */
PHP_FUNCTION(odbc_connect)2421 PHP_FUNCTION(odbc_connect)
2422 {
2423 	odbc_do_connect(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
2424 }
2425 /* }}} */
2426 
2427 /* {{{ proto resource odbc_pconnect(string DSN, string user, string password [, int cursor_option])
2428    Establish a persistent connection to a datasource */
PHP_FUNCTION(odbc_pconnect)2429 PHP_FUNCTION(odbc_pconnect)
2430 {
2431 	odbc_do_connect(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
2432 }
2433 /* }}} */
2434 
2435 /* {{{ odbc_sqlconnect */
odbc_sqlconnect(odbc_connection ** conn,char * db,char * uid,char * pwd,int cur_opt,int persistent)2436 int odbc_sqlconnect(odbc_connection **conn, char *db, char *uid, char *pwd, int cur_opt, int persistent)
2437 {
2438 	RETCODE rc;
2439 
2440 	*conn = (odbc_connection *)pemalloc(sizeof(odbc_connection), persistent);
2441 	memset(*conn, 0, sizeof(odbc_connection));
2442 	(*conn)->persistent = persistent;
2443 	SQLAllocEnv(&((*conn)->henv));
2444 	SQLAllocConnect((*conn)->henv, &((*conn)->hdbc));
2445 
2446 #if defined(HAVE_SOLID) || defined(HAVE_SOLID_30)
2447 	SQLSetConnectOption((*conn)->hdbc, SQL_TRANSLATE_OPTION,
2448 			SQL_SOLID_XLATOPT_NOCNV);
2449 #endif
2450 #ifdef HAVE_ODBC_ROUTER
2451 	{
2452 #define CONNSTRSIZE 2048
2453 	 char *lpszConnStr = emalloc(CONNSTRSIZE);
2454 	 if (lpszConnStr && db) {
2455 		 short cbszConnStr;
2456 		 if (strstr(db, ";")) {
2457 			 /* the caller has apparently passed a connection-string */
2458 			 if (strstr(db, "uid") || strstr(db, "UID")) {
2459 				 uid = NULL;
2460 			 }
2461 			 if (strstr(db, "pwd") || strstr(db, "PWD")) {
2462 				 pwd = NULL;
2463 			 }
2464 			 strlcpy( lpszConnStr, db, CONNSTRSIZE);
2465 		 }
2466 		 else {
2467 			 strcpy(lpszConnStr, "DSN=");
2468 			 strlcat(lpszConnStr, db, CONNSTRSIZE);
2469 		 }
2470 		 if (uid) {
2471 			 if (uid[0]) {
2472 				 strlcat(lpszConnStr, ";UID=", CONNSTRSIZE);
2473 				 strlcat(lpszConnStr, uid, CONNSTRSIZE);
2474 				 strlcat(lpszConnStr, ";", CONNSTRSIZE);
2475 			 }
2476 			 if (pwd) {
2477 				 if (pwd[0]) {
2478 					 strlcat(lpszConnStr, "PWD=", CONNSTRSIZE);
2479 					 strlcat(lpszConnStr, pwd, CONNSTRSIZE);
2480 					 strlcat(lpszConnStr, ";", CONNSTRSIZE);
2481 				 }
2482 			 }
2483 		 }
2484 		 rc = SQLDriverConnect((*conn)->hdbc, NULL, lpszConnStr, SQL_NTS, lpszConnStr, CONNSTRSIZE, &cbszConnStr, SQL_DRIVER_NOPROMPT);
2485 		 efree(lpszConnStr);
2486 	 }
2487 	}
2488 #else
2489 #ifdef HAVE_OPENLINK
2490 	{
2491 		char dsnbuf[1024];
2492 		short dsnbuflen;
2493 
2494 		rc = SQLDriverConnect((*conn)->hdbc, NULL, db, SQL_NTS,	dsnbuf, sizeof(dsnbuf) - 1, &dsnbuflen, SQL_DRIVER_NOPROMPT);
2495 	}
2496 #else
2497 	if (cur_opt != SQL_CUR_DEFAULT) {
2498 		rc = SQLSetConnectOption((*conn)->hdbc, SQL_ODBC_CURSORS, cur_opt);
2499 		if (rc != SQL_SUCCESS) {  /* && rc != SQL_SUCCESS_WITH_INFO ? */
2500 			odbc_sql_error(*conn, SQL_NULL_HSTMT, "SQLSetConnectOption");
2501 			SQLFreeConnect((*conn)->hdbc);
2502 			pefree(*conn, persistent);
2503 			return FALSE;
2504 		}
2505 	}
2506 /*  Possible fix for bug #10250
2507  *  Needs testing on UnixODBC < 2.0.5 though. */
2508 #if defined(HAVE_EMPRESS) || defined(HAVE_UNIXODBC) || defined(PHP_WIN32) || defined (HAVE_IODBC)
2509 /* *  Uncomment the line above, and comment line below to fully test
2510  * #ifdef HAVE_EMPRESS */
2511 	{
2512 		int     direct = 0;
2513 		char    dsnbuf[1024];
2514 		short   dsnbuflen;
2515 		char    *ldb = 0;
2516 		int		ldb_len = 0;
2517 
2518 		if (strstr((char*)db, ";")) {
2519 			direct = 1;
2520 			if (uid && !strstr ((char*)db, "uid") && !strstr((char*)db, "UID")) {
2521 				spprintf(&ldb, 0, "%s;UID=%s;PWD=%s", db, uid, pwd);
2522 			} else {
2523 				ldb_len = strlen(db)+1;
2524 				ldb = (char*) emalloc(ldb_len);
2525 				memcpy(ldb, db, ldb_len);
2526 			}
2527 		}
2528 
2529 		if (direct) {
2530 			rc = SQLDriverConnect((*conn)->hdbc, NULL, ldb, strlen(ldb), dsnbuf, sizeof(dsnbuf) - 1, &dsnbuflen, SQL_DRIVER_NOPROMPT);
2531 		} else {
2532 			rc = SQLConnect((*conn)->hdbc, db, SQL_NTS, uid, SQL_NTS, pwd, SQL_NTS);
2533 		}
2534 
2535 		if (ldb) {
2536 			efree(ldb);
2537 		}
2538 	}
2539 #else
2540 	rc = SQLConnect((*conn)->hdbc, db, SQL_NTS, uid, SQL_NTS, pwd, SQL_NTS);
2541 #endif
2542 #endif
2543 #endif
2544 	if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
2545 		odbc_sql_error(*conn, SQL_NULL_HSTMT, "SQLConnect");
2546 		SQLFreeConnect((*conn)->hdbc);
2547 		pefree((*conn), persistent);
2548 		return FALSE;
2549 	}
2550 /*	(*conn)->open = 1;*/
2551 	return TRUE;
2552 }
2553 /* }}} */
2554 
2555 /* Persistent connections: two list-types le_pconn, le_conn and a plist
2556  * where hashed connection info is stored together with index pointer to
2557  * the actual link of type le_pconn in the list. Only persistent
2558  * connections get hashed up. Normal connections use existing pconnections.
2559  * Maybe this has to change with regard to transactions on pconnections?
2560  * Possibly set autocommit to on on request shutdown.
2561  *
2562  * We do have to hash non-persistent connections, and reuse connections.
2563  * In the case where two connects were being made, without closing the first
2564  * connect, access violations were occurring.  This is because some of the
2565  * "globals" in this module should actually be per-connection variables.  I
2566  * simply fixed things to get them working for now.  Shane
2567  */
2568 /* {{{ odbc_do_connect */
odbc_do_connect(INTERNAL_FUNCTION_PARAMETERS,int persistent)2569 void odbc_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
2570 {
2571 	char *db, *uid, *pwd;
2572 	size_t db_len, uid_len, pwd_len;
2573 	zend_long pv_opt = SQL_CUR_DEFAULT;
2574 	odbc_connection *db_conn;
2575 	char *hashed_details;
2576 	int hashed_len, cur_opt;
2577 
2578 	/*  Now an optional 4th parameter specifying the cursor type
2579 	 *  defaulting to the cursors default
2580 	 */
2581 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "sss|l", &db, &db_len, &uid, &uid_len, &pwd, &pwd_len, &pv_opt) == FAILURE) {
2582 		return;
2583 	}
2584 
2585 	cur_opt = pv_opt;
2586 
2587 	if (ZEND_NUM_ARGS() > 3) {
2588 		/* Confirm the cur_opt range */
2589 		if (! (cur_opt == SQL_CUR_USE_IF_NEEDED ||
2590 			cur_opt == SQL_CUR_USE_ODBC ||
2591 			cur_opt == SQL_CUR_USE_DRIVER ||
2592 			cur_opt == SQL_CUR_DEFAULT) ) {
2593 			php_error_docref(NULL, E_WARNING, "Invalid Cursor type (%d)", cur_opt);
2594 			RETURN_FALSE;
2595 		}
2596 	}
2597 
2598 	if (ODBCG(allow_persistent) <= 0) {
2599 		persistent = 0;
2600 	}
2601 
2602 	hashed_len = spprintf(&hashed_details, 0, "%s_%s_%s_%s_%d", ODBC_TYPE, db, uid, pwd, cur_opt);
2603 
2604 	/* FIXME the idea of checking to see if our connection is already persistent
2605 		is good, but it adds a lot of overhead to non-persistent connections.  We
2606 		should look and see if we can fix that somehow */
2607 	/* try to find if we already have this link in our persistent list,
2608 	 * no matter if it is to be persistent or not
2609 	 */
2610 
2611 try_and_get_another_connection:
2612 
2613 	if (persistent) {
2614 		zend_resource *le;
2615 
2616 		/* the link is not in the persistent list */
2617 		if ((le = zend_hash_str_find_ptr(&EG(persistent_list), hashed_details, hashed_len)) == NULL) {
2618 			zend_resource new_le;
2619 
2620 			if (ODBCG(max_links) != -1 && ODBCG(num_links) >= ODBCG(max_links)) {
2621 				php_error_docref(NULL, E_WARNING, "Too many open links (%ld)", ODBCG(num_links));
2622 				efree(hashed_details);
2623 				RETURN_FALSE;
2624 			}
2625 			if (ODBCG(max_persistent) != -1 && ODBCG(num_persistent) >= ODBCG(max_persistent)) {
2626 				php_error_docref(NULL, E_WARNING,"Too many open persistent links (%ld)", ODBCG(num_persistent));
2627 				efree(hashed_details);
2628 				RETURN_FALSE;
2629 			}
2630 
2631 			if (!odbc_sqlconnect(&db_conn, db, uid, pwd, cur_opt, 1)) {
2632 				efree(hashed_details);
2633 				RETURN_FALSE;
2634 			}
2635 
2636 			new_le.type = le_pconn;
2637 			new_le.ptr = db_conn;
2638 			new_le.handle = -1;
2639 			if (zend_hash_str_update_mem(&EG(persistent_list), hashed_details, hashed_len, &new_le,
2640 						sizeof(zend_resource)) == NULL) {
2641 				free(db_conn);
2642 				efree(hashed_details);
2643 				RETURN_FALSE;
2644 			}
2645 			ODBCG(num_persistent)++;
2646 			ODBCG(num_links)++;
2647 			db_conn->res = zend_register_resource(db_conn, le_pconn);
2648 			RETVAL_RES(db_conn->res);
2649 		} else { /* found connection */
2650 			if (le->type != le_pconn) {
2651 				RETURN_FALSE;
2652 			}
2653 			/*
2654 			 * check to see if the connection is still valid
2655 			 */
2656 			db_conn = (odbc_connection *)le->ptr;
2657 
2658 			/*
2659 			 * check to see if the connection is still in place (lurcher)
2660 			 */
2661 			if(ODBCG(check_persistent)){
2662 				RETCODE ret;
2663 				UCHAR d_name[32];
2664 				SQLSMALLINT len;
2665 
2666 				ret = SQLGetInfo(db_conn->hdbc,
2667 					SQL_DATA_SOURCE_READ_ONLY,
2668 					d_name, sizeof(d_name), &len);
2669 
2670 				if(ret != SQL_SUCCESS || len == 0) {
2671 					zend_hash_str_del(&EG(persistent_list), hashed_details, hashed_len);
2672 					/* Commented out to fix a possible double closure error
2673 					 * when working with persistent connections as submitted by
2674 					 * bug #15758
2675 					 *
2676 					 * safe_odbc_disconnect(db_conn->hdbc);
2677 					 * SQLFreeConnect(db_conn->hdbc);
2678 					 */
2679 					goto try_and_get_another_connection;
2680 				}
2681 			}
2682 		}
2683 		db_conn->res = zend_register_resource(db_conn, le_pconn);
2684 		RETVAL_RES(db_conn->res);
2685 	} else { /* non persistent */
2686 		zend_resource *index_ptr, new_index_ptr;
2687 
2688 		if ((index_ptr = zend_hash_str_find_ptr(&EG(regular_list), hashed_details, hashed_len)) != NULL) {
2689 			zend_ulong conn_id;
2690 			zend_resource *p;
2691 
2692 			if (index_ptr->type != le_index_ptr) {
2693 				RETURN_FALSE;
2694 			}
2695 			conn_id = (zend_ulong)index_ptr->ptr;
2696 			p = zend_hash_index_find_ptr(&EG(regular_list), conn_id);   /* check if the connection is still there */
2697 
2698 			if (p && p->ptr && (p->type == le_conn || p->type == le_pconn)) {
2699 				GC_REFCOUNT(p)++;
2700 				RETVAL_RES(p);
2701 				efree(hashed_details);
2702 				return;
2703 			} else {
2704 				zend_hash_str_del(&EG(regular_list), hashed_details, hashed_len);
2705 			}
2706 		}
2707 		if (ODBCG(max_links) != -1 && ODBCG(num_links) >= ODBCG(max_links)) {
2708 			php_error_docref(NULL, E_WARNING,"Too many open connections (%ld)",ODBCG(num_links));
2709 			efree(hashed_details);
2710 			RETURN_FALSE;
2711 		}
2712 
2713 		if (!odbc_sqlconnect(&db_conn, db, uid, pwd, cur_opt, 0)) {
2714 			efree(hashed_details);
2715 			RETURN_FALSE;
2716 		}
2717 		db_conn->res = zend_register_resource(db_conn, le_conn);
2718 		RETVAL_RES(db_conn->res);
2719 		new_index_ptr.ptr = (void *)(zend_uintptr_t)Z_RES_HANDLE_P(return_value);
2720 		new_index_ptr.type = le_index_ptr;
2721 
2722 		if (zend_hash_str_update_mem(&EG(regular_list), hashed_details, hashed_len, (void *) &new_index_ptr,
2723 				   sizeof(zend_resource)) == NULL) {
2724 			efree(hashed_details);
2725 			RETURN_FALSE;
2726 			/* XXX Free Connection */
2727 		}
2728 		ODBCG(num_links)++;
2729 	}
2730 	efree(hashed_details);
2731 }
2732 /* }}} */
2733 
2734 /* {{{ proto void odbc_close(resource connection_id)
2735    Close an ODBC connection */
PHP_FUNCTION(odbc_close)2736 PHP_FUNCTION(odbc_close)
2737 {
2738 	zval *pv_conn;
2739 	zend_resource *p;
2740 	odbc_connection *conn;
2741 	odbc_result *res;
2742 	int is_pconn = 0;
2743 
2744 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &pv_conn) == FAILURE) {
2745 		return;
2746 	}
2747 
2748 	conn = (odbc_connection *)zend_fetch_resource2(Z_RES_P(pv_conn), "ODBC-Link", le_conn, le_pconn);
2749 	if (Z_RES_P(pv_conn)->type == le_pconn) {
2750 		is_pconn = 1;
2751 	}
2752 
2753 	ZEND_HASH_FOREACH_PTR(&EG(regular_list), p) {
2754 		if (p->ptr && (p->type == le_result)) {
2755 			res = (odbc_result *)p->ptr;
2756 			if (res->conn_ptr == conn) {
2757 				zend_list_close(p);
2758 			}
2759 		}
2760 	} ZEND_HASH_FOREACH_END();
2761 
2762 	zend_list_close(Z_RES_P(pv_conn));
2763 
2764 	if(is_pconn){
2765 		zend_hash_apply_with_argument(&EG(persistent_list),	(apply_func_arg_t) _close_pconn_with_res, (void *) Z_RES_P(pv_conn));
2766 	}
2767 }
2768 /* }}} */
2769 
2770 /* {{{ proto int odbc_num_rows(resource result_id)
2771    Get number of rows in a result */
PHP_FUNCTION(odbc_num_rows)2772 PHP_FUNCTION(odbc_num_rows)
2773 {
2774 	odbc_result *result;
2775 	SQLLEN rows;
2776 	zval *pv_res;
2777 
2778 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &pv_res) == FAILURE) {
2779 		return;
2780 	}
2781 
2782 	if ((result = (odbc_result *)zend_fetch_resource(Z_RES_P(pv_res), "ODBC result", le_result)) == NULL) {
2783 		RETURN_FALSE;
2784 	}
2785 
2786 	SQLRowCount(result->stmt, &rows);
2787 	RETURN_LONG(rows);
2788 }
2789 /* }}} */
2790 
2791 #if !defined(HAVE_SOLID) && !defined(HAVE_SOLID_30)
2792 /* {{{ proto bool odbc_next_result(resource result_id)
2793    Checks if multiple results are available */
PHP_FUNCTION(odbc_next_result)2794 PHP_FUNCTION(odbc_next_result)
2795 {
2796 	odbc_result *result;
2797 	zval *pv_res;
2798 	int rc, i;
2799 
2800 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &pv_res) == FAILURE) {
2801 		return;
2802 	}
2803 
2804 	if ((result = (odbc_result *)zend_fetch_resource(Z_RES_P(pv_res), "ODBC result", le_result)) == NULL) {
2805 		RETURN_FALSE;
2806 	}
2807 
2808 	if (result->values) {
2809 		for(i = 0; i < result->numcols; i++) {
2810 			if (result->values[i].value) {
2811 				efree(result->values[i].value);
2812 			}
2813 		}
2814 		efree(result->values);
2815 		result->values = NULL;
2816 	}
2817 
2818 	result->fetched = 0;
2819 	rc = SQLMoreResults(result->stmt);
2820 	if (rc == SQL_SUCCESS_WITH_INFO || rc == SQL_SUCCESS) {
2821 		rc = SQLFreeStmt(result->stmt, SQL_UNBIND);
2822 		SQLNumParams(result->stmt, &(result->numparams));
2823 		SQLNumResultCols(result->stmt, &(result->numcols));
2824 
2825 		if (result->numcols > 0) {
2826 			if (!odbc_bindcols(result)) {
2827 				efree(result);
2828 				RETVAL_FALSE;
2829 			}
2830 		} else {
2831 			result->values = NULL;
2832 		}
2833 		RETURN_TRUE;
2834 	} else if (rc == SQL_NO_DATA_FOUND) {
2835 		RETURN_FALSE;
2836 	} else {
2837 		odbc_sql_error(result->conn_ptr, result->stmt, "SQLMoreResults");
2838 		RETURN_FALSE;
2839 	}
2840 }
2841 /* }}} */
2842 #endif
2843 
2844 /* {{{ proto int odbc_num_fields(resource result_id)
2845    Get number of columns in a result */
PHP_FUNCTION(odbc_num_fields)2846 PHP_FUNCTION(odbc_num_fields)
2847 {
2848 	odbc_result *result;
2849 	zval *pv_res;
2850 
2851 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &pv_res) == FAILURE) {
2852 		return;
2853 	}
2854 
2855 	if ((result = (odbc_result *)zend_fetch_resource(Z_RES_P(pv_res), "ODBC result", le_result)) == NULL) {
2856 		RETURN_FALSE;
2857 	}
2858 
2859 	RETURN_LONG(result->numcols);
2860 }
2861 /* }}} */
2862 
2863 /* {{{ proto string odbc_field_name(resource result_id, int field_number)
2864    Get a column name */
PHP_FUNCTION(odbc_field_name)2865 PHP_FUNCTION(odbc_field_name)
2866 {
2867 	odbc_result *result;
2868 	zval *pv_res;
2869 	zend_long pv_num;
2870 
2871 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rl", &pv_res, &pv_num) == FAILURE) {
2872 		return;
2873 	}
2874 
2875 	if ((result = (odbc_result *)zend_fetch_resource(Z_RES_P(pv_res), "ODBC result", le_result)) == NULL) {
2876 		RETURN_FALSE;
2877 	}
2878 
2879 	if (result->numcols == 0) {
2880 		php_error_docref(NULL, E_WARNING, "No tuples available at this result index");
2881 		RETURN_FALSE;
2882 	}
2883 
2884 	if (pv_num > result->numcols) {
2885 		php_error_docref(NULL, E_WARNING, "Field index larger than number of fields");
2886 		RETURN_FALSE;
2887 	}
2888 
2889 	if (pv_num < 1) {
2890 		php_error_docref(NULL, E_WARNING, "Field numbering starts at 1");
2891 		RETURN_FALSE;
2892 	}
2893 
2894 	RETURN_STRING(result->values[pv_num - 1].name);
2895 }
2896 /* }}} */
2897 
2898 /* {{{ proto string odbc_field_type(resource result_id, int field_number)
2899    Get the datatype of a column */
PHP_FUNCTION(odbc_field_type)2900 PHP_FUNCTION(odbc_field_type)
2901 {
2902 	odbc_result	*result;
2903 	char    	tmp[32];
2904 	SQLSMALLINT	tmplen;
2905 	zval		*pv_res;
2906 	zend_long		pv_num;
2907 
2908 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rl", &pv_res, &pv_num) == FAILURE) {
2909 		return;
2910 	}
2911 
2912 	if ((result = (odbc_result *)zend_fetch_resource(Z_RES_P(pv_res), "ODBC result", le_result)) == NULL) {
2913 		RETURN_FALSE;
2914 	}
2915 
2916 	if (result->numcols == 0) {
2917 		php_error_docref(NULL, E_WARNING, "No tuples available at this result index");
2918 		RETURN_FALSE;
2919 	}
2920 
2921 	if (pv_num > result->numcols) {
2922 		php_error_docref(NULL, E_WARNING, "Field index larger than number of fields");
2923 		RETURN_FALSE;
2924 	}
2925 
2926 	if (pv_num < 1) {
2927 		php_error_docref(NULL, E_WARNING, "Field numbering starts at 1");
2928 		RETURN_FALSE;
2929 	}
2930 
2931 	PHP_ODBC_SQLCOLATTRIBUTE(result->stmt, (SQLUSMALLINT)pv_num, SQL_COLUMN_TYPE_NAME, tmp, 31, &tmplen, NULL);
2932 	RETURN_STRING(tmp)
2933 }
2934 /* }}} */
2935 
2936 /* {{{ proto int odbc_field_len(resource result_id, int field_number)
2937    Get the length (precision) of a column */
PHP_FUNCTION(odbc_field_len)2938 PHP_FUNCTION(odbc_field_len)
2939 {
2940 	odbc_column_lengths(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
2941 }
2942 /* }}} */
2943 
2944 /* {{{ proto int odbc_field_scale(resource result_id, int field_number)
2945    Get the scale of a column */
PHP_FUNCTION(odbc_field_scale)2946 PHP_FUNCTION(odbc_field_scale)
2947 {
2948 	odbc_column_lengths(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
2949 }
2950 /* }}} */
2951 
2952 /* {{{ proto int odbc_field_num(resource result_id, string field_name)
2953    Return column number */
PHP_FUNCTION(odbc_field_num)2954 PHP_FUNCTION(odbc_field_num)
2955 {
2956 	char *fname;
2957 	size_t i, field_ind, fname_len;
2958 	odbc_result *result;
2959 	zval *pv_res;
2960 
2961 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rs", &pv_res, &fname, &fname_len) == FAILURE) {
2962 		return;
2963 	}
2964 
2965 	if ((result = (odbc_result *)zend_fetch_resource(Z_RES_P(pv_res), "ODBC result", le_result)) == NULL) {
2966 		RETURN_FALSE;
2967 	}
2968 
2969 	if (result->numcols == 0) {
2970 		php_error_docref(NULL, E_WARNING, "No tuples available at this result index");
2971 		RETURN_FALSE;
2972 	}
2973 
2974 	field_ind = -1;
2975 	for(i = 0; i < result->numcols; i++) {
2976 		if (strcasecmp(result->values[i].name, fname) == 0) {
2977 			field_ind = i + 1;
2978 		}
2979 	}
2980 
2981 	if (field_ind == -1) {
2982 		RETURN_FALSE;
2983 	}
2984 	RETURN_LONG(field_ind);
2985 }
2986 /* }}} */
2987 
2988 /* {{{ proto mixed odbc_autocommit(resource connection_id [, int OnOff])
2989    Toggle autocommit mode or get status */
2990 /* There can be problems with pconnections!*/
PHP_FUNCTION(odbc_autocommit)2991 PHP_FUNCTION(odbc_autocommit)
2992 {
2993 	odbc_connection *conn;
2994 	RETCODE rc;
2995 	zval *pv_conn;
2996 	zend_long pv_onoff = 0;
2997 
2998 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "r|l", &pv_conn, &pv_onoff) == FAILURE) {
2999 		return;
3000 	}
3001 
3002 	if (!(conn = (odbc_connection *)zend_fetch_resource2(Z_RES_P(pv_conn), "ODBC-Link", le_conn, le_pconn))) {
3003 		RETURN_FALSE;
3004 	}
3005 
3006 	if (ZEND_NUM_ARGS() > 1) {
3007 		rc = SQLSetConnectOption(conn->hdbc, SQL_AUTOCOMMIT, (pv_onoff) ? SQL_AUTOCOMMIT_ON : SQL_AUTOCOMMIT_OFF);
3008 		if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
3009 			odbc_sql_error(conn, SQL_NULL_HSTMT, "Set autocommit");
3010 			RETURN_FALSE;
3011 		}
3012 		RETVAL_TRUE;
3013 	} else {
3014 		SQLINTEGER status;
3015 
3016 		rc = SQLGetConnectOption(conn->hdbc, SQL_AUTOCOMMIT, (PTR)&status);
3017 		if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
3018 			odbc_sql_error(conn, SQL_NULL_HSTMT, "Get commit status");
3019 			RETURN_FALSE;
3020 		}
3021 		RETVAL_LONG((zend_long)status);
3022 	}
3023 }
3024 /* }}} */
3025 
3026 /* {{{ proto bool odbc_commit(resource connection_id)
3027    Commit an ODBC transaction */
PHP_FUNCTION(odbc_commit)3028 PHP_FUNCTION(odbc_commit)
3029 {
3030 	odbc_transact(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
3031 }
3032 /* }}} */
3033 
3034 /* {{{ proto bool odbc_rollback(resource connection_id)
3035    Rollback a transaction */
PHP_FUNCTION(odbc_rollback)3036 PHP_FUNCTION(odbc_rollback)
3037 {
3038 	odbc_transact(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
3039 }
3040 /* }}} */
3041 
3042 /* {{{ php_odbc_lasterror */
php_odbc_lasterror(INTERNAL_FUNCTION_PARAMETERS,int mode)3043 static void php_odbc_lasterror(INTERNAL_FUNCTION_PARAMETERS, int mode)
3044 {
3045 	odbc_connection *conn;
3046 	zval *pv_handle;
3047 	char *ret;
3048 
3049 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "|r", &pv_handle) == FAILURE) {
3050 		return;
3051 	}
3052 
3053 	if (ZEND_NUM_ARGS() == 1) {
3054 		if (!(conn = (odbc_connection *)zend_fetch_resource2(Z_RES_P(pv_handle), "ODBC-Link", le_conn, le_pconn))) {
3055 			RETURN_FALSE;
3056 		}
3057 		if (mode == 0) {
3058 			ret = conn->laststate;
3059 		} else {
3060 			ret = conn->lasterrormsg;
3061 		}
3062 	} else {
3063 		if (mode == 0) {
3064 			ret = ODBCG(laststate);
3065 		} else {
3066 			ret = ODBCG(lasterrormsg);
3067 		}
3068 	}
3069 
3070 	RETURN_STRING(ret);
3071 }
3072 /* }}} */
3073 
3074 /* {{{ proto string odbc_error([resource connection_id])
3075    Get the last error code */
PHP_FUNCTION(odbc_error)3076 PHP_FUNCTION(odbc_error)
3077 {
3078 	php_odbc_lasterror(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
3079 }
3080 /* }}} */
3081 
3082 /* {{{ proto string odbc_errormsg([resource connection_id])
3083    Get the last error message */
PHP_FUNCTION(odbc_errormsg)3084 PHP_FUNCTION(odbc_errormsg)
3085 {
3086 	php_odbc_lasterror(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
3087 }
3088 /* }}} */
3089 
3090 /* {{{ proto bool odbc_setoption(resource conn_id|result_id, int which, int option, int value)
3091    Sets connection or statement options */
3092 /* This one has to be used carefully. We can't allow to set connection options for
3093    persistent connections. I think that SetStmtOption is of little use, since most
3094    of those can only be specified before preparing/executing statements.
3095    On the other hand, they can be made connection wide default through SetConnectOption
3096    - but will be overidden by calls to SetStmtOption() in odbc_prepare/odbc_do
3097 */
PHP_FUNCTION(odbc_setoption)3098 PHP_FUNCTION(odbc_setoption)
3099 {
3100 	odbc_connection *conn;
3101 	odbc_result	*result;
3102 	RETCODE rc;
3103 	zval *pv_handle;
3104 	zend_long pv_which, pv_opt, pv_val;
3105 
3106 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rlll", &pv_handle, &pv_which, &pv_opt, &pv_val) == FAILURE) {
3107 		return;
3108 	}
3109 
3110 	switch (pv_which) {
3111 		case 1:		/* SQLSetConnectOption */
3112 			if (!(conn = (odbc_connection *)zend_fetch_resource2(Z_RES_P(pv_handle), "ODBC-Link", le_conn, le_pconn))) {
3113 				RETURN_FALSE;
3114 			}
3115 
3116 			if (conn->persistent) {
3117 				php_error_docref(NULL, E_WARNING, "Unable to set option for persistent connection");
3118 				RETURN_FALSE;
3119 			}
3120 			rc = SQLSetConnectOption(conn->hdbc, (unsigned short) pv_opt, pv_val);
3121 			if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
3122 				odbc_sql_error(conn, SQL_NULL_HSTMT, "SetConnectOption");
3123 				RETURN_FALSE;
3124 			}
3125 			break;
3126 		case 2:		/* SQLSetStmtOption */
3127 			if ((result = (odbc_result *)zend_fetch_resource(Z_RES_P(pv_handle), "ODBC result", le_result)) == NULL) {
3128 				RETURN_FALSE;
3129 			}
3130 
3131 			rc = SQLSetStmtOption(result->stmt, (unsigned short) pv_opt, pv_val);
3132 
3133 			if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
3134 				odbc_sql_error(result->conn_ptr, result->stmt, "SetStmtOption");
3135 				RETURN_FALSE;
3136 			}
3137 			break;
3138 		default:
3139 			php_error_docref(NULL, E_WARNING, "Unknown option type");
3140 			RETURN_FALSE;
3141 			break;
3142 	}
3143 
3144 	RETURN_TRUE;
3145 }
3146 /* }}} */
3147 
3148 /*
3149  * metadata functions
3150  */
3151 
3152 /* {{{ proto resource odbc_tables(resource connection_id [, string qualifier [, string owner [, string name [, string table_types]]]])
3153    Call the SQLTables function */
PHP_FUNCTION(odbc_tables)3154 PHP_FUNCTION(odbc_tables)
3155 {
3156 	zval *pv_conn;
3157 	odbc_result   *result = NULL;
3158 	odbc_connection *conn;
3159 	char *cat = NULL, *schema = NULL, *table = NULL, *type = NULL;
3160 	size_t cat_len = 0, schema_len = 0, table_len = 0, type_len = 0;
3161 	RETCODE rc;
3162 
3163 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "r|s!sss", &pv_conn, &cat, &cat_len, &schema, &schema_len,
3164 		&table, &table_len, &type, &type_len) == FAILURE) {
3165 		return;
3166 	}
3167 
3168 	if (!(conn = (odbc_connection *)zend_fetch_resource2(Z_RES_P(pv_conn), "ODBC-Link", le_conn, le_pconn))) {
3169 		RETURN_FALSE;
3170 	}
3171 
3172 	result = (odbc_result *)ecalloc(1, sizeof(odbc_result));
3173 
3174 	rc = PHP_ODBC_SQLALLOCSTMT(conn->hdbc, &(result->stmt));
3175 	if (rc == SQL_INVALID_HANDLE) {
3176 		efree(result);
3177 		php_error_docref(NULL, E_WARNING, "SQLAllocStmt error 'Invalid Handle'");
3178 		RETURN_FALSE;
3179 	}
3180 
3181 	if (rc == SQL_ERROR) {
3182 		odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLAllocStmt");
3183 		efree(result);
3184 		RETURN_FALSE;
3185 	}
3186 
3187 	/* This hack is needed to access table information in Access databases (fmk) */
3188 	if (table && table_len && schema && schema_len == 0) {
3189 		schema = NULL;
3190 	}
3191 
3192 	rc = SQLTables(result->stmt,
3193 			cat, SAFE_SQL_NTS(cat),
3194 			schema,	SAFE_SQL_NTS(schema),
3195 			table, SAFE_SQL_NTS(table),
3196 			type, SAFE_SQL_NTS(type));
3197 
3198 	if (rc == SQL_ERROR) {
3199 		odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLTables");
3200 		efree(result);
3201 		RETURN_FALSE;
3202 	}
3203 
3204 	result->numparams = 0;
3205 	SQLNumResultCols(result->stmt, &(result->numcols));
3206 
3207 	if (result->numcols > 0) {
3208 		if (!odbc_bindcols(result)) {
3209 			efree(result);
3210 			RETURN_FALSE;
3211 		}
3212 	} else {
3213 		result->values = NULL;
3214 	}
3215 	result->conn_ptr = conn;
3216 	result->fetched = 0;
3217 	RETURN_RES(zend_register_resource(result, le_result));
3218 }
3219 /* }}} */
3220 
3221 /* {{{ proto resource odbc_columns(resource connection_id [, string qualifier [, string owner [, string table_name [, string column_name]]]])
3222    Returns a result identifier that can be used to fetch a list of column names in specified tables */
PHP_FUNCTION(odbc_columns)3223 PHP_FUNCTION(odbc_columns)
3224 {
3225 	zval *pv_conn;
3226 	odbc_result *result = NULL;
3227 	odbc_connection *conn;
3228 	char *cat = NULL, *schema = NULL, *table = NULL, *column = NULL;
3229 	size_t cat_len = 0, schema_len = 0, table_len = 0, column_len = 0;
3230 	RETCODE rc;
3231 
3232 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "r|s!sss", &pv_conn, &cat, &cat_len, &schema, &schema_len,
3233 		&table, &table_len, &column, &column_len) == FAILURE) {
3234 		return;
3235 	}
3236 
3237 	if (!(conn = (odbc_connection *)zend_fetch_resource2(Z_RES_P(pv_conn), "ODBC-Link", le_conn, le_pconn))) {
3238 		RETURN_FALSE;
3239 	}
3240 
3241 	result = (odbc_result *)ecalloc(1, sizeof(odbc_result));
3242 
3243 	rc = PHP_ODBC_SQLALLOCSTMT(conn->hdbc, &(result->stmt));
3244 	if (rc == SQL_INVALID_HANDLE) {
3245 		efree(result);
3246 		php_error_docref(NULL, E_WARNING, "SQLAllocStmt error 'Invalid Handle'");
3247 		RETURN_FALSE;
3248 	}
3249 
3250 	if (rc == SQL_ERROR) {
3251 		odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLAllocStmt");
3252 		efree(result);
3253 		RETURN_FALSE;
3254 	}
3255 
3256 	/*
3257 	 * Needed to make MS Access happy
3258 	 */
3259 	if (table && table_len && schema && schema_len == 0) {
3260 		schema = NULL;
3261 	}
3262 
3263 	rc = SQLColumns(result->stmt,
3264 			cat, (SQLSMALLINT) cat_len,
3265 			schema, (SQLSMALLINT) schema_len,
3266 			table, (SQLSMALLINT) table_len,
3267 			column, (SQLSMALLINT) column_len);
3268 
3269 	if (rc == SQL_ERROR) {
3270 		odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLColumns");
3271 		efree(result);
3272 		RETURN_FALSE;
3273 	}
3274 
3275 	result->numparams = 0;
3276 	SQLNumResultCols(result->stmt, &(result->numcols));
3277 
3278 	if (result->numcols > 0) {
3279 		if (!odbc_bindcols(result)) {
3280 			efree(result);
3281 			RETURN_FALSE;
3282 		}
3283 	} else {
3284 		result->values = NULL;
3285 	}
3286 	result->conn_ptr = conn;
3287 	result->fetched = 0;
3288 	RETURN_RES(zend_register_resource(result, le_result));
3289 }
3290 /* }}} */
3291 
3292 #if !defined(HAVE_DBMAKER) && !defined(HAVE_SOLID) && !defined(HAVE_SOLID_30) && !defined(HAVE_SOLID_35) && !defined(HAVE_BIRDSTEP)
3293 /* {{{ proto resource odbc_columnprivileges(resource connection_id, string catalog, string schema, string table, string column)
3294    Returns a result identifier that can be used to fetch a list of columns and associated privileges for the specified table */
PHP_FUNCTION(odbc_columnprivileges)3295 PHP_FUNCTION(odbc_columnprivileges)
3296 {
3297 	zval *pv_conn;
3298 	odbc_result *result = NULL;
3299 	odbc_connection *conn;
3300 	char *cat = NULL, *schema, *table, *column;
3301 	size_t cat_len = 0, schema_len, table_len, column_len;
3302 	RETCODE rc;
3303 
3304 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rs!sss", &pv_conn, &cat, &cat_len, &schema, &schema_len,
3305 		&table, &table_len, &column, &column_len) == FAILURE) {
3306 		return;
3307 	}
3308 
3309 	if (!(conn = (odbc_connection *)zend_fetch_resource2(Z_RES_P(pv_conn), "ODBC-Link", le_conn, le_pconn))) {
3310 		RETURN_FALSE;
3311 	}
3312 
3313 	result = (odbc_result *)ecalloc(1, sizeof(odbc_result));
3314 
3315 	rc = PHP_ODBC_SQLALLOCSTMT(conn->hdbc, &(result->stmt));
3316 	if (rc == SQL_INVALID_HANDLE) {
3317 		efree(result);
3318 		php_error_docref(NULL, E_WARNING, "SQLAllocStmt error 'Invalid Handle'");
3319 		RETURN_FALSE;
3320 	}
3321 
3322 	if (rc == SQL_ERROR) {
3323 		odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLAllocStmt");
3324 		efree(result);
3325 		RETURN_FALSE;
3326 	}
3327 
3328 	rc = SQLColumnPrivileges(result->stmt,
3329 			cat, SAFE_SQL_NTS(cat),
3330 			schema, SAFE_SQL_NTS(schema),
3331 			table, SAFE_SQL_NTS(table),
3332 			column, SAFE_SQL_NTS(column));
3333 
3334 	if (rc == SQL_ERROR) {
3335 		odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLColumnPrivileges");
3336 		efree(result);
3337 		RETURN_FALSE;
3338 	}
3339 
3340 	result->numparams = 0;
3341 	SQLNumResultCols(result->stmt, &(result->numcols));
3342 
3343 	if (result->numcols > 0) {
3344 		if (!odbc_bindcols(result)) {
3345 			efree(result);
3346 			RETURN_FALSE;
3347 		}
3348 	} else {
3349 		result->values = NULL;
3350 	}
3351 	result->conn_ptr = conn;
3352 	result->fetched = 0;
3353 	RETURN_RES(zend_register_resource(result, le_result));
3354 }
3355 /* }}} */
3356 #endif /* HAVE_DBMAKER || HAVE_SOLID*/
3357 
3358 #if !defined(HAVE_SOLID) && !defined(HAVE_SOLID_30) && !defined(HAVE_SOLID_35)
3359 /* {{{ proto resource odbc_foreignkeys(resource connection_id, string pk_qualifier, string pk_owner, string pk_table, string fk_qualifier, string fk_owner, string fk_table)
3360    Returns a result identifier to either a list of foreign keys in the specified table or a list of foreign keys in other tables that refer to the primary key in the specified table */
PHP_FUNCTION(odbc_foreignkeys)3361 PHP_FUNCTION(odbc_foreignkeys)
3362 {
3363 	zval *pv_conn;
3364 	odbc_result *result = NULL;
3365 	odbc_connection *conn;
3366 	char *pcat = NULL, *pschema, *ptable, *fcat, *fschema, *ftable;
3367 	size_t pcat_len = 0, pschema_len, ptable_len, fcat_len, fschema_len, ftable_len;
3368 	RETCODE rc;
3369 
3370 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rs!sssss", &pv_conn, &pcat, &pcat_len, &pschema, &pschema_len,
3371 		&ptable, &ptable_len, &fcat, &fcat_len, &fschema, &fschema_len, &ftable, &ftable_len) == FAILURE) {
3372 		return;
3373 	}
3374 
3375 #if defined(HAVE_DBMAKER) || defined(HAVE_IBMDB2)
3376 #define EMPTY_TO_NULL(xstr) \
3377 	if ((int)strlen((xstr)) == 0) (xstr) = NULL
3378 
3379 		EMPTY_TO_NULL(pcat);
3380 		EMPTY_TO_NULL(pschema);
3381 		EMPTY_TO_NULL(ptable);
3382 		EMPTY_TO_NULL(fcat);
3383 		EMPTY_TO_NULL(fschema);
3384 		EMPTY_TO_NULL(ftable);
3385 #endif
3386 
3387 	if (!(conn = (odbc_connection *)zend_fetch_resource2(Z_RES_P(pv_conn), "ODBC-Link", le_conn, le_pconn))) {
3388 		RETURN_FALSE;
3389 	}
3390 
3391 	result = (odbc_result *)ecalloc(1, sizeof(odbc_result));
3392 
3393 	rc = PHP_ODBC_SQLALLOCSTMT(conn->hdbc, &(result->stmt));
3394 	if (rc == SQL_INVALID_HANDLE) {
3395 		efree(result);
3396 		php_error_docref(NULL, E_WARNING, "SQLAllocStmt error 'Invalid Handle'");
3397 		RETURN_FALSE;
3398 	}
3399 
3400 	if (rc == SQL_ERROR) {
3401 		odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLAllocStmt");
3402 		efree(result);
3403 		RETURN_FALSE;
3404 	}
3405 
3406 	rc = SQLForeignKeys(result->stmt,
3407 			pcat, SAFE_SQL_NTS(pcat),
3408 			pschema, SAFE_SQL_NTS(pschema),
3409 			ptable, SAFE_SQL_NTS(ptable),
3410 			fcat, SAFE_SQL_NTS(fcat),
3411 			fschema, SAFE_SQL_NTS(fschema),
3412 			ftable, SAFE_SQL_NTS(ftable) );
3413 
3414 	if (rc == SQL_ERROR) {
3415 		odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLForeignKeys");
3416 		efree(result);
3417 		RETURN_FALSE;
3418 	}
3419 
3420 	result->numparams = 0;
3421 	SQLNumResultCols(result->stmt, &(result->numcols));
3422 
3423 	if (result->numcols > 0) {
3424 		if (!odbc_bindcols(result)) {
3425 			efree(result);
3426 			RETURN_FALSE;
3427 		}
3428 	} else {
3429 		result->values = NULL;
3430 	}
3431 	result->conn_ptr = conn;
3432 	result->fetched = 0;
3433 	RETURN_RES(zend_register_resource(result, le_result));
3434 }
3435 /* }}} */
3436 #endif /* HAVE_SOLID */
3437 
3438 /* {{{ proto resource odbc_gettypeinfo(resource connection_id [, int data_type])
3439    Returns a result identifier containing information about data types supported by the data source */
PHP_FUNCTION(odbc_gettypeinfo)3440 PHP_FUNCTION(odbc_gettypeinfo)
3441 {
3442 	zval *pv_conn;
3443 	zend_long pv_data_type = SQL_ALL_TYPES;
3444 	odbc_result *result = NULL;
3445 	odbc_connection *conn;
3446 	RETCODE rc;
3447 	SQLSMALLINT data_type;
3448 
3449 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "r|l", &pv_conn, &pv_data_type) == FAILURE) {
3450 		return;
3451 	}
3452 
3453 	data_type = (SQLSMALLINT) pv_data_type;
3454 
3455 	if (!(conn = (odbc_connection *)zend_fetch_resource2(Z_RES_P(pv_conn), "ODBC-Link", le_conn, le_pconn))) {
3456 		RETURN_FALSE;
3457 	}
3458 
3459 	result = (odbc_result *)ecalloc(1, sizeof(odbc_result));
3460 
3461 	rc = PHP_ODBC_SQLALLOCSTMT(conn->hdbc, &(result->stmt));
3462 	if (rc == SQL_INVALID_HANDLE) {
3463 		efree(result);
3464 		php_error_docref(NULL, E_WARNING, "SQLAllocStmt error 'Invalid Handle'");
3465 		RETURN_FALSE;
3466 	}
3467 
3468 	if (rc == SQL_ERROR) {
3469 		odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLAllocStmt");
3470 		efree(result);
3471 		RETURN_FALSE;
3472 	}
3473 
3474 	rc = SQLGetTypeInfo(result->stmt, data_type );
3475 
3476 	if (rc == SQL_ERROR) {
3477 		odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLGetTypeInfo");
3478 		efree(result);
3479 		RETURN_FALSE;
3480 	}
3481 
3482 	result->numparams = 0;
3483 	SQLNumResultCols(result->stmt, &(result->numcols));
3484 
3485 	if (result->numcols > 0) {
3486 		if (!odbc_bindcols(result)) {
3487 			efree(result);
3488 			RETURN_FALSE;
3489 		}
3490 	} else {
3491 		result->values = NULL;
3492 	}
3493 	result->conn_ptr = conn;
3494 	result->fetched = 0;
3495 	RETURN_RES(zend_register_resource(result, le_result));
3496 }
3497 /* }}} */
3498 
3499 /* {{{ proto resource odbc_primarykeys(resource connection_id, string qualifier, string owner, string table)
3500    Returns a result identifier listing the column names that comprise the primary key for a table */
PHP_FUNCTION(odbc_primarykeys)3501 PHP_FUNCTION(odbc_primarykeys)
3502 {
3503 	zval *pv_conn;
3504 	odbc_result   *result = NULL;
3505 	odbc_connection *conn;
3506 	char *cat = NULL, *schema = NULL, *table = NULL;
3507 	size_t cat_len = 0, schema_len, table_len;
3508 	RETCODE rc;
3509 
3510 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rs!ss", &pv_conn, &cat, &cat_len, &schema, &schema_len, &table, &table_len) == FAILURE) {
3511 		return;
3512 	}
3513 
3514 	if (!(conn = (odbc_connection *)zend_fetch_resource2(Z_RES_P(pv_conn), "ODBC-Link", le_conn, le_pconn))) {
3515 		RETURN_FALSE;
3516 	}
3517 
3518 	result = (odbc_result *)ecalloc(1, sizeof(odbc_result));
3519 
3520 	rc = PHP_ODBC_SQLALLOCSTMT(conn->hdbc, &(result->stmt));
3521 	if (rc == SQL_INVALID_HANDLE) {
3522 		efree(result);
3523 		php_error_docref(NULL, E_WARNING, "SQLAllocStmt error 'Invalid Handle'");
3524 		RETURN_FALSE;
3525 	}
3526 
3527 	if (rc == SQL_ERROR) {
3528 		odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLAllocStmt");
3529 		efree(result);
3530 		RETURN_FALSE;
3531 	}
3532 
3533 	rc = SQLPrimaryKeys(result->stmt,
3534 			cat, SAFE_SQL_NTS(cat),
3535 			schema, SAFE_SQL_NTS(schema),
3536 			table, SAFE_SQL_NTS(table) );
3537 
3538 	if (rc == SQL_ERROR) {
3539 		odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLPrimaryKeys");
3540 		efree(result);
3541 		RETURN_FALSE;
3542 	}
3543 
3544 	result->numparams = 0;
3545 	SQLNumResultCols(result->stmt, &(result->numcols));
3546 
3547 	if (result->numcols > 0) {
3548 		if (!odbc_bindcols(result)) {
3549 			efree(result);
3550 			RETURN_FALSE;
3551 		}
3552 	} else {
3553 		result->values = NULL;
3554 	}
3555 	result->conn_ptr = conn;
3556 	result->fetched = 0;
3557 	RETURN_RES(zend_register_resource(result, le_result));
3558 }
3559 /* }}} */
3560 
3561 #if !defined(HAVE_SOLID) && !defined(HAVE_SOLID_30) && !defined(HAVE_SOLID_35) && !defined(HAVE_BIRDSTEP)
3562 /* {{{ proto resource odbc_procedurecolumns(resource connection_id [, string qualifier, string owner, string proc, string column])
3563    Returns a result identifier containing the list of input and output parameters, as well as the columns that make up the result set for the specified procedures */
PHP_FUNCTION(odbc_procedurecolumns)3564 PHP_FUNCTION(odbc_procedurecolumns)
3565 {
3566 	zval *pv_conn;
3567 	odbc_result *result = NULL;
3568 	odbc_connection *conn;
3569 	char *cat = NULL, *schema = NULL, *proc = NULL, *col = NULL;
3570 	size_t cat_len = 0, schema_len = 0, proc_len = 0, col_len = 0;
3571 	RETCODE rc;
3572 
3573 	if (ZEND_NUM_ARGS() != 1 && ZEND_NUM_ARGS() != 5) {
3574 		WRONG_PARAM_COUNT;
3575 	}
3576 
3577 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "r|s!sss", &pv_conn, &cat, &cat_len, &schema, &schema_len,
3578 		&proc, &proc_len, &col, &col_len) == FAILURE) {
3579 		return;
3580 	}
3581 
3582 	if (!(conn = (odbc_connection *)zend_fetch_resource2(Z_RES_P(pv_conn), "ODBC-Link", le_conn, le_pconn))) {
3583 		RETURN_FALSE;
3584 	}
3585 
3586 	result = (odbc_result *)ecalloc(1, sizeof(odbc_result));
3587 
3588 	rc = PHP_ODBC_SQLALLOCSTMT(conn->hdbc, &(result->stmt));
3589 	if (rc == SQL_INVALID_HANDLE) {
3590 		efree(result);
3591 		php_error_docref(NULL, E_WARNING, "SQLAllocStmt error 'Invalid Handle'");
3592 		RETURN_FALSE;
3593 	}
3594 
3595 	if (rc == SQL_ERROR) {
3596 		odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLAllocStmt");
3597 		efree(result);
3598 		RETURN_FALSE;
3599 	}
3600 
3601 	rc = SQLProcedureColumns(result->stmt,
3602 			cat, SAFE_SQL_NTS(cat),
3603 			schema, SAFE_SQL_NTS(schema),
3604 			proc, SAFE_SQL_NTS(proc),
3605 			col, SAFE_SQL_NTS(col) );
3606 
3607 	if (rc == SQL_ERROR) {
3608 		odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLProcedureColumns");
3609 		efree(result);
3610 		RETURN_FALSE;
3611 	}
3612 
3613 	result->numparams = 0;
3614 	SQLNumResultCols(result->stmt, &(result->numcols));
3615 
3616 	if (result->numcols > 0) {
3617 		if (!odbc_bindcols(result)) {
3618 			efree(result);
3619 			RETURN_FALSE;
3620 		}
3621 	} else {
3622 		result->values = NULL;
3623 	}
3624 	result->conn_ptr = conn;
3625 	result->fetched = 0;
3626 	RETURN_RES(zend_register_resource(result, le_result));
3627 }
3628 /* }}} */
3629 #endif /* HAVE_SOLID */
3630 
3631 #if !defined(HAVE_SOLID) && !defined(HAVE_SOLID_30) && !defined(HAVE_SOLID_35)
3632 /* {{{ proto resource odbc_procedures(resource connection_id [, string qualifier, string owner, string name])
3633    Returns a result identifier containg the list of procedure names in a datasource */
PHP_FUNCTION(odbc_procedures)3634 PHP_FUNCTION(odbc_procedures)
3635 {
3636 	zval *pv_conn;
3637 	odbc_result   *result = NULL;
3638 	odbc_connection *conn;
3639 	char *cat = NULL, *schema = NULL, *proc = NULL;
3640 	size_t cat_len = 0, schema_len = 0, proc_len = 0;
3641 	RETCODE rc;
3642 
3643 	if (ZEND_NUM_ARGS() != 1 && ZEND_NUM_ARGS() != 4) {
3644 		WRONG_PARAM_COUNT;
3645 	}
3646 
3647 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "r|s!ss", &pv_conn, &cat, &cat_len, &schema, &schema_len, &proc, &proc_len) == FAILURE) {
3648 		return;
3649 	}
3650 
3651 	if (!(conn = (odbc_connection *)zend_fetch_resource2(Z_RES_P(pv_conn), "ODBC-Link", le_conn, le_pconn))) {
3652 		RETURN_FALSE;
3653 	}
3654 
3655 	result = (odbc_result *)ecalloc(1, sizeof(odbc_result));
3656 
3657 	rc = PHP_ODBC_SQLALLOCSTMT(conn->hdbc, &(result->stmt));
3658 	if (rc == SQL_INVALID_HANDLE) {
3659 		efree(result);
3660 		php_error_docref(NULL, E_WARNING, "SQLAllocStmt error 'Invalid Handle'");
3661 		RETURN_FALSE;
3662 	}
3663 
3664 	if (rc == SQL_ERROR) {
3665 		odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLAllocStmt");
3666 		efree(result);
3667 		RETURN_FALSE;
3668 	}
3669 
3670 	rc = SQLProcedures(result->stmt,
3671 			cat, SAFE_SQL_NTS(cat),
3672 			schema, SAFE_SQL_NTS(schema),
3673 			proc, SAFE_SQL_NTS(proc) );
3674 
3675 	if (rc == SQL_ERROR) {
3676 		odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLProcedures");
3677 		efree(result);
3678 		RETURN_FALSE;
3679 	}
3680 
3681 	result->numparams = 0;
3682 	SQLNumResultCols(result->stmt, &(result->numcols));
3683 
3684 	if (result->numcols > 0) {
3685 		if (!odbc_bindcols(result)) {
3686 			efree(result);
3687 			RETURN_FALSE;
3688 		}
3689 	} else {
3690 		result->values = NULL;
3691 	}
3692 	result->conn_ptr = conn;
3693 	result->fetched = 0;
3694 	RETURN_RES(zend_register_resource(result, le_result));
3695 }
3696 /* }}} */
3697 #endif /* HAVE_SOLID */
3698 
3699 /* {{{ proto resource odbc_specialcolumns(resource connection_id, int type, string qualifier, string owner, string table, int scope, int nullable)
3700    Returns a result identifier containing either the optimal set of columns that uniquely identifies a row in the table or columns that are automatically updated when any value in the row is updated by a transaction */
PHP_FUNCTION(odbc_specialcolumns)3701 PHP_FUNCTION(odbc_specialcolumns)
3702 {
3703 	zval *pv_conn;
3704 	zend_long vtype, vscope, vnullable;
3705 	odbc_result *result = NULL;
3706 	odbc_connection *conn;
3707 	char *cat = NULL, *schema = NULL, *name = NULL;
3708 	size_t cat_len = 0, schema_len, name_len;
3709 	SQLUSMALLINT type, scope, nullable;
3710 	RETCODE rc;
3711 
3712 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rls!ssl", &pv_conn, &vtype, &cat, &cat_len, &schema, &schema_len,
3713 		&name, &name_len, &vscope, &vnullable) == FAILURE) {
3714 		return;
3715 	}
3716 
3717 	type = (SQLUSMALLINT) vtype;
3718 	scope = (SQLUSMALLINT) vscope;
3719 	nullable = (SQLUSMALLINT) vnullable;
3720 
3721 	if (!(conn = (odbc_connection *)zend_fetch_resource2(Z_RES_P(pv_conn), "ODBC-Link", le_conn, le_pconn))) {
3722 		RETURN_FALSE;
3723 	}
3724 
3725 	result = (odbc_result *)ecalloc(1, sizeof(odbc_result));
3726 
3727 	rc = PHP_ODBC_SQLALLOCSTMT(conn->hdbc, &(result->stmt));
3728 	if (rc == SQL_INVALID_HANDLE) {
3729 		efree(result);
3730 		php_error_docref(NULL, E_WARNING, "SQLAllocStmt error 'Invalid Handle'");
3731 		RETURN_FALSE;
3732 	}
3733 
3734 	if (rc == SQL_ERROR) {
3735 		odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLAllocStmt");
3736 		efree(result);
3737 		RETURN_FALSE;
3738 	}
3739 
3740 	rc = SQLSpecialColumns(result->stmt,
3741 			type,
3742 			cat, SAFE_SQL_NTS(cat),
3743 			schema, SAFE_SQL_NTS(schema),
3744 			name, SAFE_SQL_NTS(name),
3745 			scope,
3746 			nullable);
3747 
3748 	if (rc == SQL_ERROR) {
3749 		odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLSpecialColumns");
3750 		efree(result);
3751 		RETURN_FALSE;
3752 	}
3753 
3754 	result->numparams = 0;
3755 	SQLNumResultCols(result->stmt, &(result->numcols));
3756 
3757 	if (result->numcols > 0) {
3758 		if (!odbc_bindcols(result)) {
3759 			efree(result);
3760 			RETURN_FALSE;
3761 		}
3762 	} else {
3763 		result->values = NULL;
3764 	}
3765 	result->conn_ptr = conn;
3766 	result->fetched = 0;
3767 	RETURN_RES(zend_register_resource(result, le_result));
3768 }
3769 /* }}} */
3770 
3771 /* {{{ proto resource odbc_statistics(resource connection_id, string qualifier, string owner, string name, int unique, int accuracy)
3772    Returns a result identifier that contains statistics about a single table and the indexes associated with the table */
PHP_FUNCTION(odbc_statistics)3773 PHP_FUNCTION(odbc_statistics)
3774 {
3775 	zval *pv_conn;
3776 	zend_long vunique, vreserved;
3777 	odbc_result *result = NULL;
3778 	odbc_connection *conn;
3779 	char *cat = NULL, *schema, *name;
3780 	size_t cat_len = 0, schema_len, name_len;
3781 	SQLUSMALLINT unique, reserved;
3782 	RETCODE rc;
3783 
3784 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rs!ssll", &pv_conn, &cat, &cat_len, &schema, &schema_len,
3785 		&name, &name_len, &vunique, &vreserved) == FAILURE) {
3786 		return;
3787 	}
3788 
3789 	unique = (SQLUSMALLINT) vunique;
3790 	reserved = (SQLUSMALLINT) vreserved;
3791 
3792 	if (!(conn = (odbc_connection *)zend_fetch_resource2(Z_RES_P(pv_conn), "ODBC-Link", le_conn, le_pconn))) {
3793 		RETURN_FALSE;
3794 	}
3795 
3796 	result = (odbc_result *)ecalloc(1, sizeof(odbc_result));
3797 
3798 	rc = PHP_ODBC_SQLALLOCSTMT(conn->hdbc, &(result->stmt));
3799 	if (rc == SQL_INVALID_HANDLE) {
3800 		efree(result);
3801 		php_error_docref(NULL, E_WARNING, "SQLAllocStmt error 'Invalid Handle'");
3802 		RETURN_FALSE;
3803 	}
3804 
3805 	if (rc == SQL_ERROR) {
3806 		odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLAllocStmt");
3807 		efree(result);
3808 		RETURN_FALSE;
3809 	}
3810 
3811 	rc = SQLStatistics(result->stmt,
3812 			cat, SAFE_SQL_NTS(cat),
3813 			schema, SAFE_SQL_NTS(schema),
3814 			name, SAFE_SQL_NTS(name),
3815 			unique,
3816 			reserved);
3817 
3818 	if (rc == SQL_ERROR) {
3819 		odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLStatistics");
3820 		efree(result);
3821 		RETURN_FALSE;
3822 	}
3823 
3824 	result->numparams = 0;
3825 	SQLNumResultCols(result->stmt, &(result->numcols));
3826 
3827 	if (result->numcols > 0) {
3828 		if (!odbc_bindcols(result)) {
3829 			efree(result);
3830 			RETURN_FALSE;
3831 		}
3832 	} else {
3833 		result->values = NULL;
3834 	}
3835 	result->conn_ptr = conn;
3836 	result->fetched = 0;
3837 	RETURN_RES(zend_register_resource(result, le_result));
3838 }
3839 /* }}} */
3840 
3841 #if !defined(HAVE_DBMAKER) && !defined(HAVE_SOLID) && !defined(HAVE_SOLID_30) && !defined(HAVE_SOLID_35) && !defined(HAVE_BIRDSTEP)
3842 /* {{{ proto resource odbc_tableprivileges(resource connection_id, string qualifier, string owner, string name)
3843    Returns a result identifier containing a list of tables and the privileges associated with each table */
PHP_FUNCTION(odbc_tableprivileges)3844 PHP_FUNCTION(odbc_tableprivileges)
3845 {
3846 	zval *pv_conn;
3847 	odbc_result   *result = NULL;
3848 	odbc_connection *conn;
3849 	char *cat = NULL, *schema = NULL, *table = NULL;
3850 	size_t cat_len = 0, schema_len, table_len;
3851 	RETCODE rc;
3852 
3853 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rs!ss", &pv_conn, &cat, &cat_len, &schema, &schema_len, &table, &table_len) == FAILURE) {
3854 		return;
3855 	}
3856 
3857 	if (!(conn = (odbc_connection *)zend_fetch_resource2(Z_RES_P(pv_conn), "ODBC-Link", le_conn, le_pconn))) {
3858 		RETURN_FALSE;
3859 	}
3860 
3861 	result = (odbc_result *)ecalloc(1, sizeof(odbc_result));
3862 
3863 	rc = PHP_ODBC_SQLALLOCSTMT(conn->hdbc, &(result->stmt));
3864 	if (rc == SQL_INVALID_HANDLE) {
3865 		efree(result);
3866 		php_error_docref(NULL, E_WARNING, "SQLAllocStmt error 'Invalid Handle'");
3867 		RETURN_FALSE;
3868 	}
3869 
3870 	if (rc == SQL_ERROR) {
3871 		odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLAllocStmt");
3872 		efree(result);
3873 		RETURN_FALSE;
3874 	}
3875 
3876 	rc = SQLTablePrivileges(result->stmt,
3877 			cat, SAFE_SQL_NTS(cat),
3878 			schema, SAFE_SQL_NTS(schema),
3879 			table, SAFE_SQL_NTS(table));
3880 
3881 	if (rc == SQL_ERROR) {
3882 		odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLTablePrivileges");
3883 		efree(result);
3884 		RETURN_FALSE;
3885 	}
3886 
3887 	result->numparams = 0;
3888 	SQLNumResultCols(result->stmt, &(result->numcols));
3889 
3890 	if (result->numcols > 0) {
3891 		if (!odbc_bindcols(result)) {
3892 			efree(result);
3893 			RETURN_FALSE;
3894 		}
3895 	} else {
3896 		result->values = NULL;
3897 	}
3898 	result->conn_ptr = conn;
3899 	result->fetched = 0;
3900 	RETURN_RES(zend_register_resource(result, le_result));
3901 }
3902 /* }}} */
3903 #endif /* HAVE_DBMAKER */
3904 
3905 #endif /* HAVE_UODBC */
3906 
3907 /*
3908  * Local variables:
3909  * tab-width: 4
3910  * c-basic-offset: 4
3911  * End:
3912  * vim600: sw=4 ts=4 fdm=marker
3913  * vim<600: sw=4 ts=4
3914  */
3915