From 26f5b85c3a19d42abaa79671509f9b3e4c9c70fc Mon Sep 17 00:00:00 2001 From: Sebastien Rombauts Date: Sat, 31 Mar 2012 13:52:45 +0200 Subject: [PATCH] Clenup : dispatching wrapper in Database.* and Statement.* files --- example1.vcproj | 14 +- src/SQLiteC++/{SQLiteC++.cpp => Database.cpp} | 78 +---------- src/SQLiteC++/Database.h | 74 +++++++++++ src/SQLiteC++/SQLiteC++.h | 121 +----------------- src/SQLiteC++/Statement.cpp | 81 ++++++++++++ src/SQLiteC++/Statement.h | 78 +++++++++++ 6 files changed, 254 insertions(+), 192 deletions(-) rename src/SQLiteC++/{SQLiteC++.cpp => Database.cpp} (51%) create mode 100644 src/SQLiteC++/Database.h create mode 100644 src/SQLiteC++/Statement.cpp create mode 100644 src/SQLiteC++/Statement.h diff --git a/example1.vcproj b/example1.vcproj index 4219660..907d205 100644 --- a/example1.vcproj +++ b/example1.vcproj @@ -186,13 +186,25 @@ UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}" > + + + + + + namespace SQLite @@ -43,7 +45,7 @@ Database::~Database(void) iStatement++) { // TODO (*iStatement)->Finalize(); ? - std::cout << "Unregistered statement!\n"; + std::cout << "Unregistered statement: " << (*iStatement)->getQuery().c_str() << " !\n"; } int ret = sqlite3_close(mpSQLite); @@ -71,72 +73,4 @@ void Database::unregisterStatement (Statement& aStatement) } -}; // 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++/Database.h b/src/SQLiteC++/Database.h new file mode 100644 index 0000000..6b723bf --- /dev/null +++ b/src/SQLiteC++/Database.h @@ -0,0 +1,74 @@ +/** + * @file Database.h + * @brief Management of a SQLite Database Connection. + * + * 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) + */ +#pragma once + +#include +#include +#include +#include + +namespace SQLite +{ + +// Forward declaration +class Statement; + +/** + * @brief Management of a SQLite Database Connection. + * + * A Database object manage a list of all SQLite Statements associated with the + * underlying SQLite 3 database connection. + */ +class Database +{ + friend class Statement; + +public: + /** + * @brief Open the provided database UTF-8 filename. + * + * 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); + + /** + * @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); + + /** + * @brief Register a Statement object (a SQLite query) + */ + void registerStatement (Statement& aStatement); + + /** + * @brief Unregister a Statement object + */ + void unregisterStatement (Statement& aStatement); + + /** + * @brief Filename used to open the database + */ + inline const std::string& getFilename(void) const + { + return mFilename; + } + +private: + sqlite3* mpSQLite; //!< Pointer to SQLite Database Connection Handle + std::string mFilename; //!< UTF-8 filename used to open the database + std::vector mStatementList; //!< Liste of SQL statements used with this database connexion +}; + + +}; // namespace SQLite diff --git a/src/SQLiteC++/SQLiteC++.h b/src/SQLiteC++/SQLiteC++.h index d3d4acf..37c603b 100644 --- a/src/SQLiteC++/SQLiteC++.h +++ b/src/SQLiteC++/SQLiteC++.h @@ -9,122 +9,5 @@ */ #pragma once -#include -#include -#include -#include - -namespace SQLite -{ - -class Statement; - -/** - * @brief Management of a SQLite Database Connection. - * - * A Database object manage a list of all SQLite Statements associated with the - * underlying SQLite 3 database connection. - */ -class Database -{ - friend class Statement; - -public: - /** - * @brief Open the provided database UTF-8 filename. - * - * 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); - - /** - * @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); - - /** - * @brief Register a Statement object (a SQLite query) - */ - void registerStatement (Statement& aStatement); - - /** - * @brief Unregister a Statement object - */ - void unregisterStatement (Statement& aStatement); - - /** - * @brief Filename used to open the database - */ - inline const std::string& getFilename(void) const - { - return mFilename; - } - -private: - sqlite3* mpSQLite; //!< Pointer to SQLite Database Connection Handle - std::string mFilename; //!< UTF-8 filename used to open the database - std::vector mStatementList; //!< Liste of SQL statements used with this database connexion -}; - - -/** - * @brief Encapsulation of a SQLite Statement. - * - * A Statement is a compiled SQL query ready to be executed step by step - * to provide results one row at a time. - */ -class Statement -{ -public: - /** - * @brief Compile and register the SQL query for the provided SQLite Database Connection - * - * Exception is thrown in case of error, then the Statement object is NOT constructed. - */ - explicit Statement(Database &aDatabase, const char* apQuery); - - /** - * @brief Finalize and unregister the SQL query from the SQLite Database Connection. - */ - virtual ~Statement(void); - - /** - * @brief Reset the statement to make it ready for a new execution. - */ - void reset (void); - - // TODO bind - - /** - * @brief Execute a step of the query to fetch one row of results. - */ - bool executeStep (void); - - /** - * @brief UTF-8 SQL Query. - */ - inline const std::string& getQuery(void) const - { - return mQuery; - } - - /** - * @brief True when the last row is fetched with executeStep(). - */ - inline bool isDone(void) const - { - return mbDone; - } - -private: - sqlite3_stmt* mpStmt; //!< Pointeur to SQLite Statement Object - Database& mDatabase; //!< Reference to the SQLite Database Connection - std::string mQuery; //!< UTF-8 SQL Query - bool mbDone; //!< True when the last row is fetched with executeStep() -}; - - -}; // namespace SQLite +#include "Database.h" +#include "Statement.h" diff --git a/src/SQLiteC++/Statement.cpp b/src/SQLiteC++/Statement.cpp new file mode 100644 index 0000000..96e1f24 --- /dev/null +++ b/src/SQLiteC++/Statement.cpp @@ -0,0 +1,81 @@ +/** + * @file Statement.cpp + * @brief A prepared SQLite Statement is a compiled SQL query ready to be executed. + * + * 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 "Statement.h" + +#include "Database.h" +#include + +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++/Statement.h b/src/SQLiteC++/Statement.h new file mode 100644 index 0000000..299e461 --- /dev/null +++ b/src/SQLiteC++/Statement.h @@ -0,0 +1,78 @@ +/** + * @file Statement.h + * @brief A prepared SQLite Statement is a compiled SQL query ready to be executed. + * + * 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) + */ +#pragma once + +#include +#include + +namespace SQLite +{ + +// Forward declaration +class Database; + +/** + * @brief Encapsulation of a prepared SQLite Statement. + * + * A Statement is a compiled SQL query ready to be executed step by step + * to provide results one row at a time. + */ +class Statement +{ +public: + /** + * @brief Compile and register the SQL query for the provided SQLite Database Connection + * + * Exception is thrown in case of error, then the Statement object is NOT constructed. + */ + explicit Statement(Database &aDatabase, const char* apQuery); + + /** + * @brief Finalize and unregister the SQL query from the SQLite Database Connection. + */ + virtual ~Statement(void); + + /** + * @brief Reset the statement to make it ready for a new execution. + */ + void reset (void); + + // TODO bind + + /** + * @brief Execute a step of the query to fetch one row of results. + */ + bool executeStep (void); + + /** + * @brief UTF-8 SQL Query. + */ + inline const std::string& getQuery(void) const + { + return mQuery; + } + + /** + * @brief True when the last row is fetched with executeStep(). + */ + inline bool isDone(void) const + { + return mbDone; + } + +private: + sqlite3_stmt* mpStmt; //!< Pointeur to SQLite Statement Object + Database& mDatabase; //!< Reference to the SQLite Database Connection + std::string mQuery; //!< UTF-8 SQL Query + bool mbDone; //!< True when the last row is fetched with executeStep() +}; + + +}; // namespace SQLite