Skip to content

Commit 4dff36f

Browse files
author
Ilia Alshanetsky
committed
MFH: Allow overloading of PDO constructor.
1 parent bcb7010 commit 4dff36f

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

ext/pdo/pdo_dbh.c

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ static char *dsn_from_uri(char *uri, char *buf, size_t buflen TSRMLS_DC)
205205

206206
/* {{{ proto object PDO::__construct(string dsn, string username, string passwd [, array options])
207207
*/
208-
static PHP_FUNCTION(dbh_constructor)
208+
static PHP_METHOD(PDO, dbh_constructor)
209209
{
210210
zval *object = getThis();
211211
pdo_dbh_t *dbh = NULL;
@@ -498,6 +498,7 @@ static PHP_METHOD(PDO, prepare)
498498
}
499499

500500
PDO_DBH_CLEAR_ERR();
501+
PDO_CONSTRUCT_CHECK;
501502

502503
if (ZEND_NUM_ARGS() > 1 && SUCCESS == zend_hash_index_find(Z_ARRVAL_P(options), PDO_ATTR_STATEMENT_CLASS, (void**)&opt)) {
503504
if (zend_hash_index_find(Z_ARRVAL_PP(opt), 0, (void**)&item) == FAILURE
@@ -582,6 +583,8 @@ static PHP_METHOD(PDO, beginTransaction)
582583
{
583584
pdo_dbh_t *dbh = zend_object_store_get_object(getThis() TSRMLS_CC);
584585

586+
PDO_CONSTRUCT_CHECK;
587+
585588
if (dbh->in_txn) {
586589
zend_throw_exception_ex(php_pdo_get_exception(), 0 TSRMLS_CC, "There is already an active transaction");
587590
RETURN_FALSE;
@@ -610,6 +613,8 @@ static PHP_METHOD(PDO, commit)
610613
{
611614
pdo_dbh_t *dbh = zend_object_store_get_object(getThis() TSRMLS_CC);
612615

616+
PDO_CONSTRUCT_CHECK;
617+
613618
if (!dbh->in_txn) {
614619
zend_throw_exception_ex(php_pdo_get_exception(), 0 TSRMLS_CC, "There is no active transaction");
615620
RETURN_FALSE;
@@ -631,6 +636,8 @@ static PHP_METHOD(PDO, rollBack)
631636
{
632637
pdo_dbh_t *dbh = zend_object_store_get_object(getThis() TSRMLS_CC);
633638

639+
PDO_CONSTRUCT_CHECK;
640+
634641
if (!dbh->in_txn) {
635642
zend_throw_exception_ex(php_pdo_get_exception(), 0 TSRMLS_CC, "There is no active transaction");
636643
RETURN_FALSE;
@@ -658,6 +665,8 @@ static PHP_METHOD(PDO, setAttribute)
658665
RETURN_FALSE;
659666
}
660667

668+
PDO_CONSTRUCT_CHECK;
669+
661670
switch (attr) {
662671
case PDO_ATTR_ERRMODE:
663672
convert_to_long(value);
@@ -736,6 +745,7 @@ static PHP_METHOD(PDO, getAttribute)
736745
}
737746

738747
PDO_DBH_CLEAR_ERR();
748+
PDO_CONSTRUCT_CHECK;
739749

740750
/* handle generic PDO-level atributes */
741751
switch (attr) {
@@ -792,6 +802,7 @@ static PHP_METHOD(PDO, exec)
792802
RETURN_FALSE;
793803
}
794804
PDO_DBH_CLEAR_ERR();
805+
PDO_CONSTRUCT_CHECK;
795806
ret = dbh->methods->doer(dbh, statement, statement_len TSRMLS_CC);
796807
if(ret == -1) {
797808
PDO_HANDLE_DBH_ERR();
@@ -816,6 +827,7 @@ static PHP_METHOD(PDO, lastInsertId)
816827
}
817828

818829
PDO_DBH_CLEAR_ERR();
830+
PDO_CONSTRUCT_CHECK;
819831
if (!dbh->methods->last_id) {
820832
pdo_raise_impl_error(dbh, NULL, "IM001", "driver does not support lastInsertId()" TSRMLS_CC);
821833
RETURN_FALSE;
@@ -840,6 +852,7 @@ static PHP_METHOD(PDO, errorCode)
840852
if (ZEND_NUM_ARGS()) {
841853
RETURN_FALSE;
842854
}
855+
PDO_CONSTRUCT_CHECK;
843856

844857
RETURN_STRING(dbh->error_code, 1);
845858
}
@@ -854,6 +867,7 @@ static PHP_METHOD(PDO, errorInfo)
854867
if (ZEND_NUM_ARGS()) {
855868
RETURN_FALSE;
856869
}
870+
PDO_CONSTRUCT_CHECK;
857871

858872
array_init(return_value);
859873
add_next_index_string(return_value, dbh->error_code, 1);
@@ -879,6 +893,7 @@ static PHP_METHOD(PDO, query)
879893
}
880894

881895
PDO_DBH_CLEAR_ERR();
896+
PDO_CONSTRUCT_CHECK;
882897

883898
if (!pdo_stmt_instantiate(dbh, return_value, pdo_dbstmt_ce, NULL TSRMLS_CC)) {
884899
pdo_raise_impl_error(dbh, NULL, "HY000", "failed to instantiate user supplied statement class" TSRMLS_CC);
@@ -948,6 +963,7 @@ static PHP_METHOD(PDO, quote)
948963
}
949964

950965
PDO_DBH_CLEAR_ERR();
966+
PDO_CONSTRUCT_CHECK;
951967
if (!dbh->methods->quoter) {
952968
pdo_raise_impl_error(dbh, NULL, "IM001", "driver does not support quoting" TSRMLS_CC);
953969
RETURN_FALSE;
@@ -995,7 +1011,7 @@ static PHP_METHOD(PDO, getAvailableDrivers)
9951011

9961012

9971013
function_entry pdo_dbh_functions[] = {
998-
PHP_ME_MAPPING(__construct, dbh_constructor, NULL)
1014+
ZEND_MALIAS(PDO, __construct, dbh_constructor, NULL, ZEND_ACC_PUBLIC)
9991015
PHP_ME(PDO, prepare, NULL, ZEND_ACC_PUBLIC)
10001016
PHP_ME(PDO, beginTransaction,NULL, ZEND_ACC_PUBLIC)
10011017
PHP_ME(PDO, commit, NULL, ZEND_ACC_PUBLIC)
@@ -1132,7 +1148,6 @@ void pdo_dbh_init(TSRMLS_D)
11321148
INIT_CLASS_ENTRY(ce, "PDO", pdo_dbh_functions);
11331149
pdo_dbh_ce = zend_register_internal_class(&ce TSRMLS_CC);
11341150
pdo_dbh_ce->create_object = pdo_dbh_new;
1135-
pdo_dbh_ce->constructor->common.fn_flags |= ZEND_ACC_FINAL;
11361151

11371152
memcpy(&pdo_dbh_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
11381153
pdo_dbh_object_handlers.get_method = dbh_method_get;

ext/pdo/php_pdo.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,13 @@ ZEND_END_MODULE_GLOBALS(pdo)
7070
#define REGISTER_PDO_CLASS_CONST_STRING(const_name, value) \
7171
zend_declare_class_constant_stringl(pdo_dbh_ce, const_name, sizeof(const_name)-1, value, sizeof(value)-1 TSRMLS_CC);
7272

73+
#define PDO_CONSTRUCT_CHECK \
74+
if (!dbh->driver) { \
75+
pdo_raise_impl_error(dbh, NULL, "00000", "PDO constructor was not called" TSRMLS_CC); \
76+
return; \
77+
} \
78+
79+
7380
#endif /* PHP_PDO_H */
7481

7582

ext/pdo_sqlite/sqlite_driver.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,7 @@ static PHP_METHOD(SQLite, sqliteCreateFunction)
468468
}
469469

470470
dbh = zend_object_store_get_object(getThis() TSRMLS_CC);
471+
PDO_CONSTRUCT_CHECK;
471472

472473
if (!zend_is_callable(callback, 0, &cbname)) {
473474
php_error_docref(NULL TSRMLS_CC, E_WARNING, "function '%s' is not callable", cbname);
@@ -539,6 +540,7 @@ static PHP_METHOD(SQLite, sqliteCreateAggregate)
539540
}
540541

541542
dbh = zend_object_store_get_object(getThis() TSRMLS_CC);
543+
PDO_CONSTRUCT_CHECK;
542544

543545
if (!zend_is_callable(step_callback, 0, &cbname)) {
544546
php_error_docref(NULL TSRMLS_CC, E_WARNING, "function '%s' is not callable", cbname);

0 commit comments

Comments
 (0)