mysql_mod.c 4.8 KB

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