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