00001 00002 /*! 00003 * \file 00004 * \ingroup db 00005 * \brief Database support for modules. 00006 * 00007 * Database support functions for modules. 00008 * 00009 * @cond 00010 * WARNING: 00011 * This file was autogenerated from the XML source file 00012 * ../../modules/userblacklist/kamailio-userblacklist.xml. 00013 * It can be regenerated by running 'make modules' in the db/schema 00014 * directory of the source code. You need to have xsltproc and 00015 * docbook-xsl stylesheets installed. 00016 * ALL CHANGES DONE HERE WILL BE LOST IF THE FILE IS REGENERATED 00017 * @endcond 00018 */ 00019 00020 #include "db_userblacklist.h" 00021 00022 /* database variables */ 00023 /* TODO assign read-write or read-only URI, introduce a parameter in XML */ 00024 00025 //extern str userblacklist_db_url; 00026 db_con_t * userblacklist_dbh = NULL; 00027 db_func_t userblacklist_dbf; 00028 00029 str userblacklist_table = str_init("userblacklist"); 00030 00031 /* column names */ 00032 str userblacklist_id_col = str_init("id"); 00033 str userblacklist_username_col = str_init("username"); 00034 str userblacklist_domain_col = str_init("domain"); 00035 str userblacklist_prefix_col = str_init("prefix"); 00036 str userblacklist_whitelist_col = str_init("whitelist"); 00037 00038 /* table version */ 00039 const unsigned int userblacklist_version = 1; 00040 00041 str globalblacklist_table = str_init("globalblacklist"); 00042 00043 /* column names */ 00044 str globalblacklist_id_col = str_init("id"); 00045 str globalblacklist_prefix_col = str_init("prefix"); 00046 str globalblacklist_whitelist_col = str_init("whitelist"); 00047 str globalblacklist_description_col = str_init("description"); 00048 00049 /* table version */ 00050 const unsigned int globalblacklist_version = 1; 00051 00052 00053 /* 00054 * Closes the DB connection. 00055 */ 00056 void userblacklist_db_close(void) { 00057 if (userblacklist_dbh) { 00058 userblacklist_dbf.close(userblacklist_dbh); 00059 userblacklist_dbh = NULL; 00060 } 00061 } 00062 00063 00064 /*! 00065 * Initialises the DB API, check the table version and closes the connection. 00066 * This should be called from the mod_init function. 00067 * 00068 * \return 0 means ok, -1 means an error occured. 00069 */ 00070 int userblacklist_db_init(void) { 00071 if (!userblacklist_db_url.s || !userblacklist_db_url.len) { 00072 LM_ERR("you have to set the db_url module parameter.\n"); 00073 return -1; 00074 } 00075 if (db_bind_mod(&userblacklist_db_url, &userblacklist_dbf) < 0) { 00076 LM_ERR("can't bind database module.\n"); 00077 return -1; 00078 } 00079 if ((userblacklist_dbh = userblacklist_dbf.init(&userblacklist_db_url)) == NULL) { 00080 LM_ERR("can't connect to database.\n"); 00081 return -1; 00082 } 00083 if ( 00084 (db_check_table_version(&userblacklist_dbf, userblacklist_dbh, &userblacklist_table, userblacklist_version) < 0) || 00085 (db_check_table_version(&userblacklist_dbf, userblacklist_dbh, &globalblacklist_table, globalblacklist_version) < 0) 00086 ) { 00087 LM_ERR("during table version check.\n"); 00088 userblacklist_db_close(); 00089 return -1; 00090 } 00091 userblacklist_db_close(); 00092 return 0; 00093 } 00094 00095 00096 /*! 00097 * Initialize the DB connection without checking the table version and DB URL. 00098 * This should be called from child_init. An already existing database 00099 * connection will be closed, and a new one created. 00100 * 00101 * \return 0 means ok, -1 means an error occured. 00102 */ 00103 int userblacklist_db_open(void) { 00104 if (userblacklist_dbh) { 00105 userblacklist_dbf.close(userblacklist_dbh); 00106 } 00107 if ((userblacklist_dbh = userblacklist_dbf.init(&userblacklist_db_url)) == NULL) { 00108 LM_ERR("can't connect to database.\n"); 00109 return -1; 00110 } 00111 return 0; 00112 } 00113 00114 00115 /*! 00116 * Update the variable length after eventual assignments from the config script. 00117 * This is necessary because we're using the 'str' type. 00118 */ 00119 void userblacklist_db_vars(void) { 00120 if (userblacklist_db_url.s) userblacklist_db_url.len = strlen(userblacklist_db_url.s); 00121 userblacklist_table.len = strlen(userblacklist_table.s); 00122 userblacklist_id_col.len = strlen(userblacklist_id_col.s); 00123 userblacklist_username_col.len = strlen(userblacklist_username_col.s); 00124 userblacklist_domain_col.len = strlen(userblacklist_domain_col.s); 00125 userblacklist_prefix_col.len = strlen(userblacklist_prefix_col.s); 00126 userblacklist_whitelist_col.len = strlen(userblacklist_whitelist_col.s); 00127 globalblacklist_table.len = strlen(globalblacklist_table.s); 00128 globalblacklist_id_col.len = strlen(globalblacklist_id_col.s); 00129 globalblacklist_prefix_col.len = strlen(globalblacklist_prefix_col.s); 00130 globalblacklist_whitelist_col.len = strlen(globalblacklist_whitelist_col.s); 00131 globalblacklist_description_col.len = strlen(globalblacklist_description_col.s); 00132 } 00133
1.5.6