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