ul_db_failover_func.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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_failover_func.h"
  22. #include "../../dprint.h"
  23. #include "p_usrloc_mod.h"
  24. #include "ul_db.h"
  25. static str autocommit_off = str_init("SET AUTOCOMMIT=0");
  26. static str fail_isolation_level = str_init("SET TRANSACTION ISOLATION LEVEL SERIALIZABLE");
  27. static str start_transaction = str_init("START TRANSACTION");
  28. static str commit = str_init("COMMIT");
  29. static str rollback = str_init("ROLLBACK");
  30. static str autocommit_on = str_init("SET AUTOCOMMIT=1");
  31. static char query[UL_DB_QUERY_LEN];
  32. int ul_db_failover_prepare(db_func_t * dbf, db1_con_t * dbh) {
  33. if(dbf->raw_query(dbh, &autocommit_off, NULL) < 0) {
  34. LM_ERR("could not "
  35. "set autocommit off!\n");
  36. return -2;
  37. }
  38. if(dbf->raw_query(dbh, &fail_isolation_level, NULL) < 0) {
  39. LM_ERR("could not "
  40. "set transaction isolation level!\n");
  41. return -2;
  42. }
  43. if(dbf->raw_query(dbh, &start_transaction, NULL) < 0) {
  44. LM_ERR("could not "
  45. "start transaction!\n");
  46. return -2;
  47. }
  48. return 0;
  49. }
  50. int ul_db_failover_commit(db_func_t * dbf, db1_con_t * dbh) {
  51. if(dbf->raw_query(dbh, &commit, NULL) < 0) {
  52. LM_ERR("transaction commit "
  53. "failed.\n");
  54. return -1;
  55. }
  56. if(dbf->raw_query(dbh, &autocommit_on, NULL) < 0) {
  57. LM_ERR("could not turn "
  58. "transaction autocommit on.\n");
  59. return -2;
  60. }
  61. return 0;
  62. }
  63. int ul_db_failover_rollback(db_func_t * dbf, db1_con_t * dbh) {
  64. LM_ERR("rolling back failover "
  65. "transaction.\n");
  66. if(dbf->raw_query(dbh, &rollback, NULL) < 0) {
  67. LM_ERR("could not rollback "
  68. "transaction.\n");
  69. return -1;
  70. }
  71. if(dbf->raw_query(dbh, &autocommit_on, NULL) < 0) {
  72. LM_ERR("could not set "
  73. "autocommit on.\n");
  74. return -2;
  75. }
  76. return 0;
  77. }
  78. int get_max_no_of_db_id(db_func_t * dbf, db1_con_t * dbh, int id){
  79. db1_res_t * res;
  80. db_row_t * row;
  81. int query_len, max;
  82. str tmp;
  83. query_len = 50 + reg_table.len + id_col.len + num_col.len;
  84. if(query_len > UL_DB_QUERY_LEN){
  85. LM_ERR("weird: query too long.\n");
  86. return -1;
  87. }
  88. memset(query, 0, UL_DB_QUERY_LEN);
  89. if (sprintf(query,
  90. "SELECT MAX(%.*s) "
  91. "FROM %.*s "
  92. "WHERE %.*s='%i'",
  93. num_col.len, num_col.s,
  94. reg_table.len, reg_table.s,
  95. id_col.len, id_col.s, id) < 0) {
  96. LM_ERR("could not print query\n");
  97. return -1;
  98. }
  99. tmp.s = query;
  100. tmp.len = strlen(query);
  101. if(dbf->raw_query(dbh, &tmp, &res) < 0){
  102. LM_ERR("weird: could not query %.*s.\n",
  103. reg_table.len, reg_table.s);
  104. return -1;
  105. }
  106. if(RES_ROW_N(res) == 0){
  107. LM_ERR("weird: no data found for id %i\n", id);
  108. dbf->free_result(dbh, res);
  109. return -1;
  110. }
  111. row = RES_ROWS(res);
  112. max = VAL_INT(ROW_VALUES(row));
  113. dbf->free_result(dbh, res);
  114. return max;
  115. }
  116. int store_handle_data(db_func_t * dbf, db1_con_t * dbh, ul_db_t * db, int id, int old_num, int new_id){
  117. db_key_t cols[8];
  118. db_val_t vals[8];
  119. db_key_t keys[2];
  120. db_val_t key_vals[8];
  121. db_op_t op[2];
  122. cols[0] = &id_col;
  123. vals[0].type = DB1_INT;
  124. vals[0].nul = 0;
  125. vals[0].val.int_val = new_id;
  126. cols[1] = &num_col;
  127. vals[1].type = DB1_INT;
  128. vals[1].nul = 0;
  129. vals[1].val.int_val = db->no;
  130. cols[2] = &url_col;
  131. vals[2].type = DB1_STRING;
  132. vals[2].nul = 0;
  133. vals[2].val.string_val = db->url.s;
  134. cols[3] = &error_col;
  135. vals[3].type = DB1_INT;
  136. vals[3].nul = 0;
  137. vals[3].val.int_val = db->errors;
  138. cols[4] = &failover_time_col;
  139. vals[4].type = DB1_DATETIME;
  140. vals[4].nul = 0;
  141. vals[4].val.time_val = db->failover_time;
  142. cols[5] = &spare_col;
  143. vals[5].type = DB1_INT;
  144. vals[5].nul = 0;
  145. vals[5].val.int_val = db->spare;
  146. cols[6] = &status_col;
  147. vals[6].type = DB1_INT;
  148. vals[6].nul = 0;
  149. vals[6].val.int_val = db->status;
  150. cols[7] = &risk_group_col;
  151. vals[7].type = DB1_INT;
  152. vals[7].nul = 0;
  153. vals[7].val.int_val = db->rg;
  154. keys[0] = &id_col;
  155. op[0] = OP_EQ;
  156. key_vals[0].type = DB1_INT;
  157. key_vals[0].nul = 0;
  158. key_vals[0].val.int_val = id;
  159. keys[1] = &num_col;
  160. op[1] = OP_EQ;
  161. key_vals[1].type = DB1_INT;
  162. key_vals[1].nul = 0;
  163. key_vals[1].val.int_val = old_num;
  164. if(dbf->use_table(dbh, &reg_table) < 0){
  165. LM_ERR("could not use reg_table.\n");
  166. return -1;
  167. }
  168. if(dbf->update(dbh, keys, op, key_vals, cols, vals, 2, 7) < 0){
  169. LM_ERR("could insert handle data.\n");
  170. return -1;
  171. }
  172. return 0;
  173. }
  174. int check_handle_data(db_func_t * dbf, db1_con_t * dbh, ul_db_t * db, int id){
  175. db_key_t cols[2];
  176. db_key_t keys[4];
  177. db_val_t key_vals[4];
  178. db_op_t op[4];
  179. db1_res_t * res;
  180. cols[0] = &id_col;
  181. keys[0] = &id_col;
  182. op[0] = OP_EQ;
  183. key_vals[0].type = DB1_INT;
  184. key_vals[0].nul = 0;
  185. key_vals[0].val.int_val = id;
  186. keys[1] = &num_col;
  187. op[1] = OP_EQ;
  188. key_vals[1].type = DB1_INT;
  189. key_vals[1].nul = 0;
  190. key_vals[1].val.int_val = db->no;
  191. keys[2] = &url_col;
  192. op[2] = OP_EQ;
  193. key_vals[2].type = DB1_STRING;
  194. key_vals[2].nul = 0;
  195. key_vals[2].val.string_val = db->url.s;
  196. if(dbf->use_table(dbh, &reg_table) < 0){
  197. LM_ERR("could not use "
  198. "reg table.\n");
  199. return -1;
  200. }
  201. if(dbf->query(dbh, keys, op, key_vals, cols, 3, 1, NULL, &res) < 0){
  202. LM_ERR("could not use "
  203. "query table.\n");
  204. return -1;
  205. }
  206. if(RES_ROW_N(res) == 0){
  207. dbf->free_result(dbh, res);
  208. return 1;
  209. }
  210. dbf->free_result(dbh, res);
  211. return 0;
  212. }