34
34
#include "zend_interfaces.h"
35
35
#include "pdo_dbh_arginfo.h"
36
36
37
- static zend_result pdo_dbh_attribute_set (pdo_dbh_t * dbh , zend_long attr , zval * value );
37
+ static bool pdo_dbh_attribute_set (pdo_dbh_t * dbh , zend_long attr , zval * value );
38
38
39
39
void pdo_throw_exception (unsigned int driver_errcode , char * driver_errmsg , pdo_error_type * pdo_error )
40
40
{
@@ -375,6 +375,8 @@ PHP_METHOD(PDO, __construct)
375
375
php_error_docref (NULL , E_ERROR , "Out of memory" );
376
376
}
377
377
378
+ /* pdo_dbh_attribute_set() can emit a Warning if the ERR_MODE is set to warning
379
+ * As we are in a constructor we override the behaviour by replacing the error handler */
378
380
zend_replace_error_handling (EH_THROW , pdo_exception_ce , & zeh );
379
381
380
382
if (!call_factory ) {
@@ -407,7 +409,8 @@ PHP_METHOD(PDO, __construct)
407
409
continue ;
408
410
}
409
411
ZVAL_DEREF (attr_value );
410
- /* TODO: Check that this doesn't fail? */
412
+
413
+ /* TODO: Should the constructor fail when the attribute cannot be set? */
411
414
pdo_dbh_attribute_set (dbh , long_key , attr_value );
412
415
} ZEND_HASH_FOREACH_END ();
413
416
}
@@ -675,17 +678,18 @@ PHP_METHOD(PDO, inTransaction)
675
678
}
676
679
/* }}} */
677
680
678
- static zend_result pdo_dbh_attribute_set (pdo_dbh_t * dbh , zend_long attr , zval * value ) /* {{{ */
679
- {
680
- zend_long lval ;
681
-
682
681
/* TODO: Make distinction between numeric and non-numeric strings */
683
682
#define PDO_LONG_PARAM_CHECK \
684
683
if (Z_TYPE_P(value) != IS_LONG && Z_TYPE_P(value) != IS_STRING && Z_TYPE_P(value) != IS_FALSE && Z_TYPE_P(value) != IS_TRUE) { \
685
684
zend_type_error("Attribute value must be of type int for selected attribute, %s given", zend_zval_type_name(value)); \
686
- return FAILURE ; \
685
+ return false ; \
687
686
} \
688
687
688
+ /* Return false on failure, true otherwise */
689
+ static bool pdo_dbh_attribute_set (pdo_dbh_t * dbh , zend_long attr , zval * value ) /* {{{ */
690
+ {
691
+ zend_long lval ;
692
+
689
693
switch (attr ) {
690
694
case PDO_ATTR_ERRMODE :
691
695
PDO_LONG_PARAM_CHECK ;
@@ -695,12 +699,12 @@ static zend_result pdo_dbh_attribute_set(pdo_dbh_t *dbh, zend_long attr, zval *v
695
699
case PDO_ERRMODE_WARNING :
696
700
case PDO_ERRMODE_EXCEPTION :
697
701
dbh -> error_mode = lval ;
698
- return SUCCESS ;
702
+ return true ;
699
703
default :
700
704
zend_value_error ("Error mode must be one of the PDO::ERRMODE_* constants" );
701
- return FAILURE ;
705
+ return false ;
702
706
}
703
- return FAILURE ;
707
+ return false ;
704
708
705
709
case PDO_ATTR_CASE :
706
710
PDO_LONG_PARAM_CHECK ;
@@ -710,25 +714,25 @@ static zend_result pdo_dbh_attribute_set(pdo_dbh_t *dbh, zend_long attr, zval *v
710
714
case PDO_CASE_UPPER :
711
715
case PDO_CASE_LOWER :
712
716
dbh -> desired_case = lval ;
713
- return SUCCESS ;
717
+ return true ;
714
718
default :
715
719
zend_value_error ("Case folding mode must be one of the PDO::CASE_* constants" );
716
- return FAILURE ;
720
+ return false ;
717
721
}
718
- return FAILURE ;
722
+ return false ;
719
723
720
724
case PDO_ATTR_ORACLE_NULLS :
721
725
PDO_LONG_PARAM_CHECK ;
722
726
dbh -> oracle_nulls = zval_get_long (value );
723
- return SUCCESS ;
727
+ return true ;
724
728
725
729
case PDO_ATTR_DEFAULT_FETCH_MODE :
726
730
if (Z_TYPE_P (value ) == IS_ARRAY ) {
727
731
zval * tmp ;
728
732
if ((tmp = zend_hash_index_find (Z_ARRVAL_P (value ), 0 )) != NULL && Z_TYPE_P (tmp ) == IS_LONG ) {
729
733
if (Z_LVAL_P (tmp ) == PDO_FETCH_INTO || Z_LVAL_P (tmp ) == PDO_FETCH_CLASS ) {
730
734
zend_value_error ("PDO::FETCH_INTO and PDO::FETCH_CLASS cannot be set as the default fetch mode" );
731
- return FAILURE ;
735
+ return false ;
732
736
}
733
737
}
734
738
} else {
@@ -737,15 +741,15 @@ static zend_result pdo_dbh_attribute_set(pdo_dbh_t *dbh, zend_long attr, zval *v
737
741
lval = zval_get_long (value );
738
742
if (lval == PDO_FETCH_USE_DEFAULT ) {
739
743
zend_value_error ("Fetch mode must be a bitmask of PDO::FETCH_* constants" );
740
- return FAILURE ;
744
+ return false ;
741
745
}
742
746
dbh -> default_fetch_type = lval ;
743
- return SUCCESS ;
747
+ return true ;
744
748
745
749
case PDO_ATTR_STRINGIFY_FETCHES :
746
750
PDO_LONG_PARAM_CHECK ;
747
751
dbh -> stringify = zval_get_long (value ) ? 1 : 0 ;
748
- return SUCCESS ;
752
+ return true ;
749
753
750
754
case PDO_ATTR_STATEMENT_CLASS : {
751
755
/* array(string classname, array(mixed ctor_args)) */
@@ -758,29 +762,29 @@ static zend_result pdo_dbh_attribute_set(pdo_dbh_t *dbh, zend_long attr, zval *v
758
762
"PDO::ATTR_STATEMENT_CLASS cannot be used with persistent PDO instances"
759
763
);
760
764
PDO_HANDLE_DBH_ERR ();
761
- return FAILURE ;
765
+ return false ;
762
766
}
763
767
if (Z_TYPE_P (value ) != IS_ARRAY ) {
764
768
zend_type_error ("PDO::ATTR_STATEMENT_CLASS value must be of type array, %s given" ,
765
769
zend_zval_type_name (value ));
766
- return FAILURE ;
770
+ return false ;
767
771
}
768
772
if ((item = zend_hash_index_find (Z_ARRVAL_P (value ), 0 )) == NULL ) {
769
773
zend_value_error ("PDO::ATTR_STATEMENT_CLASS value must be an array with the format "
770
774
"array(classname, constructor_args)" );
771
- return FAILURE ;
775
+ return false ;
772
776
}
773
777
if (Z_TYPE_P (item ) != IS_STRING || (pce = zend_lookup_class (Z_STR_P (item ))) == NULL ) {
774
778
zend_type_error ("PDO::ATTR_STATEMENT_CLASS class must be a valid class" );
775
- return FAILURE ;
779
+ return false ;
776
780
}
777
781
if (!instanceof_function (pce , pdo_dbstmt_ce )) {
778
782
zend_type_error ("PDO::ATTR_STATEMENT_CLASS class must be derived from PDOStatement" );
779
- return FAILURE ;
783
+ return false ;
780
784
}
781
785
if (pce -> constructor && !(pce -> constructor -> common .fn_flags & (ZEND_ACC_PRIVATE |ZEND_ACC_PROTECTED ))) {
782
786
zend_type_error ("User-supplied statement class cannot have a public constructor" );
783
- return FAILURE ;
787
+ return false ;
784
788
}
785
789
dbh -> def_stmt_ce = pce ;
786
790
if (!Z_ISUNDEF (dbh -> def_stmt_ctor_args )) {
@@ -791,11 +795,11 @@ static zend_result pdo_dbh_attribute_set(pdo_dbh_t *dbh, zend_long attr, zval *v
791
795
if (Z_TYPE_P (item ) != IS_ARRAY ) {
792
796
zend_type_error ("PDO::ATTR_STATEMENT_CLASS constructor_args must be of type ?array, %s given" ,
793
797
zend_zval_type_name (value ));
794
- return FAILURE ;
798
+ return false ;
795
799
}
796
800
ZVAL_COPY (& dbh -> def_stmt_ctor_args , item );
797
801
}
798
- return SUCCESS ;
802
+ return true ;
799
803
}
800
804
801
805
default :
@@ -808,7 +812,7 @@ static zend_result pdo_dbh_attribute_set(pdo_dbh_t *dbh, zend_long attr, zval *v
808
812
809
813
PDO_DBH_CLEAR_ERR ();
810
814
if (dbh -> methods -> set_attribute (dbh , attr , value )) {
811
- return SUCCESS ;
815
+ return true ;
812
816
}
813
817
814
818
fail :
@@ -817,7 +821,7 @@ static zend_result pdo_dbh_attribute_set(pdo_dbh_t *dbh, zend_long attr, zval *v
817
821
} else {
818
822
PDO_HANDLE_DBH_ERR ();
819
823
}
820
- return FAILURE ;
824
+ return false ;
821
825
}
822
826
/* }}} */
823
827
@@ -836,10 +840,7 @@ PHP_METHOD(PDO, setAttribute)
836
840
PDO_DBH_CLEAR_ERR ();
837
841
PDO_CONSTRUCT_CHECK ;
838
842
839
- if (pdo_dbh_attribute_set (dbh , attr , value ) != FAILURE ) {
840
- RETURN_TRUE ;
841
- }
842
- RETURN_FALSE ;
843
+ RETURN_BOOL (pdo_dbh_attribute_set (dbh , attr , value ));
843
844
}
844
845
/* }}} */
845
846
0 commit comments