forked from php/php-src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
php_odbc.c
3520 lines (2984 loc) · 94.7 KB
/
php_odbc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
+----------------------------------------------------------------------+
| Copyright (c) The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| https://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Stig Sæther Bakken <[email protected]> |
| Andreas Karajannis <[email protected]> |
| Frank M. Kromann <[email protected]> Support for DB/2 CLI |
| Kevin N. Shallow <[email protected]> |
| Daniel R. Kalowsky <[email protected]> |
+----------------------------------------------------------------------+
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "php.h"
#include "php_globals.h"
#include "zend_attributes.h"
#include "ext/standard/info.h"
#include "Zend/zend_interfaces.h"
#include "zend_smart_str.h"
#include "php_odbc.h"
#include "php_odbc_includes.h"
/* actually lives in main/ */
#include "php_odbc_utils.h"
#ifdef HAVE_UODBC
#include <fcntl.h>
#include "php_ini.h"
#define PHP_ODBC_BINMODE_PASSTHRU 0
#define PHP_ODBC_BINMODE_RETURN 1
#define PHP_ODBC_BINMODE_CONVERT 2
#include "odbc_arginfo.h"
#define CHECK_ODBC_CONNECTION(conn) \
if (conn == NULL) { \
zend_throw_error(NULL, "ODBC connection has already been closed"); \
RETURN_THROWS(); \
}
#define CHECK_ODBC_RESULT(result) \
if (result->conn_ptr == NULL) { \
zend_throw_error(NULL, "ODBC result has already been closed"); \
RETURN_THROWS(); \
}
void odbc_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent);
static void safe_odbc_disconnect(void *handle);
static void close_results_with_connection(odbc_connection *conn);
static inline odbc_result *odbc_result_from_obj(zend_object *obj);
static int le_pconn;
static zend_class_entry *odbc_connection_ce, *odbc_result_ce;
static zend_object_handlers odbc_connection_object_handlers, odbc_result_object_handlers;
#define Z_ODBC_LINK_P(zv) odbc_link_from_obj(Z_OBJ_P(zv))
#define Z_ODBC_CONNECTION_P(zv) Z_ODBC_LINK_P(zv)->connection
#define Z_ODBC_RESULT_P(zv) odbc_result_from_obj(Z_OBJ_P(zv))
static void odbc_insert_new_result(odbc_connection *connection, zval *result)
{
ZEND_ASSERT(Z_TYPE_P(result) == IS_OBJECT);
#if ZEND_DEBUG
ZEND_ASSERT(instanceof_function(Z_OBJCE_P(result), odbc_result_ce));
#endif
odbc_result *res = Z_ODBC_RESULT_P(result);
res->index = connection->results.nNextFreeElement;
zend_hash_index_add_new(&connection->results, res->index, result);
Z_ADDREF_P(result);
}
static inline odbc_link *odbc_link_from_obj(zend_object *obj)
{
return (odbc_link *)((char *)(obj) - XtOffsetOf(odbc_link, std));
}
static int _close_pconn_with_res(zval *zv, void *p)
{
zend_resource *le = Z_RES_P(zv);
if (le->ptr == p) {
return ZEND_HASH_APPLY_REMOVE;
}
return ZEND_HASH_APPLY_KEEP;
}
static int _close_pconn(zval *zv)
{
zend_resource *le = Z_RES_P(zv);
if (le->type == le_pconn) {
return ZEND_HASH_APPLY_REMOVE;
} else {
return ZEND_HASH_APPLY_KEEP;
}
}
/* disconnect, and if it fails, then issue a rollback for any pending transaction (lurcher) */
static void safe_odbc_disconnect( void *handle )
{
int ret = SQLDisconnect( handle );
if ( ret == SQL_ERROR )
{
SQLTransact( NULL, handle, SQL_ROLLBACK );
SQLDisconnect( handle );
}
}
static void free_connection(odbc_connection *conn, bool persistent)
{
/* If aborted via timer expiration, don't try to call any unixODBC function */
if (!(PG(connection_status) & PHP_CONNECTION_TIMEOUT)) {
safe_odbc_disconnect(conn->hdbc);
SQLFreeConnect(conn->hdbc);
SQLFreeEnv(conn->henv);
}
conn->hdbc = NULL;
conn->henv = NULL;
zend_hash_destroy(&conn->results);
pefree(conn, persistent);
ODBCG(num_links)--;
if (persistent) {
ODBCG(num_persistent)--;
}
}
static void odbc_link_free(odbc_link *link)
{
ZEND_ASSERT(link->connection && "link has already been closed");
close_results_with_connection(link->connection);
if (!link->persistent) {
free_connection(link->connection, link->persistent);
}
link->connection = NULL;
if (link->hash) {
zend_hash_del(&ODBCG(connections), link->hash);
zend_string_release_ex(link->hash, link->persistent);
link->hash = NULL;
}
}
static zend_object *odbc_connection_create_object(zend_class_entry *class_type)
{
odbc_link *intern = zend_object_alloc(sizeof(odbc_link), class_type);
zend_object_std_init(&intern->std, class_type);
object_properties_init(&intern->std, class_type);
return &intern->std;
}
static zend_function *odbc_connection_get_constructor(zend_object *object)
{
zend_throw_error(NULL, "Cannot directly construct Odbc\\Connection, use odbc_connect() or odbc_pconnect() instead");
return NULL;
}
static zend_result odbc_connection_cast_object(zend_object *obj, zval *result, int type)
{
if (type == IS_LONG) {
ZVAL_LONG(result, obj->handle);
return SUCCESS;
}
return zend_std_cast_object_tostring(obj, result, type);
}
static void odbc_connection_free_obj(zend_object *obj)
{
odbc_link *link = odbc_link_from_obj(obj);
if (link->connection) {
odbc_link_free(link);
}
zend_object_std_dtor(&link->std);
}
static inline odbc_result *odbc_result_from_obj(zend_object *obj)
{
return (odbc_result *)((char *)(obj) - XtOffsetOf(odbc_result, std));
}
static zend_object *odbc_result_create_object(zend_class_entry *class_type)
{
odbc_result *intern = zend_object_alloc(sizeof(odbc_result), class_type);
zend_object_std_init(&intern->std, class_type);
object_properties_init(&intern->std, class_type);
return &intern->std;
}
static zend_function *odbc_result_get_constructor(zend_object *object)
{
zend_throw_error(NULL, "Cannot directly construct Odbc\\Result, use an appropriate odbc_* function instead");
return NULL;
}
static zend_result odbc_result_cast_object(zend_object *obj, zval *result, int type)
{
if (type == IS_LONG) {
ZVAL_LONG(result, obj->handle);
return SUCCESS;
}
return zend_std_cast_object_tostring(obj, result, type);
}
static void odbc_result_free(odbc_result *res)
{
ZEND_ASSERT(res->conn_ptr && "result has already been closed");
if (res->values) {
for (int i = 0; i < res->numcols; i++) {
if (res->values[i].value) {
efree(res->values[i].value);
}
}
efree(res->values);
res->values = NULL;
res->numcols = 0;
}
/* If aborted via timer expiration, don't try to call any unixODBC function */
if (res->stmt && !(PG(connection_status) & PHP_CONNECTION_TIMEOUT)) {
#if defined(HAVE_SOLID) || defined(HAVE_SOLID_30) || defined(HAVE_SOLID_35)
SQLTransact(res->conn_ptr->henv, res->conn_ptr->hdbc,
(SQLUSMALLINT) SQL_COMMIT);
#endif
SQLFreeStmt(res->stmt,SQL_DROP);
/* We don't want the connection to be closed after the last statement has been closed
* Connections will be closed on shutdown
*/
res->stmt = NULL;
}
if (res->param_info) {
efree(res->param_info);
res->param_info = NULL;
}
HashTable *results = &res->conn_ptr->results;
res->conn_ptr = NULL;
zend_result status = zend_hash_index_del(results, res->index);
ZEND_ASSERT(status == SUCCESS);
}
static void odbc_result_free_obj(zend_object *obj)
{
odbc_result *result = odbc_result_from_obj(obj);
if (result->conn_ptr) {
odbc_result_free(result);
}
zend_object_std_dtor(&result->std);
}
#define SAFE_SQL_NTS(n) ((SQLSMALLINT) ((n)?(SQL_NTS):0))
PHP_ODBC_API ZEND_DECLARE_MODULE_GLOBALS(odbc)
static PHP_GINIT_FUNCTION(odbc);
static PHP_GSHUTDOWN_FUNCTION(odbc);
/* {{{ odbc_module_entry */
zend_module_entry odbc_module_entry = {
STANDARD_MODULE_HEADER,
"odbc",
ext_functions,
PHP_MINIT(odbc),
PHP_MSHUTDOWN(odbc),
PHP_RINIT(odbc),
PHP_RSHUTDOWN(odbc),
PHP_MINFO(odbc),
PHP_ODBC_VERSION,
PHP_MODULE_GLOBALS(odbc),
PHP_GINIT(odbc),
PHP_GSHUTDOWN(odbc),
NULL,
STANDARD_MODULE_PROPERTIES_EX
};
/* }}} */
#ifdef COMPILE_DL_ODBC
#ifdef ZTS
ZEND_TSRMLS_CACHE_DEFINE()
#endif
ZEND_GET_MODULE(odbc)
#endif
static void close_results_with_connection(odbc_connection *conn)
{
zval *p;
ZEND_HASH_FOREACH_VAL(&conn->results, p) {
odbc_result *result = Z_ODBC_RESULT_P(p);
if (result->conn_ptr) {
odbc_result_free(result);
}
} ZEND_HASH_FOREACH_END();
zend_hash_clean(&conn->results);
}
/* {{{ void _close_odbc_pconn */
static void _close_odbc_pconn(zend_resource *rsrc)
{
odbc_connection *conn = (odbc_connection *)rsrc->ptr;
close_results_with_connection(conn);
free_connection(conn, true);
rsrc->ptr = NULL;
}
/* }}} */
/* {{{ PHP_INI_DISP(display_link_nums) */
static PHP_INI_DISP(display_link_nums)
{
char *value;
if (type == PHP_INI_DISPLAY_ORIG && ini_entry->modified) {
value = ZSTR_VAL(ini_entry->orig_value);
} else if (ini_entry->value) {
value = ZSTR_VAL(ini_entry->value);
} else {
value = NULL;
}
if (value) {
if (atoi(value) == -1) {
PUTS("Unlimited");
} else {
php_printf("%s", value);
}
}
}
/* }}} */
/* {{{ PHP_INI_DISP(display_binmode) */
static PHP_INI_DISP(display_binmode)
{
char *value;
if (type == PHP_INI_DISPLAY_ORIG && ini_entry->modified) {
value = ZSTR_VAL(ini_entry->orig_value);
} else if (ini_entry->value) {
value = ZSTR_VAL(ini_entry->value);
} else {
value = NULL;
}
if (value) {
switch(atoi(value)) {
case 0:
PUTS("passthru");
break;
case 1:
PUTS("return as is");
break;
case 2:
PUTS("return as char");
break;
}
}
}
/* }}} */
/* {{{ PHP_INI_DISP(display_lrl) */
static PHP_INI_DISP(display_lrl)
{
char *value;
if (type == PHP_INI_DISPLAY_ORIG && ini_entry->modified) {
value = ZSTR_VAL(ini_entry->orig_value);
} else if (ini_entry->value) {
value = ZSTR_VAL(ini_entry->value);
} else {
value = NULL;
}
if (value) {
if (atoi(value) <= 0) {
PUTS("Passthru");
} else {
php_printf("return up to %s bytes", value);
}
}
}
/* }}} */
/* {{{ PHP_INI_DISP(display_cursortype) */
static PHP_INI_DISP(display_cursortype)
{
char *value;
if (type == PHP_INI_DISPLAY_ORIG && ini_entry->modified) {
value = ZSTR_VAL(ini_entry->orig_value);
} else if (ini_entry->value) {
value = ZSTR_VAL(ini_entry->value);
} else {
value = NULL;
}
if (value) {
switch (atoi (value))
{
case SQL_CURSOR_FORWARD_ONLY:
PUTS ("Forward Only cursor");
break;
case SQL_CURSOR_STATIC:
PUTS ("Static cursor");
break;
case SQL_CURSOR_KEYSET_DRIVEN:
PUTS ("Keyset driven cursor");
break;
case SQL_CURSOR_DYNAMIC:
PUTS ("Dynamic cursor");
break;
default:
php_printf("Unknown cursor model %s", value);
break;
}
}
}
/* }}} */
/* {{{ PHP_INI_BEGIN */
PHP_INI_BEGIN()
STD_PHP_INI_BOOLEAN("odbc.allow_persistent", "1", PHP_INI_SYSTEM, OnUpdateBool,
allow_persistent, zend_odbc_globals, odbc_globals)
STD_PHP_INI_ENTRY_EX("odbc.max_persistent", "-1", PHP_INI_SYSTEM, OnUpdateLong,
max_persistent, zend_odbc_globals, odbc_globals, display_link_nums)
STD_PHP_INI_ENTRY_EX("odbc.max_links", "-1", PHP_INI_SYSTEM, OnUpdateLong,
max_links, zend_odbc_globals, odbc_globals, display_link_nums)
STD_PHP_INI_ENTRY_EX("odbc.defaultlrl", "4096", PHP_INI_ALL, OnUpdateLong,
defaultlrl, zend_odbc_globals, odbc_globals, display_lrl)
STD_PHP_INI_ENTRY_EX("odbc.defaultbinmode", "1", PHP_INI_ALL, OnUpdateLong,
defaultbinmode, zend_odbc_globals, odbc_globals, display_binmode)
STD_PHP_INI_BOOLEAN("odbc.check_persistent", "1", PHP_INI_SYSTEM, OnUpdateBool,
check_persistent, zend_odbc_globals, odbc_globals)
STD_PHP_INI_ENTRY_EX("odbc.default_cursortype", "3", PHP_INI_ALL, OnUpdateLong,
default_cursortype, zend_odbc_globals, odbc_globals, display_cursortype)
PHP_INI_END()
/* }}} */
static PHP_GINIT_FUNCTION(odbc)
{
#if defined(COMPILE_DL_ODBC) && defined(ZTS)
ZEND_TSRMLS_CACHE_UPDATE();
#endif
odbc_globals->num_persistent = 0;
zend_hash_init(&odbc_globals->connections, 0, NULL, NULL, true);
GC_MAKE_PERSISTENT_LOCAL(&odbc_globals->connections);
}
static PHP_GSHUTDOWN_FUNCTION(odbc)
{
zend_hash_destroy(&odbc_globals->connections);
}
/* {{{ PHP_MINIT_FUNCTION */
PHP_MINIT_FUNCTION(odbc)
{
#ifdef SQLANY_BUG
ODBC_SQL_CONN_T foobar;
RETCODE rc;
#endif
REGISTER_INI_ENTRIES();
le_pconn = zend_register_list_destructors_ex(NULL, _close_odbc_pconn, "odbc link persistent", module_number);
odbc_module_entry.type = type;
register_odbc_symbols(module_number);
odbc_connection_ce = register_class_Odbc_Connection();
odbc_connection_ce->create_object = odbc_connection_create_object;
odbc_connection_ce->default_object_handlers = &odbc_connection_object_handlers;
memcpy(&odbc_connection_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
odbc_connection_object_handlers.offset = XtOffsetOf(odbc_link, std);
odbc_connection_object_handlers.free_obj = odbc_connection_free_obj;
odbc_connection_object_handlers.get_constructor = odbc_connection_get_constructor;
odbc_connection_object_handlers.clone_obj = NULL;
odbc_connection_object_handlers.cast_object = odbc_connection_cast_object;
odbc_connection_object_handlers.compare = zend_objects_not_comparable;
odbc_result_ce = register_class_Odbc_Result();
odbc_result_ce->create_object = odbc_result_create_object;
odbc_result_ce->default_object_handlers = &odbc_result_object_handlers;
memcpy(&odbc_result_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
odbc_result_object_handlers.offset = XtOffsetOf(odbc_result, std);
odbc_result_object_handlers.free_obj = odbc_result_free_obj;
odbc_result_object_handlers.get_constructor = odbc_result_get_constructor;
odbc_result_object_handlers.clone_obj = NULL;
odbc_result_object_handlers.cast_object = odbc_result_cast_object;
odbc_result_object_handlers.compare = zend_objects_not_comparable;
#if defined(HAVE_IBMDB2) && defined(_AIX)
/* atexit() handler in the DB2/AIX library segfaults in PHP CLI */
/* DB2NOEXITLIST env variable prevents DB2 from invoking atexit() */
putenv("DB2NOEXITLIST=TRUE");
#endif
return SUCCESS;
}
/* }}} */
/* {{{ PHP_RINIT_FUNCTION */
PHP_RINIT_FUNCTION(odbc)
{
ODBCG(num_links) = ODBCG(num_persistent);
memset(ODBCG(laststate), '\0', 6);
memset(ODBCG(lasterrormsg), '\0', SQL_MAX_MESSAGE_LENGTH);
return SUCCESS;
}
/* }}} */
/* {{{ PHP_RSHUTDOWN_FUNCTION */
PHP_RSHUTDOWN_FUNCTION(odbc)
{
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MSHUTDOWN_FUNCTION */
PHP_MSHUTDOWN_FUNCTION(odbc)
{
UNREGISTER_INI_ENTRIES();
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MINFO_FUNCTION */
PHP_MINFO_FUNCTION(odbc)
{
char buf[32];
php_info_print_table_start();
php_info_print_table_row(2, "ODBC Support", "enabled");
snprintf(buf, sizeof(buf), ZEND_LONG_FMT, ODBCG(num_persistent));
php_info_print_table_row(2, "Active Persistent Links", buf);
snprintf(buf, sizeof(buf), ZEND_LONG_FMT, ODBCG(num_links));
php_info_print_table_row(2, "Active Links", buf);
php_info_print_table_row(2, "ODBC library", PHP_ODBC_TYPE);
#ifdef ODBCVER
snprintf(buf, sizeof(buf), "0x%.4x", ODBCVER);
php_info_print_table_row(2, "ODBCVER", buf);
#endif
#ifndef PHP_WIN32
php_info_print_table_row(2, "ODBC_CFLAGS", PHP_ODBC_CFLAGS);
php_info_print_table_row(2, "ODBC_LFLAGS", PHP_ODBC_LFLAGS);
php_info_print_table_row(2, "ODBC_LIBS", PHP_ODBC_LIBS);
#endif
php_info_print_table_end();
DISPLAY_INI_ENTRIES();
}
/* }}} */
/* {{{ odbc_sql_error */
void odbc_sql_error(ODBC_SQL_ERROR_PARAMS)
{
SQLINTEGER error; /* Not used */
SQLSMALLINT errormsgsize; /* Not used */
RETCODE rc;
ODBC_SQL_ENV_T henv;
ODBC_SQL_CONN_T conn;
if (conn_resource) {
henv = conn_resource->henv;
conn = conn_resource->hdbc;
} else {
henv = SQL_NULL_HENV;
conn = SQL_NULL_HDBC;
}
/* This leads to an endless loop in many drivers!
*
while(henv != SQL_NULL_HENV){
do {
*/
rc = SQLError(henv, conn, stmt, (SQLCHAR *) ODBCG(laststate), &error, (SQLCHAR *) ODBCG(lasterrormsg), sizeof(ODBCG(lasterrormsg))-1, &errormsgsize);
if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
snprintf(ODBCG(laststate), sizeof(ODBCG(laststate)), "HY000");
snprintf(ODBCG(lasterrormsg), sizeof(ODBCG(lasterrormsg)), "Failed to fetch error message");
}
if (conn_resource) {
memcpy(conn_resource->laststate, ODBCG(laststate), sizeof(ODBCG(laststate)));
memcpy(conn_resource->lasterrormsg, ODBCG(lasterrormsg), sizeof(ODBCG(lasterrormsg)));
}
if (func) {
php_error_docref(NULL, E_WARNING, "SQL error: %s, SQL state %s in %s", ODBCG(lasterrormsg), ODBCG(laststate), func);
} else {
php_error_docref(NULL, E_WARNING, "SQL error: %s, SQL state %s", ODBCG(lasterrormsg), ODBCG(laststate));
}
/*
} while (SQL_SUCCEEDED(rc));
}
*/
}
/* }}} */
/* {{{ php_odbc_fetch_attribs */
void php_odbc_fetch_attribs(INTERNAL_FUNCTION_PARAMETERS, int mode)
{
odbc_result *result;
zval *pv_res;
zend_long flag;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ol", &pv_res, odbc_result_ce, &flag) == FAILURE) {
RETURN_THROWS();
}
result = Z_ODBC_RESULT_P(pv_res);
CHECK_ODBC_RESULT(result);
if (mode) {
result->longreadlen = flag;
} else {
result->binmode = flag;
}
RETURN_TRUE;
}
/* }}} */
/* {{{ odbc_bindcols */
void odbc_bindcols(odbc_result *result)
{
RETCODE rc;
int i;
SQLSMALLINT colnamelen; /* Not used */
SQLLEN displaysize;
SQLUSMALLINT colfieldid;
int charextraalloc;
result->values = (odbc_result_value *) safe_emalloc(sizeof(odbc_result_value), result->numcols, 0);
result->longreadlen = ODBCG(defaultlrl);
result->binmode = ODBCG(defaultbinmode);
for(i = 0; i < result->numcols; i++) {
charextraalloc = 0;
colfieldid = SQL_COLUMN_DISPLAY_SIZE;
rc = PHP_ODBC_SQLCOLATTRIBUTE(result->stmt, (SQLUSMALLINT)(i+1), PHP_ODBC_SQL_DESC_NAME,
result->values[i].name, sizeof(result->values[i].name), &colnamelen, 0);
result->values[i].coltype = 0;
rc = PHP_ODBC_SQLCOLATTRIBUTE(result->stmt, (SQLUSMALLINT)(i+1), SQL_COLUMN_TYPE,
NULL, 0, NULL, &result->values[i].coltype);
/* Don't bind LONG / BINARY columns, so that fetch behaviour can
* be controlled by odbc_binmode() / odbc_longreadlen()
*/
switch(result->values[i].coltype) {
case SQL_BINARY:
case SQL_VARBINARY:
case SQL_LONGVARBINARY:
case SQL_LONGVARCHAR:
#if defined(ODBCVER) && (ODBCVER >= 0x0300)
case SQL_WLONGVARCHAR:
#endif
result->values[i].value = NULL;
break;
#ifdef HAVE_ADABAS
case SQL_TIMESTAMP:
result->values[i].value = (char *)emalloc(27);
SQLBindCol(result->stmt, (SQLUSMALLINT)(i+1), SQL_C_CHAR, result->values[i].value,
27, &result->values[i].vallen);
break;
#endif /* HAVE_ADABAS */
case SQL_CHAR:
case SQL_VARCHAR:
#if defined(ODBCVER) && (ODBCVER >= 0x0300)
case SQL_WCHAR:
case SQL_WVARCHAR:
colfieldid = SQL_DESC_OCTET_LENGTH;
#else
charextraalloc = 1;
#endif
/* TODO: Check this is the intended behaviour */
ZEND_FALLTHROUGH;
default:
rc = PHP_ODBC_SQLCOLATTRIBUTE(result->stmt, (SQLUSMALLINT)(i+1), colfieldid,
NULL, 0, NULL, &displaysize);
if (rc != SQL_SUCCESS) {
displaysize = 0;
}
#if defined(ODBCVER) && (ODBCVER >= 0x0300)
if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO && colfieldid == SQL_DESC_OCTET_LENGTH) {
SQLINTEGER err;
SQLCHAR errtxt[128];
SQLCHAR state[6];
memset(errtxt, '\0', 128);
memset(state, '\0', 6);
if (SQL_SUCCESS == SQLGetDiagRec(SQL_HANDLE_STMT, result->stmt, 1, state, &err, errtxt, 128, NULL)) {
errtxt[127] = '\0';
state[5] = '\0';
php_error_docref(NULL, E_WARNING, "SQLColAttribute can't handle SQL_DESC_OCTET_LENGTH: [%s] %s", state, errtxt);
}
/* This is a quirk for ODBC 2.0 compatibility for broken driver implementations.
*/
charextraalloc = 1;
rc = SQLColAttributes(result->stmt, (SQLUSMALLINT)(i+1), SQL_COLUMN_DISPLAY_SIZE,
NULL, 0, NULL, &displaysize);
if (rc != SQL_SUCCESS) {
displaysize = 0;
}
}
/* Workaround for drivers that report NVARCHAR(MAX) columns as SQL_WVARCHAR with size 0 (bug #69975) */
if (result->values[i].coltype == SQL_WVARCHAR && displaysize == 0) {
result->values[i].coltype = SQL_WLONGVARCHAR;
result->values[i].value = NULL;
break;
}
#endif
/* Workaround for drivers that report VARCHAR(MAX) columns as SQL_VARCHAR (bug #73725) */
if (SQL_VARCHAR == result->values[i].coltype && displaysize == 0) {
result->values[i].coltype = SQL_LONGVARCHAR;
result->values[i].value = NULL;
break;
}
/* Workaround for Oracle ODBC Driver bug (#50162) when fetching TIMESTAMP column */
if (result->values[i].coltype == SQL_TIMESTAMP) {
displaysize += 3;
}
if (charextraalloc) {
/* Since we don't know the exact # of bytes, allocate extra */
displaysize *= 4;
}
result->values[i].value = (char *)emalloc(displaysize + 1);
rc = SQLBindCol(result->stmt, (SQLUSMALLINT)(i+1), SQL_C_CHAR, result->values[i].value,
displaysize + 1, &result->values[i].vallen);
break;
}
}
}
/* }}} */
/* {{{ odbc_transact */
void odbc_transact(INTERNAL_FUNCTION_PARAMETERS, int type)
{
RETCODE rc;
zval *pv_conn;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &pv_conn, odbc_connection_ce) == FAILURE) {
RETURN_THROWS();
}
odbc_connection *conn = Z_ODBC_CONNECTION_P(pv_conn);
CHECK_ODBC_CONNECTION(conn);
rc = SQLTransact(conn->henv, conn->hdbc, (SQLUSMALLINT)((type)?SQL_COMMIT:SQL_ROLLBACK));
if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) {
odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLTransact");
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} */
/* {{{ odbc_column_lengths */
void odbc_column_lengths(INTERNAL_FUNCTION_PARAMETERS, int type)
{
odbc_result *result;
#if defined(HAVE_SOLID) || defined(HAVE_SOLID_30)
/* this seems to be necessary for Solid2.3 ( tested by
* [email protected]) and Solid 3.0 (tested by [email protected])
* Solid does not seem to declare a SQLINTEGER, but it does declare a
* SQL_INTEGER which does not work (despite being the same type as a SDWORD.
* Solid 3.5 does not have this issue.
*/
SDWORD len;
#else
SQLLEN len;
#endif
zval *pv_res;
zend_long pv_num;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ol", &pv_res, odbc_result_ce, &pv_num) == FAILURE) {
RETURN_THROWS();
}
result = Z_ODBC_RESULT_P(pv_res);
CHECK_ODBC_RESULT(result);
if (pv_num < 1) {
zend_argument_value_error(2, "must be greater than 0");
RETURN_THROWS();
}
if (result->numcols == 0) {
php_error_docref(NULL, E_WARNING, "No tuples available at this result index");
RETURN_FALSE;
}
if (pv_num > result->numcols) {
php_error_docref(NULL, E_WARNING, "Field index larger than number of fields");
RETURN_FALSE;
}
PHP_ODBC_SQLCOLATTRIBUTE(result->stmt, (SQLUSMALLINT)pv_num, (SQLUSMALLINT) (type?SQL_COLUMN_SCALE:SQL_COLUMN_PRECISION), NULL, 0, NULL, &len);
RETURN_LONG(len);
}
/* }}} */
/* Main User Functions */
/* {{{ Close all ODBC connections */
PHP_FUNCTION(odbc_close_all)
{
zval *zv;
if (zend_parse_parameters_none() == FAILURE) {
RETURN_THROWS();
}
/* Loop through the link list, now close all links and their results */
ZEND_HASH_FOREACH_VAL(&ODBCG(connections), zv) {
odbc_link *link = Z_ODBC_LINK_P(zv);
if (link->connection) {
odbc_link_free(link);
}
} ZEND_HASH_FOREACH_END();
zend_hash_clean(&ODBCG(connections));
zend_hash_apply(&EG(persistent_list), _close_pconn);
}
/* }}} */
/* {{{ Handle binary column data */
PHP_FUNCTION(odbc_binmode)
{
php_odbc_fetch_attribs(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
}
/* }}} */
/* {{{ Handle LONG columns */
PHP_FUNCTION(odbc_longreadlen)
{
php_odbc_fetch_attribs(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
}
/* }}} */
/* {{{ Prepares a statement for execution */
PHP_FUNCTION(odbc_prepare)
{
zval *pv_conn;
char *query;
size_t query_len;
odbc_result *result = NULL;
RETCODE rc;
int i;
#ifdef HAVE_SQL_EXTENDED_FETCH
SQLUINTEGER scrollopts;
#endif
if (zend_parse_parameters(ZEND_NUM_ARGS(), "Os", &pv_conn, odbc_connection_ce, &query, &query_len) == FAILURE) {
RETURN_THROWS();
}
odbc_connection *conn = Z_ODBC_CONNECTION_P(pv_conn);
CHECK_ODBC_CONNECTION(conn);
object_init_ex(return_value, odbc_result_ce);
result = Z_ODBC_RESULT_P(return_value);
result->numparams = 0;
result->param_info = NULL;
rc = PHP_ODBC_SQLALLOCSTMT(conn->hdbc, &(result->stmt));
if (rc == SQL_INVALID_HANDLE) {
php_error_docref(NULL, E_WARNING, "SQLAllocStmt error 'Invalid Handle'");
zval_ptr_dtor(return_value);
RETURN_FALSE;
}
if (rc == SQL_ERROR) {
odbc_sql_error(conn, SQL_NULL_HSTMT, "SQLAllocStmt");
zval_ptr_dtor(return_value);
RETURN_FALSE;
}
#ifdef HAVE_SQL_EXTENDED_FETCH
/* Solid doesn't have ExtendedFetch, if DriverManager is used, get Info,
whether Driver supports ExtendedFetch */
rc = SQLGetInfo(conn->hdbc, SQL_FETCH_DIRECTION, (void *) &scrollopts, sizeof(scrollopts), NULL);
if (rc == SQL_SUCCESS) {
if ((result->fetch_abs = (scrollopts & SQL_FD_FETCH_ABSOLUTE))) {
/* Try to set CURSOR_TYPE to dynamic. Driver will replace this with other
type if not possible.
*/
SQLSetStmtOption(result->stmt, SQL_CURSOR_TYPE, ODBCG(default_cursortype));
}
} else {
result->fetch_abs = 0;
}
#endif
rc = SQLPrepare(result->stmt, (SQLCHAR *) query, SQL_NTS);
switch (rc) {
case SQL_SUCCESS:
break;
case SQL_SUCCESS_WITH_INFO:
odbc_sql_error(conn, result->stmt, "SQLPrepare");
break;
default:
odbc_sql_error(conn, result->stmt, "SQLPrepare");
zval_ptr_dtor(return_value);
RETURN_FALSE;
}
SQLNumParams(result->stmt, &(result->numparams));
SQLNumResultCols(result->stmt, &(result->numcols));
if (result->numcols > 0) {
odbc_bindcols(result);
} else {
result->values = NULL;
}
result->conn_ptr = conn;
result->fetched = 0;
result->param_info = (odbc_param_info *) safe_emalloc(sizeof(odbc_param_info), result->numparams, 0);
for (i=0;i<result->numparams;i++) {
rc = SQLDescribeParam(result->stmt, (SQLUSMALLINT)(i+1), &result->param_info[i].sqltype, &result->param_info[i].precision,
&result->param_info[i].scale, &result->param_info[i].nullable);
if (rc == SQL_ERROR) {
odbc_sql_error(result->conn_ptr, result->stmt, "SQLDescribeParameter");
SQLFreeStmt(result->stmt, SQL_RESET_PARAMS);
efree(result->param_info);
zval_ptr_dtor(return_value);
RETURN_FALSE;
}
}
odbc_insert_new_result(conn, return_value);
}
/* }}} */
/*
* Execute prepared SQL statement. Supports only input parameters.
*/
typedef struct odbc_params_t {
SQLLEN vallen;
int fp;
zend_string *zstr;
} odbc_params_t;
static void odbc_release_params(odbc_result *result, odbc_params_t *params) {
SQLFreeStmt(result->stmt, SQL_RESET_PARAMS);