Moving function code into a cpp file

This commit is contained in:
Sebastien Rombauts 2012-03-31 13:36:30 +02:00
parent 30562dcdab
commit d72d029aba
3 changed files with 181 additions and 111 deletions

View File

@ -185,6 +185,10 @@
Filter="h;hpp;hxx;hm;inl;inc;xsd"
UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"
>
<File
RelativePath=".\src\SQLiteC++\SQLiteC++.cpp"
>
</File>
<File
RelativePath=".\src\SQLiteC++\SQLiteC++.h"
>

142
src/SQLiteC++/SQLiteC++.cpp Normal file
View File

@ -0,0 +1,142 @@
/**
* @file SQLiteC++.cpp
* @brief SQLiteC++ is a smart and simple C++ SQLite3 wrapper.
*
* Copyright (c) 2012 Sebastien Rombauts (sebastien dot rombauts at gmail dot com)
*
* Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt
* or copy at http://opensource.org/licenses/MIT)
*/
#include "SQLiteC++.h"
#include <iostream>
namespace SQLite
{
// Open the provided database UTF-8 filename.
Database::Database(const char* apFilename, const bool abReadOnly /*= true*/, const bool abCreate /*= false*/) :
mpSQLite(NULL),
mFilename (apFilename)
{
int flags = abReadOnly?SQLITE_OPEN_READONLY:SQLITE_OPEN_READWRITE;
if (abCreate)
{
flags |= SQLITE_OPEN_CREATE;
}
int ret = sqlite3_open_v2(apFilename, &mpSQLite, flags, NULL);
if (SQLITE_OK != ret)
{
std::string strerr = sqlite3_errmsg(mpSQLite);
sqlite3_close(mpSQLite);
throw std::runtime_error(strerr);
}
}
// Close the SQLite database connection.
Database::~Database(void)
{
// check for undestroyed statements
std::vector<Statement*>::iterator iStatement;
for (iStatement = mStatementList.begin();
iStatement != mStatementList.end();
iStatement++)
{
// TODO (*iStatement)->Finalize(); ?
std::cout << "Unregistered statement!\n";
}
int ret = sqlite3_close(mpSQLite);
if (SQLITE_OK != ret)
{
std::cout << sqlite3_errmsg(mpSQLite);
}
}
// Register a Statement object (a SQLite query)
void Database::registerStatement (Statement& aStatement)
{
mStatementList.push_back (&aStatement);
}
// Unregister a Statement object
void Database::unregisterStatement (Statement& aStatement)
{
std::vector<Statement*>::iterator iStatement;
iStatement = std::find (mStatementList.begin(), mStatementList.end(), &aStatement);
if (mStatementList.end() != iStatement)
{
mStatementList.erase (iStatement);
}
}
}; // namespace SQLite
namespace SQLite
{
// Compile and register the SQL query for the provided SQLite Database Connection
Statement::Statement(Database &aDatabase, const char* apQuery) :
mDatabase(aDatabase),
mQuery(apQuery),
mbDone(false)
{
int ret = sqlite3_prepare_v2(mDatabase.mpSQLite, mQuery.c_str(), mQuery.size(), &mpStmt, NULL);
if (SQLITE_OK != ret)
{
throw std::runtime_error(sqlite3_errmsg(mDatabase.mpSQLite));
}
mDatabase.registerStatement(*this);
}
//Finalize and unregister the SQL query from the SQLite Database Connection.
Statement::~Statement(void)
{
int ret = sqlite3_finalize(mpStmt);
if (SQLITE_OK != ret)
{
std::cout << sqlite3_errmsg(mDatabase.mpSQLite);
}
mDatabase.unregisterStatement(*this);
}
// Reset the statement to make it ready for a new execution
void Statement::reset (void)
{
mbDone = false;
int ret = sqlite3_reset(mpStmt);
if (SQLITE_OK != ret)
{
throw std::runtime_error(sqlite3_errmsg(mDatabase.mpSQLite));
}
}
// Execute a step of the query to fetch one row of results
bool Statement::executeStep (void)
{
bool bOk = false;
if (false == mbDone)
{
int ret = sqlite3_step(mpStmt);
if (SQLITE_ROW == ret)
{
bOk = true;
}
else if (SQLITE_DONE == ret)
{
bOk = true;
mbDone = true;
}
else
{
throw std::runtime_error(sqlite3_errmsg(mDatabase.mpSQLite));
}
}
return bOk;
}
}; // namespace SQLite

View File

