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