mysql_mod.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * $Id$
  3. *
  4. * MySQL module interface
  5. *
  6. * Copyright (C) 2001-2003 FhG Fokus
  7. *
  8. * This file is part of Kamailio, a free SIP server.
  9. *
  10. * ser is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version
  14. *
  15. * For a license to use the Kamailio software under conditions
  16. * other than those described here, or to purchase support for this
  17. * software, please contact iptel.org by e-mail at the following addresses:
  18. * [email protected]
  19. *
  20. * Kamailio is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program; if not, write to the Free Software
  27. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  28. */
  29. /*
  30. * History:
  31. * --------
  32. * 2003-03-11 updated to the new module exports interface (andrei)
  33. * 2003-03-16 flags export parameter added (janakj)
  34. * 2013-04-02 added transaction support ([email protected])
  35. */
  36. /** @addtogroup mysql
  37. * @{
  38. */
  39. #include "mysql_mod.h"
  40. #include "km_db_mysql.h"
  41. #include "my_uri.h"
  42. #include "my_con.h"
  43. #include "my_cmd.h"
  44. #include "my_fld.h"
  45. #include "my_res.h"
  46. #include "../../sr_module.h"
  47. #include "../../lib/srdb2/db.h"
  48. #include "../../dprint.h"
  49. int my_ping_interval = 5 * 60; /* Default is 5 minutes */
  50. unsigned int my_connect_to = 2; /* 2 s by default */
  51. unsigned int my_send_to = 0; /* enabled only for mysql >= 5.25 */
  52. unsigned int my_recv_to = 0; /* enabled only for mysql >= 5.25 */
  53. unsigned int my_retries = 1; /* Number of retries when command fails */
  54. unsigned long my_client_ver = 0;
  55. struct mysql_counters_h mysql_cnts_h;
  56. counter_def_t mysql_cnt_defs[] = {
  57. {&mysql_cnts_h.driver_err, "driver_errors", 0, 0, 0,
  58. "incremented each time a Mysql error happened because the server/connection has failed."},
  59. {0, 0, 0, 0, 0, 0 }
  60. };
  61. #define DEFAULT_MY_SEND_TO 2 /* in seconds */
  62. #define DEFAULT_MY_RECV_TO 4 /* in seconds */
  63. static int mysql_mod_init(void);
  64. MODULE_VERSION
  65. /*
  66. * MySQL database module interface
  67. */
  68. static cmd_export_t cmds[] = {
  69. {"db_ctx", (cmd_function)NULL, 0, 0, 0},
  70. {"db_con", (cmd_function)my_con, 0, 0, 0},
  71. {"db_uri", (cmd_function)my_uri, 0, 0, 0},
  72. {"db_cmd", (cmd_function)my_cmd, 0, 0, 0},
  73. {"db_put", (cmd_function)my_cmd_exec, 0, 0, 0},
  74. {"db_del", (cmd_function)my_cmd_exec, 0, 0, 0},
  75. {"db_get", (cmd_function)my_cmd_exec, 0, 0, 0},
  76. {"db_upd", (cmd_function)my_cmd_exec, 0, 0, 0},
  77. {"db_sql", (cmd_function)my_cmd_exec, 0, 0, 0},
  78. {"db_res", (cmd_function)my_res, 0, 0, 0},
  79. {"db_fld", (cmd_function)my_fld, 0, 0, 0},
  80. {"db_first", (cmd_function)my_cmd_first, 0, 0, 0},
  81. {"db_next", (cmd_function)my_cmd_next, 0, 0, 0},
  82. {"db_setopt", (cmd_function)my_setopt, 0, 0, 0},
  83. {"db_getopt", (cmd_function)my_getopt, 0, 0, 0},
  84. {"db_bind_api", (cmd_function)db_mysql_bind_api, 0, 0, 0},
  85. {0, 0, 0, 0, 0}
  86. };
  87. /*
  88. * Exported parameters
  89. */
  90. static param_export_t params[] = {
  91. {"ping_interval", PARAM_INT, &my_ping_interval},
  92. {"connect_timeout", PARAM_INT, &my_connect_to},
  93. {"send_timeout", PARAM_INT, &my_send_to},
  94. {"receive_timeout", PARAM_INT, &my_recv_to},
  95. {"retries", PARAM_INT, &my_retries},
  96. {"timeout_interval", INT_PARAM, &db_mysql_timeout_interval},
  97. {"auto_reconnect", INT_PARAM, &db_mysql_auto_reconnect},
  98. {"insert_delayed", INT_PARAM, &db_mysql_insert_all_delayed},
  99. {"update_affected_found", INT_PARAM, &db_mysql_update_affected_found},
  100. {0, 0, 0}
  101. };
  102. struct module_exports exports = {
  103. "db_mysql",
  104. cmds,
  105. 0, /* RPC method */
  106. params, /* module parameters */
  107. mysql_mod_init, /* module initialization function */
  108. 0, /* response function*/
  109. 0, /* destroy function */
  110. 0, /* oncancel function */
  111. 0 /* per-child init function */
  112. };
  113. int mod_register(char *path, int *dlflags, void *p1, void *p2)
  114. {
  115. if(db_mysql_alloc_buffer()<0)
  116. return -1;
  117. return 0;
  118. }
  119. static int mysql_mod_init(void)
  120. {
  121. #if MYSQL_VERSION_ID >= 40101
  122. my_client_ver = mysql_get_client_version();
  123. if ((my_client_ver >= 50025) ||
  124. ((my_client_ver >= 40122) &&
  125. (my_client_ver < 50000))) {
  126. if (my_send_to == 0) {
  127. my_send_to= DEFAULT_MY_SEND_TO;
  128. }
  129. if (my_recv_to == 0) {
  130. my_recv_to= DEFAULT_MY_RECV_TO;
  131. }
  132. } else if (my_recv_to || my_send_to) {
  133. LOG(L_WARN, "WARNING: mysql send or received timeout set, but "
  134. " not supported by the installed mysql client library"
  135. " (needed at least 4.1.22 or 5.0.25, but installed %ld)\n",
  136. my_client_ver);
  137. }
  138. #else
  139. if (my_recv_to || my_send_to) {
  140. LOG(L_WARN, "WARNING: mysql send or received timeout set, but "
  141. " not supported by the mysql client library used to compile"
  142. " the mysql module (needed at least 4.1.1 but "
  143. " compiled against %ld)\n", MYSQL_VERSION_ID);
  144. }
  145. #endif
  146. if (counter_register_array("mysql", mysql_cnt_defs) < 0)
  147. goto error;
  148. return kam_mysql_mod_init();
  149. error:
  150. return -1;
  151. }
  152. /** @} */