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