db_matrix.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*!
  2. * \file
  3. * \ingroup db
  4. * \brief Database support for modules.
  5. *
  6. * Database support functions for modules.
  7. *
  8. * @cond
  9. * WARNING:
  10. * This file was autogenerated from the XML source file
  11. * ../../modules/matrix/kamailio-matrix.xml.
  12. * It can be regenerated by running 'make modules' in the db/schema
  13. * directory of the source code. You need to have xsltproc and
  14. * docbook-xsl stylesheets installed.
  15. * ALL CHANGES DONE HERE WILL BE LOST IF THE FILE IS REGENERATED
  16. * @endcond
  17. */
  18. #include "db_matrix.h"
  19. /* database variables */
  20. /* TODO assign read-write or read-only URI, introduce a parameter in XML */
  21. //extern str matrix_db_url;
  22. db1_con_t * matrix_dbh = NULL;
  23. db_func_t matrix_dbf;
  24. str matrix_table = str_init("matrix");
  25. /* column names */
  26. str matrix_first_col = str_init("first");
  27. str matrix_second_col = str_init("second");
  28. str matrix_res_col = str_init("res");
  29. /* table version */
  30. const unsigned int matrix_version = 1;
  31. /*
  32. * Closes the DB connection.
  33. */
  34. void matrix_db_close(void) {
  35. if (matrix_dbh) {
  36. matrix_dbf.close(matrix_dbh);
  37. matrix_dbh = NULL;
  38. }
  39. }
  40. /*!
  41. * Initialises the DB API, check the table version and closes the connection.
  42. * This should be called from the mod_init function.
  43. *
  44. * \return 0 means ok, -1 means an error occured.
  45. */
  46. int matrix_db_init(void) {
  47. if (!matrix_db_url.s || !matrix_db_url.len) {
  48. LM_ERR("you have to set the db_url module parameter.\n");
  49. return -1;
  50. }
  51. if (db_bind_mod(&matrix_db_url, &matrix_dbf) < 0) {
  52. LM_ERR("can't bind database module.\n");
  53. return -1;
  54. }
  55. if ((matrix_dbh = matrix_dbf.init(&matrix_db_url)) == NULL) {
  56. LM_ERR("can't connect to database.\n");
  57. return -1;
  58. }
  59. if (
  60. (db_check_table_version(&matrix_dbf, matrix_dbh, &matrix_table, matrix_version) < 0)
  61. ) {
  62. LM_ERR("during table version check.\n");
  63. matrix_db_close();
  64. return -1;
  65. }
  66. matrix_db_close();
  67. return 0;
  68. }
  69. /*!
  70. * Initialize the DB connection without checking the table version and DB URL.
  71. * This should be called from child_init. An already existing database
  72. * connection will be closed, and a new one created.
  73. *
  74. * \return 0 means ok, -1 means an error occured.
  75. */
  76. int matrix_db_open(void) {
  77. if (matrix_dbh) {
  78. matrix_dbf.close(matrix_dbh);
  79. }
  80. if ((matrix_dbh = matrix_dbf.init(&matrix_db_url)) == NULL) {
  81. LM_ERR("can't connect to database.\n");
  82. return -1;
  83. }
  84. return 0;
  85. }