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