mirror of
https://github.com/cuberite/SQLiteCpp.git
synced 2025-08-04 09:46:02 -04:00
Clenup : dispatching wrapper in Database.* and Statement.* files
This commit is contained in:
parent
d72d029aba
commit
26f5b85c3a
@ -186,13 +186,25 @@
|
||||
UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"
|
||||
>
|
||||
<File
|
||||
RelativePath=".\src\SQLiteC++\SQLiteC++.cpp"
|
||||
RelativePath=".\src\SQLiteC++\Database.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\src\SQLiteC++\Database.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\src\SQLiteC++\SQLiteC++.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\src\SQLiteC++\Statement.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\src\SQLiteC++\Statement.h"
|
||||
>
|
||||
</File>
|
||||
</Filter>
|
||||
<Filter
|
||||
Name="example1"
|
||||
|
@ -1,13 +1,15 @@
|
||||
/**
|
||||
* @file SQLiteC++.cpp
|
||||
* @brief SQLiteC++ is a smart and simple C++ SQLite3 wrapper.
|
||||
* @file Database.cpp
|
||||
* @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)
|
||||
*/
|
||||
#include "SQLiteC++.h"
|
||||
#include "Database.h"
|
||||
|
||||
#include "Statement.h"
|
||||
#include <iostream>
|
||||
|
||||
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
|
74
src/SQLiteC++/Database.h
Normal file
74
src/SQLiteC++/Database.h
Normal file
@ -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 <sqlite3.h>
|
||||
#include <stdexcept>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
|
||||
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<Statement*> mStatementList; //!< Liste of SQL statements used with this database connexion
|
||||
};
|
||||
|
||||
|
||||
}; // namespace SQLite
|
@ -9,122 +9,5 @@
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <sqlite3.h>
|
||||
#include <stdexcept>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
|
||||
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<Statement*> 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"
|
||||
|
81
src/SQLiteC++/Statement.cpp
Normal file
81
src/SQLiteC++/Statement.cpp
Normal file
@ -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 <iostream>
|
||||
|
||||
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
|
78
src/SQLiteC++/Statement.h
Normal file
78
src/SQLiteC++/Statement.h
Normal file
@ -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 <sqlite3.h>
|
||||
#include <stdexcept>
|
||||
|
||||
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
|
Loading…
x
Reference in New Issue
Block a user