Main Page | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

dbpool.h

Go to the documentation of this file.
00001 /* ==================================================================== 
00002  * The Kannel Software License, Version 1.0 
00003  * 
00004  * Copyright (c) 2001-2008 Kannel Group  
00005  * Copyright (c) 1998-2001 WapIT Ltd.   
00006  * All rights reserved. 
00007  * 
00008  * Redistribution and use in source and binary forms, with or without 
00009  * modification, are permitted provided that the following conditions 
00010  * are met: 
00011  * 
00012  * 1. Redistributions of source code must retain the above copyright 
00013  *    notice, this list of conditions and the following disclaimer. 
00014  * 
00015  * 2. Redistributions in binary form must reproduce the above copyright 
00016  *    notice, this list of conditions and the following disclaimer in 
00017  *    the documentation and/or other materials provided with the 
00018  *    distribution. 
00019  * 
00020  * 3. The end-user documentation included with the redistribution, 
00021  *    if any, must include the following acknowledgment: 
00022  *       "This product includes software developed by the 
00023  *        Kannel Group (http://www.kannel.org/)." 
00024  *    Alternately, this acknowledgment may appear in the software itself, 
00025  *    if and wherever such third-party acknowledgments normally appear. 
00026  * 
00027  * 4. The names "Kannel" and "Kannel Group" must not be used to 
00028  *    endorse or promote products derived from this software without 
00029  *    prior written permission. For written permission, please  
00030  *    contact org@kannel.org. 
00031  * 
00032  * 5. Products derived from this software may not be called "Kannel", 
00033  *    nor may "Kannel" appear in their name, without prior written 
00034  *    permission of the Kannel Group. 
00035  * 
00036  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 
00037  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
00038  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
00039  * DISCLAIMED.  IN NO EVENT SHALL THE KANNEL GROUP OR ITS CONTRIBUTORS 
00040  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,  
00041  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT  
00042  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR  
00043  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,  
00044  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE  
00045  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,  
00046  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
00047  * ==================================================================== 
00048  * 
00049  * This software consists of voluntary contributions made by many 
00050  * individuals on behalf of the Kannel Group.  For more information on  
00051  * the Kannel Group, please see <http://www.kannel.org/>. 
00052  * 
00053  * Portions of this software are based upon software originally written at  
00054  * WapIT Ltd., Helsinki, Finland for the Kannel project.  
00055  */ 
00056 
00057 /*
00058  * dbpool.h - database pool functions
00059  *
00060  * Stipe Tolj <stolj@wapme-group.de>
00061  * Alexander Malysh <a.malysh@centrium.de>
00062  */
00063 
00064 #ifndef GWDBPOOL_H
00065 #define GWDBPOOL_H
00066 
00067 #if defined(HAVE_MYSQL) || defined(HAVE_SDB) || \
00068     defined(HAVE_ORACLE) || defined(HAVE_SQLITE) || \
00069     defined(HAVE_PGSQL) || defined(HAVE_SQLITE3)
00070 #define HAVE_DBPOOL 1
00071 #endif
00072 
00073 /* supported databases for connection pools */
00074 enum db_type {
00075     DBPOOL_MYSQL, DBPOOL_SDB, DBPOOL_ORACLE, DBPOOL_SQLITE, DBPOOL_PGSQL,
00076     DBPOOL_SQLITE3
00077 };
00078 
00079 
00080 /*
00081  * The DBPool type. It is opaque: do not touch it except via the functions
00082  * defined in this header.
00083  */
00084 typedef struct DBPool DBPool;
00085 
00086 /*
00087  * The DBPoolConn type. It stores the abtracted pointer to a database 
00088  * specific connection and the pool pointer itself to allow easy
00089  * re-storage into the pool (also disallowing to insert the conn into an
00090  * other pool).
00091  */
00092  typedef struct {
00093     void *conn; /* the pointer holding the database specific connection */
00094     DBPool *pool; /* pointer of the pool where this connection belongs to */
00095 }  DBPoolConn;
00096 
00097 typedef struct {
00098     Octstr *host;
00099     long port;
00100     Octstr *username;
00101     Octstr *password;
00102     Octstr *database;
00103 } MySQLConf;
00104 
00105 /*
00106  * TODO Think how to get rid of it and have generic Conf struct
00107  */
00108 typedef struct {
00109     Octstr *username;
00110     Octstr *password;
00111     Octstr *tnsname;
00112 } OracleConf;
00113 
00114 typedef struct {
00115     Octstr *url;
00116 } SDBConf;
00117 
00118 typedef struct {
00119     Octstr *file;
00120 } SQLiteConf;
00121 
00122 typedef struct {
00123     Octstr *file;
00124 } SQLite3Conf;
00125 
00126 typedef struct {
00127     Octstr *host;
00128     long port;
00129     Octstr *username;
00130     Octstr *password;
00131     Octstr *database;
00132     Octstr *options;    /* yet not used */
00133     Octstr *tty;        /* yet not used */
00134 } PgSQLConf;
00135 
00136 typedef union {
00137     MySQLConf *mysql;
00138     SDBConf *sdb;
00139     OracleConf *oracle;
00140     SQLiteConf *sqlite;
00141     SQLite3Conf *sqlite3;
00142     PgSQLConf *pgsql;
00143 } DBConf;
00144 
00145 /*
00146  * Create a database pool with #connections of connections. The pool
00147  * is stored within a queue list.
00148  * Returns a pointer to the pool object on success or NULL if the
00149  * creation fails.
00150  */
00151 DBPool *dbpool_create(enum db_type db_type, DBConf *conf, unsigned int connections);
00152 
00153 /*
00154  * Destroys the database pool. Includes also shutdowning all existing
00155  * connections within the pool queue.
00156  */
00157 void dbpool_destroy(DBPool *p);
00158 
00159 /*
00160  * Increase the connection size of the pool by #conn connections.
00161  * Beware that you can't increase a pool size to more then the initial
00162  * dbpool_create() call defined and opened the maximum pool connections.
00163  * Returns how many connections have been additionally created and
00164  * inserted to the pool.
00165  */
00166 unsigned int dbpool_increase(DBPool *p, unsigned int conn);
00167 
00168 /*
00169  * Decrease the connection size of the pool by #conn connections.
00170  * A pool size can only by reduced up to 0. So if the caller specifies
00171  * to close more connections then there are in the pool, all connections
00172  * are closed.
00173  * Returns how many connections have been shutdown and deleted from the
00174  * pool queue.
00175  */
00176 unsigned int dbpool_decrease(DBPool *p, unsigned int conn);
00177 
00178 /*
00179  * Return the number of connections that are currently queued in the pool.
00180  */
00181 long dbpool_conn_count(DBPool *p);
00182 
00183 /*
00184  * Gets and active connection from the pool and returns it.
00185  * The caller can use it then for queuery operations and has to put it
00186  * back into the pool via dbpool_conn_produce(conn).
00187  * If no connection is in pool and DBPool is not in destroying phase then
00188  * will block until connection is available otherwise returns NULL.
00189  */
00190 DBPoolConn *dbpool_conn_consume(DBPool *p);
00191 
00192 /*
00193  * Returns a used connection to the pool again.
00194  * The connection is returned to it's domestic pool for further extraction
00195  * using dbpool_conn_consume().
00196  */
00197 void dbpool_conn_produce(DBPoolConn *conn);
00198 
00199 int dbpool_conn_select(DBPoolConn *conn, const Octstr *sql, List *binds, List **result);
00200 int dbpool_conn_update(DBPoolConn *conn, const Octstr *sql, List *binds);
00201 
00202 /*
00203  * Perfoms a check of all connections within the pool and tries to
00204  * re-establish the same ammount of connections if there are broken
00205  * connections within the pool.
00206  * (This operation can only be performed if the database allows such
00207  * operations by its API.)
00208  * Returns how many connections within the pool have been checked and
00209  * are still considered active.
00210  */
00211 unsigned int dbpool_check(DBPool *p);
00212 
00213 
00214 #endif
See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.