-
Notifications
You must be signed in to change notification settings - Fork 511
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add RowIterator and Row class fundation
- Loading branch information
1 parent
3fdd78e
commit 81d0fe5
Showing
9 changed files
with
239 additions
and
24 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/** | ||
* @file Row.h | ||
* @ingroup SQLiteCpp | ||
* @brief TODO: | ||
* | ||
* Copyright (c) 2015 Shibao HONG ([email protected]) | ||
* Copyright (c) 2015-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/RowExecutor.h> | ||
|
||
#include <string> | ||
|
||
namespace SQLite | ||
{ | ||
|
||
|
||
class Row | ||
{ | ||
public: | ||
Row(RowExecutor::TStatementWeakPtr apRow, std::size_t aID); | ||
|
||
std::size_t getRowNumber() const | ||
{ | ||
return ID; | ||
} | ||
|
||
/** | ||
* @brief Test if the column value is NULL | ||
* | ||
* @param[in] aIndex Index of the column, starting at 0 | ||
* | ||
* @return true if the column value is NULL | ||
* | ||
* Throw an exception if the specified index is out of the [0, getColumnCount()) range. | ||
*/ | ||
bool isColumnNull(const int aIndex) const; | ||
|
||
/** | ||
* @brief Return a pointer to the text value (NULL terminated string) of the column. | ||
* | ||
* @warning The value pointed at is only valid while the statement is valid (ie. not finalized), | ||
* thus you must copy it before using it beyond its scope (to a std::string for instance). | ||
*/ | ||
const char* getText(uint32_t aColumnID) const noexcept; | ||
|
||
private: | ||
RowExecutor::TStatementWeakPtr mpRow; | ||
std::size_t ID; | ||
}; | ||
|
||
} // namespace SQLite |
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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
/** | ||
* @file RowExecutor.h | ||
* @ingroup SQLiteCpp | ||
* @brief TODO: | ||
* @brief Step executor for SQLite prepared Statement Object | ||
* | ||
* Copyright (c) 2015 Shibao HONG ([email protected]) | ||
* Copyright (c) 2015-2021 Sebastien Rombauts ([email protected]) | ||
|
@@ -130,17 +130,17 @@ class RowExecutor | |
int getChanges() const noexcept; | ||
|
||
/// Return the number of columns in the result set returned by the prepared statement | ||
int getColumnCount() const | ||
int getColumnCount() const noexcept | ||
{ | ||
return mColumnCount; | ||
} | ||
/// true when a row has been fetched with executeStep() | ||
bool hasRow() const | ||
bool hasRow() const noexcept | ||
{ | ||
return mbHasRow; | ||
} | ||
/// true when the last executeStep() had no more row to fetch | ||
bool isDone() const | ||
bool isDone() const noexcept | ||
{ | ||
return mbDone; | ||
} | ||
|
@@ -167,7 +167,10 @@ class RowExecutor | |
* | ||
* @return raw pointer to Statement Object | ||
*/ | ||
TStatementPtr getStatement() const noexcept; | ||
TStatementPtr getStatement() const noexcept | ||
{ | ||
return mpStatement; | ||
} | ||
|
||
/** | ||
* @brief Return a prepared SQLite Statement Object. | ||
|
@@ -176,6 +179,19 @@ class RowExecutor | |
* @return raw pointer to Prepared Statement Object | ||
*/ | ||
sqlite3_stmt* getPreparedStatement() const; | ||
|
||
/** | ||
* @brief Return a prepared SQLite Statement Object. | ||
* | ||
* Throw an exception if the statement object was not prepared. | ||
* @return raw pointer to Prepared Statement Object | ||
*/ | ||
TRowWeakPtr getExecutorWeakPtr() const | ||
{ | ||
return mpRowExecutor; | ||
} | ||
|
||
//////////////////////////////////////////////////////////////////////////// | ||
|
||
/** | ||
* @brief Check if a return code equals SQLITE_OK, else throw a SQLite::Exception with the SQLite error message | ||
|
@@ -222,9 +238,13 @@ class RowExecutor | |
sqlite3* mpSQLite{}; //!< Pointer to SQLite Database Connection Handle | ||
TStatementPtr mpStatement{}; //!< Shared Pointer to the prepared SQLite Statement Object | ||
|
||
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() | ||
bool mbDone{ false }; //!< true when the last executeStep() had no more row to fetch | ||
/// Shared Pointer to this object. | ||
/// Allows RowIterator to execute next step | ||
TRowPtr mpRowExecutor{}; | ||
|
||
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() | ||
bool mbDone = false; //!< true when the last executeStep() had no more row to fetch | ||
|
||
/// Map of columns index by name (mutable so getColumnIndex can be const) | ||
mutable TColumnsMap mColumnNames{}; | ||
|
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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
/** | ||
* @file Statement.h | ||
* @ingroup SQLiteCpp | ||
* @brief A prepared SQLite Statement is a compiled SQL query ready to be executed, pointing to a row of result. | ||
* @brief A prepared SQLite Statement Object binder and Column getter. | ||
* | ||
* Copyright (c) 2012-2021 Sebastien Rombauts ([email protected]) | ||
* | ||
|
@@ -541,8 +541,64 @@ class Statement : public RowExecutor | |
/// Return the number of bind parameters in the statement | ||
int getBindParameterCount() const noexcept; | ||
|
||
//////////////////////////////////////////////////////////////////////////// | ||
|
||
class RowIterator | ||
{ | ||
public: | ||
using iterator_category = std::input_iterator_tag; | ||
using value_type = Row; | ||
using reference = const Row&; | ||
using pointer = const Row*; | ||
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) {} | ||
|
||
reference operator*() const | ||
{ | ||
return mRow; | ||
} | ||
pointer operator->() const noexcept | ||
{ | ||
return &mRow; | ||
} | ||
|
||
reference operator++() noexcept | ||
{ | ||
mRow = Row(mpStatement, ++mID); | ||
advance(); | ||
return mRow; | ||
} | ||
value_type operator++(int) | ||
{ | ||
Row copy{ mRow }; | ||
mRow = Row(mpStatement, ++mID); | ||
advance(); | ||
return copy; | ||
} | ||
|
||
bool operator==(const RowIterator& aIt) const; | ||
bool operator!=(const RowIterator& aIt) const | ||
{ | ||
return !(*this == aIt); | ||
} | ||
|
||
private: | ||
void advance() noexcept; | ||
|
||
TStatementWeakPtr mpStatement{}; | ||
TRowWeakPtr mpRow{}; | ||
uint16_t mID{}; | ||
Row mRow{ mpStatement, mID }; | ||
}; | ||
|
||
RowIterator begin(); | ||
RowIterator end(); | ||
|
||
private: | ||
std::string mQuery; //!< UTF-8 SQL Query | ||
std::string mQuery; //!< UTF-8 SQL Query, | ||
}; | ||
|
||
|
||
|
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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/** | ||
* @file Row.cpp | ||
* @ingroup SQLiteCpp | ||
* @brief TODO: | ||
* | ||
* Copyright (c) 2015 Shibao HONG ([email protected]) | ||
* Copyright (c) 2015-2021 Sebastien Rombauts ([email protected]) | ||
* | ||
* Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt | ||
* or copy at http://opensource.org/licenses/MIT) | ||
*/ | ||
#include <SQLiteCpp/Row.h> | ||
|
||
#include <SQLiteCpp/Exception.h> | ||
|
||
#include <sqlite3.h> | ||
|
||
namespace SQLite | ||
{ | ||
|
||
|
||
Row::Row(RowExecutor::TStatementWeakPtr apRow, std::size_t aID) : | ||
mpRow(apRow), ID(aID) | ||
{ | ||
} | ||
|
||
bool Row::isColumnNull(const int aIndex) const | ||
{ | ||
return false; | ||
} | ||
|
||
const char* Row::getText(uint32_t aColumnID) const noexcept | ||
{ | ||
auto statement = mpRow.lock(); | ||
|
||
|
||
auto pText = reinterpret_cast<const char*>(sqlite3_column_text(statement.get(), aColumnID)); | ||
return (pText ? pText : ""); | ||
} | ||
|
||
} // namespace SQLite |
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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
/** | ||
* @file RowExecutor.cpp | ||
* @ingroup SQLiteCpp | ||
* @brief TODO: | ||
* @brief Step executor for SQLite prepared Statement Object | ||
* | ||
* Copyright (c) 2015 Shibao HONG ([email protected]) | ||
* Copyright (c) 2015-2021 Sebastien Rombauts ([email protected]) | ||
|
@@ -24,6 +24,10 @@ namespace SQLite | |
{ | ||
prepareStatement(aQuery); | ||
createColumnInfo(); | ||
|
||
mpRowExecutor.swap(TRowPtr(this, [](const RowExecutor* const) { | ||
//empty destructor to make shared_ptr without ownership | ||
})); | ||
} | ||
|
||
void SQLite::RowExecutor::prepareStatement(const std::string& aQuery) | ||
|
@@ -165,12 +169,6 @@ namespace SQLite | |
return sqlite3_errmsg(mpSQLite); | ||
} | ||
|
||
// Return std::shared_ptr with SQLite statement object | ||
RowExecutor::TStatementPtr RowExecutor::getStatement() const noexcept | ||
{ | ||
return mpStatement; | ||
} | ||
|
||
// Return prepered SQLite statement object or throw | ||
sqlite3_stmt* RowExecutor::getPreparedStatement() const | ||
{ | ||
|
Oops, something went wrong.