* Initialize database module * No function should be called before this */
| 292 | * No function should be called before this |
| 293 | */ |
| 294 | db_con_t* db_do_init(const str* url, void* (*new_connection)(const struct db_id *)) |
| 295 | { |
| 296 | struct db_id *id = NULL; |
| 297 | struct pool_con *con = NULL; |
| 298 | db_con_t *res = NULL; |
| 299 | int con_size = 0; |
| 300 | |
| 301 | if (!url || !url->s || !new_connection) { |
| 302 | LM_ERR("invalid parameter value\n"); |
| 303 | return 0; |
| 304 | } |
| 305 | |
| 306 | con_size = sizeof(db_con_t) + sizeof(void *) + url->len; |
| 307 | if (url->len > MAX_URL_LENGTH) |
| 308 | { |
| 309 | LM_ERR("SQL URL too long\n"); |
| 310 | return 0; |
| 311 | } |
| 312 | |
| 313 | /* this is the root memory for this database connection. */ |
| 314 | res = (db_con_t*)pkg_malloc(con_size); |
| 315 | if (!res) { |
| 316 | LM_ERR("no private memory left\n"); |
| 317 | return 0; |
| 318 | } |
| 319 | |
| 320 | memset(res, 0, con_size); |
| 321 | |
| 322 | /* fill in the URL info */ |
| 323 | res->url.s = (char *)res + sizeof(db_con_t) + sizeof(void *); |
| 324 | res->url.len = url->len; |
| 325 | memcpy(res->url.s,url->s,url->len); |
| 326 | |
| 327 | id = new_db_id(url); |
| 328 | if (!id) { |
| 329 | LM_ERR("cannot parse URL '%.*s'\n", url->len, url->s); |
| 330 | goto err; |
| 331 | } |
| 332 | |
| 333 | /* Find the connection in the pool */ |
| 334 | con = pool_get(id); |
| 335 | if (!con) { |
| 336 | LM_DBG("connection %p not found in pool\n", id); |
| 337 | /* Not in the pool yet */ |
| 338 | con = (struct pool_con *)new_connection(id); |
| 339 | if (!con) { |
| 340 | LM_ERR("could not add connection to the pool\n"); |
| 341 | goto err; |
| 342 | } |
| 343 | pool_insert(con); |
| 344 | LM_DBG("connection %p inserted in pool as %p\n", id,con); |
| 345 | } else { |
| 346 | LM_DBG("connection %p found in pool as %p\n", id,con); |
| 347 | free_db_id(id); |
| 348 | } |
| 349 | |
| 350 | if (!con->transfers) { |
| 351 | con->transfers = pkg_malloc(db_max_async_connections * |
no test coverage detected