OpenDNSSEC-enforcer  1.4.1
database_connection_lite.c
Go to the documentation of this file.
1 /*
2  * $Id: database_connection_lite.c 7018 2013-02-05 13:59:43Z 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  * DbConnect - Connect to Database
51  *
52  * Description:
53  * Creates a connection to the specified database using the parameters
54  * supplied. If successful, the handle to the connection is stored
55  * locally, for retrieval by DbHandle().
56  *
57  * Should there be an error, a suitable message is output.
58  *
59  * Arguments:
60  * DB_HANDLE* dbhandle
61  * Address of a location into which the connection handle is put. This
62  * is also stored locally for retrieval by DbHandle(). If this argument
63  * is NULL, no handle is returned through the function call.
64  *
65  * Note that if a handle for an active connection is already stored
66  * locally, this function will overwrite it, regardless of success or
67  * failure.
68  *
69  * const char* database
70  * name of database (NULL to pick up the default).
71  *
72  * ...
73  * Optional arguments.
74  *
75  * These are used for the MySql implementation, sqlite doesn't need them
76  *
77  * Returns:
78  * int
79  * 0 Success
80  * Other Error on connection. The message will have been logged via
81  * the MsgLog() function.
82 -*/
83 
84 int DbConnect(DB_HANDLE* dbhandle, const char* database, ...)
85 {
86  sqlite3* connection = NULL; /* Local database handle */
87  va_list ap; /* Argument pointer */
88  int status = 0; /* Return status */
89 
90  /* Initialize if not already done so */
91 
92  DbInit();
93 
94  /* Get arguments */
95 
96  va_start(ap, database);
97  va_end(ap);
98 
99  /* ... and connect */
100 
101  status = sqlite3_open(database, &connection);
102  /* status = sqlite3_open_v2(database, &connection, SQLITE_OPEN_READWRITE | SQLITE_OPEN_FULLMUTEX, NULL); */
103 
104  if (status) {
105  /* Unable to connect */
106  status = MsgLog(DBS_CONNFAIL, sqlite3_errmsg(connection));
107  }
108 
109  /* Store the returned handle for retrieval by DbHandle() */
110 
111  m_dbhandle = connection;
112 
113  /* ... and pass back to the caller via the argument list */
114 
115  if (dbhandle) {
116  *dbhandle = (DB_HANDLE) connection;
117  }
118 
119  /* Check the version against what we have in database.h */
120  if (status == 0) {
121  status = db_version_check();
122  }
123 
124  return status;
125 }
126 
127 
128 /*+
129  * DbDisconnect - Disconnect from Database
130  *
131  * Description:
132  * Disconnects from the current database. If there is no current database,
133  * this is a no-op.
134  *
135  * Arguments:
136  * DB_HANDLE dbhandle
137  * Pointer to the connection handle. After this function is called,
138  * the handle is invalid.
139  *
140  * If the handle passed to this function is the same as the one stored
141  * locally (and returned by DbHandle()), then the local copy is zeroed.
142  *
143  * Returns:
144  * int
145  * Status return. One of:
146  *
147  * 0 Success
148  * DBS_NOTCONN Not connected to a database
149  * None.
150 -*/
151 
152 int DbDisconnect(DB_HANDLE dbhandle)
153 {
154  int status = 0; /* Return status */
155 
156  if (dbhandle) {
157  if (dbhandle == m_dbhandle) {
158  m_dbhandle = NULL;
159  }
160  sqlite3_close((sqlite3*) dbhandle);
161  }
162  else {
163  status = MsgLog(DBS_NOTCONN);
164  }
165 
166  return status;
167 }
168 
169 
170 
171 /*+
172  * DbConnected - Check if Connected to a Database
173  *
174  * Description:
175  * Interrogates the connection status.
176  *
177  * Arguments:
178  * DB_HANDLE dbhandle
179  * Handle to the connection.
180  *
181  * Returns:
182  * int
183  * true if connected to a database, false otherwise.
184 -*/
185 
186 int DbConnected(DB_HANDLE dbhandle)
187 {
188  return dbhandle != NULL;
189 }
190 
191 
192 
193 /*+
194  * DbCheckConnected - Check If Connected
195  *
196  * Description:
197  * Checks if connected to the database, and if not, outputs an error.
198  *
199  * Arguments:
200  * DB_HANDLE dbhandle
201  * Handle to the connection.
202  *
203  * Returns:
204  * int
205  * 1 if connected, 0 if not.
206 -*/
207 
209 {
210  int connected;
211 
212  connected = DbConnected(dbhandle);
213  if (! connected) {
215  }
216 
217  return connected;
218 }
219 
220 
221 /*+
222  * DbHandle - Return Database Handle
223  *
224  * Description:
225  * Returns the handle to the database (the pointer to the MYSQL
226  * structure).
227  *
228  * Arguments:
229  * None.
230  *
231  * Returns:
232  * DB_HANDLE
233  * Database handle, which is NULL if none is stored.
234 -*/
235 
237 {
238  return (DB_HANDLE) m_dbhandle;
239 }