-
Notifications
You must be signed in to change notification settings - Fork 7.7k
/
interbase.c
3133 lines (2695 loc) · 85.9 KB
/
interbase.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
/*
+----------------------------------------------------------------------+
| PHP Version 4 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2003 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 2.02 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available at through the world-wide-web at |
| http://www.php.net/license/2_02.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 |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Jouni Ahto <jouni.ahto@exdec.fi> |
| Andrew Avdeev <andy@rsc.mv.ru> |
+----------------------------------------------------------------------+
*/
/* $Id$ */
/* TODO: Arrays, roles?
A lot... */
/*
Changes:
2001-05-31: Jeremy Bettis <jeremy@deadbeef.com>
- If a blob handle was expected and something else was
received create a blob and add the value to it.
- If the incoming argument to a bind parameter is NULL
then store a NULL in the database.
- More verbose date errors.
1999-09-21: Ivo Panacek <ivop@regionet.cz>
- added COMPILE_DL section
- more verbose php_info_ibase function
mostly stolen from pgsql.c for now
1999-10-05: Ivo Panacek <ivop@regionet.cz>
- safe rinit/rfinish: check for NULL so
rfinish could be called repeatedly
emalloc & co. replaced with malloc & co.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_ini.h"
#include "ext/standard/php_standard.h"
#include "php_globals.h"
#include "php_interbase.h"
#if HAVE_IBASE
#include <ibase.h>
#include <time.h>
#include "ext/standard/fsock.h"
#include "ext/standard/info.h"
#ifdef SQL_INT64
#include <math.h>
#endif
#ifndef SQL_DIALECT_CURRENT
#define SQL_DIALECT_CURRENT 1
#endif
/*
#define IBDEBUG(a) php_printf("::: %s (%d)\n", a, __LINE__);
*/
#define IBDEBUG(a)
#define SAFE_STRING(s) ((s)?(s):"")
/* {{{ extension definition structures */
function_entry ibase_functions[] = {
PHP_FE(ibase_connect, NULL)
PHP_FE(ibase_pconnect, NULL)
PHP_FE(ibase_close, NULL)
PHP_FE(ibase_query, NULL)
PHP_FE(ibase_fetch_row, NULL)
PHP_FE(ibase_fetch_assoc, NULL)
PHP_FE(ibase_fetch_object, NULL)
PHP_FE(ibase_free_result, NULL)
PHP_FE(ibase_prepare, NULL)
PHP_FE(ibase_execute, NULL)
PHP_FE(ibase_free_query, NULL)
#if HAVE_STRFTIME
PHP_FE(ibase_timefmt, NULL)
#endif
PHP_FE(ibase_num_fields, NULL)
PHP_FE(ibase_field_info, NULL)
PHP_FE(ibase_trans, NULL)
PHP_FE(ibase_commit, NULL)
PHP_FE(ibase_rollback, NULL)
PHP_FE(ibase_blob_info, NULL)
PHP_FE(ibase_blob_create, NULL)
PHP_FE(ibase_blob_add, NULL)
PHP_FE(ibase_blob_cancel, NULL)
PHP_FE(ibase_blob_close, NULL)
PHP_FE(ibase_blob_open, NULL)
PHP_FE(ibase_blob_get, NULL)
PHP_FE(ibase_blob_echo, NULL)
PHP_FE(ibase_blob_import, NULL)
PHP_FE(ibase_errmsg, NULL)
#ifdef SQL_DIALECT_V6
PHP_FE(ibase_add_user, NULL)
PHP_FE(ibase_modify_user, NULL)
PHP_FE(ibase_delete_user, NULL)
#endif
{NULL, NULL, NULL}
};
zend_module_entry ibase_module_entry =
{
STANDARD_MODULE_HEADER,
"interbase",
ibase_functions,
PHP_MINIT(ibase),
PHP_MSHUTDOWN(ibase),
PHP_RINIT(ibase),
PHP_RSHUTDOWN(ibase),
PHP_MINFO(ibase),
NO_VERSION_YET,
STANDARD_MODULE_PROPERTIES
};
#ifdef COMPILE_DL_INTERBASE
ZEND_GET_MODULE(ibase)
#define DL_MALLOC(size) malloc(size)
#define DL_STRDUP(str) strdup(str)
#define DL_FREE(ptr) free(ptr)
#else
#define DL_MALLOC(size) emalloc(size)
#define DL_STRDUP(str) estrdup(str)
#define DL_FREE(ptr) efree(ptr)
#endif
/* True globals, no need for thread safety */
static int le_blob, le_link, le_plink, le_result, le_query, le_trans;
ZEND_DECLARE_MODULE_GLOBALS(ibase)
/* }}} */
/* {{{ internal macros, functions and structures */
#define IB_STATUS (IBG(status))
/* Fill ib_link and trans_n with the correct database link and transaction slot number. */
static void get_link_trans(INTERNAL_FUNCTION_PARAMETERS, zval **link_id, ibase_db_link **ib_link, int *trans_n, int *trans_id) {
int type;
void *resource = NULL;
ibase_tr_link *ib_trans;
IBDEBUG("Transaction or database link?");
if ((resource = zend_list_find(Z_LVAL_PP(link_id), &type))) {
IBDEBUG("Found in list");
if (type == le_trans) {
/* Transaction resource. Fetch it, database link resource
is stored in ib_trans->link_rsrc. */
IBDEBUG("Type is le_trans");
*trans_id = (Z_LVAL_PP(link_id));
ZEND_FETCH_RESOURCE(ib_trans, ibase_tr_link *, link_id, -1, "InterBase transaction", le_trans);
*trans_n = ib_trans->trans_num;
ZEND_FETCH_RESOURCE2(resource, ibase_db_link *, NULL, ib_trans->link_rsrc, "InterBase link", le_link, le_plink);
} else {
IBDEBUG("Type is le_[p]link");
/* Database link resource, use default transaction (=0). */
*trans_n = 0;
ZEND_FETCH_RESOURCE2(resource, ibase_db_link *, link_id, -1, "InterBase link", le_link, le_plink);
}
}
*ib_link = resource;
}
#define RESET_ERRMSG { IBG(errmsg)[0] = '\0';}
#define TEST_ERRMSG ( IBG(errmsg)[0] != '\0')
/* sql variables union
used for convert and binding input variables
*/
typedef struct {
union {
short sval;
float fval;
ISC_QUAD qval;
#ifdef ISC_TIMESTAMP
ISC_TIMESTAMP tsval;
ISC_DATE dtval;
ISC_TIME tmval;
#endif
} val;
short sqlind;
} BIND_BUF;
/* get blob identifier from argument
on empty unset argument ib_blob set to NULL
*/
#define GET_BLOB_ID_ARG(blob_arg, ib_blob)\
{\
if (Z_TYPE_P(blob_arg) == IS_STRING && Z_STRLEN_P(blob_arg) == 0) {\
ib_blob = NULL;\
} else if (Z_TYPE_P(blob_arg) != IS_STRING\
|| Z_STRLEN_P(blob_arg) != sizeof(ibase_blob_handle)\
|| ((ibase_blob_handle *)(Z_STRVAL_P(blob_arg)))->bl_handle != 0){\
_php_ibase_module_error("Invalid blob id");\
RETURN_FALSE;\
} else {\
ib_blob = (ibase_blob_handle *)Z_STRVAL_P(blob_arg);\
}\
}
/* get blob handle from argument
note: blob already open when handle active
*/
#define GET_BLOB_HANDLE_ARG(blob_arg, blob_ptr) \
{ \
int type; \
convert_to_long_ex(blob_arg); \
blob_ptr = (ibase_blob_handle *) zend_list_find(Z_LVAL_PP(blob_arg), &type); \
if (type!=le_blob) { \
_php_ibase_module_error("%d is not blob handle", Z_LVAL_PP(blob_arg)); \
RETURN_FALSE; \
} \
}
/* blob information struct */
typedef struct {
ISC_LONG max_segment; /* Length of longest segment */
ISC_LONG num_segments; /* Total number of segments */
ISC_LONG total_length; /* Total length of blob */
int bl_stream; /* blob is stream ? */
} IBASE_BLOBINFO;
/* }}} */
/* error handling ---------------------------- */
/* {{{ proto string ibase_errmsg(void)
Return error message */
PHP_FUNCTION(ibase_errmsg)
{
if (ZEND_NUM_ARGS() != 0) {
WRONG_PARAM_COUNT;
}
if (IBG(errmsg[0])) {
RETURN_STRING(IBG(errmsg), 1);
}
RETURN_FALSE;
}
/* }}} */
/* {{{ _php_ibase_error(TSRMLS_D)
print interbase error and save it for ibase_errmsg() */
static void _php_ibase_error(TSRMLS_D)
{
char *s;
ISC_STATUS *statusp;
s = IBG(errmsg);
statusp = IB_STATUS;
while ((s - IBG(errmsg)) < MAX_ERRMSG - (IBASE_MSGSIZE + 2) && isc_interprete(s, &statusp)) {
strcat(IBG(errmsg), " ");
s = IBG(errmsg) + strlen(IBG(errmsg));
}
php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", IBG(errmsg));
}
/* }}} */
/* {{{ _php_ibase_module_error()
print php interbase module error and save it for ibase_errmsg() */
static void _php_ibase_module_error(char *msg, ...)
{
va_list ap;
TSRMLS_FETCH();
va_start(ap, msg);
/* vsnprintf NUL terminates the buf and writes at most n-1 chars+NUL */
vsnprintf(IBG(errmsg), MAX_ERRMSG, msg, ap);
va_end(ap);
php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", IBG(errmsg));
}
/* }}} */
/* destructors ---------------------- */
/* {{{ _php_ibase_free_xsqlda() */
/* not actual destructor ... */
static void _php_ibase_free_xsqlda(XSQLDA *sqlda)
{
int i;
XSQLVAR *var;
IBDEBUG("Free XSQLDA?");
if (sqlda) {
IBDEBUG("Freeing XSQLDA...");
var = sqlda->sqlvar;
for (i = 0; i < sqlda->sqld; i++, var++) {
efree(var->sqldata);
if(var->sqlind)
efree(var->sqlind);
}
efree(sqlda);
}
}
/* }}} */
/* {{{ _php_ibase_commit_link() */
static void _php_ibase_commit_link(ibase_db_link *link)
{
int i;
TSRMLS_FETCH();
IBDEBUG("Checking transactions to close...");
if (link->trans[0] != NULL) { /* commit default */
IBDEBUG("Committing default transaction...");
if (isc_commit_transaction(IB_STATUS, &link->trans[0])) {
_php_ibase_error(TSRMLS_C);
}
link->trans[0] = NULL;
}
for (i = 1; i < IBASE_TRANS_ON_LINK; i++) {
if (link->trans[i] != NULL) {
IBDEBUG("Rolling back other transactions...");
if (isc_rollback_transaction(IB_STATUS, &link->trans[i])) {
_php_ibase_error(TSRMLS_C);
}
link->trans[i] = NULL;
}
}
}
/* }}} */
static void php_ibase_commit_link_rsrc(zend_rsrc_list_entry *rsrc TSRMLS_DC)
{
ibase_db_link *link = (ibase_db_link *)rsrc->ptr;
_php_ibase_commit_link(link);
}
/* {{{ _php_ibase_close_link() */
static void _php_ibase_close_link(zend_rsrc_list_entry *rsrc TSRMLS_DC)
{
ibase_db_link *link = (ibase_db_link *)rsrc->ptr;
_php_ibase_commit_link(link);
IBDEBUG("Closing normal link...");
isc_detach_database(IB_STATUS, &link->link);
IBG(num_links)--;
efree(link);
}
/* }}} */
/* {{{ _php_ibase_close_plink() */
static void _php_ibase_close_plink(zend_rsrc_list_entry *rsrc TSRMLS_DC)
{
ibase_db_link *link = (ibase_db_link *)rsrc->ptr;
_php_ibase_commit_link(link);
IBDEBUG("Closing permanent link...");
isc_detach_database(IB_STATUS, &link->link);
IBG(num_persistent)--;
IBG(num_links)--;
free(link);
}
/* }}} */
/* {{{ _php_ibase_free_result() */
static void _php_ibase_free_result(zend_rsrc_list_entry *rsrc TSRMLS_DC)
{
char tr_items[] = {isc_info_tra_id };
char tmp[32]; /* should be enough as on the Api doc */
ibase_result *ib_result = (ibase_result *)rsrc->ptr;
IBDEBUG("Freeing result...");
if (ib_result){
_php_ibase_free_xsqlda(ib_result->out_sqlda);
isc_transaction_info(IB_STATUS, &ib_result->trans,sizeof(tr_items), tr_items, sizeof(tmp), tmp );
/* we have a transaction still open and we really want to drop the statement ? */
if ( !(IB_STATUS[0] && IB_STATUS[1]) && ib_result->drop_stmt && ib_result->stmt ) {
IBDEBUG("Dropping statement handle (free_result)...");
if (isc_dsql_free_statement(IB_STATUS, &ib_result->stmt, DSQL_drop)) {
_php_ibase_error(TSRMLS_C);
}
} else {
/* Shouldn't be here unless query was select and had parameter
placeholders, in which case ibase_execute handles this???
(Testing seems to confirm the decision was a right one.)
*/
IBDEBUG("Closing statement handle...");
/*
if (isc_dsql_free_statement(IB_STATUS, &ib_result->stmt, DSQL_close)) {
_php_ibase_error();
}
*/
}
if (ib_result->out_array) {
efree(ib_result->out_array);
}
efree(ib_result);
}
}
/* }}} */
/* {{{ _php_ibase_free_query() */
static void _php_ibase_free_query(ibase_query *ib_query)
{
char tr_items[] = {isc_info_tra_id };
char tmp[32] ; /* ...should be enough as on the Api doc */
TSRMLS_FETCH();
IBDEBUG("Freeing query...");
if (ib_query) {
if (ib_query->in_sqlda) {
efree(ib_query->in_sqlda);
}
if (ib_query->out_sqlda) {
efree(ib_query->out_sqlda);
}
isc_transaction_info(IB_STATUS, &ib_query->trans,sizeof(tr_items), tr_items, sizeof(tmp), tmp );
/* we have the trans still open and a statement to drop? */
if ( !(IB_STATUS[0] && IB_STATUS[1]) && ib_query->stmt) {
IBDEBUG("Dropping statement handle (free_query)...");
if (isc_dsql_free_statement(IB_STATUS, &ib_query->stmt, DSQL_drop)){
_php_ibase_error(TSRMLS_C);
}
}
if (ib_query->in_array) {
efree(ib_query->in_array);
}
if (ib_query->out_array) {
efree(ib_query->out_array);
}
efree(ib_query);
}
}
/* }}} */
static void php_ibase_free_query_rsrc(zend_rsrc_list_entry *rsrc TSRMLS_DC)
{
ibase_query *query = (ibase_query *)rsrc->ptr;
_php_ibase_free_query(query);
}
/* {{{ _php_ibase_free_blob() */
static void _php_ibase_free_blob(zend_rsrc_list_entry *rsrc TSRMLS_DC)
{
ibase_blob_handle *ib_blob = (ibase_blob_handle *)rsrc->ptr;
if (ib_blob->bl_handle != NULL) { /* blob open*/
if (isc_cancel_blob(IB_STATUS, &ib_blob->bl_handle)) {
php_error_docref(NULL TSRMLS_CC, E_ERROR, "You can lose data. Close any blob after reading of writing it. Use ibase_blob_close() before calling ibase_close()");
}
}
efree(ib_blob);
}
/* }}} */
/* {{{ _php_ibase_free_trans() */
static void _php_ibase_free_trans(zend_rsrc_list_entry *rsrc TSRMLS_DC)
{
ibase_tr_link *ib_trans = (ibase_tr_link *)rsrc->ptr;
ibase_db_link *ib_link;
int type;
void *ptr;
ptr = zend_list_find(ib_trans->link_rsrc, &type); /* check if the link is still there */
if (ptr && (type==le_link || type==le_plink)) {
ib_link = (ibase_db_link *) zend_fetch_resource(NULL TSRMLS_CC, ib_trans->link_rsrc, "InterBase link", NULL, 2, le_link, le_plink);
if (ib_link) {
if (ib_link->trans[ib_trans->trans_num] != NULL) {
IBDEBUG("Rolling back unhandled transaction...");
if (isc_rollback_transaction(IB_STATUS, &ib_link->trans[ib_trans->trans_num])) {
_php_ibase_error(TSRMLS_C);
}
ib_link->trans[ib_trans->trans_num] = NULL;
}
}
}
efree(ib_trans);
}
/* }}} */
/* {{{ startup, shutdown and info functions */
PHP_INI_BEGIN()
STD_PHP_INI_BOOLEAN("ibase.allow_persistent", "1", PHP_INI_SYSTEM, OnUpdateInt, allow_persistent, zend_ibase_globals, ibase_globals)
STD_PHP_INI_ENTRY_EX("ibase.max_persistent", "-1", PHP_INI_SYSTEM, OnUpdateInt, max_persistent, zend_ibase_globals, ibase_globals, display_link_numbers)
STD_PHP_INI_ENTRY_EX("ibase.max_links", "-1", PHP_INI_SYSTEM, OnUpdateInt, max_links, zend_ibase_globals, ibase_globals, display_link_numbers)
STD_PHP_INI_ENTRY("ibase.default_user", NULL, PHP_INI_ALL, OnUpdateString, default_user, zend_ibase_globals, ibase_globals)
STD_PHP_INI_ENTRY("ibase.default_password", NULL, PHP_INI_ALL, OnUpdateString, default_password, zend_ibase_globals, ibase_globals)
STD_PHP_INI_ENTRY("ibase.timestampformat", "%m/%d/%Y %H:%M:%S", PHP_INI_ALL, OnUpdateString, cfg_timestampformat, zend_ibase_globals, ibase_globals)
STD_PHP_INI_ENTRY("ibase.dateformat", "%m/%d/%Y", PHP_INI_ALL, OnUpdateString, cfg_dateformat, zend_ibase_globals, ibase_globals)
STD_PHP_INI_ENTRY("ibase.timeformat", "%H:%M:%S", PHP_INI_ALL, OnUpdateString, cfg_timeformat, zend_ibase_globals, ibase_globals)
PHP_INI_END()
static void php_ibase_init_globals(zend_ibase_globals *ibase_globals)
{
ibase_globals->timestampformat = NULL;
ibase_globals->dateformat = NULL;
ibase_globals->timeformat = NULL;
ibase_globals->errmsg = NULL;
ibase_globals->num_persistent = 0;
}
PHP_MINIT_FUNCTION(ibase)
{
ZEND_INIT_MODULE_GLOBALS(ibase, php_ibase_init_globals, NULL);
REGISTER_INI_ENTRIES();
le_result = zend_register_list_destructors_ex(_php_ibase_free_result, NULL, "interbase result", module_number);
le_query = zend_register_list_destructors_ex(php_ibase_free_query_rsrc, NULL, "interbase query", module_number);
le_blob = zend_register_list_destructors_ex(_php_ibase_free_blob, NULL, "interbase blob", module_number);
le_link = zend_register_list_destructors_ex(_php_ibase_close_link, NULL, "interbase link", module_number);
le_plink = zend_register_list_destructors_ex(php_ibase_commit_link_rsrc, _php_ibase_close_plink, "interbase link persistent", module_number);
le_trans = zend_register_list_destructors_ex(_php_ibase_free_trans, NULL, "interbase transaction", module_number);
REGISTER_LONG_CONSTANT("IBASE_DEFAULT", PHP_IBASE_DEFAULT, CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("IBASE_TEXT", PHP_IBASE_TEXT, CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("IBASE_UNIXTIME", PHP_IBASE_UNIXTIME, CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("IBASE_READ", PHP_IBASE_READ, CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("IBASE_COMMITTED", PHP_IBASE_COMMITTED, CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("IBASE_CONSISTENCY", PHP_IBASE_CONSISTENCY, CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("IBASE_NOWAIT", PHP_IBASE_NOWAIT, CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("IBASE_TIMESTAMP", PHP_IBASE_TIMESTAMP, CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("IBASE_DATE", PHP_IBASE_DATE, CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("IBASE_TIME", PHP_IBASE_TIME, CONST_PERSISTENT);
return SUCCESS;
}
PHP_RINIT_FUNCTION(ibase)
{
IBG(default_link)= -1;
IBG(num_links) = IBG(num_persistent);
if (IBG(timestampformat))
DL_FREE(IBG(timestampformat));
IBG(timestampformat) = DL_STRDUP(IBG(cfg_timestampformat));
if (IBG(dateformat))
DL_FREE(IBG(dateformat));
IBG(dateformat) = DL_STRDUP(IBG(cfg_dateformat));
if (IBG(timeformat))
DL_FREE(IBG(timeformat));
IBG(timeformat) = DL_STRDUP(IBG(cfg_timeformat));
if (IBG(errmsg))
DL_FREE(IBG(errmsg));
IBG(errmsg) = DL_MALLOC(sizeof(char)*MAX_ERRMSG+1);
return SUCCESS;
}
PHP_MSHUTDOWN_FUNCTION(ibase)
{
UNREGISTER_INI_ENTRIES();
return SUCCESS;
}
PHP_RSHUTDOWN_FUNCTION(ibase)
{
if (IBG(timestampformat))
DL_FREE(IBG(timestampformat));
IBG(timestampformat) = NULL;
if (IBG(dateformat))
DL_FREE(IBG(dateformat));
IBG(dateformat) = NULL;
if (IBG(timeformat))
DL_FREE(IBG(timeformat));
IBG(timeformat) = NULL;
if (IBG(errmsg))
DL_FREE(IBG(errmsg));
IBG(errmsg) = NULL;
return SUCCESS;
}
PHP_MINFO_FUNCTION(ibase)
{
char tmp[32];
php_info_print_table_start();
php_info_print_table_row(2, "Interbase Support", "enabled");
php_info_print_table_row(2, "Revision", "$Revision$");
#ifdef COMPILE_DL_INTERBASE
php_info_print_table_row(2, "Dynamic Module", "yes");
#endif
php_info_print_table_row(2, "Allow Persistent Links", (IBG(allow_persistent)?"Yes":"No") );
if (IBG(max_persistent) == -1) {
snprintf(tmp, 31, "%ld/unlimited", IBG(num_persistent));
} else {
snprintf(tmp, 31, "%ld/%ld", IBG(num_persistent), IBG(max_persistent));
}
tmp[31]=0;
php_info_print_table_row(2, "Persistent Links", tmp );
if (IBG(max_links) == -1) {
snprintf(tmp, 31, "%ld/unlimited", IBG(num_links));
} else {
snprintf(tmp, 31, "%ld/%ld", IBG(num_links), IBG(max_links));
}
tmp[31]=0;
php_info_print_table_row(2, "Total Links", tmp );
php_info_print_table_row(2, "Timestamp Format", IBG(timestampformat) );
php_info_print_table_row(2, "Date Format", IBG(dateformat) );
php_info_print_table_row(2, "Time Format", IBG(timeformat) );
php_info_print_table_end();
}
/* }}} */
/* {{{ _php_ibase_attach_db() */
static int _php_ibase_attach_db(char *server, char *uname, char *passwd, char *charset, int buffers, char *role, isc_db_handle *db)
{
char dpb_buffer[256], *dpb, *p;
int dpb_length, len;
TSRMLS_FETCH();
dpb = dpb_buffer;
*dpb++ = isc_dpb_version1;
if (uname != NULL && (len = strlen(uname))) {
*dpb++ = isc_dpb_user_name;
*dpb++ = len;
for (p = uname; *p;) {
*dpb++ = *p++;
}
}
if (passwd != NULL && (len = strlen(passwd))) {
*dpb++ = isc_dpb_password;
*dpb++ = strlen(passwd);
for (p = passwd; *p;) {
*dpb++ = *p++;
}
}
if (charset != NULL && (len = strlen(charset))) {
*dpb++ = isc_dpb_lc_ctype;
*dpb++ = strlen(charset);
for (p = charset; *p;) {
*dpb++ = *p++;
}
}
if (buffers) {
*dpb++ = isc_dpb_num_buffers;
*dpb++ = 1;
*dpb++ = buffers;
}
#ifdef isc_dpb_sql_role_name
if (role != NULL && (len = strlen(role))) {
*dpb++ = isc_dpb_sql_role_name;
*dpb++ = strlen(role);
for (p = role; *p;) {
*dpb++ = *p++;
}
}
#endif
dpb_length = dpb - dpb_buffer;
if (isc_attach_database(IB_STATUS, (short) strlen(server), server, db, (short) dpb_length, dpb_buffer)) {
_php_ibase_error(TSRMLS_C);
return FAILURE;
}
return SUCCESS;
}
/* }}} */
/* {{{ _php_ibase_connect() */
static void _php_ibase_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
{
pval ***args;
char *ib_server = NULL, *ib_uname, *ib_passwd, *ib_charset = NULL, *ib_buffers = NULL, *ib_dialect = NULL, *ib_role = NULL;
int i, ib_uname_len, ib_passwd_len;
isc_db_handle db_handle = NULL;
char *hashed_details;
int hashed_details_length = 0;
ibase_db_link *ib_link = NULL;
RESET_ERRMSG;
ib_uname = IBG(default_user);
ib_passwd = IBG(default_password);
ib_uname_len = ib_uname ? strlen(ib_uname) : 0;
ib_passwd_len = ib_passwd ? strlen(ib_passwd) : 0;
if(ZEND_NUM_ARGS() < 1 || ZEND_NUM_ARGS() > 7){
WRONG_PARAM_COUNT;
}
args = (pval ***) emalloc(sizeof(pval **)*ZEND_NUM_ARGS());
if (zend_get_parameters_array_ex(ZEND_NUM_ARGS(), args) == FAILURE) {
efree(args);
RETURN_FALSE;
}
switch(ZEND_NUM_ARGS()) {
case 7:
convert_to_string_ex(args[6]);
ib_role = Z_STRVAL_PP(args[6]);
hashed_details_length += Z_STRLEN_PP(args[6]);
/* fallout */
case 6:
convert_to_string_ex(args[5]);
ib_dialect = Z_STRVAL_PP(args[5]);
hashed_details_length += Z_STRLEN_PP(args[5]);
/* fallout */
case 5:
convert_to_string_ex(args[4]);
ib_buffers = Z_STRVAL_PP(args[4]);
hashed_details_length += Z_STRLEN_PP(args[4]);
/* fallout */
case 4:
convert_to_string_ex(args[3]);
ib_charset = Z_STRVAL_PP(args[3]);
hashed_details_length += Z_STRLEN_PP(args[3]);
/* fallout */
case 3:
convert_to_string_ex(args[2]);
ib_passwd = Z_STRVAL_PP(args[2]);
hashed_details_length += Z_STRLEN_PP(args[2]);
/* fallout */
case 2:
convert_to_string_ex(args[1]);
ib_uname = Z_STRVAL_PP(args[1]);
hashed_details_length += Z_STRLEN_PP(args[1]);
/* fallout */
case 1:
convert_to_string_ex(args[0]);
ib_server = Z_STRVAL_PP(args[0]);
hashed_details_length += Z_STRLEN_PP(args[0]);
} /* case */
efree(args);
hashed_details = (char *) emalloc(hashed_details_length+strlen("ibase_%s_%s_%s_%s_%s_%s_%s")+1);
sprintf(hashed_details, "ibase_%s_%s_%s_%s_%s_%s_%s", SAFE_STRING(ib_server), SAFE_STRING(ib_uname), SAFE_STRING(ib_passwd), SAFE_STRING(ib_charset), SAFE_STRING(ib_buffers), SAFE_STRING(ib_dialect), SAFE_STRING(ib_role));
if (persistent) {
list_entry *le;
int open_new_connection = 1;
if (zend_hash_find(&EG(persistent_list), hashed_details, hashed_details_length+1, (void **) &le) != FAILURE) {
char tmp_1[] = {isc_info_base_level, isc_info_end};
char tmp_2[8]; /* Enough? Hope so... */
if (Z_TYPE_P(le) != le_plink) {
RETURN_FALSE;
}
/* Check if connection has timed out */
ib_link = (ibase_db_link *) le->ptr;
if (!isc_database_info(IB_STATUS, &ib_link->link, sizeof(tmp_1), tmp_1, sizeof(tmp_2), tmp_2)) {
open_new_connection = 0;
}
}
/* There was no previous connection to use or it has timed out */
if (open_new_connection) {
list_entry new_le;
if ((IBG(max_links) != -1) && (IBG(num_links) >= IBG(max_links))) {
_php_ibase_module_error("Too many open links (%d)", IBG(num_links));
efree(hashed_details);
RETURN_FALSE;
}
if ((IBG(max_persistent) != -1) && (IBG(num_persistent) >= IBG(max_persistent))) {
_php_ibase_module_error("Too many open persistent links (%d)", IBG(num_persistent));
efree(hashed_details);
RETURN_FALSE;
}
/* create the ib_link */
if (_php_ibase_attach_db(ib_server, ib_uname, ib_passwd, ib_charset,
(ib_buffers ? strtoul(ib_buffers, NULL, 0) : 0),
ib_role, &db_handle) == FAILURE) {
efree(hashed_details);
RETURN_FALSE;
}
ib_link = (ibase_db_link *) malloc(sizeof(ibase_db_link));
ib_link->link = db_handle;
ib_link->dialect = (ib_dialect ? (unsigned short) strtoul(ib_dialect, NULL, 10) : SQL_DIALECT_CURRENT);
for (i = 0; i < IBASE_TRANS_ON_LINK; i++)
ib_link->trans[i] = NULL;
/* hash it up */
Z_TYPE(new_le) = le_plink;
new_le.ptr = ib_link;
if (zend_hash_update(&EG(persistent_list), hashed_details, hashed_details_length+1, (void *) &new_le, sizeof(list_entry), NULL)==FAILURE) {
efree(hashed_details);
free(ib_link);
RETURN_FALSE;
}
IBG(num_links)++;
IBG(num_persistent)++;
}
ZEND_REGISTER_RESOURCE(return_value, ib_link, le_plink);
} else {
list_entry *index_ptr, new_index_ptr;
/* first we check the hash for the hashed_details key. if it exists,
* it should point us to the right offset where the actual ib_link sits.
* if it doesn't, open a new ib_link, add it to the resource list,
* and add a pointer to it with hashed_details as the key.
*/
if (zend_hash_find(&EG(regular_list), hashed_details, hashed_details_length+1, (void **) &index_ptr)==SUCCESS) {
int type, xlink;
void *ptr;
if (Z_TYPE_P(index_ptr) != le_index_ptr) {
RETURN_FALSE;
}
xlink = (int) index_ptr->ptr;
ptr = zend_list_find(xlink, &type); /* check if the xlink is still there */
if (ptr && (type==le_link || type==le_plink)) {
zend_list_addref(xlink);
Z_LVAL_P(return_value) = xlink;
Z_TYPE_P(return_value) = IS_RESOURCE;
IBG(default_link) = Z_LVAL_P(return_value);
efree(hashed_details);
return;
} else {
zend_hash_del(&EG(regular_list), hashed_details, hashed_details_length+1);
}
}
if ((IBG(max_links) != -1) && (IBG(num_links) >= IBG(max_links))) {
_php_ibase_module_error("Too many open links (%d)", IBG(num_links));
efree(hashed_details);
RETURN_FALSE;
}
/* create the ib_link */
if (_php_ibase_attach_db(ib_server, ib_uname, ib_passwd, ib_charset,
(ib_buffers ? strtoul(ib_buffers, NULL, 0) : 0),
ib_role, &db_handle) == FAILURE) {
efree(hashed_details);
RETURN_FALSE;
}
ib_link = (ibase_db_link *) emalloc(sizeof(ibase_db_link));
ib_link->link = db_handle;
ib_link->dialect = (ib_dialect ? (unsigned short) strtoul(ib_dialect, NULL, 10) : SQL_DIALECT_CURRENT);
for (i = 0; i < IBASE_TRANS_ON_LINK; i++)
ib_link->trans[i] = NULL;
ZEND_REGISTER_RESOURCE(return_value, ib_link, le_link);
/* add it to the hash */
new_index_ptr.ptr = (void *) Z_LVAL_P(return_value);
Z_TYPE(new_index_ptr) = le_index_ptr;
if (zend_hash_update(&EG(regular_list), hashed_details, hashed_details_length+1, (void *) &new_index_ptr, sizeof(list_entry), NULL)==FAILURE) {
efree(hashed_details);
RETURN_FALSE;
}
IBG(num_links)++;
}
efree(hashed_details);
zend_list_addref(Z_LVAL_P(return_value));
IBG(default_link) = Z_LVAL_P(return_value);
}
/* }}} */
/* {{{ proto int ibase_connect(string database [, string username] [, string password] [, string charset] [, int buffers] [, int dialect] [, string role])
Open a connection to an InterBase database */
PHP_FUNCTION(ibase_connect)
{
_php_ibase_connect(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
}
/* }}} */
/* {{{ proto int ibase_pconnect(string database [, string username] [, string password] [, string charset] [, int buffers] [, int dialect] [, string role])
Open a persistent connection to an InterBase database */
PHP_FUNCTION(ibase_pconnect)
{
_php_ibase_connect(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
}
/* }}} */
/* {{{ proto int ibase_close([int link_identifier])
Close an InterBase connection */
PHP_FUNCTION(ibase_close)
{
zval **link_arg;
ibase_db_link *ib_link;
int link_id;
RESET_ERRMSG;
switch (ZEND_NUM_ARGS()) {
case 0:
link_id = IBG(default_link);
break;
case 1:
if (zend_get_parameters_ex(1, &link_arg) == FAILURE) {
RETURN_FALSE;
}
convert_to_long_ex(link_arg);
link_id = Z_LVAL_PP(link_arg);
break;
default:
WRONG_PARAM_COUNT;
break;
}
ZEND_FETCH_RESOURCE2(ib_link, ibase_db_link *, link_arg, link_id, "InterBase link", le_link, le_plink);
zend_list_delete(link_id);
RETURN_TRUE;
}
/* }}} */
/* {{{ _php_ibase_alloc_array() */
static int _php_ibase_alloc_array(ibase_array **ib_arrayp, int *array_cntp,
XSQLDA *sqlda, isc_db_handle link, isc_tr_handle trans)
{
#define IB_ARRAY (*ib_arrayp)
int i, dim, ar_cnt, ar_length;
XSQLVAR *var;
TSRMLS_FETCH();
IB_ARRAY = NULL;
ar_cnt = 0; /* find arrays */
var = sqlda->sqlvar;
for (i = 0; i < sqlda->sqld; i++, var++) {
if ((var->sqltype & ~1) == SQL_ARRAY)
ar_cnt++;
}
if (ar_cnt) { /* have arrays ? */
*array_cntp = ar_cnt;
IB_ARRAY = emalloc(sizeof(ibase_array)*ar_cnt);
ar_cnt = 0;
var = sqlda->sqlvar;
for (i = 0; i < sqlda->sqld; i++, var++) {
if ((var->sqltype & ~1) == SQL_ARRAY) {
ISC_ARRAY_DESC *ar_desc = &IB_ARRAY[ar_cnt].ar_desc;
if (isc_array_lookup_bounds(IB_STATUS, &link, &trans,
var->relname, var->sqlname, ar_desc)) {
_php_ibase_error(TSRMLS_C);
efree(IB_ARRAY);
IB_ARRAY = NULL;
return FAILURE;
}
switch (ar_desc->array_desc_dtype) {
case blr_text:
case blr_text2:
IB_ARRAY[ar_cnt].el_type = SQL_TEXT;
IB_ARRAY[ar_cnt].el_size = ar_desc->array_desc_length+1;
break;
case blr_short:
IB_ARRAY[ar_cnt].el_type = SQL_SHORT;
IB_ARRAY[ar_cnt].el_size = sizeof(short);
break;
case blr_long:
IB_ARRAY[ar_cnt].el_type = SQL_LONG;
IB_ARRAY[ar_cnt].el_size = sizeof(long);
break;
case blr_float:
IB_ARRAY[ar_cnt].el_type = SQL_FLOAT;