db_userblacklist.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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/userblacklist/kamailio-userblacklist.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_userblacklist.h"
  19. /* database variables */
  20. /* TODO assign read-write or read-only URI, introduce a parameter in XML */
  21. //extern str userblacklist_db_url;
  22. db1_con_t * userblacklist_dbh = NULL;
  23. db_func_t userblacklist_dbf;
  24. str userblacklist_table = str_init("userblacklist");
  25. /* column names */
  26. str userblacklist_id_col = str_init("id");
  27. str userblacklist_username_col = str_init("username");
  28. str userblacklist_domain_col = str_init("domain");
  29. str userblacklist_prefix_col = str_init("prefix");
  30. str userblacklist_whitelist_col = str_init("whitelist");
  31. /* table version */
  32. const unsigned int userblacklist_version = 1;
  33. str globalblacklist_table = str_init("globalblacklist");
  34. /* column names */
  35. str globalblacklist_id_col = str_init("id");
  36. str globalblacklist_prefix_col = str_init("prefix");
  37. str globalblacklist_whitelist_col = str_init("whitelist");
  38. str globalblacklist_description_col = str_init("description");
  39. /* table version */
  40. const unsigned int globalblacklist_version = 1;
  41. /*
  42. * Closes the DB connection.
  43. */
  44. void userblacklist_db_close(void) {
  45. if (userblacklist_dbh) {
  46. userblacklist_dbf.close(userblacklist_dbh);
  47. userblacklist_dbh = NULL;
  48. }
  49. }
  50. /*!
  51. * Initialises the DB API, check the table version and closes the connection.
  52. * This should be called from the mod_init function.
  53. *
  54. * \return 0 means ok, -1 means an error occured.
  55. */
  56. int userblacklist_db_init(void) {
  57. if (!userblacklist_db_url.s || !userblacklist_db_url.len) {
  58. LM_ERR("you have to set the db_url module parameter.\n");
  59. return -1;
  60. }
  61. if (db_bind_mod(&userblacklist_db_url, &userblacklist_dbf) < 0) {
  62. LM_ERR("can't bind database module.\n");
  63. return -1;
  64. }
  65. if ((userblacklist_dbh = userblacklist_dbf.init(&userblacklist_db_url)) == NULL) {
  66. LM_ERR("can't connect to database.\n");
  67. return -1;
  68. }
  69. if (
  70. (db_check_table_version(&userblacklist_dbf, userblacklist_dbh, &userblacklist_table, userblacklist_version) < 0) ||
  71. (db_check_table_version(&userblacklist_dbf, userblacklist_dbh, &globalblacklist_table, globalblacklist_version) < 0)
  72. ) {
  73. LM_ERR("during table version check.\n");
  74. userblacklist_db_close();
  75. return -1;
  76. }
  77. userblacklist_db_close();
  78. return 0;
  79. }
  80. /*!
  81. * Initialize the DB connection without checking the table version and DB URL.
  82. * This should be called from child_init. An already existing database
  83. * connection will be closed, and a new one created.
  84. *
  85. * \return 0 means ok, -1 means an error occured.
  86. */
  87. int userblacklist_db_open(void) {
  88. if (userblacklist_dbh) {
  89. userblacklist_dbf.close(userblacklist_dbh);
  90. }
  91. if ((userblacklist_dbh = userblacklist_dbf.init(&userblacklist_db_url)) == NULL) {
  92. LM_ERR("can't connect to database.\n");
  93. return -1;
  94. }
  95. return 0;
  96. }