| 633 | } |
| 634 | |
| 635 | cachedb_con* cachedb_do_init(str *url,void* (*new_connection)(struct cachedb_id *)) |
| 636 | { |
| 637 | struct cachedb_id* id; |
| 638 | cachedb_con* res; |
| 639 | void *con; |
| 640 | |
| 641 | if (url == NULL || url->s == NULL || new_connection == NULL) { |
| 642 | LM_ERR("NULL parameter provided\n"); |
| 643 | return 0; |
| 644 | } |
| 645 | |
| 646 | res = pkg_malloc(sizeof(cachedb_con) + url->len); |
| 647 | if (res == NULL) { |
| 648 | LM_ERR("no more pkg mem\n"); |
| 649 | return 0; |
| 650 | } |
| 651 | |
| 652 | id = NULL; |
| 653 | |
| 654 | memset(res,0,sizeof(cachedb_con) + url->len); |
| 655 | res->url.s = (char *)res + sizeof(cachedb_con); |
| 656 | res->url.len = url->len; |
| 657 | memcpy(res->url.s,url->s,url->len); |
| 658 | |
| 659 | id = new_cachedb_id(url); |
| 660 | if (!id) { |
| 661 | LM_ERR("cannot parse url [%s]\n", db_url_escape(url)); |
| 662 | pkg_free(res); |
| 663 | return 0; |
| 664 | } |
| 665 | |
| 666 | con = cachedb_pool_get(id); |
| 667 | if (con == NULL) { |
| 668 | LM_DBG("opening new connection\n"); |
| 669 | con = new_connection(id); |
| 670 | if (con == NULL) { |
| 671 | LM_ERR("failed to open connection\n"); |
| 672 | goto err; |
| 673 | } |
| 674 | |
| 675 | cachedb_pool_insert((cachedb_pool_con *)con); |
| 676 | } else { |
| 677 | LM_DBG("connection already in pool\n"); |
| 678 | free_cachedb_id(id); |
| 679 | } |
| 680 | |
| 681 | res->data = con; |
| 682 | return res; |
| 683 | |
| 684 | err: |
| 685 | if (res) |
| 686 | pkg_free(res); |
| 687 | if (id) |
| 688 | free_cachedb_id(id); |
| 689 | |
| 690 | return 0; |
| 691 | } |
| 692 |
no test coverage detected