00001 /* 00002 * $Id: database_connection_lite.c 1942 2009-09-30 11:21:48Z sion $ 00003 * 00004 * Copyright (c) 2008-2009 Nominet UK. All rights reserved. 00005 * 00006 * Redistribution and use in source and binary forms, with or without 00007 * modification, are permitted provided that the following conditions 00008 * are met: 00009 * 1. Redistributions of source code must retain the above copyright 00010 * notice, this list of conditions and the following disclaimer. 00011 * 2. Redistributions in binary form must reproduce the above copyright 00012 * notice, this list of conditions and the following disclaimer in the 00013 * documentation and/or other materials provided with the distribution. 00014 * 00015 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 00016 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 00017 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00018 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 00019 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00020 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 00021 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00022 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 00023 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 00024 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN 00025 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00026 * 00027 */ 00028 00029 /*+ 00030 * database_connection_lite.c - Database Connection Functions 00031 * 00032 * Description: 00033 * Contains the database management functions (such as connect and 00034 * disconnect) and holds session-specific database information. 00035 -*/ 00036 00037 #include <stdarg.h> 00038 #include <stdlib.h> 00039 00040 #include <sqlite3.h> 00041 00042 #include "ksm/database.h" 00043 #include "ksm/dbsdef.h" 00044 #include "ksm/message.h" 00045 00046 static sqlite3* m_dbhandle = NULL; /* Non-NULL if connected */ 00047 00048 00049 00050 /*+ 00051 * DbConnect - Connect to Database 00052 * 00053 * Description: 00054 * Creates a connection to the specified database using the parameters 00055 * supplied. If successful, the handle to the connection is stored 00056 * locally, for retrieval by DbHandle(). 00057 * 00058 * Should there be an error, a suitable message is output. 00059 * 00060 * Arguments: 00061 * DB_HANDLE* dbhandle 00062 * Address of a location into which the connection handle is put. This 00063 * is also stored locally for retrieval by DbHandle(). If this argument 00064 * is NULL, no handle is returned through the function call. 00065 * 00066 * Note that if a handle for an active connection is already stored 00067 * locally, this function will overwrite it, regardless of success or 00068 * failure. 00069 * 00070 * const char* database 00071 * name of database (NULL to pick up the default). 00072 * 00073 * ... 00074 * Optional arguments. 00075 * 00076 * These are used for the MySql implementation, sqlite doesn't need them 00077 * 00078 * Returns: 00079 * int 00080 * 0 Success 00081 * Other Error on connection. The message will have been logged via 00082 * the MsgLog() function. 00083 -*/ 00084 00085 int DbConnect(DB_HANDLE* dbhandle, const char* database, ...) 00086 { 00087 sqlite3* connection = NULL; /* Local database handle */ 00088 va_list ap; /* Argument pointer */ 00089 int status = 0; /* Return status */ 00090 00091 /* Initialize if not already done so */ 00092 00093 DbInit(); 00094 00095 /* Get arguments */ 00096 00097 va_start(ap, database); 00098 va_end(ap); 00099 00100 /* ... and connect */ 00101 00102 status = sqlite3_open(database, &connection); 00103 /* status = sqlite3_open_v2(database, &connection, SQLITE_OPEN_READWRITE | SQLITE_OPEN_FULLMUTEX, NULL); */ 00104 00105 if (status) { 00106 /* Unable to connect */ 00107 status = MsgLog(DBS_CONNFAIL, sqlite3_errmsg(connection)); 00108 } 00109 00110 /* Store the returned handle for retrieval by DbHandle() */ 00111 00112 m_dbhandle = connection; 00113 00114 /* ... and pass back to the caller via the argument list */ 00115 00116 if (dbhandle) { 00117 *dbhandle = (DB_HANDLE) connection; 00118 } 00119 00120 /* Check the version against what we have in database.h */ 00121 if (status == 0) { 00122 status = db_version_check(); 00123 } 00124 00125 return status; 00126 } 00127 00128 00129 /*+ 00130 * DbDisconnect - Disconnect from Database 00131 * 00132 * Description: 00133 * Disconnects from the current database. If there is no current database, 00134 * this is a no-op. 00135 * 00136 * Arguments: 00137 * DB_HANDLE dbhandle 00138 * Pointer to the connection handle. After this function is called, 00139 * the handle is invalid. 00140 * 00141 * If the handle passed to this function is the same as the one stored 00142 * locally (and returned by DbHandle()), then the local copy is zeroed. 00143 * 00144 * Returns: 00145 * int 00146 * Status return. One of: 00147 * 00148 * 0 Success 00149 * DBS_NOTCONN Not connected to a database 00150 * None. 00151 -*/ 00152 00153 int DbDisconnect(DB_HANDLE dbhandle) 00154 { 00155 int status = 0; /* Return status */ 00156 00157 if (dbhandle) { 00158 if (dbhandle == m_dbhandle) { 00159 m_dbhandle = NULL; 00160 } 00161 sqlite3_close((sqlite3*) dbhandle); 00162 } 00163 else { 00164 status = MsgLog(DBS_NOTCONN); 00165 } 00166 00167 return status; 00168 } 00169 00170 00171 00172 /*+ 00173 * DbConnected - Check if Connected to a Database 00174 * 00175 * Description: 00176 * Interrogates the connection status. 00177 * 00178 * Arguments: 00179 * DB_HANDLE dbhandle 00180 * Handle to the connection. 00181 * 00182 * Returns: 00183 * int 00184 * true if connected to a database, false otherwise. 00185 -*/ 00186 00187 int DbConnected(DB_HANDLE dbhandle) 00188 { 00189 return dbhandle != NULL; 00190 } 00191 00192 00193 00194 /*+ 00195 * DbCheckConnected - Check If Connected 00196 * 00197 * Description: 00198 * Checks if connected to the database, and if not, outputs an error. 00199 * 00200 * Arguments: 00201 * DB_HANDLE dbhandle 00202 * Handle to the connection. 00203 * 00204 * Returns: 00205 * int 00206 * 1 if connected, 0 if not. 00207 -*/ 00208 00209 int DbCheckConnected(DB_HANDLE dbhandle) 00210 { 00211 int connected; 00212 00213 connected = DbConnected(dbhandle); 00214 if (! connected) { 00215 MsgLog(DBS_NOTCONERR); 00216 } 00217 00218 return connected; 00219 } 00220 00221 00222 /*+ 00223 * DbHandle - Return Database Handle 00224 * 00225 * Description: 00226 * Returns the handle to the database (the pointer to the MYSQL 00227 * structure). 00228 * 00229 * Arguments: 00230 * None. 00231 * 00232 * Returns: 00233 * DB_HANDLE 00234 * Database handle, which is NULL if none is stored. 00235 -*/ 00236 00237 DB_HANDLE DbHandle(void) 00238 { 00239 return (DB_HANDLE) m_dbhandle; 00240 }