ul_db_query.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /* sp-ul_db module
  2. *
  3. * Copyright (C) 2007 1&1 Internet AG
  4. *
  5. * This file is part of Kamailio, a free SIP server.
  6. *
  7. * Kamailio is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version
  11. *
  12. * Kamailio is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "ul_db_query.h"
  22. #include "ul_db.h"
  23. static int order_dbs(ul_db_handle_t * handle, int order[]);
  24. static int db_exec_query(db_func_t * dbf, db1_con_t * dbh, str * table,
  25. db_key_t* _k, db_op_t * _op, db_val_t * _v,
  26. db_key_t * _c, int _n, int _nc, db_key_t _o,
  27. db1_res_t ** _r);
  28. int db_query(ul_db_handle_t * handle, db1_con_t *** _r_h, db_func_t ** _r_f,
  29. str * table, db_key_t* _k, db_op_t * _op, db_val_t * _v,
  30. db_key_t * _c, int _n, int _nc, db_key_t _o, db1_res_t ** _r, int rw) {
  31. int order[DB_NUM];
  32. int i;
  33. int err[DB_NUM];
  34. int ret = -1;
  35. order_dbs(handle, order);
  36. memset(err, 0 , sizeof(int) * DB_NUM);
  37. if(!handle || !table || !table->s || !_r_h) {
  38. LM_ERR("NULL pointer in parameter.\n");
  39. return -1;
  40. }
  41. i = 0;
  42. do {
  43. LM_DBG("now trying id %i, db %i.\n", handle->id, handle->db[order[i]].no);
  44. if(handle->db[order[i]].status == DB_ON) {
  45. if((ret = db_exec_query(&handle->db[order[i]].dbf, handle->db[order[i]].dbh, table, _k, _op, _v, _c, _n, _nc, _o, _r)) < 0) {
  46. LM_ERR("could not query table %.*s error on id %i, db %i.\n", table->len, table->s, handle->id, handle->db[order[i]].no);
  47. if(rw) {
  48. if(err[i] == 0 && handle->db[order[i]].status == DB_ON) {
  49. if(db_handle_error(handle, handle->db[order[i]].no) < 0) {
  50. LM_ERR("could not handle error on id %i, db %i.\n", handle->id, handle->db[order[i]].no);
  51. } else {
  52. err[i] = 1;
  53. i--;
  54. }
  55. }
  56. }
  57. }
  58. }
  59. i++;
  60. } while((ret < 0) && (i < DB_NUM));
  61. i--;
  62. LM_DBG("returned handle is for id %i, db %i\n", handle->id, handle->db[order[i]].no);
  63. *_r_h = &handle->db[order[i]].dbh;
  64. *_r_f = &handle->db[order[i]].dbf;
  65. return ret;
  66. }
  67. static int order_dbs(ul_db_handle_t * handle, int order[]) {
  68. int i,j, tmp;
  69. for(i=0; i<DB_NUM; i++) {
  70. order[i] = i;
  71. }
  72. for(i=0; i<DB_NUM; i++) {
  73. for(j=i+1; j<DB_NUM; j++) {
  74. if((handle->db[i].status == DB_OFF || handle->db[i].status == DB_INACTIVE) && (handle->db[j].status == DB_ON)) {
  75. tmp = order[i];
  76. order[i] = order[j];
  77. order[j] = tmp;
  78. } else if(handle->db[i].failover_time > handle->db[j].failover_time) {
  79. tmp = order[i];
  80. order[i] = order[j];
  81. order[j] = tmp;
  82. }
  83. }
  84. }
  85. return 0;
  86. }
  87. static int db_exec_query(db_func_t * dbf, db1_con_t * dbh, str * table,
  88. db_key_t* _k, db_op_t * _op, db_val_t * _v,
  89. db_key_t * _c, int _n, int _nc, db_key_t _o,
  90. db1_res_t ** _r) {
  91. if(!dbf || !dbh || !table || !table->s) {
  92. LM_ERR("NULL pointer in parameter.\n");
  93. return -1;
  94. }
  95. if(dbf->use_table(dbh, table) < 0) {
  96. LM_ERR("could not use table %.*s.\n", table->len, table->s);
  97. return -1;
  98. }
  99. if(dbf->query(dbh, _k, _op, _v, _c, _n, _nc, _o, _r) < 0) {
  100. LM_ERR("could not query table %.*s.\n", table->len, table->s);
  101. return -1;
  102. }
  103. return 0;
  104. }