/***************************** {{{ LICENSE ********************************\ * dbobj, the relational object persistence module for PHP * * Copyright (C) 2006 Adam Banko * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the Free Software * * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * \***************************** LICENSE }}} ********************************/ #include "localClassConfig.h" #include "dbDriver.h" #define STATIC_CONNPARAMS_PROPERTY_NAME "connectionParameters" extern zend_class_entry *dbobj_class_entry_ptr; static void localClassConfig_dtor(void *ptr) { //DBConnection and classConfig are destoryed from another place pe_e_ptr_freeer(ptr); } //{{{ php_dbobj_MINIT_localClassConfig DBOBJ_API void php_dbobj_MINIT_localClassConfig() { { zval *q; q = pemalloc(sizeof(*q),1); INIT_ZVAL(*q); ZVAL_NULL(q); zend_hash_add(&dbobj_class_entry_ptr->default_static_members, STATIC_CONNPARAMS_PROPERTY_NAME , sizeof(STATIC_CONNPARAMS_PROPERTY_NAME), (void**)&q, sizeof(zval*), NULL); } }//}}} //{{{ php_dbobj_RINIT_localClassConfig DBOBJ_API void php_dbobj_RINIT_localClassConfig(TSRMLS_D) { if(zend_hash_init_ex(&DBOBJ_G(localClassConfigs), 0, NULL, localClassConfig_dtor, 0, 0) == FAILURE) { DBOBJ_ERROR("error while initializing localClassConfigs"); } }//}}} //{{{ php_dbobj_RSHUTDOWN_localClassConfig DBOBJ_API void php_dbobj_RSHUTDOWN_localClassConfig(TSRMLS_D) { zend_hash_destroy(&DBOBJ_G(localClassConfigs)); { zval* connParams; connParams = zend_read_static_property(dbobj_class_entry_ptr, STATIC_CONNPARAMS_PROPERTY_NAME , sizeof(STATIC_CONNPARAMS_PROPERTY_NAME)-1, /*silent*/ 0 TSRMLS_CC); assert(connParams); zval_dtor(connParams); } }//}}} //{{{ php_dbobj_MSHUTDOWN_localClassConfig DBOBJ_API void php_dbobj_MSHUTDOWN_localClassConfig() { }//}}} //{{{getConnection_get_string_from_hash static inline uint getConnection_get_string_from_hash(char** ret_str, HashTable* ht, STRINGLD(key), char* connectionAlias) { zval** retP; if(zend_hash_find(ht, STRINGLU(key), (void**)&retP) == FAILURE) { DBOBJ_ERROR("dbobj::" STATIC_CONNPARAMS_PROPERTY_NAME "['%s']['%s'] has to defined to use '%s' as a connectionAlias!",connectionAlias, key, connectionAlias); } if(Z_TYPE_PP(retP) != IS_STRING) { DBOBJ_ERROR("dbobj::" STATIC_CONNPARAMS_PROPERTY_NAME "['%s']['%s'] has to be a string!",connectionAlias, key); } *ret_str = Z_STRVAL_PP(retP); return Z_STRLEN_PP(retP); }//}}} //{{{getConnection static DBConnection localClassConfig_getConnection(classConfig cConf TSRMLS_DC) { const static char* staticPropName = STATIC_CONNPARAMS_PROPERTY_NAME; const static uint staticPropNameL = sizeof(STATIC_CONNPARAMS_PROPERTY_NAME)-1; char *connectionAlias = cConf->connectionAlias; uint connectionAliasL = cConf->connectionAliasL; zval **retP, *connParams, *connParam; char *driver, *dsn, *username, *password; uint driverL, dsnL, usernameL, passwordL; //find dbobj::connectionParameters connParams = zend_read_static_property(dbobj_class_entry_ptr, (char*)STRINGLU(staticPropName), 1 /*silent*/ TSRMLS_CC); if(!connParams) { DBOBJ_ERROR("dbobj::" STATIC_CONNPARAMS_PROPERTY_NAME " has to be defined!"); } if(Z_TYPE_P(connParams) != IS_ARRAY) { DBOBJ_ERROR("dbobj::" STATIC_CONNPARAMS_PROPERTY_NAME " has to be an array!"); } //find connParams[$connAlias] if(zend_hash_find(Z_ARRVAL_P(connParams), STRINGLU(connectionAlias)+1, (void**)&retP) == FAILURE) { DBOBJ_ERROR("dbobj::" STATIC_CONNPARAMS_PROPERTY_NAME "['%s'] has to defined to use '%s' as a connectionAlias!",connectionAlias, connectionAlias); } connParam = *retP; if(Z_TYPE_P(connParam) != IS_ARRAY) { DBOBJ_ERROR("dbobj::" STATIC_CONNPARAMS_PROPERTY_NAME "['%s'] has to be an array!",connectionAlias); } driverL = getConnection_get_string_from_hash(&driver, Z_ARRVAL_P(connParam), "driver", sizeof("driver"), connectionAlias); dsnL = getConnection_get_string_from_hash(&dsn, Z_ARRVAL_P(connParam), "dsn", sizeof("dsn"), connectionAlias); usernameL = getConnection_get_string_from_hash(&username, Z_ARRVAL_P(connParam), "username", sizeof("username"), connectionAlias); passwordL = getConnection_get_string_from_hash(&password, Z_ARRVAL_P(connParam), "password", sizeof("password"), connectionAlias); //create the connection return dbDriver_getConnection(STRINGLU(driver), STRINGLU(dsn), STRINGLU(username), STRINGLU(password), Z_ARRVAL_P(connParams) TSRMLS_CC); }//}}} //{{{register void localClassConfig_register(LocalClassConfig lConf TSRMLS_DC) { zend_hash_add(&DBOBJ_G(localClassConfigs), lConf->cConf->className, lConf->cConf->classNameL, &lConf, sizeof(lConf), NULL); }//}}} /* {{{ create * * creates a localClassConfig for a given classname, and registers it * @ret the newly created localClassConfig */ static LocalClassConfig localClassConfig_create(STRINGLD(className), bool supressError TSRMLS_DC) { LocalClassConfig ret = emalloc(sizeof(*ret)); ret->cConf = dbobj_helper_get_class_config(STRINGLU(className), supressError TSRMLS_CC); ret->conn = localClassConfig_getConnection(ret->cConf TSRMLS_CC); localClassConfig_register(ret TSRMLS_CC); return ret; } //}}} //{{{ get DBOBJ_API LocalClassConfig localClassConfig_get(const STRINGLD(origClassName), bool supressError TSRMLS_DC) { LocalClassConfig *retP; char* className = zend_str_tolower_dup(STRINGLU(origClassName)); uint classNameL = origClassNameL; if(zend_hash_find(&DBOBJ_G(localClassConfigs), STRINGLU(className), (void**)&retP) == SUCCESS) { return *retP; } return localClassConfig_create(className, classNameL, supressError TSRMLS_CC); }//}}} /* * vim600: noet sw=4 ts=4 fdm=marker * vim<600: noet sw=4 ts=4 */