#ifndef __SQLITEHELPER_H
#define __SQLITEHELPER_H
#include <sqlite3.h>
#include <stdint.h>
#include <DirectoryServiceCore/DSSemaphore.h>
#include <DirectoryServiceCore/DSMutexSemaphore.h>
typedef enum
{
kSQLTypeDouble = 1,
kSQLTypeInt,
kSQLTypeInt64,
kSQLTypeText,
kSQLTypeDone = 99
} SQLValueType;
class SQLiteHelper
{
public:
SQLiteHelper( const char *inDatabasePath, uint32_t inExpectedVersion );
~SQLiteHelper( void );
bool OpenDatabase( bool inIntegrityCheck = false );
void CreateDatabase( void );
void CloseDatabase( void );
void RemoveDatabase( void );
#if defined(DEBUG_LOCKS)
#define LockDatabase() LockDatabaseDebug( __FILE__, __LINE__ )
#define UnlockDatabase() UnlockDatabaseDebug( __FILE__, __LINE__ )
inline void LockDatabaseDebug( const char *file, int line ) { fMutex.WaitDebug(file, line); }
inline void UnlockDatabaseDebug( const char *file, int line ) { fMutex.SignalDebug(file, line); }
#elif defined(DEBUG_LOCKS_HISTORY)
#define LockDatabase() LockDatabaseDebug( __FILE__, __LINE__ )
#define UnlockDatabase() UnlockDatabaseDebug( __FILE__, __LINE__ )
inline void LockDatabaseDebug( const char *file, int line ) { fMutex.WaitDebugHistory(file, line); }
inline void UnlockDatabaseDebug( const char *file, int line ) { fMutex.SignalDebugHistory(file, line); }
#else
inline void LockDatabase( void ) { fMutex.WaitLock(); }
inline void UnlockDatabase( void ) { fMutex.SignalLock(); }
#endif
int ExecSync( const char *command, int length = -1 );
int ExecSyncWithTypes( const char *command, int length, ... );
int Prepare( const char *command, int length, sqlite3_stmt **stmt, const char **pzTail = NULL );
int Step( sqlite3_stmt *inStmt );
int Finalize( sqlite3_stmt *&inStmt );
bool BeginTransaction( const char *inName = NULL );
void EndTransaction( const char *inName = NULL );
void RollbackTransaction( const char *inName = NULL );
bool IsNewDatabase( void ) { return fNewDatabase; }
private:
DSMutexSemaphore fMutex;
sqlite3 *fDatabase;
char *fDatabasePath;
uint32_t fVersion;
bool fNewDatabase;
private:
bool IntegrityCheck( void );
bool IsDatabaseVersionCurrent( void );
};
#endif