@ -1,4 +1,5 @@
/**
* @file SQLiteC++.h
* @brief SQLiteC++ is a smart and simple C++ SQLite3 wrapper.
*
* Copyright (c) 2012 Sebastien Rombauts (sebastien dot rombauts at gmail dot com)
@ -34,66 +35,29 @@ public:
*
* Exception is thrown in case of error, then the Database object is NOT constructed.
*/
explicit Database(const char* apFilename, const bool abReadOnly = true, const bool abCreate = false) :
mpSQLite(NULL),
mFilename (apFilename)
{
int flags = abReadOnly?SQLITE_OPEN_READONLY:SQLITE_OPEN_READWRITE;
if (abCreate)
{
flags |= SQLITE_OPEN_CREATE;
}
explicit Database(const char* apFilename, const bool abReadOnly = true, const bool abCreate = false);
int ret = sqlite3_open_v2(apFilename, &mpSQLite, flags, NULL);
if (SQLITE_OK != ret)
{
std::string strerr = sqlite3_errmsg(mpSQLite);
sqlite3_close(mpSQLite);
throw std::runtime_error(strerr);
}
}
/**
* @brief Close the SQLite database connection.
*
* All SQLite statements must have been finalized before,
* so all Statement objects must have been unregistered.
*/
virtual ~Database(void)
{
// check for undestroyed statements
std::vector<Statement*>::iterator iStatement;
for (iStatement = mStatementList.begin();
iStatement != mStatementList.end();
iStatement++)
{
// TODO (*iStatement)->Finalize(); ?
std::cout << "Unregistered statement!\n";
}
virtual ~Database(void);
int ret = sqlite3_close(mpSQLite);
if (SQLITE_OK != ret)
{
std::cout << sqlite3_errmsg(mpSQLite);
}
}
/**
* @brief Register a Statement object (a SQLite query)
*/
void registerStatement (Statement& aStatement);
/// Register a Statement object (a SQLite query)
void registerStatement (Statement& aStatement)
{
mStatementList.push_back (&aStatement);
}
/// Unregister a Statement object
void unregisterStatement (Statement& aStatement)
{
std::vector<Statement*>::iterator iStatement;
iStatement = std::find (mStatementList.begin(), mStatementList.end(), &aStatement);
if (mStatementList.end() != iStatement)
{
mStatementList.erase (iStatement);
}
}
/**
* @brief Unregister a Statement object
*/
void unregisterStatement (Statement& aStatement);
/// Filename used to open the database
/**
* @brief Filename used to open the database
*/
inline const std::string& getFilename(void) const
{
return mFilename;
@ -120,76 +84,36 @@ public:
*
* Exception is thrown in case of error, then the Statement object is NOT constructed.
*/
explicit Statement(Database &aDatabase, const char* apQuery) :
mDatabase(aDatabase),
mQuery(apQuery),
mbDone(false)
{
int ret = sqlite3_prepare_v2(mDatabase.mpSQLite, mQuery.c_str(), mQuery.size(), &mpStmt, NULL);
if (SQLITE_OK != ret)
{
throw std::runtime_error(sqlite3_errmsg(mDatabase.mpSQLite));
}
mDatabase.registerStatement(*this);
}
explicit Statement(Database &aDatabase, const char* apQuery);
/**
* @brief Finalize and unregister the SQL query from the SQLite Database Connection.
*/
virtual ~Statement(void)
{
int ret = sqlite3_finalize(mpStmt);
if (SQLITE_OK != ret)
{
std::cout << sqlite3_errmsg(mDatabase.mpSQLite);
}
mDatabase.unregisterStatement(*this);
}
virtual ~Statement(void);
/// Reset the statement to make it ready for a new execution
void reset (void)
{
mbDone = false;
int ret = sqlite3_reset(mpStmt);
if (SQLITE_OK != ret)
{
throw std::runtime_error(sqlite3_errmsg(mDatabase.mpSQLite));
}
}
/**
* @brief Reset the statement to make it ready for a new execution.
*/
void reset (void);
// TODO bind
/// Execute a step of the query to fetch one row of results
bool executeStep (void)
{
bool bOk = false;
/**
* @brief Execute a step of the query to fetch one row of results.
*/
bool executeStep (void);
if (false == mbDone)
{
int ret = sqlite3_step(mpStmt);
if (SQLITE_ROW == ret)
{
bOk = true;
}
else if (SQLITE_DONE == ret)
{
bOk = true;
mbDone = true;
}
else
{
throw std::runtime_error(sqlite3_errmsg(mDatabase.mpSQLite));
}
}
return bOk;
}
/// UTF-8 SQL Query
/**
* @brief UTF-8 SQL Query.
*/
inline const std::string& getQuery(void) const
{
return mQuery;
}
/// True when the last row is fetched with executeStep()
/**
* @brief True when the last row is fetched with executeStep().
*/
inline bool isDone(void) const
{
return mbDone;