Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Row: New class with iterators (WIP) #363

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ build
example1
*.a

.vs/
.vscode/
/SQLiteCpp.sln
*.ncb
Expand Down
7 changes: 7 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,11 @@ set(SQLITECPP_SRC
${PROJECT_SOURCE_DIR}/src/Column.cpp
${PROJECT_SOURCE_DIR}/src/Database.cpp
${PROJECT_SOURCE_DIR}/src/Exception.cpp
${PROJECT_SOURCE_DIR}/src/Row.cpp
${PROJECT_SOURCE_DIR}/src/Savepoint.cpp
${PROJECT_SOURCE_DIR}/src/Statement.cpp
${PROJECT_SOURCE_DIR}/src/StatementExecutor.cpp
${PROJECT_SOURCE_DIR}/src/StatementPtr.cpp
${PROJECT_SOURCE_DIR}/src/Transaction.cpp
)
source_group(src FILES ${SQLITECPP_SRC})
Expand All @@ -119,8 +122,11 @@ set(SQLITECPP_INC
${PROJECT_SOURCE_DIR}/include/SQLiteCpp/Column.h
${PROJECT_SOURCE_DIR}/include/SQLiteCpp/Database.h
${PROJECT_SOURCE_DIR}/include/SQLiteCpp/Exception.h
${PROJECT_SOURCE_DIR}/include/SQLiteCpp/Row.h
${PROJECT_SOURCE_DIR}/include/SQLiteCpp/Savepoint.h
${PROJECT_SOURCE_DIR}/include/SQLiteCpp/Statement.h
${PROJECT_SOURCE_DIR}/include/SQLiteCpp/StatementExecutor.h
${PROJECT_SOURCE_DIR}/include/SQLiteCpp/StatementPtr.h
${PROJECT_SOURCE_DIR}/include/SQLiteCpp/Transaction.h
${PROJECT_SOURCE_DIR}/include/SQLiteCpp/VariadicBind.h
${PROJECT_SOURCE_DIR}/include/SQLiteCpp/ExecuteMany.h
Expand All @@ -138,6 +144,7 @@ set(SQLITECPP_TESTS
tests/VariadicBind_test.cpp
tests/Exception_test.cpp
tests/ExecuteMany_test.cpp
tests/Iterators.cpp
)
source_group(tests FILES ${SQLITECPP_TESTS})

Expand Down
78 changes: 40 additions & 38 deletions include/SQLiteCpp/Column.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@
*/
#pragma once

#include <SQLiteCpp/Statement.h>
#include <SQLiteCpp/Exception.h>
#include <SQLiteCpp/StatementPtr.h>

#include <ostream>
#include <string>
#include <memory>

// Forward declarations to avoid inclusion of <sqlite3.h> in a header
struct sqlite3_stmt;

namespace SQLite
{
Expand Down Expand Up @@ -53,11 +51,23 @@ class Column
* @param[in] aStmtPtr Shared pointer to the prepared SQLite Statement Object.
* @param[in] aIndex Index of the column in the row of result, starting at 0
*/
explicit Column(const Statement::TStatementPtr& aStmtPtr, int aIndex);
explicit Column(const TStatementPtr& aStatementPtr, int_fast16_t aIndex) noexcept :
mStatementPtr(aStatementPtr),
mIndex(aIndex), mRowIndex(mStatementPtr->mCurrentStep) {}

// default destructor: the finalization will be done by the destructor of the last shared pointer
// default copy constructor and assignment operator are perfectly suited :
// they copy the Statement::Ptr which in turn increments the reference counter.
Column(const Column&) noexcept = default;
Column& operator=(const Column&) noexcept = default;

Column(Column&& aColumn) noexcept = default;
Column& operator=(Column&& aColumn) noexcept = default;

/**
* @return Column index in statement return table.
*/
int_fast16_t getIndex() const noexcept
{
return mIndex;
}

/**
* @brief Return a pointer to the named assigned to this result column (potentially aliased)
Expand All @@ -78,27 +88,27 @@ class Column
#endif

/// Return the integer value of the column.
int32_t getInt() const noexcept;
int32_t getInt() const;
/// Return the 32bits unsigned integer value of the column (note that SQLite3 does not support unsigned 64bits).
uint32_t getUInt() const noexcept;
uint32_t getUInt() const;
/// Return the 64bits integer value of the column (note that SQLite3 does not support unsigned 64bits).
int64_t getInt64() const noexcept;
int64_t getInt64() const;
/// Return the double (64bits float) value of the column
double getDouble() const noexcept;
double getDouble() 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(const char* apDefaultValue = "") const noexcept;
const char* getText(const char* apDefaultValue = "") const;
/**
* @brief Return a pointer to the binary blob value 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 void* getBlob() const noexcept;
const void* getBlob() const;
/**
* @brief Return a std::string for a TEXT or BLOB column.
*
Expand Down Expand Up @@ -151,12 +161,12 @@ class Column
* - size in bytes of the binary blob returned by getBlob()
* - 0 for a NULL value
*/
int getBytes() const noexcept;
int getBytes() const;

/// Alias returning the number of bytes used by the text (or blob) value of the column
int size() const noexcept
int size() const
{
return getBytes ();
return getBytes();
}

/// Inline cast operators to basic types
Expand Down Expand Up @@ -228,8 +238,19 @@ class Column
}

private:
Statement::TStatementPtr mStmtPtr; ///< Shared Pointer to the prepared SQLite Statement Object
int mIndex; ///< Index of the column in the row of result, starting at 0
/**
* @brief Returns pointer to SQLite Statement Object to use with sqlite3 API.
* Checks if SQLite::Column is used with proper statement step.
*
* @throws SQLite::Exception when statement has changed since this object constrution.
*
* @return Raw pointer to SQLite Statement Object
*/
sqlite3_stmt* getStatement() const;

TStatementPtr mStatementPtr; ///< Shared Pointer to the prepared SQLite Statement Object
int_fast16_t mIndex; ///< Index of the column in the row of result, starting at 0
std::size_t mRowIndex; ///< Index of the statement row, starting at 0
};

/**
Expand All @@ -244,24 +265,5 @@ class Column
*/
std::ostream& operator<<(std::ostream& aStream, const Column& aColumn);

#if __cplusplus >= 201402L || (defined(_MSC_VER) && _MSC_VER >= 1900) // c++14: Visual Studio 2015

// Create an instance of T from the first N columns, see declaration in Statement.h for full details
template<typename T, int N>
T Statement::getColumns()
{
checkRow();
checkIndex(N - 1);
return getColumns<T>(std::make_integer_sequence<int, N>{});
}

// Helper function called by getColums<typename T, int N>
template<typename T, const int... Is>
T Statement::getColumns(const std::integer_sequence<int, Is...>)
{
return T{Column(mpPreparedStatement, Is)...};
}

#endif

} // namespace SQLite
1 change: 1 addition & 0 deletions include/SQLiteCpp/Database.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*/
#pragma once

#include <SQLiteCpp/Exception.h>
#include <SQLiteCpp/Column.h>

// c++17: MinGW GCC version > 8
Expand Down
Loading