]> granicus.if.org Git - php/commitdiff
MFH: Allow overloading of PDO constructor.
authorIlia Alshanetsky <iliaa@php.net>
Tue, 20 Sep 2005 19:52:24 +0000 (19:52 +0000)
committerIlia Alshanetsky <iliaa@php.net>
Tue, 20 Sep 2005 19:52:24 +0000 (19:52 +0000)
ext/pdo/pdo_dbh.c
ext/pdo/php_pdo.h
ext/pdo_sqlite/sqlite_driver.c

index 2f2c80ddb6990be28574a78b32bdb700d8fa138d..775bbb9b1dd2a74cab9e0790fc445d7006d3c455 100755 (executable)
@@ -205,7 +205,7 @@ static char *dsn_from_uri(char *uri, char *buf, size_t buflen TSRMLS_DC)
 
 /* {{{ proto object PDO::__construct(string dsn, string username, string passwd [, array options])
    */
-static PHP_FUNCTION(dbh_constructor)
+static PHP_METHOD(PDO, dbh_constructor)
 {
        zval *object = getThis();
        pdo_dbh_t *dbh = NULL;
@@ -498,6 +498,7 @@ static PHP_METHOD(PDO, prepare)
        }
        
        PDO_DBH_CLEAR_ERR();
+       PDO_CONSTRUCT_CHECK;
 
        if (ZEND_NUM_ARGS() > 1 && SUCCESS == zend_hash_index_find(Z_ARRVAL_P(options), PDO_ATTR_STATEMENT_CLASS, (void**)&opt)) {
                if (zend_hash_index_find(Z_ARRVAL_PP(opt), 0, (void**)&item) == FAILURE
@@ -582,6 +583,8 @@ static PHP_METHOD(PDO, beginTransaction)
 {
        pdo_dbh_t *dbh = zend_object_store_get_object(getThis() TSRMLS_CC);
 
+       PDO_CONSTRUCT_CHECK;
+
        if (dbh->in_txn) {
                zend_throw_exception_ex(php_pdo_get_exception(), 0 TSRMLS_CC, "There is already an active transaction");
                RETURN_FALSE;
@@ -610,6 +613,8 @@ static PHP_METHOD(PDO, commit)
 {
        pdo_dbh_t *dbh = zend_object_store_get_object(getThis() TSRMLS_CC);
 
+       PDO_CONSTRUCT_CHECK;
+
        if (!dbh->in_txn) {
                zend_throw_exception_ex(php_pdo_get_exception(), 0 TSRMLS_CC, "There is no active transaction");
                RETURN_FALSE;
@@ -631,6 +636,8 @@ static PHP_METHOD(PDO, rollBack)
 {
        pdo_dbh_t *dbh = zend_object_store_get_object(getThis() TSRMLS_CC);
 
+       PDO_CONSTRUCT_CHECK;
+
        if (!dbh->in_txn) {
                zend_throw_exception_ex(php_pdo_get_exception(), 0 TSRMLS_CC, "There is no active transaction");
                RETURN_FALSE;
@@ -658,6 +665,8 @@ static PHP_METHOD(PDO, setAttribute)
                RETURN_FALSE;
        }
 
+       PDO_CONSTRUCT_CHECK;
+
        switch (attr) {
                case PDO_ATTR_ERRMODE:
                        convert_to_long(value);
@@ -736,6 +745,7 @@ static PHP_METHOD(PDO, getAttribute)
        }
 
        PDO_DBH_CLEAR_ERR();
+       PDO_CONSTRUCT_CHECK;
 
        /* handle generic PDO-level atributes */
        switch (attr) {
@@ -792,6 +802,7 @@ static PHP_METHOD(PDO, exec)
                RETURN_FALSE;
        }
        PDO_DBH_CLEAR_ERR();
+       PDO_CONSTRUCT_CHECK;
        ret = dbh->methods->doer(dbh, statement, statement_len TSRMLS_CC);
        if(ret == -1) {
                PDO_HANDLE_DBH_ERR();
@@ -816,6 +827,7 @@ static PHP_METHOD(PDO, lastInsertId)
        }
 
        PDO_DBH_CLEAR_ERR();
+       PDO_CONSTRUCT_CHECK;
        if (!dbh->methods->last_id) {
                pdo_raise_impl_error(dbh, NULL, "IM001", "driver does not support lastInsertId()" TSRMLS_CC);
                RETURN_FALSE;
@@ -840,6 +852,7 @@ static PHP_METHOD(PDO, errorCode)
        if (ZEND_NUM_ARGS()) {
                RETURN_FALSE;
        }
+       PDO_CONSTRUCT_CHECK;
 
        RETURN_STRING(dbh->error_code, 1);
 }
@@ -854,6 +867,7 @@ static PHP_METHOD(PDO, errorInfo)
        if (ZEND_NUM_ARGS()) {
                RETURN_FALSE;
        }
+       PDO_CONSTRUCT_CHECK;
 
        array_init(return_value);
        add_next_index_string(return_value, dbh->error_code, 1);
@@ -879,6 +893,7 @@ static PHP_METHOD(PDO, query)
        }
        
        PDO_DBH_CLEAR_ERR();
+       PDO_CONSTRUCT_CHECK;
 
        if (!pdo_stmt_instantiate(dbh, return_value, pdo_dbstmt_ce, NULL TSRMLS_CC)) {
                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)
        }
        
        PDO_DBH_CLEAR_ERR();
+       PDO_CONSTRUCT_CHECK;
        if (!dbh->methods->quoter) {
                pdo_raise_impl_error(dbh, NULL, "IM001", "driver does not support quoting" TSRMLS_CC);
                RETURN_FALSE;
@@ -995,7 +1011,7 @@ static PHP_METHOD(PDO, getAvailableDrivers)
 
 
 function_entry pdo_dbh_functions[] = {
-       PHP_ME_MAPPING(__construct, dbh_constructor,    NULL)
+       ZEND_MALIAS(PDO, __construct, dbh_constructor,  NULL,                   ZEND_ACC_PUBLIC)
        PHP_ME(PDO, prepare,            NULL,                                   ZEND_ACC_PUBLIC)
        PHP_ME(PDO, beginTransaction,NULL,                                      ZEND_ACC_PUBLIC)
        PHP_ME(PDO, commit,                     NULL,                                   ZEND_ACC_PUBLIC)
@@ -1132,7 +1148,6 @@ void pdo_dbh_init(TSRMLS_D)
        INIT_CLASS_ENTRY(ce, "PDO", pdo_dbh_functions);
        pdo_dbh_ce = zend_register_internal_class(&ce TSRMLS_CC);
        pdo_dbh_ce->create_object = pdo_dbh_new;
-       pdo_dbh_ce->constructor->common.fn_flags |= ZEND_ACC_FINAL;
 
        memcpy(&pdo_dbh_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
        pdo_dbh_object_handlers.get_method = dbh_method_get;
index ba63cbfb97b948a27ac010557fac4809a8007076..c4b9c94c68177a730c1c02d115f7769ead35e8df 100755 (executable)
@@ -70,6 +70,13 @@ ZEND_END_MODULE_GLOBALS(pdo)
 #define REGISTER_PDO_CLASS_CONST_STRING(const_name, value) \
        zend_declare_class_constant_stringl(pdo_dbh_ce, const_name, sizeof(const_name)-1, value, sizeof(value)-1 TSRMLS_CC);
 
+#define PDO_CONSTRUCT_CHECK    \
+       if (!dbh->driver) {     \
+               pdo_raise_impl_error(dbh, NULL, "00000", "PDO constructor was not called" TSRMLS_CC);   \
+               return; \
+       }       \
+
+
 #endif /* PHP_PDO_H */
 
 
index e3734d4a7c28468756516ca7431b76140cadc7eb..ff935ab0316b74be5d58e3a77c3f9a5326aa22b5 100644 (file)
@@ -468,6 +468,7 @@ static PHP_METHOD(SQLite, sqliteCreateFunction)
        }
        
        dbh = zend_object_store_get_object(getThis() TSRMLS_CC);
+       PDO_CONSTRUCT_CHECK;
 
        if (!zend_is_callable(callback, 0, &cbname)) {
                php_error_docref(NULL TSRMLS_CC, E_WARNING, "function '%s' is not callable", cbname);
@@ -539,6 +540,7 @@ static PHP_METHOD(SQLite, sqliteCreateAggregate)
        }
        
        dbh = zend_object_store_get_object(getThis() TSRMLS_CC);
+       PDO_CONSTRUCT_CHECK;
 
        if (!zend_is_callable(step_callback, 0, &cbname)) {
                php_error_docref(NULL TSRMLS_CC, E_WARNING, "function '%s' is not callable", cbname);