db_matrix.c

Go to the documentation of this file.
00001 
00020 #include "db_matrix.h"
00021 
00022 /* database variables */
00023 /* TODO assign read-write or read-only URI, introduce a parameter in XML */
00024 
00025 //extern str matrix_db_url;
00026 db1_con_t * matrix_dbh = NULL;
00027 db_func_t matrix_dbf;
00028 
00029 str matrix_table = str_init("matrix");
00030 
00031 /* column names */
00032 str matrix_first_col = str_init("first");
00033 str matrix_second_col = str_init("second");
00034 str matrix_res_col = str_init("res");
00035 
00036 /* table version */
00037 const unsigned int matrix_version = 1;
00038 
00039 
00040 /*
00041  * Closes the DB connection.
00042  */
00043 void matrix_db_close(void) {
00044         if (matrix_dbh) {
00045                 matrix_dbf.close(matrix_dbh);
00046                 matrix_dbh = NULL;
00047         }
00048 }
00049 
00050 
00057 int matrix_db_init(void) {
00058         if (!matrix_db_url.s || !matrix_db_url.len) {
00059                 LM_ERR("you have to set the db_url module parameter.\n");
00060                 return -1;
00061         }
00062         if (db_bind_mod(&matrix_db_url, &matrix_dbf) < 0) {
00063                 LM_ERR("can't bind database module.\n");
00064                 return -1;
00065         }
00066         if ((matrix_dbh = matrix_dbf.init(&matrix_db_url)) == NULL) {
00067                 LM_ERR("can't connect to database.\n");
00068                 return -1;
00069         }
00070         if (
00071         (db_check_table_version(&matrix_dbf, matrix_dbh, &matrix_table, matrix_version) < 0)
00072         ) {
00073                 LM_ERR("during table version check.\n");
00074                 matrix_db_close();
00075                 return -1;
00076         }
00077         matrix_db_close();
00078         return 0;
00079 }
00080 
00081 
00089 int matrix_db_open(void) {
00090         if (matrix_dbh) {
00091                 matrix_dbf.close(matrix_dbh);
00092         }
00093         if ((matrix_dbh = matrix_dbf.init(&matrix_db_url)) == NULL) {
00094                 LM_ERR("can't connect to database.\n");
00095                 return -1;
00096         }
00097         return 0;
00098 }
00099 
00100 
00105 void matrix_db_vars(void) {
00106         if (matrix_db_url.s) matrix_db_url.len = strlen(matrix_db_url.s);
00107         matrix_table.len = strlen(matrix_table.s);
00108         matrix_first_col.len = strlen(matrix_first_col.s);
00109         matrix_second_col.len = strlen(matrix_second_col.s);
00110         matrix_res_col.len = strlen(matrix_res_col.s);
00111 }
00112