db_cassandra.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * $Id$
  3. *
  4. * CASSANDRA module interface
  5. *
  6. * Copyright (C) 2012 1&1 Internet AG
  7. *
  8. * This file is part of Kamailio, a free SIP server.
  9. *
  10. * Kamailio 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. * Kamailio is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. *
  24. * History:
  25. * --------
  26. * 2012-01 first version (Anca Vamanu)
  27. */
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include "../../sr_module.h"
  31. #include "../../lib/srdb1/db.h"
  32. #include "../../dprint.h"
  33. #include "dbcassa_base.h"
  34. #include "dbcassa_table.h"
  35. unsigned int cassa_conn_timeout= 1000;
  36. unsigned int cassa_send_timeout= 2000;
  37. unsigned int cassa_recv_timeout= 4000;
  38. unsigned int cassa_retries= 1;
  39. unsigned int cassa_auto_reconnect = 1;
  40. static int cassa_mod_init(void);
  41. static void mod_destroy(void);
  42. int db_cassa_bind_api(db_func_t *dbb);
  43. str dbcassa_schema_path={0, 0};
  44. MODULE_VERSION
  45. /*
  46. * database module interface
  47. */
  48. static cmd_export_t cmds[] = {
  49. {"db_bind_api", (cmd_function)db_cassa_bind_api, 0, 0, 0},
  50. {0, 0, 0, 0, 0}
  51. };
  52. static param_export_t params[] = {
  53. {"schema_path", PARAM_STR, &dbcassa_schema_path.s},
  54. {"connect_timeout", PARAM_INT, &cassa_conn_timeout},
  55. {"send_timeout", PARAM_INT, &cassa_send_timeout},
  56. {"receive_timeout", PARAM_INT, &cassa_recv_timeout},
  57. {"retries", PARAM_INT, &cassa_retries},
  58. {"auto_reconnect", INT_PARAM, &cassa_auto_reconnect},
  59. {0, 0, 0}
  60. };
  61. struct module_exports exports = {
  62. "db_cassandra",
  63. DEFAULT_DLFLAGS, /* dlopen flags */
  64. cmds,
  65. params, /* module parameters */
  66. 0, /* exported statistics */
  67. 0, /* exported MI functions */
  68. 0, /* exported pseudo-variables */
  69. 0, /* extra processes */
  70. cassa_mod_init, /* module initialization function */
  71. 0, /* response function*/
  72. mod_destroy, /* destroy function */
  73. 0 /* per-child init function */
  74. };
  75. static int cassa_mod_init(void)
  76. {
  77. if(!dbcassa_schema_path.s) {
  78. LM_ERR("Set the schema_path parameter to the path of the directory"
  79. " where the table schemas are found (they must be described in cassa special format)\n");
  80. return -1;
  81. }
  82. dbcassa_schema_path.len = strlen(dbcassa_schema_path.s);
  83. return dbcassa_read_table_schemas();
  84. }
  85. db1_con_t *db_cassa_init(const str* _url)
  86. {
  87. return db_do_init(_url, (void* (*)()) db_cassa_new_connection);
  88. }
  89. /*!
  90. * \brief Close database when the database is no longer needed
  91. * \param _h closed connection, as returned from db_cassa_init
  92. * \note free all memory and resources
  93. */
  94. void db_cassa_close(db1_con_t* _h)
  95. {
  96. db_do_close(_h, (void (*)()) db_cassa_free_connection);
  97. }
  98. /*
  99. * Store name of table that will be used by
  100. * subsequent database functions
  101. */
  102. int db_cassa_use_table(db1_con_t* _h, const str* _t)
  103. {
  104. return db_use_table(_h, _t);
  105. }
  106. int db_cassa_bind_api(db_func_t *dbb)
  107. {
  108. if(dbb==NULL)
  109. return -1;
  110. memset(dbb, 0, sizeof(db_func_t));
  111. dbb->use_table = db_cassa_use_table;
  112. dbb->init = db_cassa_init;
  113. dbb->close = db_cassa_close;
  114. dbb->query = db_cassa_query;
  115. dbb->free_result = db_cassa_free_result;
  116. dbb->insert = db_cassa_insert;
  117. dbb->replace = db_cassa_replace;
  118. dbb->insert_update = db_cassa_insert;
  119. dbb->delete = db_cassa_delete;
  120. dbb->update = db_cassa_update;
  121. dbb->raw_query = db_cassa_raw_query;
  122. return 0;
  123. }
  124. static void mod_destroy(void)
  125. {
  126. dbcassa_destroy_htable();
  127. }