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