Skip to content

Commit

Permalink
Add assert and tweak comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
yangacer committed Jun 20, 2019
1 parent bd105f1 commit d5f2003
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 24 deletions.
10 changes: 8 additions & 2 deletions sqlite3cpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,15 @@ row_iter &row_iter::operator++() {
return *this;
}

row const &row_iter::operator*() const noexcept { return m_row; }
row const &row_iter::operator*() const noexcept {
assert(is_valid());
return m_row;
}

row const *row_iter::operator->() const noexcept { return &m_row; }
row const *row_iter::operator->() const noexcept {
assert(is_valid());
return &m_row;
}

bool row_iter::operator==(row_iter const &i) const noexcept {
return m_session.lock() == i.m_session.lock();
Expand Down
35 changes: 19 additions & 16 deletions sqlite3cpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ struct SQLITE3CPP_EXPORT row_iter {
row const &operator*() const noexcept;
row const *operator->() const noexcept;
bool is_valid() const noexcept;

private:
friend struct cursor;
row_iter() noexcept {}
Expand All @@ -140,9 +141,9 @@ struct SQLITE3CPP_EXPORT cursor {
// cursor csr = db.make_cursor();
// csr.execute("insert into MyTable values(?)", 123);
//
// Binded text types, i.e. string, string_view, char const*, are referenced by
// row_iter and cursor. They need to be valid until next call to |execute()|,
// cursor reaches end of life.
// Binded text types, i.e. string, string_view, and char const*, are
// referenced by row_iter and cursor. They need to be valid until next call to
// |execute()| or cursor reaches end of life.
//
// Only poisitioned binding is supported currently. Named
// binding is not supported yet.
Expand All @@ -152,8 +153,9 @@ struct SQLITE3CPP_EXPORT cursor {
// Execute multiple SQL statements.
cursor &executescript(std::string const &sql);

// Row iterator to begin of **reamin** query results. This row_iter becomes
// invalid after the cursor it referenced has been detroyed.
// Row iterator to begin query results. This row_iter becomes
// invalid after the cursor it referenced has been detroyed or another
// |begin()| has been called.
row_iter begin() noexcept;

// Row itertor to end of query results (next to the last one of result).
Expand Down Expand Up @@ -200,18 +202,15 @@ struct SQLITE3CPP_EXPORT transaction {

// Issue `commit;` or `end;` when this transaction instance being destroyed.
void commit() noexcept;

private:
database &m_db;
params_t m_params;
};

struct SQLITE3CPP_EXPORT database {
using xfunc_t = std::function<void(sqlite3_context *, int, sqlite3_value **)>;
using xfinal_t = std::function<void(sqlite3_context *)>;
using xreset_t = std::function<void()>;

// Create a database connection to |urn|. |urn| could be `:memory:` or a filename.
// |urn| should be encoded in UTF-8.
// Create a database connection to |urn|. |urn| could be `:memory:` or a
// filename. |urn| should be encoded in UTF-8.
database(std::string const &urn);

// Create a cursor per current database for executing SQL statements.
Expand Down Expand Up @@ -263,11 +262,15 @@ struct SQLITE3CPP_EXPORT database {
std::string version() const;

private:
struct aggregate_wrapper_t {
xfunc_t step;
xfinal_t fin;
xreset_t reset;
xreset_t release;
using xfunc_t = std::function<void(sqlite3_context *, int, sqlite3_value **)>;
using xfinal_t = std::function<void(sqlite3_context *)>;
using xreset_t = std::function<void()>;

struct aggregate_wrapper_t {
xfunc_t step;
xfinal_t fin;
xreset_t reset;
xreset_t release;
};

static void forward(sqlite3_context *ctx, int argc, sqlite3_value **argv);
Expand Down
11 changes: 5 additions & 6 deletions sqlite3cpp.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ inline int bind_val(sqlite3_stmt *stmt, int index, std::string const &val) {
return sqlite3_bind_text(stmt, index, val.c_str(), val.size(), SQLITE_STATIC);
}

inline int bind_val(sqlite3_stmt *stmt, int index, std::string_view const &val) {
inline int bind_val(sqlite3_stmt *stmt, int index, std::string_view val) {
return sqlite3_bind_text(stmt, index, val.data(), val.size(), SQLITE_STATIC);
}

Expand All @@ -172,7 +172,8 @@ inline int bind_val(sqlite3_stmt *stmt, int index, std::nullptr_t _) {
template <typename T, typename... Args>
void bind_to_stmt(sqlite3_stmt *stmt, int index, T &&val, Args &&... args) {
int ec = 0;
if (0 != (ec = bind_val(stmt, index, std::forward<T>(val)))) throw error(ec);
if (0 != (ec = bind_val(stmt, index, std::forward<T>(val))))
throw error(ec);
bind_to_stmt(stmt, index + 1, std::forward<Args>(args)...);
}

Expand Down Expand Up @@ -241,7 +242,7 @@ R invoke(std::function<R(Args...)> func, int argc, sqlite3_value **argv) {
}

template <typename R, typename... Args>
database::xfunc_t make_invoker(std::function<R(Args...)> &&func) {
auto make_invoker(std::function<R(Args...)> &&func) {
if constexpr (std::is_void_v<R>) {
return [func](sqlite3_context *ctx, int argc, sqlite3_value **argv) {
invoke(func, argc, argv);
Expand Down Expand Up @@ -305,7 +306,6 @@ namespace sqlite3cpp {
*/
template <typename... Cols>
std::tuple<Cols...> row::to() const {
// TODO: Report errors
std::tuple<Cols...> result;
detail::enumerate(
[this](int index, auto &&tuple_value) {
Expand All @@ -320,8 +320,7 @@ std::tuple<Cols...> row::to() const {
*/
template <typename... Args>
cursor &cursor::execute(std::string const &sql, Args &&... args) {
// TODO: Support rebind params (which means step() error for not binded params
// should be ignored.
// TODO: Support rebind params
sqlite3_stmt *stmt = 0;
int ec = 0;

Expand Down

0 comments on commit d5f2003

Please sign in to comment.