-
Notifications
You must be signed in to change notification settings - Fork 510
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add StatementPtr - better structure for Statement shared_ptr
- Loading branch information
1 parent
4117377
commit b72479a
Showing
12 changed files
with
235 additions
and
149 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,23 +3,22 @@ | |
* @ingroup SQLiteCpp | ||
* @brief Step executor for SQLite prepared Statement Object | ||
* | ||
* Copyright (c) 2015 Shibao HONG ([email protected]) | ||
* Copyright (c) 2015-2021 Sebastien Rombauts ([email protected]) | ||
* Copyright (c) 2012-2021 Sebastien Rombauts ([email protected]) | ||
* | ||
* Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt | ||
* or copy at http://opensource.org/licenses/MIT) | ||
*/ | ||
#pragma once | ||
|
||
#include <SQLiteCpp/StatementPtr.h> | ||
#include <SQLiteCpp/Row.h> | ||
#include <SQLiteCpp/Exception.h> | ||
|
||
#include <iterator> | ||
#include <memory> | ||
#include <string> | ||
#include <map> | ||
|
||
// Forward declaration to avoid inclusion of <sqlite3.h> in a header | ||
struct sqlite3_stmt; | ||
|
||
namespace SQLite | ||
{ | ||
|
@@ -44,24 +43,13 @@ extern const int OK; ///< SQLITE_OK | |
class StatementExecutor | ||
{ | ||
public: | ||
/// Shared pointer to SQLite Prepared Statement Object | ||
using TStatementPtr = std::shared_ptr<sqlite3_stmt>; | ||
|
||
/// Weak pointer to SQLite Prepared Statement Object | ||
using TStatementWeakPtr = std::weak_ptr<sqlite3_stmt>; | ||
|
||
/// Shared pointer to SQLite StatementExecutor | ||
using TRowPtr = std::shared_ptr<StatementExecutor>; | ||
|
||
/// Weak pointer to SQLite StatementExecutor | ||
using TRowWeakPtr = std::weak_ptr<StatementExecutor>; | ||
|
||
/// Type to store columns names and indexes | ||
using TColumnsMap = std::map<std::string, int, std::less<>>; | ||
|
||
StatementExecutor(const StatementExecutor&) = delete; | ||
StatementExecutor(StatementExecutor&&) = default; | ||
StatementExecutor& operator=(const StatementExecutor&) = delete; | ||
|
||
StatementExecutor(StatementExecutor&&) = default; | ||
StatementExecutor& operator=(StatementExecutor&&) = default; | ||
|
||
/// Reset the statement to make it ready for a new execution. Throws an exception on error. | ||
|
@@ -180,8 +168,8 @@ class StatementExecutor | |
using difference_type = std::ptrdiff_t; | ||
|
||
RowIterator() = default; | ||
RowIterator(TStatementWeakPtr apStatement, TRowWeakPtr apRow, uint16_t aID) : | ||
mpStatement(apStatement), mpRow(apRow), mID(aID), mRow(apStatement, aID) {} | ||
RowIterator(TRowWeakPtr apStatement, uint16_t aID) : | ||
mpStatement(apStatement), mID(aID), mRow(apStatement, aID) {} | ||
|
||
reference operator*() const | ||
{ | ||
|
@@ -216,8 +204,7 @@ class StatementExecutor | |
/// Executing next statement step | ||
void advance() noexcept; | ||
|
||
TStatementWeakPtr mpStatement{}; //!< Weak pointer to SQLite Statement Object | ||
TRowWeakPtr mpRow{}; //!< Weak pointer to StatementExecutor Object | ||
TRowWeakPtr mpStatement{}; //!< Weak pointer to prepared Statement Object | ||
uint16_t mID{}; //!< Current row number | ||
|
||
/// Internal row object storage | ||
|
@@ -256,9 +243,9 @@ class StatementExecutor | |
* | ||
* @return raw pointer to Statement Object | ||
*/ | ||
TStatementPtr getStatement() const noexcept | ||
StatementPtr::TStatementPtr getStatement() const noexcept | ||
{ | ||
return mpStatement; | ||
return mpStatement->mpStatement; | ||
} | ||
|
||
/** | ||
|
@@ -277,7 +264,7 @@ class StatementExecutor | |
*/ | ||
TRowWeakPtr getExecutorWeakPtr() const | ||
{ | ||
return mpRowExecutor; | ||
return mpStatement; | ||
} | ||
|
||
//////////////////////////////////////////////////////////////////////////// | ||
|
@@ -291,7 +278,7 @@ class StatementExecutor | |
{ | ||
if (SQLite::OK != aRet) | ||
{ | ||
throw SQLite::Exception(mpSQLite, aRet); | ||
throw SQLite::Exception(mpStatement->mpConnection, aRet); | ||
} | ||
} | ||
|
||
|
@@ -318,18 +305,17 @@ class StatementExecutor | |
} | ||
|
||
private: | ||
/// Create prepared SQLite Statement Object | ||
void prepareStatement(const std::string& aQuery); | ||
|
||
/// Get column number and create map with columns names | ||
void createColumnInfo(); | ||
|
||
sqlite3* mpSQLite{}; //!< Pointer to SQLite Database Connection Handle | ||
TStatementPtr mpStatement{}; //!< Shared Pointer to the prepared SQLite Statement Object | ||
// xD | ||
bool checkReturnCode(int aReturnCode) const; | ||
// xD | ||
bool checkReturnCode(int aReturnCode, int aErrorCode) const; | ||
|
||
/// Shared Pointer to this object. | ||
/// Allows RowIterator to execute next step | ||
TRowPtr mpRowExecutor{}; | ||
TRowPtr mpStatement{}; | ||
|
||
int mColumnCount = 0; //!< Number of columns in the result of the prepared statement | ||
bool mbHasRow = false; //!< true when a row has been fetched with executeStep() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/** | ||
* @file StatementPtr.h | ||
* @ingroup SQLiteCpp | ||
* @brief Pointer for prepared SQLite Statement Object | ||
* | ||
* Copyright (c) 2022 Sebastien Rombauts ([email protected]) | ||
* | ||
* Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt | ||
* or copy at http://opensource.org/licenses/MIT) | ||
*/ | ||
#pragma once | ||
|
||
#include <memory> | ||
#include <string> | ||
|
||
// Forward declaration to avoid inclusion of <sqlite3.h> in a header | ||
struct sqlite3; | ||
struct sqlite3_stmt; | ||
|
||
namespace SQLite | ||
{ | ||
|
||
|
||
/** | ||
* @brief Container for SQLite Statement pointer. | ||
* | ||
* You should never create this object unless you are expanding SQLiteCPP API. | ||
*/ | ||
struct StatementPtr | ||
{ | ||
/** | ||
* @brief Don't create this object unless you are expanding SQLiteCPP API. | ||
* | ||
* @param[in] apSQLite the SQLite Database Connection | ||
* @param[in] aQuery an UTF-8 encoded query string | ||
* | ||
* @throws Exception is thrown in case of error, then the StatementPtr object is NOT constructed. | ||
*/ | ||
StatementPtr(sqlite3* apSQLite, const std::string& aQuery); | ||
|
||
/// Shared pointer to SQLite prepared Statement Object | ||
using TStatementPtr = std::shared_ptr<sqlite3_stmt>; | ||
|
||
sqlite3* const mpConnection; //!< Pointer to SQLite Database Connection Handle | ||
TStatementPtr const mpStatement; //!< Shared Pointer to the prepared SQLite Statement Object | ||
std::size_t mCurrentStep = 0; //!< Current step of prepared Statement Object | ||
|
||
/// Resets SQLite Statement Object | ||
int reset() noexcept; | ||
|
||
/// Execute next step of SQLite Statement Object | ||
int step() noexcept; | ||
|
||
/** | ||
* @brief Returns pointer to prepared SQLite Statement Object. | ||
* Use this ONLY on sqlite3 function! | ||
* | ||
* @return Pointer to SQLite Statement Object | ||
*/ | ||
sqlite3_stmt* getPreparedStatement() const; | ||
|
||
private: | ||
/// Create prepared SQLite Statement Object | ||
TStatementPtr prepareStatement(sqlite3* apConnection, const std::string& aQuery) const; | ||
}; | ||
|
||
|
||
/// Shared pointer to SQLite StatementPtr | ||
using TRowPtr = std::shared_ptr<StatementPtr>; | ||
|
||
/// Weak pointer to SQLite StatementPtr | ||
using TRowWeakPtr = std::weak_ptr<StatementPtr>; | ||
|
||
|
||
} // namespace SQLite |
Oops, something went wrong.