forked from FirebirdSQL/php-firebird
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ibase_query.c
2196 lines (1879 loc) · 59.1 KB
/
ibase_query.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 7, 8 |
+----------------------------------------------------------------------+
| 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: |
| http://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: Jouni Ahto <[email protected]> |
| Andrew Avdeev <[email protected]> |
| Ard Biesheuvel <[email protected]> |
| Martin Koeditz <[email protected]> |
| others |
+----------------------------------------------------------------------+
| You'll find history on Github |
| https://github.com/FirebirdSQL/php-firebird/commits/master |
+----------------------------------------------------------------------+
*/
#include <stdbool.h>
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_ini.h"
#if HAVE_IBASE
#include "ext/standard/php_standard.h"
#include "php_interbase.h"
#include "php_ibase_includes.h"
#define ISC_LONG_MIN INT_MIN
#define ISC_LONG_MAX INT_MAX
#define QUERY_RESULT 1
#define EXECUTE_RESULT 2
#define FETCH_ROW 1
#define FETCH_ARRAY 2
typedef struct {
ISC_ARRAY_DESC ar_desc;
ISC_LONG ar_size; /* size of entire array in bytes */
unsigned short el_type, el_size;
} ibase_array;
typedef struct {
ibase_db_link* link;
isc_stmt_handle stmt;
} ibase_statement;
typedef struct {
ibase_db_link *link;
ibase_trans *trans;
isc_stmt_handle stmt;
zend_resource* stmt_res;
unsigned short type;
unsigned char has_more_rows, statement_type;
XSQLDA *out_sqlda;
ibase_array out_array[1]; /* last member */
} ibase_result;
typedef struct _ib_query {
ibase_db_link *link;
ibase_trans *trans;
zend_resource *result_res;
isc_stmt_handle stmt;
zend_resource *stmt_res;
XSQLDA *in_sqlda, *out_sqlda;
ibase_array *in_array, *out_array;
unsigned short in_array_cnt, out_array_cnt;
unsigned short dialect;
char statement_type;
char *query;
zend_resource *trans_res;
} ibase_query;
typedef struct {
unsigned short vary_length;
char vary_string[1];
} IBVARY;
/* sql variables union
* used for convert and binding input variables
*/
typedef struct {
union {
// Boolean data type exists since FB 3.0
#ifdef SQL_BOOLEAN
FB_BOOLEAN bval;
#endif
short sval;
float fval;
ISC_LONG lval;
ISC_QUAD qval;
ISC_TIMESTAMP tsval;
ISC_DATE dtval;
ISC_TIME tmval;
} val;
short sqlind;
} BIND_BUF;
static int le_statement, le_result, le_query;
#define LE_RESULT "Firebird/InterBase result"
#define LE_QUERY "Firebird/InterBase query"
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);
}
}
/* }}} */
static void _php_ibase_free_statement(zend_resource* rsrc) /* {{{ */
{
ibase_statement* ib_stmt = (ibase_statement*)rsrc->ptr;
IBDEBUG("Freeing statement by dtor...");
if (ib_stmt) {
if (ib_stmt->stmt) {
static char info[] = { isc_info_base_level, isc_info_end };
char res_buf[8];
IBDEBUG("Dropping statement handle (free_stmt_handle)...");
/* Only free statement if db-connection is still open */
if (SUCCESS == isc_database_info(IB_STATUS, &ib_stmt->link->handle,
sizeof(info), info, sizeof(res_buf), res_buf)) {
if (isc_dsql_free_statement(IB_STATUS, &ib_stmt->stmt, DSQL_drop)) {
_php_ibase_error();
}
}
}
efree(ib_stmt);
}
}
/* }}} */
static void _php_ibase_free_result(zend_resource *rsrc) /* {{{ */
{
ibase_result *ib_result = (ibase_result *) rsrc->ptr;
IBDEBUG("Freeing result by dtor...");
if (ib_result) {
_php_ibase_free_xsqlda(ib_result->out_sqlda);
if (ib_result->stmt_res != NULL) {
zend_list_delete(ib_result->stmt_res);
ib_result->stmt_res = NULL;
}
efree(ib_result);
}
}
/* }}} */
static void _php_ibase_free_query(ibase_query *ib_query) /* {{{ */
{
IBDEBUG("Freeing query...");
if (ib_query->in_sqlda) {
efree(ib_query->in_sqlda);
}
if (ib_query->out_sqlda) {
efree(ib_query->out_sqlda);
}
if (ib_query->stmt_res != NULL) {
zend_list_delete(ib_query->stmt_res);
ib_query->stmt_res = NULL;
}
if (ib_query->result_res != NULL) {
zend_list_delete(ib_query->result_res);
ib_query->result_res = NULL;
}
if (ib_query->in_array) {
efree(ib_query->in_array);
}
if (ib_query->out_array) {
efree(ib_query->out_array);
}
if (ib_query->query) {
efree(ib_query->query);
}
}
/* }}} */
static void php_ibase_free_query_rsrc(zend_resource *rsrc) /* {{{ */
{
ibase_query *ib_query = (ibase_query *)rsrc->ptr;
if (ib_query != NULL) {
IBDEBUG("Preparing to free query by dtor...");
_php_ibase_free_query(ib_query);
efree(ib_query);
}
}
/* }}} */
void php_ibase_query_minit(INIT_FUNC_ARGS) /* {{{ */
{
le_statement = zend_register_list_destructors_ex(_php_ibase_free_statement, NULL,
"interbase statement", module_number);
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);
}
/* }}} */
static int _php_ibase_alloc_array(ibase_array **ib_arrayp, XSQLDA *sqlda, /* {{{ */
isc_db_handle link, isc_tr_handle trans, unsigned short *array_cnt)
{
unsigned short i, n;
ibase_array *ar;
/* first check if we have any arrays at all */
for (i = *array_cnt = 0; i < sqlda->sqld; ++i) {
if ((sqlda->sqlvar[i].sqltype & ~1) == SQL_ARRAY) {
++*array_cnt;
}
}
if (! *array_cnt) return SUCCESS;
ar = safe_emalloc(sizeof(ibase_array), *array_cnt, 0);
for (i = n = 0; i < sqlda->sqld; ++i) {
unsigned short dim;
zend_ulong ar_size = 1;
XSQLVAR *var = &sqlda->sqlvar[i];
if ((var->sqltype & ~1) == SQL_ARRAY) {
ibase_array *a = &ar[n++];
ISC_ARRAY_DESC *ar_desc = &a->ar_desc;
if (isc_array_lookup_bounds(IB_STATUS, &link, &trans, var->relname,
var->sqlname, ar_desc)) {
_php_ibase_error();
efree(ar);
return FAILURE;
}
switch (ar_desc->array_desc_dtype) {
case blr_text:
case blr_text2:
a->el_type = SQL_TEXT;
a->el_size = ar_desc->array_desc_length;
break;
// Boolean data type exists since FB 3.0
#ifdef SQL_BOOLEAN
case blr_bool:
a->el_type = SQL_BOOLEAN;
a->el_size = sizeof(FB_BOOLEAN);
break;
#endif
case blr_short:
a->el_type = SQL_SHORT;
a->el_size = sizeof(short);
break;
case blr_long:
a->el_type = SQL_LONG;
a->el_size = sizeof(ISC_LONG);
break;
case blr_float:
a->el_type = SQL_FLOAT;
a->el_size = sizeof(float);
break;
case blr_double:
a->el_type = SQL_DOUBLE;
a->el_size = sizeof(double);
break;
case blr_int64:
a->el_type = SQL_INT64;
a->el_size = sizeof(ISC_INT64);
break;
case blr_timestamp:
a->el_type = SQL_TIMESTAMP;
a->el_size = sizeof(ISC_TIMESTAMP);
break;
case blr_sql_date:
a->el_type = SQL_TYPE_DATE;
a->el_size = sizeof(ISC_DATE);
break;
case blr_sql_time:
a->el_type = SQL_TYPE_TIME;
a->el_size = sizeof(ISC_TIME);
break;
case blr_varying:
case blr_varying2:
/**
* IB has a strange way of handling VARCHAR arrays. It doesn't store
* the length in the first short, as with VARCHAR fields. It does,
* however, expect the extra short to be allocated for each element.
*/
a->el_type = SQL_TEXT;
a->el_size = ar_desc->array_desc_length + sizeof(short);
break;
case blr_quad:
case blr_blob_id:
case blr_cstring:
case blr_cstring2:
/**
* These types are mentioned as array types in the manual, but I
* wouldn't know how to create an array field with any of these
* types. I assume these types are not applicable to arrays, and
* were mentioned erroneously.
*/
default:
_php_ibase_module_error("Unsupported array type %d in relation '%s' column '%s'",
ar_desc->array_desc_dtype, var->relname, var->sqlname);
efree(ar);
return FAILURE;
} /* switch array_desc_type */
/* calculate elements count */
for (dim = 0; dim < ar_desc->array_desc_dimensions; dim++) {
ar_size *= 1 + ar_desc->array_desc_bounds[dim].array_bound_upper
-ar_desc->array_desc_bounds[dim].array_bound_lower;
}
a->ar_size = a->el_size * ar_size;
} /* if SQL_ARRAY */
} /* for column */
*ib_arrayp = ar;
return SUCCESS;
}
/* }}} */
/* allocate and prepare query */
static int _php_ibase_alloc_query(ibase_query *ib_query, ibase_db_link *link, /* {{{ */
ibase_trans *trans, char *query, unsigned short dialect, zend_resource *trans_res)
{
static char info_type[] = {isc_info_sql_stmt_type};
char result[8];
/* Return FAILURE, if querystring is empty */
if (*query == '\0') {
php_error_docref(NULL, E_WARNING, "Querystring empty.");
return FAILURE;
}
ib_query->link = link;
ib_query->trans = trans;
ib_query->result_res = NULL;
ib_query->stmt = 0;
ib_query->stmt_res = NULL;
ib_query->in_array = NULL;
ib_query->out_array = NULL;
ib_query->dialect = dialect;
ib_query->query = estrdup(query);
ib_query->trans_res = trans_res;
ib_query->out_sqlda = NULL;
ib_query->in_sqlda = NULL;
if (isc_dsql_allocate_statement(IB_STATUS, &link->handle, &ib_query->stmt)) {
_php_ibase_error();
goto _php_ibase_alloc_query_error;
}
{ /* allocate zend resource for statement*/
ibase_statement* ib_stmt = emalloc(sizeof(ibase_statement));
ib_stmt->link = link;
ib_stmt->stmt = ib_query->stmt;
ib_query->stmt_res = zend_register_resource(ib_stmt, le_statement);
}
ib_query->out_sqlda = (XSQLDA *) emalloc(XSQLDA_LENGTH(1));
ib_query->out_sqlda->sqln = 1;
ib_query->out_sqlda->version = SQLDA_CURRENT_VERSION;
if (isc_dsql_prepare(IB_STATUS, &ib_query->trans->handle, &ib_query->stmt,
0, query, dialect, ib_query->out_sqlda)) {
_php_ibase_error();
goto _php_ibase_alloc_query_error;
}
/* find out what kind of statement was prepared */
if (isc_dsql_sql_info(IB_STATUS, &ib_query->stmt, sizeof(info_type),
info_type, sizeof(result), result)) {
_php_ibase_error();
goto _php_ibase_alloc_query_error;
}
ib_query->statement_type = result[3];
/* not enough output variables ? */
if (ib_query->out_sqlda->sqld > ib_query->out_sqlda->sqln) {
ib_query->out_sqlda = erealloc(ib_query->out_sqlda, XSQLDA_LENGTH(ib_query->out_sqlda->sqld));
ib_query->out_sqlda->sqln = ib_query->out_sqlda->sqld;
ib_query->out_sqlda->version = SQLDA_CURRENT_VERSION;
if (isc_dsql_describe(IB_STATUS, &ib_query->stmt, SQLDA_CURRENT_VERSION, ib_query->out_sqlda)) {
_php_ibase_error();
goto _php_ibase_alloc_query_error;
}
}
/* maybe have input placeholders? */
ib_query->in_sqlda = emalloc(XSQLDA_LENGTH(1));
ib_query->in_sqlda->sqln = 1;
ib_query->in_sqlda->version = SQLDA_CURRENT_VERSION;
if (isc_dsql_describe_bind(IB_STATUS, &ib_query->stmt, SQLDA_CURRENT_VERSION, ib_query->in_sqlda)) {
_php_ibase_error();
goto _php_ibase_alloc_query_error;
}
/* not enough input variables ? */
if (ib_query->in_sqlda->sqln < ib_query->in_sqlda->sqld) {
ib_query->in_sqlda = erealloc(ib_query->in_sqlda, XSQLDA_LENGTH(ib_query->in_sqlda->sqld));
ib_query->in_sqlda->sqln = ib_query->in_sqlda->sqld;
ib_query->in_sqlda->version = SQLDA_CURRENT_VERSION;
if (isc_dsql_describe_bind(IB_STATUS, &ib_query->stmt,
SQLDA_CURRENT_VERSION, ib_query->in_sqlda)) {
_php_ibase_error();
goto _php_ibase_alloc_query_error;
}
}
/* no, haven't placeholders at all */
if (ib_query->in_sqlda->sqld == 0) {
efree(ib_query->in_sqlda);
ib_query->in_sqlda = NULL;
} else if (FAILURE == _php_ibase_alloc_array(&ib_query->in_array, ib_query->in_sqlda,
link->handle, trans->handle, &ib_query->in_array_cnt)) {
goto _php_ibase_alloc_query_error;
}
if (ib_query->out_sqlda->sqld == 0) {
efree(ib_query->out_sqlda);
ib_query->out_sqlda = NULL;
} else if (FAILURE == _php_ibase_alloc_array(&ib_query->out_array, ib_query->out_sqlda,
link->handle, trans->handle, &ib_query->out_array_cnt)) {
goto _php_ibase_alloc_query_error;
}
return SUCCESS;
_php_ibase_alloc_query_error:
if (ib_query->out_sqlda) {
efree(ib_query->out_sqlda);
}
if (ib_query->in_sqlda) {
efree(ib_query->in_sqlda);
}
if (ib_query->out_array) {
efree(ib_query->out_array);
}
if (ib_query->query) {
efree(ib_query->query);
}
return FAILURE;
}
/* }}} */
static int _php_ibase_bind_array(zval *val, char *buf, zend_ulong buf_size, /* {{{ */
ibase_array *array, int dim)
{
zval null_val, *pnull_val = &null_val;
int u_bound = array->ar_desc.array_desc_bounds[dim].array_bound_upper,
l_bound = array->ar_desc.array_desc_bounds[dim].array_bound_lower,
dim_len = 1 + u_bound - l_bound;
ZVAL_NULL(pnull_val);
if (dim < array->ar_desc.array_desc_dimensions) {
zend_ulong slice_size = buf_size / dim_len;
unsigned short i;
zval *subval = val;
if (Z_TYPE_P(val) == IS_ARRAY) {
zend_hash_internal_pointer_reset(Z_ARRVAL_P(val));
}
for (i = 0; i < dim_len; ++i) {
if (Z_TYPE_P(val) == IS_ARRAY &&
(subval = zend_hash_get_current_data(Z_ARRVAL_P(val))) == NULL)
{
subval = pnull_val;
}
if (_php_ibase_bind_array(subval, buf, slice_size, array, dim+1) == FAILURE)
{
return FAILURE;
}
buf += slice_size;
if (Z_TYPE_P(val) == IS_ARRAY) {
zend_hash_move_forward(Z_ARRVAL_P(val));
}
}
if (Z_TYPE_P(val) == IS_ARRAY) {
zend_hash_internal_pointer_reset(Z_ARRVAL_P(val));
}
} else {
/* expect a single value */
if (Z_TYPE_P(val) == IS_NULL) {
memset(buf, 0, buf_size);
} else if (array->ar_desc.array_desc_scale < 0) {
/* no coercion for array types */
double l;
convert_to_double(val);
if (Z_DVAL_P(val) > 0) {
l = Z_DVAL_P(val) * pow(10, -array->ar_desc.array_desc_scale) + .5;
} else {
l = Z_DVAL_P(val) * pow(10, -array->ar_desc.array_desc_scale) - .5;
}
switch (array->el_type) {
case SQL_SHORT:
if (l > SHRT_MAX || l < SHRT_MIN) {
_php_ibase_module_error("Array parameter exceeds field width");
return FAILURE;
}
*(short*) buf = (short) l;
break;
case SQL_LONG:
if (l > ISC_LONG_MAX || l < ISC_LONG_MIN) {
_php_ibase_module_error("Array parameter exceeds field width");
return FAILURE;
}
*(ISC_LONG*) buf = (ISC_LONG) l;
break;
case SQL_INT64:
{
long double l;
convert_to_string(val);
if (!sscanf(Z_STRVAL_P(val), "%Lf", &l)) {
_php_ibase_module_error("Cannot convert '%s' to long double",
Z_STRVAL_P(val));
return FAILURE;
}
if (l > 0) {
*(ISC_INT64 *) buf = (ISC_INT64) (l * pow(10,
-array->ar_desc.array_desc_scale) + .5);
} else {
*(ISC_INT64 *) buf = (ISC_INT64) (l * pow(10,
-array->ar_desc.array_desc_scale) - .5);
}
}
break;
}
} else {
struct tm t = { 0, 0, 0, 0, 0, 0 };
switch (array->el_type) {
#ifndef HAVE_STRPTIME
unsigned short n;
#endif
#if (SIZEOF_ZEND_LONG < 8)
ISC_INT64 l;
#endif
case SQL_SHORT:
convert_to_long(val);
if (Z_LVAL_P(val) > SHRT_MAX || Z_LVAL_P(val) < SHRT_MIN) {
_php_ibase_module_error("Array parameter exceeds field width");
return FAILURE;
}
*(short *) buf = (short) Z_LVAL_P(val);
break;
case SQL_LONG:
convert_to_long(val);
#if (SIZEOF_ZEND_LONG > 4)
if (Z_LVAL_P(val) > ISC_LONG_MAX || Z_LVAL_P(val) < ISC_LONG_MIN) {
_php_ibase_module_error("Array parameter exceeds field width");
return FAILURE;
}
#endif
*(ISC_LONG *) buf = (ISC_LONG) Z_LVAL_P(val);
break;
case SQL_INT64:
#if (SIZEOF_ZEND_LONG >= 8)
convert_to_long(val);
*(zend_long *) buf = Z_LVAL_P(val);
#else
convert_to_string(val);
if (!sscanf(Z_STRVAL_P(val), "%" LL_MASK "d", &l)) {
_php_ibase_module_error("Cannot convert '%s' to long integer",
Z_STRVAL_P(val));
return FAILURE;
} else {
*(ISC_INT64 *) buf = l;
}
#endif
break;
case SQL_FLOAT:
convert_to_double(val);
*(float*) buf = (float) Z_DVAL_P(val);
break;
// Boolean data type exists since FB 3.0
#ifdef SQL_BOOLEAN
case SQL_BOOLEAN:
convert_to_boolean(val);
// On Windows error unresolved symbol Z_BVAL_P is thrown, so we use Z_LVAL_P
*(FB_BOOLEAN*) buf = Z_LVAL_P(val);
break;
#endif
case SQL_DOUBLE:
convert_to_double(val);
*(double*) buf = Z_DVAL_P(val);
break;
case SQL_TIMESTAMP:
convert_to_string(val);
#ifdef HAVE_STRPTIME
strptime(Z_STRVAL_P(val), INI_STR("ibase.timestampformat"), &t);
#else
n = sscanf(Z_STRVAL_P(val), "%d%*[/]%d%*[/]%d %d%*[:]%d%*[:]%d",
&t.tm_mon, &t.tm_mday, &t.tm_year, &t.tm_hour, &t.tm_min, &t.tm_sec);
if (n != 3 && n != 6) {
_php_ibase_module_error("Invalid date/time format (expected 3 or 6 fields, got %d."
" Use format 'm/d/Y H:i:s'. You gave '%s')", n, Z_STRVAL_P(val));
return FAILURE;
}
t.tm_year -= 1900;
t.tm_mon--;
#endif
isc_encode_timestamp(&t, (ISC_TIMESTAMP * ) buf);
break;
case SQL_TYPE_DATE:
convert_to_string(val);
#ifdef HAVE_STRPTIME
strptime(Z_STRVAL_P(val), INI_STR("ibase.dateformat"), &t);
#else
n = sscanf(Z_STRVAL_P(val), "%d%*[/]%d%*[/]%d", &t.tm_mon, &t.tm_mday, &t.tm_year);
if (n != 3) {
_php_ibase_module_error("Invalid date format (expected 3 fields, got %d. "
"Use format 'm/d/Y' You gave '%s')", n, Z_STRVAL_P(val));
return FAILURE;
}
t.tm_year -= 1900;
t.tm_mon--;
#endif
isc_encode_sql_date(&t, (ISC_DATE *) buf);
break;
case SQL_TYPE_TIME:
convert_to_string(val);
#ifdef HAVE_STRPTIME
strptime(Z_STRVAL_P(val), INI_STR("ibase.timeformat"), &t);
#else
n = sscanf(Z_STRVAL_P(val), "%d%*[:]%d%*[:]%d", &t.tm_hour, &t.tm_min, &t.tm_sec);
if (n != 3) {
_php_ibase_module_error("Invalid time format (expected 3 fields, got %d. "
"Use format 'H:i:s'. You gave '%s')", n, Z_STRVAL_P(val));
return FAILURE;
}
#endif
isc_encode_sql_time(&t, (ISC_TIME *) buf);
break;
default:
convert_to_string(val);
strlcpy(buf, Z_STRVAL_P(val), buf_size);
}
}
}
return SUCCESS;
}
/* }}} */
static int _php_ibase_bind(XSQLDA *sqlda, zval *b_vars, BIND_BUF *buf, /* {{{ */
ibase_query *ib_query)
{
int i, array_cnt = 0, rv = SUCCESS;
for (i = 0; i < sqlda->sqld; ++i) { /* bound vars */
zval *b_var = &b_vars[i];
XSQLVAR *var = &sqlda->sqlvar[i];
var->sqlind = &buf[i].sqlind;
/* check if a NULL should be inserted */
switch (Z_TYPE_P(b_var)) {
int force_null;
case IS_STRING:
force_null = 0;
/* for these types, an empty string can be handled like a NULL value */
switch (var->sqltype & ~1) {
case SQL_SHORT:
case SQL_LONG:
case SQL_INT64:
case SQL_FLOAT:
case SQL_DOUBLE:
case SQL_TIMESTAMP:
case SQL_TYPE_DATE:
case SQL_TYPE_TIME:
force_null = (Z_STRLEN_P(b_var) == 0);
}
if (! force_null) break;
case IS_NULL:
buf[i].sqlind = -1;
if (var->sqltype & SQL_ARRAY) ++array_cnt;
continue;
}
/* if we make it to this point, we must provide a value for the parameter */
buf[i].sqlind = 0;
var->sqldata = (void*)&buf[i].val;
switch (var->sqltype & ~1) {
struct tm t;
case SQL_TIMESTAMP:
case SQL_TYPE_DATE:
case SQL_TYPE_TIME:
if (Z_TYPE_P(b_var) == IS_LONG) {
struct tm *res;
res = php_gmtime_r(&Z_LVAL_P(b_var), &t);
if (!res) {
return FAILURE;
}
} else {
#ifdef HAVE_STRPTIME
char *format = INI_STR("ibase.timestampformat");
convert_to_string(b_var);
switch (var->sqltype & ~1) {
case SQL_TYPE_DATE:
format = INI_STR("ibase.dateformat");
break;
case SQL_TYPE_TIME:
format = INI_STR("ibase.timeformat");
}
if (!strptime(Z_STRVAL_P(b_var), format, &t)) {
/* strptime() cannot handle it, so let IB have a try */
break;
}
#else /* ifndef HAVE_STRPTIME */
break; /* let IB parse it as a string */
#endif
}
switch (var->sqltype & ~1) {
default: /* == case SQL_TIMESTAMP */
isc_encode_timestamp(&t, &buf[i].val.tsval);
break;
case SQL_TYPE_DATE:
isc_encode_sql_date(&t, &buf[i].val.dtval);
break;
case SQL_TYPE_TIME:
isc_encode_sql_time(&t, &buf[i].val.tmval);
break;
}
continue;
case SQL_BLOB:
convert_to_string(b_var);
if (Z_STRLEN_P(b_var) != BLOB_ID_LEN ||
!_php_ibase_string_to_quad(Z_STRVAL_P(b_var), &buf[i].val.qval)) {
ibase_blob ib_blob = { 0, BLOB_INPUT };
if (isc_create_blob(IB_STATUS, &ib_query->link->handle,
&ib_query->trans->handle, &ib_blob.bl_handle, &ib_blob.bl_qd)) {
_php_ibase_error();
return FAILURE;
}
if (_php_ibase_blob_add(b_var, &ib_blob) != SUCCESS) {
return FAILURE;
}
if (isc_close_blob(IB_STATUS, &ib_blob.bl_handle)) {
_php_ibase_error();
return FAILURE;
}
buf[i].val.qval = ib_blob.bl_qd;
}
continue;
// Boolean data type exists since FB 3.0
#ifdef SQL_BOOLEAN
case SQL_BOOLEAN:
switch (Z_TYPE_P(b_var)) {
case IS_LONG:
case IS_DOUBLE:
case IS_TRUE:
case IS_FALSE:
*(FB_BOOLEAN *)var->sqldata = zend_is_true(b_var) ? FB_TRUE : FB_FALSE;
break;
case IS_STRING:
{
zend_long lval;
double dval;
if ((Z_STRLEN_P(b_var) == 0)) {
*(FB_BOOLEAN *)var->sqldata = FB_FALSE;
break;
}
switch (is_numeric_string(Z_STRVAL_P(b_var), Z_STRLEN_P(b_var), &lval, &dval, 0)) {
case IS_LONG:
*(FB_BOOLEAN *)var->sqldata = (lval != 0) ? FB_TRUE : FB_FALSE;
break;
case IS_DOUBLE:
*(FB_BOOLEAN *)var->sqldata = (dval != 0) ? FB_TRUE : FB_FALSE;
break;
default:
if (!zend_binary_strncasecmp(Z_STRVAL_P(b_var), Z_STRLEN_P(b_var), "true", 4, 4)) {
*(FB_BOOLEAN *)var->sqldata = FB_TRUE;
} else if (!zend_binary_strncasecmp(Z_STRVAL_P(b_var), Z_STRLEN_P(b_var), "false", 5, 5)) {
*(FB_BOOLEAN *)var->sqldata = FB_FALSE;
} else {
_php_ibase_module_error("Parameter %d: cannot convert string to boolean", i+1);
rv = FAILURE;
continue;
}
}
break;
}
case IS_NULL:
buf[i].sqlind = -1;
break;
default:
_php_ibase_module_error("Parameter %d: must be boolean", i+1);
rv = FAILURE;
continue;
}
var->sqltype = SQL_BOOLEAN;
continue;
#endif
case SQL_ARRAY:
if (Z_TYPE_P(b_var) != IS_ARRAY) {
convert_to_string(b_var);
if (Z_STRLEN_P(b_var) != BLOB_ID_LEN ||
!_php_ibase_string_to_quad(Z_STRVAL_P(b_var), &buf[i].val.qval)) {
_php_ibase_module_error("Parameter %d: invalid array ID",i+1);
rv = FAILURE;
}
} else {
/* convert the array data into something IB can understand */
ibase_array *ar = &ib_query->in_array[array_cnt];
void *array_data = emalloc(ar->ar_size);
ISC_QUAD array_id = { 0, 0 };
if (FAILURE == _php_ibase_bind_array(b_var, array_data, ar->ar_size,
ar, 0)) {
_php_ibase_module_error("Parameter %d: failed to bind array argument", i+1);
efree(array_data);
rv = FAILURE;
continue;
}
if (isc_array_put_slice(IB_STATUS, &ib_query->link->handle, &ib_query->trans->handle,
&array_id, &ar->ar_desc, array_data, &ar->ar_size)) {
_php_ibase_error();
efree(array_data);
return FAILURE;
}
buf[i].val.qval = array_id;
efree(array_data);
}
++array_cnt;
continue;
} /* switch */
/* we end up here if none of the switch cases handled the field */
convert_to_string(b_var);
var->sqldata = Z_STRVAL_P(b_var);
var->sqllen = Z_STRLEN_P(b_var);
var->sqltype = SQL_TEXT;
} /* for */
return rv;
}
/* }}} */
static void _php_ibase_alloc_xsqlda(XSQLDA *sqlda) /* {{{ */
{
int i;
for (i = 0; i < sqlda->sqld; i++) {
XSQLVAR *var = &sqlda->sqlvar[i];
switch (var->sqltype & ~1) {
case SQL_TEXT:
var->sqldata = safe_emalloc(sizeof(char), var->sqllen, 0);
break;
case SQL_VARYING:
var->sqldata = safe_emalloc(sizeof(char), var->sqllen + sizeof(short), 0);
break;
// Boolean data type exists since FB 3.0
#ifdef SQL_BOOLEAN
case SQL_BOOLEAN:
var->sqldata = emalloc(sizeof(FB_BOOLEAN));
break;
#endif
case SQL_SHORT:
var->sqldata = emalloc(sizeof(short));
break;
case SQL_LONG:
var->sqldata = emalloc(sizeof(ISC_LONG));
break;
case SQL_FLOAT:
var->sqldata = emalloc(sizeof(float));
break;
case SQL_DOUBLE:
var->sqldata = emalloc(sizeof(double));
break;
case SQL_INT64:
var->sqldata = emalloc(sizeof(ISC_INT64));
break;
case SQL_TIMESTAMP:
var->sqldata = emalloc(sizeof(ISC_TIMESTAMP));
break;
case SQL_TYPE_DATE:
var->sqldata = emalloc(sizeof(ISC_DATE));
break;
case SQL_TYPE_TIME:
var->sqldata = emalloc(sizeof(ISC_TIME));
break;
case SQL_BLOB:
case SQL_ARRAY:
var->sqldata = emalloc(sizeof(ISC_QUAD));
break;
} /* switch */
if (var->sqltype & 1) { /* sql NULL flag */
var->sqlind = emalloc(sizeof(short));
} else {
var->sqlind = NULL;
}
} /* for */
}
/* }}} */
static int _php_ibase_exec(INTERNAL_FUNCTION_PARAMETERS, ibase_result **ib_resultp, /* {{{ */
ibase_query *ib_query, zval *args)
{
XSQLDA *in_sqlda = NULL, *out_sqlda = NULL;
BIND_BUF *bind_buf = NULL;
int i, rv = FAILURE;
static char info_count[] = { isc_info_sql_records };
char result[64];
ISC_STATUS isc_result;
int argc = ib_query->in_sqlda ? ib_query->in_sqlda->sqld : 0;
RESET_ERRMSG;
for (i = 0; i < argc; ++i) {
SEPARATE_ZVAL(&args[i]);
}
switch (ib_query->statement_type) {
isc_tr_handle tr;
ibase_tr_list **l;
ibase_trans *trans;
case isc_info_sql_stmt_start_trans:
/* a SET TRANSACTION statement should be executed with a NULL trans handle */
tr = 0;
if (isc_dsql_execute_immediate(IB_STATUS, &ib_query->link->handle, &tr, 0,
ib_query->query, ib_query->dialect, NULL)) {
_php_ibase_error();
goto _php_ibase_exec_error;
}