db_cassandra.c

00001 /*
00002  * $Id$
00003  *
00004  * CASSANDRA module interface
00005  *
00006  * Copyright (C) 2012 1&1 Internet AG
00007  *
00008  * This file is part of Kamailio, a free SIP server.
00009  *
00010  * Kamailio is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2 of the License, or
00013  * (at your option) any later version
00014  *
00015  * Kamailio is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  * GNU General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU General Public License 
00021  * along with this program; if not, write to the Free Software 
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00023  *
00024  * History:
00025  * --------
00026  * 2012-01  first version (Anca Vamanu)
00027  */
00028 
00029 #include <stdio.h>
00030 #include <stdlib.h>
00031 
00032 #include "../../sr_module.h"
00033 #include "../../lib/srdb1/db.h"
00034 #include "../../dprint.h"
00035 
00036 #include "dbcassa_base.h"
00037 #include "dbcassa_table.h"
00038 
00039 unsigned int cassa_conn_timeout= 1000;
00040 unsigned int cassa_send_timeout= 2000;
00041 unsigned int cassa_recv_timeout= 4000;
00042 unsigned int cassa_retries= 1;
00043 unsigned int cassa_auto_reconnect = 1;
00044 
00045 static int cassa_mod_init(void);
00046 static void mod_destroy(void);
00047 int db_cassa_bind_api(db_func_t *dbb);
00048 str dbcassa_schema_path={0, 0};
00049 
00050 MODULE_VERSION
00051 
00052 /*
00053  *  database module interface
00054  */
00055 static cmd_export_t cmds[] = {
00056         {"db_bind_api",  (cmd_function)db_cassa_bind_api, 0, 0, 0},
00057         {0, 0, 0, 0, 0}
00058 };
00059 
00060 
00061 static param_export_t params[] = {
00062         {"schema_path",      PARAM_STR,  &dbcassa_schema_path.s},
00063         {"connect_timeout",  PARAM_INT,  &cassa_conn_timeout},
00064         {"send_timeout",     PARAM_INT,  &cassa_send_timeout},
00065         {"receive_timeout",  PARAM_INT,  &cassa_recv_timeout},
00066         {"retries",          PARAM_INT,  &cassa_retries},
00067         {"auto_reconnect",   INT_PARAM,  &cassa_auto_reconnect},
00068         {0, 0, 0}
00069 };
00070 
00071 
00072 struct module_exports exports = {
00073         "db_cassandra",
00074         DEFAULT_DLFLAGS, /* dlopen flags */
00075         cmds,
00076         params,          /* module parameters */
00077         0,               /* exported statistics */
00078         0,               /* exported MI functions */
00079         0,               /* exported pseudo-variables */
00080         0,               /* extra processes */
00081         cassa_mod_init,  /* module initialization function */
00082         0,               /* response function*/
00083         mod_destroy,     /* destroy function */
00084         0                /* per-child init function */
00085 };
00086 
00087 static int cassa_mod_init(void)
00088 {
00089         if(!dbcassa_schema_path.s) {
00090                 LM_ERR("Set the schema_path parameter to the path of the directory"
00091                                 " where the table schemas are found (they must be described in cassa special format)\n");
00092                 return -1;
00093         }
00094         dbcassa_schema_path.len = strlen(dbcassa_schema_path.s);
00095 
00096         return dbcassa_read_table_schemas();
00097 }
00098 
00099 db1_con_t *db_cassa_init(const str* _url)
00100 {
00101         return db_do_init(_url,  (void* (*)()) db_cassa_new_connection);
00102 }
00103 
00104 
00110 void db_cassa_close(db1_con_t* _h)
00111 {
00112         db_do_close(_h, (void (*)()) db_cassa_free_connection);
00113 }
00114 
00115 /*
00116  * Store name of table that will be used by
00117  * subsequent database functions
00118  */
00119 int db_cassa_use_table(db1_con_t* _h, const str* _t)
00120 {
00121         return db_use_table(_h, _t);
00122 }
00123 
00124 
00125 
00126 int db_cassa_bind_api(db_func_t *dbb)
00127 {
00128         if(dbb==NULL)
00129                 return -1;
00130 
00131         memset(dbb, 0, sizeof(db_func_t));
00132 
00133         dbb->use_table        = db_cassa_use_table;
00134         dbb->init             = db_cassa_init;
00135         dbb->close            = db_cassa_close;
00136         dbb->query            = db_cassa_query;
00137         dbb->free_result      = db_cassa_free_result;
00138         dbb->insert           = db_cassa_insert;
00139         dbb->replace          = db_cassa_insert;
00140         dbb->insert_update    = db_cassa_insert;
00141         dbb->delete           = db_cassa_delete;
00142         dbb->update           = db_cassa_update;
00143 
00144         return 0;
00145 }
00146 
00147 static void mod_destroy(void)
00148 {
00149         dbcassa_destroy_htable();
00150 }