OpenDNSSEC-enforcer  1.4.8.2
database.h
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 #ifndef KSM_DATABASE_H
28 #define KSM_DATABASE_H
29 
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33 
34 /*+
35  * database.h - Database Functions
36  *
37  * Description:
38  * Holds definitions and prototypes for the database module.
39 -*/
40 
41 #include <stdlib.h>
42 
43 #define KSM_DB_VERSION 4 /* This needs to match that given in the dbadmin table */
44 
45 #define MYSQL_DB 1
46 #define SQLITE_DB 2
47 
48 #ifdef USE_MYSQL
49 
50 #include <mysql.h>
51 
52 typedef MYSQL* DB_HANDLE; /* Connection handle */
53 typedef unsigned long DB_ID; /* Database row identification */
54 
55 struct db_result { /* Result structure */
56  unsigned int magic; /* Identification */
57  int count; /* Field count */
58  DB_HANDLE handle; /* Parent database handle */
59  MYSQL_RES* data; /* Pointer to the result set */
60 };
61 #define DB_RESULT_MAGIC (0x10203044)
62 
63 typedef struct db_result* DB_RESULT; /* Handle to a result set */
64 
65 struct db_row { /* Row structure */
66  unsigned int magic; /* Idenfification */
67  DB_RESULT result; /* Parent result structure */
68  MYSQL_ROW data; /* Actual row of data */
69 };
70 #define DB_ROW_MAGIC (0xbedea133)
71 typedef struct db_row* DB_ROW; /* Handle to the row structure */
72 
73 #else
74 
75 #include <sqlite3.h>
76 
77 typedef sqlite3* DB_HANDLE; /* Connection handle*/
78 typedef unsigned long DB_ID; /* Database row identification */
79 
80 struct db_result { /* Result structure */
81  unsigned int magic; /* Identification */
82  int count; /* Field count */
83  DB_HANDLE handle; /* Parent database handle */
84  sqlite3_stmt* data; /* current result set (or as close to
85  this as sqlite gets) */
86  short first_row; /* Set to 1 when no rows have been fetched */
87 };
88 #define DB_RESULT_MAGIC (0x10203044)
89 
90 typedef struct db_result* DB_RESULT; /* Handle to a result set */
91 
92 /* need to typedef DB_ROW to avoid changing MySQL calls */
93 struct db_row { /* Row structure */
94  unsigned int magic; /* Idenfification */
95  DB_RESULT result; /* Parent result structure */
96 };
97 #define DB_ROW_MAGIC (0xbedea133)
98 typedef struct db_row* DB_ROW;
99 
100 #endif
101 
102 /* Initialization and rundown */
103 
104 void DbInit(void);
105 void DbRundown(void);
106 
107 /* Basic connection to the database */
108 
109 int DbConnect(DB_HANDLE* dbhandle, const char* database, ...);
110 int DbDisconnect(DB_HANDLE dbhandle);
111 int DbConnected(DB_HANDLE dbhandle);
112 int DbCheckConnected(DB_HANDLE dbhandle);
113 
114 DB_HANDLE DbHandle(void);
115 
116 /* Various basic information access functions */
117 
118 int DbExecuteSql(DB_HANDLE handle, const char* stmt_str, DB_RESULT* result);
119 void DbFreeResult(DB_RESULT result);
120 int DbFetchRow(DB_RESULT result, DB_ROW* row);
121 void DbFreeRow(DB_ROW row);
122 int DbString(DB_ROW row, int field_index, char** result);
123 void DbStringFree(char* string);
124 
125 /* Derived information access functions */
126 
127 int DbExecuteSqlNoResult(DB_HANDLE dbhandle, const char* stmt_str);
128 int DbUnsignedLong(DB_ROW row, int field_index, unsigned long* value);
129 int DbInt(DB_ROW row, int field_index, int *value);
130 int DbIntQuery(DB_HANDLE handle, int* value, const char* query);
131 int DbStringBuffer(DB_ROW row, int field_index, char* buffer, size_t buflen);
132 int DbRowId(DB_ROW, DB_ID* id);
133 
134 /* Others */
135 
136 const char* DbErrmsg(DB_HANDLE handle);
137 int DbErrno(DB_HANDLE handle);
138 int DbLastRowId(DB_HANDLE handle, DB_ID* id);
139 
140 /* Transaction stuff */
141 
142 int DbBeginTransaction(void);
143 int DbCommit(void);
144 int DbRollback(void);
145 
146 /* Utility "quote" function */
147 int DbQuoteString(DB_HANDLE handle, const char* in, char* buffer, size_t buflen);
148 
149 /* Create the SQL for a date difference */
150 int DbDateDiff(const char* start, int delta, int sign, char* buffer, size_t buflen);
151 
152 /* What sort of DB are we running */
153 
154 int DbFlavour(void);
155 int db_version_check(void);
156 
157 #ifdef __cplusplus
158 };
159 #endif
160 
161 #endif /* KSM_DATABASE_H */
sqlite3 * DB_HANDLE
Definition: database.h:77
DB_HANDLE DbHandle(void)
short first_row
Definition: database.h:86
int DbFlavour(void)
int DbCommit(void)
DB_HANDLE handle
Definition: database.h:83
int DbBeginTransaction(void)
int DbStringBuffer(DB_ROW row, int field_index, char *buffer, size_t buflen)
unsigned int magic
Definition: database.h:81
sqlite3_stmt * data
Definition: database.h:84
struct db_row * DB_ROW
Definition: database.h:98
void DbInit(void)
int DbExecuteSql(DB_HANDLE handle, const char *stmt_str, DB_RESULT *result)
int DbDisconnect(DB_HANDLE dbhandle)
int DbExecuteSqlNoResult(DB_HANDLE dbhandle, const char *stmt_str)
int db_version_check(void)
struct db_result * DB_RESULT
Definition: database.h:90
int DbConnected(DB_HANDLE dbhandle)
int DbIntQuery(DB_HANDLE handle, int *value, const char *query)
unsigned long DB_ID
Definition: database.h:78
int DbConnect(DB_HANDLE *dbhandle, const char *database,...)
void DbStringFree(char *string)
int DbLastRowId(DB_HANDLE handle, DB_ID *id)
void DbRundown(void)
const char * DbErrmsg(DB_HANDLE handle)
int DbRollback(void)
int DbDateDiff(const char *start, int delta, int sign, char *buffer, size_t buflen)
DB_RESULT result
Definition: database.h:95
int DbQuoteString(DB_HANDLE handle, const char *in, char *buffer, size_t buflen)
void DbFreeResult(DB_RESULT result)
int DbCheckConnected(DB_HANDLE dbhandle)
int DbString(DB_ROW row, int field_index, char **result)
int count
Definition: database.h:82
int DbInt(DB_ROW row, int field_index, int *value)
int DbErrno(DB_HANDLE handle)
int DbRowId(DB_ROW, DB_ID *id)
int DbFetchRow(DB_RESULT result, DB_ROW *row)
unsigned int magic
Definition: database.h:94
void DbFreeRow(DB_ROW row)
int DbUnsignedLong(DB_ROW row, int field_index, unsigned long *value)