From d72d029aba5d6b7d20ada225be2179c0494d89c9 Mon Sep 17 00:00:00 2001 From: Sebastien Rombauts Date: Sat, 31 Mar 2012 13:36:30 +0200 Subject: [PATCH] Moving function code into a cpp file --- example1.vcproj | 4 + src/SQLiteC++/SQLiteC++.cpp | 142 +++++++++++++++++++++++++++++++++++ src/SQLiteC++/SQLiteC++.h | 146 +++++++++--------------------------- 3 files changed, 181 insertions(+), 111 deletions(-) create mode 100644 src/SQLiteC++/SQLiteC++.cpp diff --git a/example1.vcproj b/example1.vcproj index 8e13611..4219660 100644 --- a/example1.vcproj +++ b/example1.vcproj @@ -185,6 +185,10 @@ Filter="h;hpp;hxx;hm;inl;inc;xsd" UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}" > + + diff --git a/src/SQLiteC++/SQLiteC++.cpp b/src/SQLiteC++/SQLiteC++.cpp new file mode 100644 index 0000000..b1b1be9 --- /dev/null +++ b/src/SQLiteC++/SQLiteC++.cpp @@ -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 + +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::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::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 diff --git a/src/SQLiteC++/SQLiteC++.h b/src/SQLiteC++/SQLiteC++.h index 3fd6082..d3d4acf 100644 --- a/src/SQLiteC++/SQLiteC++.h +++ b/src/SQLiteC++/SQLiteC++.h @@ -1,8 +1,9 @@ /** + * @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) - * + * 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) */ @@ -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; - } - - 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); - } - } + explicit Database(const char* apFilename, const bool abReadOnly = true, const bool abCreate = false); + /** * @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::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::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;