/* * Initialize database connection */ db1_con_t* dbt_init(const str* _sqlurl) { db1_con_t* _res; str _s; char dbt_path[DBT_PATH_LEN]; if (!_sqlurl || !_sqlurl->s) { LM_ERR("invalid parameter value\n"); return NULL; } _s.s = _sqlurl->s; _s.len = _sqlurl->len; if(_s.len <= DBT_ID_LEN || strncmp(_s.s, DBT_ID, DBT_ID_LEN)!=0) { LM_ERR("invalid database URL - should be:" " <%s[/]path/to/directory> Current: %s\n", DBT_ID, _s.s); return NULL; } /* * it would be possible to use the _sqlurl here, but the core API is * defined with a const str*, so this code would be not valid. */ _s.s += DBT_ID_LEN; _s.len -= DBT_ID_LEN; if(_s.s[0]!='/') { if(sizeof(CFG_DIR)+_s.len+2 > DBT_PATH_LEN) { LM_ERR("path to database is too long\n"); return NULL; } strcpy(dbt_path, CFG_DIR); dbt_path[sizeof(CFG_DIR)] = '/'; strncpy(&dbt_path[sizeof(CFG_DIR)+1], _s.s, _s.len); _s.len += sizeof(CFG_DIR); _s.s = dbt_path; } _res = pkg_malloc(sizeof(db1_con_t)+sizeof(dbt_con_t)); if (!_res) { LM_ERR("no pkg memory left\n"); return NULL; } memset(_res, 0, sizeof(db1_con_t) + sizeof(dbt_con_t)); _res->tail = (unsigned long)((char*)_res+sizeof(db1_con_t)); LM_INFO("using database at: %.*s\n", _s.len, _s.s); DBT_CON_CONNECTION(_res) = dbt_cache_get_db(&_s); if (!DBT_CON_CONNECTION(_res)) { LM_ERR("cannot get the link to database\n"); return NULL; } return _res; }
/* * Initialize database connection */ db_con_t* dbt_init(const char* _sqlurl) { db_con_t* _res; str _s; char dbt_path[DBT_PATH_LEN]; if (!_sqlurl) { #ifdef DBT_EXTRA_DEBUG LOG(L_ERR, "DBT:dbt_init: Invalid parameter value\n"); #endif return NULL; } _s.s = (char*)_sqlurl; _s.len = strlen(_sqlurl); if(_s.len <= DBT_ID_LEN || strncmp(_s.s, DBT_ID, DBT_ID_LEN)!=0) { LOG(L_ERR, "DBT:dbt_init: invalid database URL - should be:" " <%s[/]path/to/directory>\n", DBT_ID); return NULL; } _s.s += DBT_ID_LEN; _s.len -= DBT_ID_LEN; if(_s.s[0]!='/') { if(sizeof(CFG_DIR)+_s.len+2 > DBT_PATH_LEN) { LOG(L_ERR, "DBT:dbt_init: path to database is too long\n"); return NULL; } strcpy(dbt_path, CFG_DIR); dbt_path[sizeof(CFG_DIR)] = '/'; strncpy(&dbt_path[sizeof(CFG_DIR)+1], _s.s, _s.len); _s.len += sizeof(CFG_DIR); _s.s = dbt_path; } _res = pkg_malloc(sizeof(db_con_t)+sizeof(dbt_con_t)); if (!_res) { LOG(L_ERR, "DBT:dbt_init: No memory left\n"); return NULL; } memset(_res, 0, sizeof(db_con_t) + sizeof(dbt_con_t)); _res->tail = (unsigned long)((char*)_res+sizeof(db_con_t)); DBT_CON_CONNECTION(_res) = dbt_cache_get_db(&_s); if (!DBT_CON_CONNECTION(_res)) { LOG(L_ERR, "DBT:dbt_init: cannot get the link to database\n"); return NULL; } return _res; }