ul_db_form_query.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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_form_query.h"
  22. #include "ul_db_tran.h"
  23. #include "ul_db.h"
  24. #include "p_usrloc_mod.h"
  25. static int db_do_query(ul_db_op_t ul_op, db_func_t * dbf, db1_con_t * dbh, str * table, db_key_t* _k, db_op_t* _o,
  26. db_val_t* _v, db_key_t* _uk, db_val_t* _uv, int _n, int _un);
  27. int db_submit_query(ul_db_op_t ul_op, ul_db_handle_t * handle, str * table, db_key_t* _k, db_op_t* _o,
  28. db_val_t* _v, db_key_t* _uk, db_val_t* _uv, int _n, int _un) {
  29. int i;
  30. int working_c[DB_NUM];
  31. int working_r[DB_NUM];
  32. int errors = 0;
  33. int w;
  34. if(!handle || !table || !table->s) {
  35. LM_ERR("NULL pointer in parameter.\n");
  36. return -1;
  37. }
  38. if(db_use_transactions) {
  39. for(i=0; i<DB_NUM; i++) {
  40. working_c[i] = 0;
  41. working_r[i] = 0;
  42. }
  43. if(ul_db_tran_start(handle, working_r) < 0) {
  44. LM_ERR("error during starting transaction"
  45. " on table %.*s with id %i.\n", table->len, table->s, handle->id);
  46. w = get_working_sum(working_r, DB_NUM);
  47. if(db_check_policy(DB_POL_MOD, w, handle->working) < 0) {
  48. ul_db_tran_rollback(handle, working_r);
  49. return -1;
  50. }
  51. }
  52. for(i=0; i<DB_NUM; i++) {
  53. working_c[i] = working_r[i];
  54. if((handle->db[i].status == DB_ON) && (working_c[i])) {
  55. if(db_do_query(ul_op, &handle->db[i].dbf, handle->db[i].dbh, table, _k, _o, _v, _uk, _uv, _n, _un) < 0) {
  56. LM_ERR("error during querying "
  57. "table %.*s with id %i on db %i.\n",
  58. table->len, table->s, handle->id, i);
  59. if(db_handle_error(handle, handle->db[i].no) < 0) {
  60. LM_CRIT("could not handle error on db %i, handle, %i\n",
  61. handle->id, handle->db[i].no);
  62. }
  63. errors++;
  64. working_c[i] = 0;
  65. } else {
  66. working_r[i] = 0;
  67. }
  68. }
  69. }
  70. w = get_working_sum(working_c, DB_NUM);
  71. if(errors > 0) {
  72. ul_db_tran_rollback(handle, working_r);
  73. if(db_check_policy(DB_POL_MOD, w, handle->working) < 0) {
  74. ul_db_tran_rollback(handle, working_c);
  75. return -1;
  76. }
  77. }
  78. return ul_db_tran_commit(handle, working_c);
  79. } else {
  80. for(i=0; i<DB_NUM; i++) {
  81. if(handle->db[i].status == DB_ON) {
  82. if(db_do_query(ul_op, &handle->db[i].dbf, handle->db[i].dbh, table, _k, _o, _v, _uk, _uv, _n, _un) < 0) {
  83. if(db_handle_error(handle, handle->db[i].no) < 0) {
  84. LM_CRIT("could not handle error on db %i, handle, %i\n",
  85. handle->id, handle->db[i].no);
  86. }
  87. return -1;
  88. }
  89. }
  90. }
  91. return 0;
  92. }
  93. }
  94. static int db_do_query(ul_db_op_t ul_op, db_func_t * dbf, db1_con_t * dbh, str * table, db_key_t* _k, db_op_t* _o,
  95. db_val_t* _v, db_key_t* _uk, db_val_t* _uv, int _n, int _un) {
  96. if(dbf->use_table(dbh, table) < 0) {
  97. LM_ERR("error in use table %.*s.\n", table->len, table->s);
  98. return -1;
  99. }
  100. switch(ul_op) {
  101. case UL_DB_INS:
  102. if(dbf->insert(dbh, _k, _v, _n) < 0) {
  103. LM_ERR("error in inserting into "
  104. "table %.*s.\n", table->len, table->s);
  105. return -1;
  106. }
  107. return 0;
  108. case UL_DB_REPL:
  109. if(dbf->replace(dbh, _k, _v, _n, _un, 0) < 0) {
  110. LM_ERR("error in replacing in "
  111. "table %.*s.\n", table->len, table->s);
  112. return -1;
  113. }
  114. return 0;
  115. case UL_DB_INS_UPD:
  116. if(dbf->insert_update(dbh, _k, _v, _n ) < 0) {
  117. LM_ERR("error in inserting/updating in "
  118. "table %.*s.\n", table->len, table->s);
  119. return -1;
  120. }
  121. return 0;
  122. case UL_DB_UPD:
  123. if(dbf->update(dbh, _k, _o, _v, _uk, _uv, _n, _un) < 0) {
  124. LM_ERR("error in updating "
  125. "table %.*s.\n", table->len, table->s);
  126. return -1;
  127. }
  128. return 0;
  129. case UL_DB_DEL:
  130. if(dbf->delete(dbh, _k, _o, _v, _n) < 0) {
  131. LM_ERR("error in deleting from table %.*s.\n", table->len, table->s);
  132. return -1;
  133. }
  134. return 0;
  135. default: return -1;
  136. }
  137. return 0;
  138. }