OpenDNSSEC-enforcer
1.3.8
|
00001 /* 00002 * $Id: database.h 3776 2010-08-24 14:55:39Z sion $ 00003 * 00004 * Copyright (c) 2008-2009 Nominet UK. All rights reserved. 00005 * 00006 * Redistribution and use in source and binary forms, with or without 00007 * modification, are permitted provided that the following conditions 00008 * are met: 00009 * 1. Redistributions of source code must retain the above copyright 00010 * notice, this list of conditions and the following disclaimer. 00011 * 2. Redistributions in binary form must reproduce the above copyright 00012 * notice, this list of conditions and the following disclaimer in the 00013 * documentation and/or other materials provided with the distribution. 00014 * 00015 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 00016 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 00017 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00018 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 00019 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00020 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 00021 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00022 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 00023 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 00024 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN 00025 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00026 * 00027 */ 00028 00029 #ifndef KSM_DATABASE_H 00030 #define KSM_DATABASE_H 00031 00032 #ifdef __cplusplus 00033 extern "C" { 00034 #endif 00035 00036 /*+ 00037 * database.h - Database Functions 00038 * 00039 * Description: 00040 * Holds definitions and prototypes for the database module. 00041 -*/ 00042 00043 #include <stdlib.h> 00044 00045 #define KSM_DB_VERSION 2 /* This needs to match that given in the dbadmin table */ 00046 00047 #define MYSQL_DB 1 00048 #define SQLITE_DB 2 00049 00050 #ifdef USE_MYSQL 00051 00052 #include <mysql.h> 00053 00054 typedef MYSQL* DB_HANDLE; /* Connection handle */ 00055 typedef unsigned long DB_ID; /* Database row identification */ 00056 00057 struct db_result { /* Result structure */ 00058 unsigned int magic; /* Identification */ 00059 int count; /* Field count */ 00060 DB_HANDLE handle; /* Parent database handle */ 00061 MYSQL_RES* data; /* Pointer to the result set */ 00062 }; 00063 #define DB_RESULT_MAGIC (0x10203044) 00064 00065 typedef struct db_result* DB_RESULT; /* Handle to a result set */ 00066 00067 struct db_row { /* Row structure */ 00068 unsigned int magic; /* Idenfification */ 00069 DB_RESULT result; /* Parent result structure */ 00070 MYSQL_ROW data; /* Actual row of data */ 00071 }; 00072 #define DB_ROW_MAGIC (0xbedea133) 00073 typedef struct db_row* DB_ROW; /* Handle to the row structure */ 00074 00075 #else 00076 00077 #include <sqlite3.h> 00078 00079 typedef sqlite3* DB_HANDLE; /* Connection handle*/ 00080 typedef unsigned long DB_ID; /* Database row identification */ 00081 00082 struct db_result { /* Result structure */ 00083 unsigned int magic; /* Identification */ 00084 int count; /* Field count */ 00085 DB_HANDLE handle; /* Parent database handle */ 00086 sqlite3_stmt* data; /* current result set (or as close to 00087 this as sqlite gets) */ 00088 short first_row; /* Set to 1 when no rows have been fetched */ 00089 }; 00090 #define DB_RESULT_MAGIC (0x10203044) 00091 00092 typedef struct db_result* DB_RESULT; /* Handle to a result set */ 00093 00094 /* need to typedef DB_ROW to avoid changing MySQL calls */ 00095 struct db_row { /* Row structure */ 00096 unsigned int magic; /* Idenfification */ 00097 DB_RESULT result; /* Parent result structure */ 00098 }; 00099 #define DB_ROW_MAGIC (0xbedea133) 00100 typedef struct db_row* DB_ROW; 00101 00102 #endif 00103 00104 /* Initialization and rundown */ 00105 00106 void DbInit(void); 00107 void DbRundown(void); 00108 00109 /* Basic connection to the database */ 00110 00111 int DbConnect(DB_HANDLE* dbhandle, const char* database, ...); 00112 int DbDisconnect(DB_HANDLE dbhandle); 00113 int DbConnected(DB_HANDLE dbhandle); 00114 int DbCheckConnected(DB_HANDLE dbhandle); 00115 00116 DB_HANDLE DbHandle(void); 00117 00118 /* Various basic information access functions */ 00119 00120 int DbExecuteSql(DB_HANDLE handle, const char* stmt_str, DB_RESULT* result); 00121 void DbFreeResult(DB_RESULT result); 00122 int DbFetchRow(DB_RESULT result, DB_ROW* row); 00123 void DbFreeRow(DB_ROW row); 00124 int DbString(DB_ROW row, int field_index, char** result); 00125 void DbStringFree(char* string); 00126 00127 /* Derived information access functions */ 00128 00129 int DbExecuteSqlNoResult(DB_HANDLE dbhandle, const char* stmt_str); 00130 int DbUnsignedLong(DB_ROW row, int field_index, unsigned long* value); 00131 int DbInt(DB_ROW row, int field_index, int *value); 00132 int DbIntQuery(DB_HANDLE handle, int* value, const char* query); 00133 int DbStringBuffer(DB_ROW row, int field_index, char* buffer, size_t buflen); 00134 int DbRowId(DB_ROW, DB_ID* id); 00135 00136 /* Others */ 00137 00138 const char* DbErrmsg(DB_HANDLE handle); 00139 int DbErrno(DB_HANDLE handle); 00140 int DbLastRowId(DB_HANDLE handle, DB_ID* id); 00141 00142 /* Transaction stuff */ 00143 00144 int DbBeginTransaction(void); 00145 int DbCommit(void); 00146 int DbRollback(void); 00147 00148 /* What sort of DB are we running */ 00149 00150 int DbFlavour(void); 00151 int db_version_check(void); 00152 00153 #ifdef __cplusplus 00154 }; 00155 #endif 00156 00157 #endif /* KSM_DATABASE_H */