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