db_userblacklist.c

Go to the documentation of this file.
00001 
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 db1_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 
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 
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 
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