-
Notifications
You must be signed in to change notification settings - Fork 7.8k
/
Copy pathpdo_stmt.c
2426 lines (2082 loc) · 68.1 KB
/
pdo_stmt.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. |
+----------------------------------------------------------------------+
| Author: Wez Furlong <[email protected]> |
| Marcus Boerger <[email protected]> |
| Sterling Hughes <[email protected]> |
+----------------------------------------------------------------------+
*/
/* The PDO Statement Handle Class */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "ext/standard/php_var.h"
#include "php_pdo.h"
#include "php_pdo_driver.h"
#include "php_pdo_int.h"
#include "zend_exceptions.h"
#include "zend_interfaces.h"
#include "php_memory_streams.h"
#include "pdo_stmt_arginfo.h"
#define PHP_STMT_GET_OBJ \
pdo_stmt_t *stmt = Z_PDO_STMT_P(ZEND_THIS); \
if (!stmt->dbh) { \
zend_throw_error(NULL, "%s object is uninitialized", ZSTR_VAL(Z_OBJ(EX(This))->ce->name)); \
RETURN_THROWS(); \
} \
static inline bool rewrite_name_to_position(pdo_stmt_t *stmt, struct pdo_bound_param_data *param) /* {{{ */
{
if (stmt->bound_param_map) {
/* rewriting :name to ? style.
* We need to fixup the parameter numbers on the parameters.
* If we find that a given named parameter has been used twice,
* we will raise an error, as we can't be sure that it is safe
* to bind multiple parameters onto the same zval in the underlying
* driver */
zend_string *name;
int position = 0;
if (stmt->named_rewrite_template) {
/* this is not an error here */
return 1;
}
if (!param->name) {
/* do the reverse; map the parameter number to the name */
if ((name = zend_hash_index_find_ptr(stmt->bound_param_map, param->paramno)) != NULL) {
param->name = zend_string_copy(name);
return 1;
}
/* TODO Error? */
pdo_raise_impl_error(stmt->dbh, stmt, "HY093", "parameter was not defined");
return 0;
}
ZEND_HASH_FOREACH_PTR(stmt->bound_param_map, name) {
if (!zend_string_equals(name, param->name)) {
position++;
continue;
}
if (param->paramno >= 0) {
/* TODO Error? */
pdo_raise_impl_error(stmt->dbh, stmt, "IM001", "PDO refuses to handle repeating the same :named parameter for multiple positions with this driver, as it might be unsafe to do so. Consider using a separate name for each parameter instead");
return -1;
}
param->paramno = position;
return 1;
} ZEND_HASH_FOREACH_END();
/* TODO Error? */
pdo_raise_impl_error(stmt->dbh, stmt, "HY093", "parameter was not defined");
return 0;
}
return 1;
}
/* }}} */
/* trigger callback hook for parameters */
static bool dispatch_param_event(pdo_stmt_t *stmt, enum pdo_param_event event_type) /* {{{ */
{
bool ret = 1, is_param = 1;
struct pdo_bound_param_data *param;
HashTable *ht;
if (stmt->dbh->skip_param_evt & (1 << event_type)) {
return 1;
}
if (!stmt->methods->param_hook) {
return 1;
}
ht = stmt->bound_params;
iterate:
if (ht) {
ZEND_HASH_FOREACH_PTR(ht, param) {
if (!stmt->methods->param_hook(stmt, param, event_type)) {
ret = 0;
break;
}
} ZEND_HASH_FOREACH_END();
}
if (ret && is_param) {
ht = stmt->bound_columns;
is_param = 0;
goto iterate;
}
return ret;
}
/* }}} */
bool pdo_stmt_describe_columns(pdo_stmt_t *stmt) /* {{{ */
{
int col;
stmt->columns = ecalloc(stmt->column_count, sizeof(struct pdo_column_data));
for (col = 0; col < stmt->column_count; col++) {
if (!stmt->methods->describer(stmt, col)) {
return false;
}
/* if we are applying case conversions on column names, do so now */
if (stmt->dbh->native_case != stmt->dbh->desired_case && stmt->dbh->desired_case != PDO_CASE_NATURAL) {
zend_string *orig_name = stmt->columns[col].name;
switch (stmt->dbh->desired_case) {
case PDO_CASE_LOWER:
stmt->columns[col].name = zend_string_tolower(orig_name);
zend_string_release(orig_name);
break;
case PDO_CASE_UPPER:
stmt->columns[col].name = zend_string_toupper(orig_name);
zend_string_release(orig_name);
break;
EMPTY_SWITCH_DEFAULT_CASE()
}
}
/* update the column index on named bound parameters */
if (stmt->bound_columns) {
struct pdo_bound_param_data *param;
if ((param = zend_hash_find_ptr(stmt->bound_columns,
stmt->columns[col].name)) != NULL) {
param->paramno = col;
}
}
}
return true;
}
/* }}} */
static void pdo_stmt_reset_columns(pdo_stmt_t *stmt) {
if (stmt->columns) {
int i;
struct pdo_column_data *cols = stmt->columns;
for (i = 0; i < stmt->column_count; i++) {
if (cols[i].name) {
zend_string_release_ex(cols[i].name, 0);
}
}
efree(stmt->columns);
}
stmt->columns = NULL;
stmt->column_count = 0;
}
/**
* Change the column count on the statement. If it differs from the previous one,
* discard existing columns information.
*/
PDO_API void php_pdo_stmt_set_column_count(pdo_stmt_t *stmt, int new_count)
{
/* Columns not yet "described". */
if (!stmt->columns) {
stmt->column_count = new_count;
return;
}
/* The column count has not changed: No need to reload columns description.
* Note: Do not handle attribute name change, without column count change. */
if (new_count == stmt->column_count) {
return;
}
/* Free previous columns to force reload description. */
pdo_stmt_reset_columns(stmt);
stmt->column_count = new_count;
}
static void get_lazy_object(pdo_stmt_t *stmt, zval *return_value) /* {{{ */
{
if (Z_ISUNDEF(stmt->lazy_object_ref)) {
pdo_row_t *row = zend_object_alloc(sizeof(pdo_row_t), pdo_row_ce);
row->stmt = stmt;
zend_object_std_init(&row->std, pdo_row_ce);
ZVAL_OBJ(&stmt->lazy_object_ref, &row->std);
GC_ADDREF(&stmt->std);
GC_DELREF(&row->std);
}
ZVAL_COPY(return_value, &stmt->lazy_object_ref);
}
/* }}} */
static void param_dtor(zval *el) /* {{{ */
{
struct pdo_bound_param_data *param = (struct pdo_bound_param_data *)Z_PTR_P(el);
/* tell the driver that it is going away */
if (param->stmt->methods->param_hook) {
param->stmt->methods->param_hook(param->stmt, param, PDO_PARAM_EVT_FREE);
}
if (param->name) {
zend_string_release_ex(param->name, 0);
}
if (!Z_ISUNDEF(param->parameter)) {
zval_ptr_dtor(¶m->parameter);
ZVAL_UNDEF(¶m->parameter);
}
if (!Z_ISUNDEF(param->driver_params)) {
zval_ptr_dtor(¶m->driver_params);
}
efree(param);
}
/* }}} */
static bool really_register_bound_param(struct pdo_bound_param_data *param, pdo_stmt_t *stmt, bool is_param) /* {{{ */
{
HashTable *hash;
zval *parameter;
struct pdo_bound_param_data *pparam = NULL;
hash = is_param ? stmt->bound_params : stmt->bound_columns;
if (!hash) {
ALLOC_HASHTABLE(hash);
zend_hash_init(hash, 13, NULL, param_dtor, 0);
if (is_param) {
stmt->bound_params = hash;
} else {
stmt->bound_columns = hash;
}
}
if (!Z_ISREF(param->parameter)) {
parameter = ¶m->parameter;
} else {
parameter = Z_REFVAL(param->parameter);
}
if (PDO_PARAM_TYPE(param->param_type) == PDO_PARAM_STR && param->max_value_len <= 0 && !Z_ISNULL_P(parameter)) {
if (!try_convert_to_string(parameter)) {
return 0;
}
} else if (PDO_PARAM_TYPE(param->param_type) == PDO_PARAM_INT && (Z_TYPE_P(parameter) == IS_FALSE || Z_TYPE_P(parameter) == IS_TRUE)) {
convert_to_long(parameter);
} else if (PDO_PARAM_TYPE(param->param_type) == PDO_PARAM_BOOL && Z_TYPE_P(parameter) == IS_LONG) {
convert_to_boolean(parameter);
}
param->stmt = stmt;
param->is_param = is_param;
if (Z_REFCOUNTED(param->driver_params)) {
Z_ADDREF(param->driver_params);
}
if (!is_param && param->name && stmt->columns) {
/* try to map the name to the column */
int i;
for (i = 0; i < stmt->column_count; i++) {
if (zend_string_equals(stmt->columns[i].name, param->name)) {
param->paramno = i;
break;
}
}
/* if you prepare and then execute passing an array of params keyed by names,
* then this will trigger, and we don't want that */
if (param->paramno == -1) {
/* Should this always be an Error? */
char *tmp;
/* TODO Error? */
spprintf(&tmp, 0, "Did not find column name '%s' in the defined columns; it will not be bound", ZSTR_VAL(param->name));
pdo_raise_impl_error(stmt->dbh, stmt, "HY000", tmp);
efree(tmp);
}
}
if (param->name) {
if (is_param && ZSTR_VAL(param->name)[0] != ':') {
zend_string *temp = zend_string_alloc(ZSTR_LEN(param->name) + 1, 0);
ZSTR_VAL(temp)[0] = ':';
memmove(ZSTR_VAL(temp) + 1, ZSTR_VAL(param->name), ZSTR_LEN(param->name) + 1);
param->name = temp;
} else {
param->name = zend_string_init(ZSTR_VAL(param->name), ZSTR_LEN(param->name), 0);
}
}
if (is_param && !rewrite_name_to_position(stmt, param)) {
if (param->name) {
zend_string_release_ex(param->name, 0);
param->name = NULL;
}
return 0;
}
/* ask the driver to perform any normalization it needs on the
* parameter name. Note that it is illegal for the driver to take
* a reference to param, as it resides in transient storage only
* at this time. */
if (stmt->methods->param_hook) {
if (!stmt->methods->param_hook(stmt, param, PDO_PARAM_EVT_NORMALIZE)) {
PDO_HANDLE_STMT_ERR();
if (param->name) {
zend_string_release_ex(param->name, 0);
param->name = NULL;
}
return 0;
}
}
/* delete any other parameter registered with this number.
* If the parameter is named, it will be removed and correctly
* disposed of by the hash_update call that follows */
if (param->paramno >= 0) {
zend_hash_index_del(hash, param->paramno);
}
/* allocate storage for the parameter, keyed by its "canonical" name */
if (param->name) {
pparam = zend_hash_update_mem(hash, param->name, param, sizeof(struct pdo_bound_param_data));
} else {
pparam = zend_hash_index_update_mem(hash, param->paramno, param, sizeof(struct pdo_bound_param_data));
}
/* tell the driver we just created a parameter */
if (stmt->methods->param_hook) {
if (!stmt->methods->param_hook(stmt, pparam, PDO_PARAM_EVT_ALLOC)) {
PDO_HANDLE_STMT_ERR();
/* undo storage allocation; the hash will free the parameter
* name if required */
if (pparam->name) {
zend_hash_del(hash, pparam->name);
} else {
zend_hash_index_del(hash, pparam->paramno);
}
/* param->parameter is freed by hash dtor */
ZVAL_UNDEF(¶m->parameter);
return 0;
}
}
return 1;
}
/* }}} */
/* {{{ Execute a prepared statement, optionally binding parameters */
PHP_METHOD(PDOStatement, execute)
{
zval *input_params = NULL;
int ret = 1;
ZEND_PARSE_PARAMETERS_START(0, 1)
Z_PARAM_OPTIONAL
Z_PARAM_ARRAY_OR_NULL(input_params)
ZEND_PARSE_PARAMETERS_END();
PHP_STMT_GET_OBJ;
PDO_STMT_CLEAR_ERR();
if (input_params) {
struct pdo_bound_param_data param;
zval *tmp;
zend_string *key = NULL;
zend_ulong num_index;
if (stmt->bound_params) {
zend_hash_destroy(stmt->bound_params);
FREE_HASHTABLE(stmt->bound_params);
stmt->bound_params = NULL;
}
ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(input_params), num_index, key, tmp) {
memset(¶m, 0, sizeof(param));
if (key) {
/* yes this is correct. we don't want to count the null byte. ask wez */
param.name = key;
param.paramno = -1;
} else {
/* we're okay to be zero based here */
/* num_index is unsignend */
param.paramno = num_index;
}
param.param_type = PDO_PARAM_STR;
ZVAL_COPY(¶m.parameter, tmp);
if (!really_register_bound_param(¶m, stmt, 1)) {
if (!Z_ISUNDEF(param.parameter)) {
zval_ptr_dtor(¶m.parameter);
}
RETURN_FALSE;
}
} ZEND_HASH_FOREACH_END();
}
if (PDO_PLACEHOLDER_NONE == stmt->supports_placeholders) {
/* handle the emulated parameter binding,
* stmt->active_query_string holds the query with binds expanded and
* quoted.
*/
/* string is leftover from previous calls so PDOStatement::debugDumpParams() can access */
if (stmt->active_query_string) {
zend_string_release(stmt->active_query_string);
stmt->active_query_string = NULL;
}
ret = pdo_parse_params(stmt, stmt->query_string, &stmt->active_query_string);
if (ret == 0) {
/* no changes were made */
stmt->active_query_string = zend_string_copy(stmt->query_string);
ret = 1;
} else if (ret == -1) {
/* something broke */
RETURN_FALSE;
}
} else if (!dispatch_param_event(stmt, PDO_PARAM_EVT_EXEC_PRE)) {
PDO_HANDLE_STMT_ERR();
RETURN_FALSE;
}
if (stmt->methods->executer(stmt)) {
if (!stmt->executed) {
/* this is the first execute */
if (stmt->dbh->alloc_own_columns && !stmt->columns) {
/* for "big boy" drivers, we need to allocate memory to fetch
* the results into, so lets do that now */
ret = pdo_stmt_describe_columns(stmt);
}
stmt->executed = 1;
}
if (ret && !dispatch_param_event(stmt, PDO_PARAM_EVT_EXEC_POST)) {
PDO_HANDLE_STMT_ERR();
RETURN_FALSE;
}
RETURN_BOOL(ret);
}
PDO_HANDLE_STMT_ERR();
RETURN_FALSE;
}
/* }}} */
static inline void fetch_value(pdo_stmt_t *stmt, zval *dest, int colno, enum pdo_param_type *type_override) /* {{{ */
{
if (colno < 0) {
zend_value_error("Column index must be greater than or equal to 0");
ZVAL_NULL(dest);
return;
}
if (colno >= stmt->column_count) {
zend_value_error("Invalid column index");
ZVAL_NULL(dest);
return;
}
ZVAL_NULL(dest);
stmt->methods->get_col(stmt, colno, dest, type_override);
if (Z_TYPE_P(dest) == IS_STRING && Z_STRLEN_P(dest) == 0
&& stmt->dbh->oracle_nulls == PDO_NULL_EMPTY_STRING) {
zval_ptr_dtor_str(dest);
ZVAL_NULL(dest);
}
/* If stringification is requested, override with PDO_PARAM_STR. */
enum pdo_param_type pdo_param_str = PDO_PARAM_STR;
if (stmt->dbh->stringify) {
type_override = &pdo_param_str;
}
if (type_override && Z_TYPE_P(dest) != IS_NULL) {
switch (*type_override) {
case PDO_PARAM_INT:
convert_to_long(dest);
break;
case PDO_PARAM_BOOL:
convert_to_boolean(dest);
break;
case PDO_PARAM_STR:
if (Z_TYPE_P(dest) == IS_FALSE) {
/* Return "0" rather than "", because this is what database drivers that
* don't have a dedicated boolean type would return. */
zval_ptr_dtor_nogc(dest);
ZVAL_INTERNED_STR(dest, ZSTR_CHAR('0'));
} else if (Z_TYPE_P(dest) == IS_RESOURCE) {
/* Convert LOB stream to string */
php_stream *stream;
php_stream_from_zval_no_verify(stream, dest);
zend_string *str = php_stream_copy_to_mem(stream, PHP_STREAM_COPY_ALL, 0);
zval_ptr_dtor_nogc(dest);
if (str == NULL) {
ZVAL_EMPTY_STRING(dest);
} else {
ZVAL_STR(dest, str);
}
} else {
convert_to_string(dest);
}
break;
case PDO_PARAM_NULL:
convert_to_null(dest);
break;
case PDO_PARAM_LOB:
if (Z_TYPE_P(dest) == IS_STRING) {
php_stream *stream =
php_stream_memory_open(TEMP_STREAM_READONLY, Z_STR_P(dest));
zval_ptr_dtor_str(dest);
php_stream_to_zval(stream, dest);
}
break;
default:
break;
}
}
if (Z_TYPE_P(dest) == IS_NULL && stmt->dbh->oracle_nulls == PDO_NULL_TO_STRING) {
ZVAL_EMPTY_STRING(dest);
}
}
/* }}} */
static bool do_fetch_common(pdo_stmt_t *stmt, enum pdo_fetch_orientation ori, zend_long offset) /* {{{ */
{
if (!stmt->executed) {
return 0;
}
if (!dispatch_param_event(stmt, PDO_PARAM_EVT_FETCH_PRE)) {
return 0;
}
if (!stmt->methods->fetcher(stmt, ori, offset)) {
return 0;
}
/* some drivers might need to describe the columns now */
if (!stmt->columns && !pdo_stmt_describe_columns(stmt)) {
return 0;
}
if (!dispatch_param_event(stmt, PDO_PARAM_EVT_FETCH_POST)) {
return 0;
}
if (stmt->bound_columns) {
/* update those bound column variables now */
struct pdo_bound_param_data *param;
ZEND_HASH_FOREACH_PTR(stmt->bound_columns, param) {
if (param->paramno >= 0) {
if (!Z_ISREF(param->parameter)) {
continue;
}
/* delete old value */
zval_ptr_dtor(Z_REFVAL(param->parameter));
/* set new value */
fetch_value(stmt, Z_REFVAL(param->parameter), param->paramno, ¶m->param_type);
/* TODO: some smart thing that avoids duplicating the value in the
* general loop below. For now, if you're binding output columns,
* it's better to use LAZY or BOUND fetches if you want to shave
* off those cycles */
}
} ZEND_HASH_FOREACH_END();
}
return 1;
}
/* }}} */
static bool pdo_do_key_pair_fetch(pdo_stmt_t *stmt, enum pdo_fetch_orientation ori, zend_long offset, HashTable *container)
{
if (!do_fetch_common(stmt, ori, offset)) {
return false;
}
if (stmt->column_count != 2) {
pdo_raise_impl_error(stmt->dbh, stmt, "HY000", "PDO::FETCH_KEY_PAIR fetch mode requires the result set to contain exactly 2 columns.");
return false;
}
zval key, val;
fetch_value(stmt, &key, 0, NULL);
fetch_value(stmt, &val, 1, NULL);
if (Z_TYPE(key) == IS_LONG) {
zend_hash_index_update(container, Z_LVAL(key), &val);
} else {
convert_to_string(&key);
zend_symtable_update(container, Z_STR(key), &val);
}
zval_ptr_dtor(&key);
return true;
}
/* Return value MUST be an initialized object */
static bool pdo_call_fetch_object_constructor(zend_function *constructor, HashTable *ctor_args, zval *return_value)
{
zval retval_constructor_call;
zend_fcall_info fci = {
.size = sizeof(zend_fcall_info),
.function_name = {},
.object = Z_OBJ_P(return_value),
.retval = &retval_constructor_call,
.param_count = 0,
.params = NULL,
.named_params = ctor_args,
};
zend_fcall_info_cache fcc = {
.function_handler = constructor,
.object = Z_OBJ_P(return_value),
.called_scope = Z_OBJCE_P(return_value),
.calling_scope = NULL,
.closure = NULL,
};
zend_call_function(&fci, &fcc);
bool failed = Z_ISUNDEF(retval_constructor_call);
zval_ptr_dtor(&retval_constructor_call);
return failed;
}
/* Performs a row fetch, the value is stored into return_value according to HOW.
* retun_value MUST be safely destroyable as it will be freed if an error occurs. */
static bool do_fetch(pdo_stmt_t *stmt, zval *return_value, enum pdo_fetch_type how, enum pdo_fetch_orientation ori, zend_long offset, zval *group_key) /* {{{ */
{
int flags;
zend_class_entry *ce = NULL;
HashTable *ctor_arguments = NULL;
int column_index_to_fetch = 0;
zval *fetch_function_params = NULL;
uint32_t fetch_function_param_num = 0;
if (how == PDO_FETCH_USE_DEFAULT) {
how = stmt->default_fetch_type;
}
flags = how & PDO_FETCH_FLAGS;
how = how & ~PDO_FETCH_FLAGS;
if (!do_fetch_common(stmt, ori, offset)) {
return false;
}
if (how == PDO_FETCH_BOUND) {
RETVAL_TRUE;
return true;
}
if (how == PDO_FETCH_LAZY) {
get_lazy_object(stmt, return_value);
return true;
}
/* When fetching a column we only do one value fetch, so handle it separately */
if (how == PDO_FETCH_COLUMN) {
int colno = stmt->fetch.column;
if ((flags & PDO_FETCH_GROUP) && stmt->fetch.column == -1) {
colno = 1;
}
if (colno < 0 ) {
zend_value_error("Column index must be greater than or equal to 0");
return false;
}
if (colno >= stmt->column_count) {
zend_value_error("Invalid column index");
return false;
}
if (flags == PDO_FETCH_GROUP && stmt->fetch.column == -1) {
fetch_value(stmt, return_value, 1, NULL);
} else if (flags == PDO_FETCH_GROUP && colno) {
fetch_value(stmt, return_value, 0, NULL);
} else {
fetch_value(stmt, return_value, colno, NULL);
}
if (group_key) {
if (flags == PDO_FETCH_GROUP && stmt->fetch.column > 0) {
fetch_value(stmt, group_key, colno, NULL);
} else {
fetch_value(stmt, group_key, 0, NULL);
}
convert_to_string(group_key);
}
return true;
}
stmt->in_fetch = true;
switch (how) {
case PDO_FETCH_USE_DEFAULT:
case PDO_FETCH_ASSOC:
case PDO_FETCH_BOTH:
case PDO_FETCH_NUM:
case PDO_FETCH_NAMED:
if (!group_key) {
array_init_size(return_value, stmt->column_count);
} else {
array_init(return_value);
}
break;
case PDO_FETCH_OBJ:
ce = zend_standard_class_def;
object_init(return_value);
break;
case PDO_FETCH_CLASS:
ce = stmt->fetch.cls.ce;
if (flags & PDO_FETCH_CLASSTYPE) {
zval ce_name_from_column;
fetch_value(stmt, &ce_name_from_column, column_index_to_fetch++, NULL);
/* This used to use try_convert_to_string() which would silently support integers, floats, null
* even if any such value could not generate a valid class name, as no class was found it would
* then proceed to use stdClass */
// TODO Raise PDO implementation error when the column name is not a string
if (Z_TYPE(ce_name_from_column) == IS_STRING) {
ce = zend_lookup_class(Z_STR(ce_name_from_column));
}
/* Use default CE if present */
if (ce == NULL) {
ce = zend_standard_class_def;
}
zval_ptr_dtor(&ce_name_from_column);
} else {
/* This can happen if the fetch flags are set via PDO::setAttribute()
* $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_CLASS);
* See ext/pdo/tests/bug_38253.phpt */
if (UNEXPECTED(ce == NULL)) {
pdo_raise_impl_error(stmt->dbh, stmt, "HY000", "No fetch class specified");
goto in_fetch_error;
}
ctor_arguments = stmt->fetch.cls.ctor_args;
}
ZEND_ASSERT(ce != NULL);
if (flags & PDO_FETCH_SERIALIZE) {
if (!ce->unserialize) {
/* As this option is deprecated we do not bother to mention the class name. */
pdo_raise_impl_error(stmt->dbh, stmt, "HY000", "cannot unserialize class");
goto in_fetch_error;
}
} else {
if (UNEXPECTED(object_init_ex(return_value, ce) != SUCCESS)) {
goto in_fetch_error;
}
if (ce->constructor && (flags & PDO_FETCH_PROPS_LATE)) {
bool failed = pdo_call_fetch_object_constructor(ce->constructor, ctor_arguments, return_value);
if (UNEXPECTED(failed)) {
zval_ptr_dtor(return_value);
goto in_fetch_error;
}
}
}
break;
case PDO_FETCH_INTO:
/* This can happen if the fetch flags are set via PDO::setAttribute()
* $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_INTO);
* See ext/pdo/tests/bug_38253.phpt */
if (stmt->fetch.into == NULL) {
pdo_raise_impl_error(stmt->dbh, stmt, "HY000", "No fetch-into object specified.");
goto in_fetch_error;
}
ZVAL_OBJ_COPY(return_value, stmt->fetch.into);
/* We want the behaviour of fetching into an object to be called from the global scope rather
* than the object scope */
ce = NULL;
break;
case PDO_FETCH_FUNC:
/* This can happen if the fetch flags are set via PDO::setAttribute()
* $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_FUNC);
* See ext/pdo/tests/bug_38253.phpt */
if (UNEXPECTED(!ZEND_FCC_INITIALIZED(stmt->fetch.func.fcc))) {
pdo_raise_impl_error(stmt->dbh, stmt, "HY000", "No fetch function specified");
goto in_fetch_error;
}
/* There will be at most stmt->column_count parameters.
* However, if we fetch a group key we will have over allocated. */
fetch_function_params = safe_emalloc(sizeof(zval), stmt->column_count, 0);
break;
EMPTY_SWITCH_DEFAULT_CASE();
}
if (group_key) {
fetch_value(stmt, group_key, column_index_to_fetch, NULL);
convert_to_string(group_key);
column_index_to_fetch++;
}
if (how == PDO_FETCH_CLASS && (flags & PDO_FETCH_SERIALIZE)) {
zval unserialization_string;
fetch_value(stmt, &unserialization_string, column_index_to_fetch, NULL);
const unsigned char *str = (const unsigned char*) "";
size_t str_len = 0;
if (Z_TYPE(unserialization_string) == IS_STRING) {
str = (unsigned char*) Z_STRVAL(unserialization_string);
str_len = Z_STRLEN(unserialization_string);
}
zend_result unserialize_res = ce->unserialize(return_value, ce, str, str_len, NULL);
zval_ptr_dtor(&unserialization_string);
if (unserialize_res == FAILURE) {
pdo_raise_impl_error(stmt->dbh, stmt, "HY000", "cannot unserialize class");
zval_ptr_dtor(return_value);
goto in_fetch_error;
}
column_index_to_fetch++;
}
for (; column_index_to_fetch < stmt->column_count; column_index_to_fetch++) {
zval val;
fetch_value(stmt, &val, column_index_to_fetch, NULL);
zend_string *column_name = stmt->columns[column_index_to_fetch].name;
switch (how) {
case PDO_FETCH_ASSOC:
zend_symtable_update(Z_ARRVAL_P(return_value), column_name, &val);
break;
case PDO_FETCH_USE_DEFAULT:
case PDO_FETCH_BOTH:
zend_symtable_update(Z_ARRVAL_P(return_value), column_name, &val);
if (zend_hash_index_add(Z_ARRVAL_P(return_value), column_index_to_fetch, &val) != NULL) {
Z_TRY_ADDREF(val);
}
break;
case PDO_FETCH_NAMED:
/* already have an item with this name? */
{
zval *curr_val = zend_hash_find(Z_ARRVAL_P(return_value), column_name);
if (curr_val != NULL) {
zval arr;
if (Z_TYPE_P(curr_val) != IS_ARRAY) {
/* a little bit of black magic here:
* we're creating a new array and swapping it for the
* zval that's already stored in the hash under the name
* we want. We then add that zval to the array.
* This is effectively the same thing as:
* if (!is_array($hash[$name])) {
* $hash[$name] = array($hash[$name]);
* }
* */
zval cur;
array_init(&arr);
ZVAL_COPY_VALUE(&cur, curr_val);
ZVAL_COPY_VALUE(curr_val, &arr);
zend_hash_next_index_insert_new(Z_ARRVAL(arr), &cur);
} else {
ZVAL_COPY_VALUE(&arr, curr_val);
}
zend_hash_next_index_insert_new(Z_ARRVAL(arr), &val);
} else {
zend_hash_update(Z_ARRVAL_P(return_value), column_name, &val);
}
}
break;
case PDO_FETCH_NUM:
zend_hash_next_index_insert_new(Z_ARRVAL_P(return_value), &val);
break;
case PDO_FETCH_CLASS:
case PDO_FETCH_OBJ:
case PDO_FETCH_INTO:
zend_update_property_ex(ce, Z_OBJ_P(return_value), column_name, &val);
zval_ptr_dtor(&val);
break;
case PDO_FETCH_FUNC:
ZVAL_COPY_VALUE(&fetch_function_params[fetch_function_param_num++], &val);
break;
EMPTY_SWITCH_DEFAULT_CASE();
}
}
/* Run constructor for objects if not already run and not unserialized */
if (how == PDO_FETCH_CLASS && ce->constructor && !(flags & (PDO_FETCH_PROPS_LATE | PDO_FETCH_SERIALIZE))) {
bool failed = pdo_call_fetch_object_constructor(ce->constructor, ctor_arguments, return_value);
if (UNEXPECTED(failed)) {
zval_ptr_dtor(return_value);
goto in_fetch_error;
}
} else if (how == PDO_FETCH_FUNC) {
zend_call_known_fcc(&stmt->fetch.func.fcc, return_value, fetch_function_param_num, fetch_function_params, NULL);
/* Free FCI parameters that were allocated in the previous loop */
for (uint32_t param_num = 0; param_num < fetch_function_param_num; param_num++) {
zval_ptr_dtor(&fetch_function_params[param_num]);
}
efree(fetch_function_params);
}
stmt->in_fetch = false;
return true;
in_fetch_error:
stmt->in_fetch = false;
return false;
}
/* }}} */
// TODO Error on the following cases:
// Using any fetch flag with PDO_FETCH_KEY_PAIR
// Combining PDO_FETCH_UNIQUE and PDO_FETCH_GROUP
// Using PDO_FETCH_UNIQUE or PDO_FETCH_GROUP outside of fetchAll()
// Combining PDO_FETCH_PROPS_LATE with a fetch mode different than PDO_FETCH_CLASS
// Improve error detection when combining PDO_FETCH_USE_DEFAULT with
// Reject PDO_FETCH_INTO mode with fetch_all as no support
// Reject $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, value); bypass
static bool pdo_stmt_verify_mode(pdo_stmt_t *stmt, zend_long mode, uint32_t mode_arg_num, bool fetch_all) /* {{{ */
{
int flags = mode & PDO_FETCH_FLAGS;
mode = mode & ~PDO_FETCH_FLAGS;
if (mode < 0 || mode >= PDO_FETCH__MAX) {
zend_argument_value_error(mode_arg_num, "must be a bitmask of PDO::FETCH_* constants");
return 0;
}
if (mode == PDO_FETCH_USE_DEFAULT) {
flags = stmt->default_fetch_type & PDO_FETCH_FLAGS;
mode = stmt->default_fetch_type & ~PDO_FETCH_FLAGS;
}
switch(mode) {
case PDO_FETCH_FUNC:
if (!fetch_all) {
zend_value_error("Can only use PDO::FETCH_FUNC in PDOStatement::fetchAll()");
return 0;
}
return 1;
case PDO_FETCH_LAZY:
if (fetch_all) {
zend_argument_value_error(mode_arg_num, "cannot be PDO::FETCH_LAZY in PDOStatement::fetchAll()");
return 0;
}
ZEND_FALLTHROUGH;
default:
if ((flags & PDO_FETCH_SERIALIZE) == PDO_FETCH_SERIALIZE) {
zend_argument_value_error(mode_arg_num, "must use PDO::FETCH_SERIALIZE with PDO::FETCH_CLASS");
return 0;
}
if ((flags & PDO_FETCH_CLASSTYPE) == PDO_FETCH_CLASSTYPE) {
zend_argument_value_error(mode_arg_num, "must use PDO::FETCH_CLASSTYPE with PDO::FETCH_CLASS");
return 0;
}
ZEND_FALLTHROUGH;