Repository navigation
Expand file tree
/
Copy pathibase_query.c
More file actions
2253 lines (1938 loc) · 64.7 KB
/
Copy pathibase_query.c
File metadata and controls
2253 lines (1938 loc) · 64.7 KB
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 |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Jouni Ahto <jouni.ahto@exdec.fi> |
| Andrew Avdeev <andy@simgts.mv.ru> |
| Ard Biesheuvel <a.k.biesheuvel@its.tudelft.nl> |
| Martin Koeditz <martin.koeditz@it-syn.de> |
| Martins Lazdans <marrtins@dqdp.net> |
| others |
+----------------------------------------------------------------------+
| You'll find history on Github |
| https://github.com/FirebirdSQL/php-firebird/commits/master |
+----------------------------------------------------------------------+
*/
// #pragma GCC diagnostic error "-Wextra"
// #pragma GCC diagnostic error "-Wall"
// #pragma GCC diagnostic ignored "-Wimplicit-fallthrough"
#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 "ext/date/php_date.h"
#include "zend_interfaces.h"
#include "php_interbase.h"
#include "php_ibase_includes.h"
#include "firebird_utils.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
#ifndef PHP_DATE_INIT_CTOR
# define PHP_DATE_INIT_CTOR 0x01
#endif
#ifndef PHP_DATE_INIT_FORMAT
# define PHP_DATE_INIT_FORMAT 0x02
#endif
/*
* php_date_initialize()'s last argument changed meaning in PHP 8.0. Before that it is a
* plain `int ctor` whose only effect is whether a failed parse warns -- parsing against a
* format is selected by passing a non-NULL format, not by a flag. Passing the 8.x flag
* there would warn on every parse we deliberately let fail.
*/
#if PHP_VERSION_ID >= 80000
# define FBP_DATE_INIT_FORMAT PHP_DATE_INIT_FORMAT
#else
# define FBP_DATE_INIT_FORMAT 0
#endif
static int le_query;
static void _php_ibase_alloc_xsqlda_vars(XSQLDA *sqlda, ISC_SHORT *nullinds);
static int _php_ibase_set_query_info(ibase_query *ib_query);
static int _php_ibase_fetch_query_res(zval *from, ibase_query **ib_query);
static int _php_ibase_alloc_ht_aliases(ibase_query *ib_query);
static void _php_ibase_alloc_ht_ind(ibase_query *ib_query);
static void _php_ibase_free_query_impl(INTERNAL_FUNCTION_PARAMETERS, int as_result);
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);
}
efree(sqlda);
}
}
/* }}} */
static void _php_ibase_free_query(ibase_query *ib_query) /* {{{ */
{
IBDEBUG("Freeing query...");
if (ib_query->stmt) {
ISC_STATUS_ARRAY status;
isc_dsql_free_statement(status, &ib_query->stmt, DSQL_drop);
}
if(ib_query->out_nullind)efree(ib_query->out_nullind);
if(ib_query->out_sql_types)efree(ib_query->out_sql_types);
if(ib_query->bind_buf)efree(ib_query->bind_buf);
if(ib_query->in_sqlda)efree(ib_query->in_sqlda); // Note to myself: no need for _php_ibase_free_xsqlda()
if(ib_query->out_sqlda)_php_ibase_free_xsqlda(ib_query->out_sqlda);
if(ib_query->query)efree(ib_query->query);
if(ib_query->ht_aliases)zend_array_destroy(ib_query->ht_aliases);
if(ib_query->ht_ind)zend_array_destroy(ib_query->ht_ind);
if(ib_query->date_scratch)OBJ_RELEASE(ib_query->date_scratch);
if(ib_query->sql_types)efree(ib_query->sql_types);
if(ib_query->sql_lens)efree(ib_query->sql_lens);
efree(ib_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);
}
}
/* }}} */
void php_ibase_query_minit(INIT_FUNC_ARGS) /* {{{ */
{
(void)type;
le_query = zend_register_list_destructors_ex(php_ibase_free_query_rsrc, NULL,
LE_QUERY, module_number);
}
/* }}} */
/* allocate and prepare query */
static int _php_ibase_prepare(ibase_query **new_query, ibase_db_link *link, /* {{{ */
ibase_trans *trans, zend_resource *trans_res, char *query)
{
/* Return FAILURE, if querystring is empty */
if (*query == '\0') {
php_error_docref(NULL, E_WARNING, "Querystring empty.");
return FAILURE;
}
ibase_query *ib_query = ecalloc(1, sizeof(ibase_query));
ib_query->res = zend_register_resource(ib_query, le_query);
ib_query->link = link;
ib_query->trans = trans;
ib_query->trans_res = trans_res;
ib_query->dialect = link->dialect;
ib_query->query = estrdup(query);
if (isc_dsql_allocate_statement(IB_STATUS, &link->handle, &ib_query->stmt)) {
_php_ibase_error();
goto _php_ibase_alloc_query_error;
}
// TODO: provide query_len
if (isc_dsql_prepare(IB_STATUS, &ib_query->trans->handle, &ib_query->stmt,
0, query, link->dialect, NULL)) {
IBDEBUG("isc_dsql_prepare() failed\n");
_php_ibase_error();
goto _php_ibase_alloc_query_error;
}
if(_php_ibase_set_query_info(ib_query)){
goto _php_ibase_alloc_query_error;
}
if(ib_query->out_fields_count) {
ib_query->out_sqlda = (XSQLDA *) emalloc(XSQLDA_LENGTH(ib_query->out_fields_count));
ib_query->out_sqlda->sqln = ib_query->out_fields_count;
ib_query->out_sqlda->version = SQLDA_CURRENT_VERSION;
if (isc_dsql_describe(IB_STATUS, &ib_query->stmt, SQLDA_CURRENT_VERSION, ib_query->out_sqlda)) {
IBDEBUG("isc_dsql_describe() failed\n");
_php_ibase_error();
goto _php_ibase_alloc_query_error;
}
assert(ib_query->out_sqlda->sqln == ib_query->out_sqlda->sqld);
assert(ib_query->out_sqlda->sqld == ib_query->out_fields_count);
ib_query->out_nullind = safe_emalloc(sizeof(*ib_query->out_nullind), ib_query->out_sqlda->sqld, 0);
/* Remember what the server said before _php_ibase_alloc_xsqlda_vars() rewrites
* CHAR as VARCHAR, so ibase_field_info() can still report the declared type. */
ib_query->out_sql_types = safe_emalloc(sizeof(*ib_query->out_sql_types), ib_query->out_sqlda->sqld, 0);
for (int i = 0; i < ib_query->out_sqlda->sqld; i++) {
ib_query->out_sql_types[i] = ib_query->out_sqlda->sqlvar[i].sqltype;
}
_php_ibase_alloc_xsqlda_vars(ib_query->out_sqlda, ib_query->out_nullind);
}
if(ib_query->in_fields_count) {
ib_query->in_sqlda = emalloc(XSQLDA_LENGTH(ib_query->in_fields_count));
ib_query->in_sqlda->sqln = ib_query->in_fields_count;
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)) {
IBDEBUG("isc_dsql_describe_bind() failed\n");
_php_ibase_error();
goto _php_ibase_alloc_query_error;
}
ib_query->sql_types = emalloc(sizeof(*ib_query->sql_types) * ib_query->in_fields_count);
ib_query->sql_lens = emalloc(sizeof(*ib_query->sql_lens) * ib_query->in_fields_count);
for (int i = 0; i < ib_query->in_fields_count; i++) {
ib_query->sql_types[i] = ib_query->in_sqlda->sqlvar[i].sqltype;
ib_query->sql_lens[i] = ib_query->in_sqlda->sqlvar[i].sqllen;
}
assert(ib_query->in_sqlda->sqln == ib_query->in_sqlda->sqld);
assert(ib_query->in_sqlda->sqld == ib_query->in_fields_count);
ib_query->bind_buf = safe_emalloc(sizeof(BIND_BUF), ib_query->in_sqlda->sqld, 0);
}
*new_query = ib_query;
return SUCCESS;
_php_ibase_alloc_query_error:
zend_list_delete(ib_query->res);
return FAILURE;
}
/* }}} */
/*
* Scale a plain integer up to the representation a NUMERIC/DECIMAL column expects on the
* wire: with sqlscale -2, the value 5 becomes 500 so that it reads back as 5.00. Returns
* FAILURE if the scaled value would not fit the target integer type, in which case the
* caller should fall back to letting Firebird parse and report on the value.
*/
static int _php_ibase_apply_scale(int64_t *val, int sqlscale, int64_t min, int64_t max) /* {{{ */
{
if (sqlscale >= 0) {
return SUCCESS;
}
int negative = (*val < 0);
/* Magnitude of the limit on the side we are on; the negative range holds one more */
uint64_t max_mag = negative ? (uint64_t)(-(min + 1)) + 1 : (uint64_t)max;
uint64_t mag = negative ? 0 - (uint64_t)*val : (uint64_t)*val;
for (int i = sqlscale; i < 0; i++) {
if (mag > max_mag / 10) {
return FAILURE;
}
mag *= 10;
}
*val = negative ? (int64_t)(0 - mag) : (int64_t)mag;
return SUCCESS;
}
/* }}} */
static int _php_ibase_bind(ibase_query *ib_query, zval *b_vars) /* {{{ */
{
int64_t long_val;
struct tm t;
const char *tformat = NULL;
int64_t long_min, long_max;
assert(ib_query->in_fields_count > 0);
for (int i = 0; i < ib_query->in_sqlda->sqld; ++i) { /* bound vars */
zval *b_var = &b_vars[i];
XSQLVAR *var = &ib_query->in_sqlda->sqlvar[i];
// We need keep track of original type because XSQLVAR type could get modified
var->sqltype = ib_query->sql_types[i];
var->sqllen = ib_query->sql_lens[i];
BIND_BUF *buf = &ib_query->bind_buf[i];
var->sqlind = &buf->nullind;
var->sqldata = (void*)&buf->val;
ISC_SHORT sqltype = var->sqltype & ~1;
/* 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 (sqltype) {
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:
#if FB_API_VER >= 40
case SQL_INT128:
case SQL_DEC16:
case SQL_DEC34:
case SQL_TIMESTAMP_TZ:
case SQL_TIME_TZ:
#endif
force_null = (Z_STRLEN_P(b_var) == 0);
}
if (! force_null) break;
case IS_NULL:
/*
* The null indicator only exists for a nullable parameter: describe_bind
* clears the low bit of sqltype otherwise, and Firebird then ignores
* sqlind and reads sqldata literally -- which here is an unwritten slot,
* so the row would silently get whatever was on the heap. Refuse instead.
*/
if (!(var->sqltype & 1)) {
_php_ibase_module_error("Parameter %d: cannot bind NULL to a NOT NULL column", i+1);
return FAILURE;
}
*var->sqlind = -1;
continue;
}
/* if we make it to this point, we must provide a value for the parameter */
*var->sqlind = 0;
// Type checks and conversations
switch (sqltype)
{
case SQL_FLOAT:
case SQL_DOUBLE:
switch (Z_TYPE_P(b_var))
{
case IS_DOUBLE: break;
case IS_LONG:
case IS_TRUE:
case IS_FALSE:
convert_to_double(b_var);
break;
case IS_STRING:
/*
* convert_to_double() would silently yield 0.0 for a non-numeric
* string, storing garbage instead of reporting it. Let Firebird
* parse the string and raise a conversion error, as before.
*/
goto cast_to_string;
default:
goto cast_to_string;
}
/*
* Firebird has no representation for infinity or NaN: every engine operation
* that would produce one raises an arithmetic exception instead, and the engine
* is documented not to let these values escape (FirebirdSQL/firebird#2991,
* #2917, #2992, #1780). Binding the raw IEEE754 bits bypasses all of that -- on
* 2.5 through 5.0 the value reaches the page unchecked, leaving a column the
* engine could never produce itself and where later arithmetic fails far from
* the INSERT responsible. Hand these to Firebird as a string so it rejects them
* the way it always has, with a recoverable error rather than a fatal one.
*/
if (zend_isinf(Z_DVAL_P(b_var)) || zend_isnan(Z_DVAL_P(b_var))) {
goto cast_to_string;
}
/*
* A double larger than FLT_MAX would silently become infinity in the
* cast below, slipping past the guard above. Let Firebird report it.
*/
if (sqltype == SQL_FLOAT) {
double d_val = Z_DVAL_P(b_var);
if (d_val > (double)FLT_MAX || d_val < -(double)FLT_MAX) {
goto cast_to_string;
}
}
break;
case SQL_TYPE_DATE:
case SQL_TIMESTAMP:
case SQL_TYPE_TIME:
#if FB_API_VER >= 40
case SQL_TIMESTAMP_TZ:
case SQL_TIME_TZ:
#endif
switch (Z_TYPE_P(b_var))
{
case IS_LONG: {
/* zend_long and time_t differ in width on 32-bit builds, so
* copy rather than aliasing the zval's storage. */
time_t t_sec = (time_t) Z_LVAL_P(b_var);
if (!php_gmtime_r(&t_sec, &t)) {
_php_ibase_module_error("Argument %d: could not parse timestamp", i+1);
return FAILURE;
}
break;
}
/* Parsed below against ibase.dateformat and friends, see parse_datetime */
case IS_STRING:
break;
default:
goto cast_to_string;
}
break;
#if FB_API_VER >= 40
case SQL_DEC16:
case SQL_DEC34:
case SQL_INT128:
goto cast_to_string;
#endif
case SQL_SHORT:
case SQL_LONG:
case SQL_INT64:
if (sqltype == SQL_SHORT) {
long_min = INT16_MIN;
long_max = INT16_MAX;
} else if (sqltype == SQL_LONG) {
long_min = INT32_MIN;
long_max = INT32_MAX;
} else if (sqltype == SQL_INT64) {
long_min = INT64_MIN;
long_max = INT64_MAX;
} else {
// Unreachable
}
switch (Z_TYPE_P(b_var))
{
case IS_LONG:
long_val = Z_LVAL_P(b_var);
break;
case IS_TRUE:
long_val = 1;
break;
case IS_FALSE:
long_val = 0;
break;
default:
goto cast_to_string;
}
/*
* NUMERIC/DECIMAL are described as SQL_SHORT/LONG/INT64 with a negative sqlscale
* and travel as a *scaled* integer: NUMERIC(10,2) value 5.00 is sent as 500.
*/
if (_php_ibase_apply_scale(&long_val, var->sqlscale, long_min, long_max) == FAILURE) {
goto cast_to_string;
}
// Let firebird handle overflows
if (long_val < long_min || long_val > long_max) {
goto cast_to_string;
}
break;
}
// Setting the data
switch (sqltype)
{
case SQL_SHORT:
buf->val.lval = (ISC_SHORT)long_val;
break;
case SQL_LONG:
buf->val.lval = (ISC_LONG)long_val;
break;
case SQL_INT64:
buf->val.lval = (ISC_INT64)long_val;
break;
case SQL_FLOAT:
buf->val.fval = (float)Z_DVAL_P(b_var);
break;
case SQL_DOUBLE:
buf->val.dval = (double)Z_DVAL_P(b_var);
break;
case SQL_TYPE_DATE:
switch (Z_TYPE_P(b_var))
{
case IS_LONG:
isc_encode_sql_date(&t, &buf->val.dtval);
break;
case IS_STRING:
tformat = IBG(dateformat);
goto parse_datetime;
default:
fbp_fatal("BUG: unreachable - other zval types already went to cast_to_string");
}
break;
case SQL_TIMESTAMP:
switch (Z_TYPE_P(b_var))
{
case IS_LONG:
{
ISC_TIMESTAMP *ts = &buf->val.tsval;
isc_encode_sql_date(&t, &ts->timestamp_date);
isc_encode_sql_time(&t, &ts->timestamp_time);
} break;
case IS_STRING:
tformat = IBG(timestampformat);
goto parse_datetime;
default:
fbp_fatal("BUG: unreachable - other zval types already went to cast_to_string");
}
break;
case SQL_TYPE_TIME:
switch (Z_TYPE_P(b_var))
{
/* An integer binds as the time of day of that unix timestamp, as before
* the rewrite: 0 -> 00:00:00, -1 -> 23:59:59 */
case IS_LONG:
isc_encode_sql_time(&t, &buf->val.tmval);
break;
case IS_STRING:
tformat = IBG(timeformat);
goto parse_datetime;
default:
fbp_fatal("BUG: unreachable - other zval types already went to cast_to_string");
}
break;
#if FB_API_VER >= 40
case SQL_TIMESTAMP_TZ:
switch (Z_TYPE_P(b_var))
{
case IS_LONG:
{
ISC_TIMESTAMP_TZ *ts = &buf->val.tsval_tz;
isc_encode_sql_date(&t, &ts->utc_timestamp.timestamp_date);
isc_encode_sql_time(&t, &ts->utc_timestamp.timestamp_time);
/* GMT: Firebird offset ids are minutes + 1439, so 0 means -23:59 */
ts->time_zone = 1439;
} break;
case IS_STRING:
tformat = IBG(timestampformat_tz);
goto parse_datetime;
default:
fbp_fatal("BUG: unreachable - other zval types already went to cast_to_string");
}
break;
case SQL_TIME_TZ:
switch (Z_TYPE_P(b_var))
{
case IS_LONG:
isc_encode_sql_time(&t, &buf->val.tmval_tz.utc_time);
buf->val.tmval_tz.time_zone = 1439; /* GMT: offset ids are minutes + 1439, so 0 means -23:59 */
break;
case IS_STRING:
tformat = IBG(timeformat_tz);
goto parse_datetime;
default:
fbp_fatal("BUG: unreachable - other zval types already went to cast_to_string");
}
break;
#endif
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->val.qval)) {
ibase_blob ib_blob = { 0 };
ib_blob.type = 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) {
ISC_STATUS_ARRAY cancel_status;
isc_cancel_blob(cancel_status, &ib_blob.bl_handle);
return FAILURE;
}
if (isc_close_blob(IB_STATUS, &ib_blob.bl_handle)) {
_php_ibase_error();
return FAILURE;
}
buf->val.qval = ib_blob.bl_qd;
}
break;
#ifdef SQL_BOOLEAN
case SQL_BOOLEAN:
switch (Z_TYPE_P(b_var)) {
case IS_LONG:
case IS_DOUBLE:
case IS_TRUE:
case IS_FALSE:
buf->val.bval = 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)) {
buf->val.bval = FB_FALSE;
break;
}
switch (is_numeric_string(Z_STRVAL_P(b_var), Z_STRLEN_P(b_var), &lval, &dval, 0)) {
case IS_LONG:
buf->val.bval = (lval != 0) ? FB_TRUE : FB_FALSE;
break;
case IS_DOUBLE:
buf->val.bval = (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)) {
buf->val.bval = FB_TRUE;
} else if (!zend_binary_strncasecmp(Z_STRVAL_P(b_var), Z_STRLEN_P(b_var), "false", 5, 5)) {
buf->val.bval = FB_FALSE;
} else {
_php_ibase_module_error("Parameter %d: cannot convert string to boolean", i+1);
return FAILURE;
}
}
break;
}
default:
_php_ibase_module_error("Parameter %d: must be boolean", i+1);
return FAILURE;
}
break;
#endif
case SQL_ARRAY:
fbp_fatal("SQL_ARRAY type is not supported");
default:
fbp_warning("Unknown sqltype: %d %s:%d. Probably compiled against outdated fbclient library (%d)", sqltype, __FILE__, __LINE__, FB_API_VER);
case SQL_TEXT:
case SQL_VARYING:
cast_to_string:
if (Z_TYPE_P(b_var) == IS_DOUBLE) {
/*
* convert_to_string() formats with the `precision` ini -- 14 significant
* digits by default -- which silently drops digits the column could
* hold: 1234567.891234567 into NUMERIC(18,9) arrived as
* 1234567.891234600. Format with `serialize_precision` instead, the
* same call var_export() and json_encode() make; its default of -1
* selects the shortest text that reads back as the identical double.
* Firebird then rounds to the column's own scale.
*/
char dbuf[PHP_DOUBLE_MAX_LENGTH];
php_gcvt(Z_DVAL_P(b_var), (int)PG(serialize_precision), '.', 'E', dbuf);
ZVAL_STRING(b_var, dbuf);
} else {
convert_to_string(b_var);
}
if (Z_STRLEN_P(b_var) > SHRT_MAX - 2) {
_php_ibase_module_error("Parameter %d: string buffer overflow (%zu bytes, max %d)", i+1, Z_STRLEN_P(b_var), SHRT_MAX - 2);
return FAILURE;
}
var->sqltype = SQL_TEXT | (var->sqltype & 1);
var->sqldata = Z_STRVAL_P(b_var);
var->sqllen = (ISC_SHORT)Z_STRLEN_P(b_var);
break;
} /* switch */
continue;
parse_datetime: {
zval dateo;
php_date_obj *obj;
/*
* No format configured for this type: don't try to parse at all, just hand
* the string to Firebird, which accepts its own spellings and reports what
* it cannot read.
*/
if (tformat == NULL || *tformat == '\0') {
goto cast_to_string;
}
php_date_instantiate(php_date_get_date_ce(), &dateo);
obj = Z_PHPDATE_P(&dateo);
// php_printf(" tformat: %s\n", tformat);
/*
* A string that does not match the configured format is handed to Firebird to parse,
* as it was before the rewrite -- it accepts spellings of its own ('2025-11-06' for a
* TIMESTAMP, a trailing time on a DATE) and reports what it cannot read itself.
*/
if (!php_date_initialize(obj, Z_STRVAL_P(b_var), Z_STRLEN_P(b_var), tformat, NULL, FBP_DATE_INIT_FORMAT)) {
zval_ptr_dtor(&dateo);
goto cast_to_string;
}
switch (sqltype) {
case SQL_TYPE_TIME:
buf->val.tmval = fbu_encode_time(IBG(master_instance), (unsigned)obj->time->h, (unsigned)obj->time->i, (unsigned)obj->time->s, FB_USEC_TO_FRACTIONS(obj->time->us));
break;
case SQL_TYPE_DATE:
buf->val.dtval = fbu_encode_date(IBG(master_instance), (unsigned)obj->time->y, (unsigned)obj->time->m, (unsigned)obj->time->d);
break;
case SQL_TIMESTAMP:
ISC_TIMESTAMP *ts = &buf->val.tsval;
ts->timestamp_date = fbu_encode_date(IBG(master_instance), (unsigned)obj->time->y, (unsigned)obj->time->m, (unsigned)obj->time->d);
ts->timestamp_time = fbu_encode_time(IBG(master_instance), (unsigned)obj->time->h, (unsigned)obj->time->i, (unsigned)obj->time->s, FB_USEC_TO_FRACTIONS(obj->time->us));
break;
#if FB_API_VER >= 40
case SQL_TIME_TZ:
fbu_encode_time_tz(IBG(master_instance), &buf->val.tmval_tz, (unsigned)obj->time->h, (unsigned)obj->time->i, (unsigned)obj->time->s, FB_USEC_TO_FRACTIONS(obj->time->us), obj->time->tz_info->name);
break;
case SQL_TIMESTAMP_TZ:
fbu_encode_timestamp_tz(IBG(master_instance), &buf->val.tsval_tz,
(unsigned)obj->time->y, (unsigned)obj->time->m, (unsigned)obj->time->d,
(unsigned)obj->time->h, (unsigned)obj->time->i, (unsigned)obj->time->s, FB_USEC_TO_FRACTIONS(obj->time->us),
obj->time->tz_info->name
);
break;
#endif
default: fbp_fatal("BUG: unreachable - only date/time sqltypes goto parse_datetime");
}
zval_ptr_dtor(&dateo);
} // parse_datetime:
} /* for */
return SUCCESS;
}
/* }}} */
static void _php_ibase_alloc_xsqlda_vars(XSQLDA *sqlda, ISC_SHORT *nullinds) /* {{{ */
{
for (int i = 0; i < sqlda->sqld; i++) {
XSQLVAR *var = &sqlda->sqlvar[i];
// Convert CHAR to VARCHAR to avoid dealiong with padding with
// multi character charsets
if ((var->sqltype & ~1) == SQL_TEXT) {
var->sqltype = SQL_VARYING | (var->sqltype & 1);
}
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;
#ifdef SQL_BOOLEAN
case SQL_BOOLEAN:
var->sqldata = emalloc(sizeof(FB_BOOLEAN));
break;
#endif
case SQL_SHORT:
var->sqldata = emalloc(sizeof(ISC_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_ARRAY:
fbp_fatal("SQL_ARRAY type is not supported");
case SQL_BLOB:
case SQL_QUAD:
var->sqldata = emalloc(sizeof(ISC_QUAD));
break;
#if FB_API_VER >= 40
// These are converted to VARCHAR via isc_dpb_set_bind tag at connect
// case SQL_DEC16:
// case SQL_DEC34:
// case SQL_INT128:
case SQL_TIMESTAMP_TZ:
var->sqldata = emalloc(sizeof(ISC_TIMESTAMP_TZ));
break;
case SQL_TIME_TZ:
var->sqldata = emalloc(sizeof(ISC_TIME_TZ));
break;
#endif
default:
fbp_fatal("Unknown sqltype: %d for field %s %s:%d. Probably compiled against outdated fbclient library (%d)", var->sqltype, var->sqlname, __FILE__, __LINE__, FB_API_VER);
} /* switch */
if (var->sqltype & 1) { /* sql NULL flag */
var->sqlind = &nullinds[i];
} else {
var->sqlind = NULL;
}
} /* for */
}
/* }}} */
static int _php_ibase_exec(INTERNAL_FUNCTION_PARAMETERS, ibase_query *ib_query, zval *args, int bind_n) /* {{{ */
{
int i, rv = FAILURE;
static char info_count[] = { isc_info_sql_records };
char result[64];
ISC_STATUS isc_result;
int argc = ib_query->in_fields_count;
(void)execute_data;
RESET_ERRMSG;
RETVAL_FALSE;
if (bind_n != argc) {
php_error_docref(NULL, (bind_n < argc) ? E_WARNING : E_NOTICE,
"Statement expects %d arguments, %d given", argc, bind_n);
if (bind_n < argc) {
return FAILURE;
}
}
/* Have we used this cursor before and it's still open (exec proc has no cursor) ? */
if (ib_query->is_open && ib_query->statement_type != isc_info_sql_stmt_exec_procedure) {
IBDEBUG("Implicitly closing a cursor");
// TODO: ini configurable warning
// php_error_docref(NULL, E_NOTICE, "Implicitly closing a cursor");
if (isc_dsql_free_statement(IB_STATUS, &ib_query->stmt, DSQL_close)) {
_php_ibase_error();
return FAILURE;
}
ib_query->is_open = 0;
}
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_ex_error;
}
trans = (ibase_trans *) emalloc(sizeof(ibase_trans));
trans->handle = tr;
trans->link_cnt = 1;
trans->affected_rows = 0;
trans->db_link[0] = ib_query->link;
if (ib_query->link->tr_list == NULL) {
ib_query->link->tr_list = (ibase_tr_list *) emalloc(sizeof(ibase_tr_list));
ib_query->link->tr_list->trans = NULL;
ib_query->link->tr_list->next = NULL;
}
/* link the transaction into the connection-transaction list */
for (l = &ib_query->link->tr_list; *l != NULL; l = &(*l)->next);
*l = (ibase_tr_list *) emalloc(sizeof(ibase_tr_list));
(*l)->trans = trans;
(*l)->next = NULL;
RETVAL_RES(zend_register_resource(trans, le_trans));
Z_TRY_ADDREF_P(return_value);
return SUCCESS;
case isc_info_sql_stmt_commit:
case isc_info_sql_stmt_rollback:
if (isc_dsql_execute_immediate(IB_STATUS, &ib_query->link->handle,
&ib_query->trans->handle, 0, ib_query->query, ib_query->dialect, NULL)) {
_php_ibase_error();
goto _php_ibase_ex_error;
}
if (ib_query->trans->handle == 0 && ib_query->trans_res != NULL) {
/* transaction was released by the query and was a registered resource,
so we have to release it */
zend_list_delete(ib_query->trans_res);
ib_query->trans_res = NULL;
}
RETVAL_TRUE;
return SUCCESS;
default:
RETVAL_FALSE;
}
if (ib_query->in_fields_count) { /* has placeholders */
IBDEBUG("Query wants XSQLDA for input");
if (_php_ibase_bind(ib_query, args) == FAILURE) {
IBDEBUG("Could not bind input XSQLDA");
goto _php_ibase_ex_error;
}
}
if (ib_query->statement_type == isc_info_sql_stmt_exec_procedure) {
isc_result = isc_dsql_execute2(IB_STATUS, &ib_query->trans->handle,
&ib_query->stmt, SQLDA_CURRENT_VERSION, ib_query->in_sqlda, ib_query->out_sqlda);
} else {
isc_result = isc_dsql_execute(IB_STATUS, &ib_query->trans->handle,
&ib_query->stmt, SQLDA_CURRENT_VERSION, ib_query->in_sqlda);
}
if (isc_result) {
IBDEBUG("Could not execute query");
_php_ibase_error();
goto _php_ibase_ex_error;
}
ib_query->trans->affected_rows = 0;
if (ib_query->out_sqlda) { /* output variables in select, select for update */
ib_query->has_more_rows = 1;
ib_query->is_open = 1;
RETVAL_RES(ib_query->res);
Z_TRY_ADDREF_P(return_value);
return SUCCESS;
}
switch (ib_query->statement_type) {
unsigned long affected_rows;
case isc_info_sql_stmt_insert:
case isc_info_sql_stmt_update:
case isc_info_sql_stmt_delete:
case isc_info_sql_stmt_exec_procedure:
if (isc_dsql_sql_info(IB_STATUS, &ib_query->stmt, sizeof(info_count),
info_count, sizeof(result), result)) {
_php_ibase_error();
goto _php_ibase_ex_error;
}
affected_rows = 0;
if (result[0] == isc_info_sql_records) {
unsigned i = 3, result_size = isc_vax_integer(&result[1],2);
while (result[i] != isc_info_end && i < result_size) {
short len = (short)isc_vax_integer(&result[i+1],2);
if (result[i] != isc_info_req_select_count) {
affected_rows += isc_vax_integer(&result[i+3],len);
}
i += len+3;
}
}
ib_query->trans->affected_rows = affected_rows;
if (!ib_query->out_sqlda) { /* no result set is being returned */
if (affected_rows) {
RETVAL_LONG(affected_rows);
} else {
RETVAL_TRUE;
}
break;
}
default:
RETVAL_TRUE;
}
rv = SUCCESS;
_php_ibase_ex_error:
return rv;
}
/* }}} */