OpenDNSSEC-enforcer  1.3.16
database_connection_lite.c
Go to the documentation of this file.
1 /*
2  * $Id: database_connection_lite.c 1942 2009-09-30 11:21:48Z sion $
3  *
4  * Copyright (c) 2008-2009 Nominet UK. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
19  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
21  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
23  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
25  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  */
28 
29 /*+
30  * database_connection_lite.c - Database Connection Functions
31  *
32  * Description:
33  * Contains the database management functions (such as connect and
34  * disconnect) and holds session-specific database information.
35 -*/
36 
37 #include <stdarg.h>
38 #include <stdlib.h>
39 
40 #include <sqlite3.h>
41 
42 #include "ksm/database.h"
43 #include "ksm/dbsdef.h"
44 #include "ksm/message.h"
45 
46 static sqlite3* m_dbhandle = NULL; /* Non-NULL if connected */
47 
48 
49 
50 /*+
51  * DbConnect - Connect to Database
52  *
53  * Description:
54  * Creates a connection to the specified database using the parameters
55  * supplied. If successful, the handle to the connection is stored
56  * locally, for retrieval by DbHandle().
57  *
58  * Should there be an error, a suitable message is output.
59  *
60  * Arguments:
61  * DB_HANDLE* dbhandle
62  * Address of a location into which the connection handle is put. This
63  * is also stored locally for retrieval by DbHandle(). If this argument
64  * is NULL, no handle is returned through the function call.
65  *
66  * Note that if a handle for an active connection is already stored
67  * locally, this function will overwrite it, regardless of success or
68  * failure.
69  *
70  * const char* database
71  * name of database (NULL to pick up the default).
72  *
73  * ...
74  * Optional arguments.
75  *
76  * These are used for the MySql implementation, sqlite doesn't need them
77  *
78  * Returns:
79  * int
80  * 0 Success
81  * Other Error on connection. The message will have been logged via
82  * the MsgLog() function.
83 -*/
84 
85 int DbConnect(DB_HANDLE* dbhandle, const char* database, ...)
86 {
87  sqlite3* connection = NULL; /* Local database handle */
88  va_list ap; /* Argument pointer */
89  int status = 0; /* Return status */
90 
91  /* Initialize if not already done so */
92 
93  DbInit();
94 
95  /* Get arguments */
96 
97  va_start(ap, database);
98  va_end(ap);
99 
100  /* ... and connect */
101 
102  status = sqlite3_open(database, &connection);
103  /* status = sqlite3_open_v2(database, &connection, SQLITE_OPEN_READWRITE | SQLITE_OPEN_FULLMUTEX, NULL); */
104 
105  if (status) {
106  /* Unable to connect */
107  status = MsgLog(DBS_CONNFAIL, sqlite3_errmsg(connection));
108  }
109 
110  /* Store the returned handle for retrieval by DbHandle() */
111 
112  m_dbhandle = connection;
113 
114  /* ... and pass back to the caller via the argument list */
115 
116  if (dbhandle) {
117  *dbhandle = (DB_HANDLE) connection;
118  }
119 
120  /* Check the version against what we have in database.h */
121  if (status == 0) {
122  status = db_version_check();
123  }
124 
125  return status;
126 }
127 
128 
129 /*+
130  * DbDisconnect - Disconnect from Database
131  *
132  * Description:
133  * Disconnects from the current database. If there is no current database,
134  * this is a no-op.
135  *
136  * Arguments:
137  * DB_HANDLE dbhandle
138  * Pointer to the connection handle. After this function is called,
139  * the handle is invalid.
140  *
141  * If the handle passed to this function is the same as the one stored
142  * locally (and returned by DbHandle()), then the local copy is zeroed.
143  *
144  * Returns:
145  * int
146  * Status return. One of:
147  *
148  * 0 Success
149  * DBS_NOTCONN Not connected to a database
150  * None.
151 -*/
152 
153 int DbDisconnect(DB_HANDLE dbhandle)
154 {
155  int status = 0; /* Return status */
156 
157  if (dbhandle) {
158  if (dbhandle == m_dbhandle) {
159  m_dbhandle = NULL;
160  }
161  sqlite3_close((sqlite3*) dbhandle);
162  }
163  else {
164  status = MsgLog(DBS_NOTCONN);
165  }
166 
167  return status;
168 }
169 
170 
171 
172 /*+
173  * DbConnected - Check if Connected to a Database
174  *
175  * Description:
176  * Interrogates the connection status.
177  *
178  * Arguments:
179  * DB_HANDLE dbhandle
180  * Handle to the connection.
181  *
182  * Returns:
183  * int
184  * true if connected to a database, false otherwise.
185 -*/
186 
187 int DbConnected(DB_HANDLE dbhandle)
188 {
189  return dbhandle != NULL;
190 }
191 
192 
193 
194 /*+
195  * DbCheckConnected - Check If Connected
196  *
197  * Description:
198  * Checks if connected to the database, and if not, outputs an error.
199  *
200  * Arguments:
201  * DB_HANDLE dbhandle
202  * Handle to the connection.
203  *
204  * Returns:
205  * int
206  * 1 if connected, 0 if not.
207 -*/
208 
210 {
211  int connected;
212 
213  connected = DbConnected(dbhandle);
214  if (! connected) {
216  }
217 
218  return connected;
219 }
220 
221 
222 /*+
223  * DbHandle - Return Database Handle
224  *
225  * Description:
226  * Returns the handle to the database (the pointer to the MYSQL
227  * structure).
228  *
229  * Arguments:
230  * None.
231  *
232  * Returns:
233  * DB_HANDLE
234  * Database handle, which is NULL if none is stored.
235 -*/
236 
238 {
239  return (DB_HANDLE) m_dbhandle;
240 }
sqlite3 * DB_HANDLE
Definition: database.h:79
#define DBS_NOTCONN
Definition: dbsdef.h:55
int db_version_check(void)
#define DBS_CONNFAIL
Definition: dbsdef.h:48
int MsgLog(int status,...)
Definition: message.c:338
void DbInit(void)
DB_HANDLE DbHandle(void)
int DbCheckConnected(DB_HANDLE dbhandle)
int DbDisconnect(DB_HANDLE dbhandle)
int DbConnected(DB_HANDLE dbhandle)
#define DBS_NOTCONERR
Definition: dbsdef.h:54
int DbConnect(DB_HANDLE *dbhandle, const char *database,...)