1 /* 2 * Copyright (C) 2001-2003 FhG FOKUS 3 * Copyright (C) 2006-2007 iptelorg GmbH 4 * 5 * This file is part of Kamailio, a free SIP server. 6 * 7 * Kamailio is free software; you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by 9 * the Free Software Foundation; either version 2 of the License, or 10 * (at your option) any later version 11 * 12 * Kamailio is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License 18 * along with this program; if not, write to the Free Software 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 20 */ 21 22 #ifndef _DB_CTX_H 23 #define _DB_CTX_H 1 24 25 /** \ingroup DB_API 26 * @{ 27 */ 28 29 #include "db_drv.h" 30 #include "db_gen.h" 31 #include "db_con.h" 32 33 #include "../../core/str.h" 34 #include "../../core/list.h" 35 36 #ifdef __cplusplus 37 extern "C" { 38 #endif /* __cplusplus */ 39 40 struct db_ctx; 41 42 43 /* This structure is stored in a linked list inside db_ctx 44 * and is used to lookup driver-specific data based on module 45 * name. A driver can have multiple connections in a context but 46 * it should have only one structure attached to db_ctx structure 47 * (which will be shared by all the connections of that driver in 48 * db_ctx. 49 */ 50 struct db_ctx_data { 51 str module; 52 db_drv_t* data; 53 SLIST_ENTRY(db_ctx_data) next; 54 }; 55 56 57 typedef struct db_ctx { 58 db_gen_t gen; /* Generic data common for all DB API structures */ 59 str id; /* Text id of the context */ 60 int con_n; /* Number of connections in the context */ 61 SLIST_HEAD(, db_ctx_data) data; 62 struct db_con* con[DB_PAYLOAD_MAX]; 63 } db_ctx_t; 64 65 66 /* 67 * Create a new database context 68 */ 69 struct db_ctx* db_ctx(const char* id); 70 71 72 /* Remove the database context structure 73 * from the linked list and free all memory 74 * used by the structure 75 */ 76 void db_ctx_free(struct db_ctx* ctx); 77 78 79 /* 80 * Add a new database to database context 81 */ 82 int db_add_db(struct db_ctx* ctx, const char* uri); 83 84 85 /* 86 * Attempt to connect all connections in the 87 * context 88 */ 89 int db_connect(struct db_ctx* ctx); 90 91 92 /* 93 * Disconnect all database connections in the 94 * context 95 */ 96 void db_disconnect(struct db_ctx* ctx); 97 98 99 #ifdef __cplusplus 100 } 101 #endif /* __cplusplus */ 102 103 /** @} */ 104 105 #endif /* _DB_CTX_H */ 106