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