Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions column.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ const char* column::name() const {
return sqlite3_column_name(stmt.raw(), idx);
}

#if defined(SQLITE_ENABLE_COLUMN_METADATA)

const char* column::database_name() const {
return sqlite3_column_database_name(stmt.raw(), idx);
}
Expand All @@ -26,6 +28,8 @@ const char* column::origin_name() const {
return sqlite3_column_origin_name(stmt.raw(), idx);
}

#endif

int column::type() const {
return sqlite3_column_type(stmt.raw(), idx);
}
Expand Down
2 changes: 2 additions & 0 deletions column.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ class column {
/** Wraps [`sqlite3_column_name()`](http://www.sqlite.org/c3ref/column_name.html) */
const char* name() const;
/** Wraps [`sqlite3_column_database_name()`](http://www.sqlite.org/c3ref/column_database_name.html) */
#if defined(SQLITE_ENABLE_COLUMN_METADATA)
const char* database_name() const;
/** Wraps [`sqlite3_column_table_name()`](http://www.sqlite.org/c3ref/column_database_name.html) */
const char* table_name() const;
/** Wraps [`sqlite3_column_origin_name()`](http://www.sqlite.org/c3ref/column_database_name.html) */
const char* origin_name() const;
#endif

// TODO: Better return type
/** Wraps [`sqlite3_column_type()`](http://www.sqlite.org/c3ref/column_blob.html) */
Expand Down
14 changes: 9 additions & 5 deletions connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ struct collation_data_t {

namespace detail {

class connection_callback_table {
public:
struct connection_callback_table {
std::unique_ptr<connection::commit_handler_t> commit_handler;
std::unique_ptr<connection::rollback_handler_t> rollback_handler;
std::unique_ptr<connection::update_handler_t> update_handler;
Expand Down Expand Up @@ -53,6 +52,11 @@ connection:: connection(const std::string &filename, int flags) : handle(nullptr
open(filename, flags);
}

connection::connection(connection&& other) noexcept
: handle(other.handle), callbacks(std::move(other.callbacks)) {
other.handle = nullptr;
}

connection::~connection() noexcept {
if (handle) {
close();
Expand Down Expand Up @@ -100,7 +104,7 @@ bool connection::autocommit() const {
return sqlite3_get_autocommit(handle);
}

uint64_t connection::last_insert_rowid() const {
int64_t connection::last_insert_rowid() const {
return sqlite3_last_insert_rowid(handle);
}

Expand Down Expand Up @@ -317,12 +321,12 @@ statement connection::prepare(const char *sql) {
int rv;
sqlite3_stmt *stmt = nullptr;

rv = sqlite3_prepare_v2(handle, sql, std::strlen(sql)+1, &stmt, nullptr);
rv = sqlite3_prepare_v2(handle, sql, int(std::strlen(sql)+1), &stmt, nullptr);
if (rv != SQLITE_OK) {
throw static_error(rv);
}

return statement(*this, stmt);
return statement(stmt);
}

statement connection::prepare(const std::string &sql) {
Expand Down
13 changes: 9 additions & 4 deletions connection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class connection {
private:
sqlite3 *handle;

friend class detail::connection_callback_table;
friend struct detail::connection_callback_table;
std::unique_ptr<detail::connection_callback_table> callbacks;

// On-demand initialization of callback table
Expand All @@ -79,8 +79,13 @@ class connection {
// Don't copy, move
connection(const connection&) = delete;
connection& operator=(const connection&) = delete;
connection(connection&&) = default;
connection& operator=(connection&&) = default;

connection(connection&& other) noexcept;
connection& operator=(connection&& other) noexcept {
this->~connection();
return *::new (static_cast<void*>(this)) auto(
std::move(other));
}

/**
* The database file name.
Expand Down Expand Up @@ -132,7 +137,7 @@ class connection {
*/
bool autocommit() const;

uint64_t last_insert_rowid() const;
int64_t last_insert_rowid() const;

/**
* Get the database connection status.
Expand Down
6 changes: 5 additions & 1 deletion context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,17 @@ void context::result(const char *value, bool copy) {

template<>
void context::result(const std::string &value, bool copy) {
sqlite3_result_text(handle, value.c_str(), value.length(), (copy ? SQLITE_TRANSIENT : SQLITE_STATIC));
sqlite3_result_text(handle, value.c_str(), int(value.size()), (copy ? SQLITE_TRANSIENT : SQLITE_STATIC));
}

template<>
void context::result(const blob &value, bool copy) {
if (value.data) {
#if !defined(sqlite3_result_blob)
sqlite3_result_blob(handle, value.data, value.length, (copy ? SQLITE_TRANSIENT : SQLITE_STATIC));
#else
sqlite3_result_blob64(handle, value.data, unsigned(value.length), (copy ? SQLITE_TRANSIENT : SQLITE_STATIC));
#endif
}
else {
sqlite3_result_zeroblob(handle, value.length);
Expand Down
4 changes: 2 additions & 2 deletions context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace sqxx {

// Aggregate function interoperability
namespace detail {
class aggregate_data;
struct aggregate_data;
}

/** Wraps `struct sqlite3_context` */
Expand Down Expand Up @@ -90,7 +90,7 @@ class context {

private:
// This is used internally by our aggregate functions
friend class detail::aggregate_data;
friend struct detail::aggregate_data;
void* aggregate_context(int bytes);

public:
Expand Down
4 changes: 2 additions & 2 deletions global.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,11 @@ bool complete(const std::string &sql) {
return complete(sql.c_str());
}

uint64_t memory_used() {
int64_t memory_used() {
return sqlite3_memory_used();
}

uint64_t memory_highwater(bool reset) {
int64_t memory_highwater(bool reset) {
return sqlite3_memory_highwater(reset);
}

Expand Down
4 changes: 2 additions & 2 deletions global.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,14 @@ bool complete(const std::string &sql);
*
* Wraps [`sqlite3_memory_used()`](http://www.sqlite.org/c3ref/memory_highwater.html)
*/
uint64_t memory_used();
int64_t memory_used();

/**
* Memory allocator statistics
*
* Wraps [`sqlite3_memory_highwater()`](http://www.sqlite.org/c3ref/memory_highwater.html)
*/
uint64_t memory_highwater(bool reset=false);
int64_t memory_highwater(bool reset=false);

/**
* Pseuso-random number generator
Expand Down
18 changes: 11 additions & 7 deletions statement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

namespace sqxx {

statement::statement(connection &conn_arg, sqlite3_stmt *handle_arg)
: handle(handle_arg), conn(conn_arg), completed(true) {
statement::statement(sqlite3_stmt *handle_arg)
: handle(handle_arg), completed(true) {
}

statement::~statement() {
Expand Down Expand Up @@ -115,15 +115,19 @@ void statement::bind<const char*>(int idx, const char *value, bool copy) {

template<>
void statement::bind<std::string>(int idx, const std::string &value, bool copy) {
int rv = sqlite3_bind_text(handle, idx+1, value.c_str(), value.length(), (copy ? SQLITE_TRANSIENT : SQLITE_STATIC));
int rv = sqlite3_bind_text(handle, idx+1, value.c_str(), int(value.size()), (copy ? SQLITE_TRANSIENT : SQLITE_STATIC));
if (rv != SQLITE_OK)
throw static_error(rv);
}

template<>
void statement::bind<blob>(int idx, const blob &value, bool copy) {
if (value.data) {
#if !defined(sqlite3_bind_blob)
int rv = sqlite3_bind_blob(handle, idx+1, value.data, value.length, (copy ? SQLITE_TRANSIENT : SQLITE_STATIC));
#else
int rv = sqlite3_bind_blob64(handle, idx+1, value.data, unsigned(value.length), (copy ? SQLITE_TRANSIENT : SQLITE_STATIC));
#endif
if (rv != SQLITE_OK)
throw static_error(rv);
}
Expand Down Expand Up @@ -168,16 +172,16 @@ template<>
std::string statement::val<std::string>(int idx) const {
// Correct order to call functions according to http://www.sqlite.org/c3ref/column_blob.html
const unsigned char* text = sqlite3_column_text(handle, idx);
int bytes = sqlite3_column_bytes(handle, idx);
return std::string(reinterpret_cast<const char*>(text), bytes);
auto bytes = sqlite3_column_bytes(handle, idx);
return std::string(reinterpret_cast<const char*>(text), size_t(bytes));
}

template<>
blob statement::val<blob>(int idx) const {
// Correct order to call functions according to http://www.sqlite.org/c3ref/column_blob.html
const void *data = sqlite3_column_blob(handle, idx);
int bytes = sqlite3_column_bytes(handle, idx);
return blob(data, bytes);
auto bytes = sqlite3_column_bytes(handle, idx);
return blob(data, int(bytes));
}


Expand Down
32 changes: 19 additions & 13 deletions statement.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,24 @@ class statement {
private:
sqlite3_stmt *handle;

public:
connection &conn;

protected:
bool completed;

public:
statement() noexcept : handle(nullptr), completed(false) {
}

/**
* Constructs a statement object from a connection object and a C API
* statement handle.
* Constructs a statement object from a C API statement handle.
*
* Usually not called directly, use `connection::prepare()` instead.
*
* @conn_arg The connection object associated with the statement. A reference
* is stored and the connection object must stay alive until the constructed
* `statement` is destroyed.
* The connection object associated with the statement must stay alive
* until the `statement` is destroyed.
*
* @handle_arg A C API statement handle. The constructed `statement` takes
* ownership of the C API handle and will close it on destruction.
*/
statement(connection &conn_arg, sqlite3_stmt *handle_arg);
explicit statement(sqlite3_stmt *handle_arg);
/*
* Destroys the object, closing the managed C API handle, if necessary.
*/
Expand All @@ -61,10 +58,19 @@ class statement {
statement(const statement &) = delete;
/** Copy assignment is disabled */
statement& operator=(const statement&) = delete;

/** Move construction is enabled */
statement(statement &&) = default;
statement(statement&& other) noexcept
: handle(other.handle), completed(other.completed) {
other.handle = nullptr;
}

/** Move assignment is enabled */
statement& operator=(statement&&) = default;
statement& operator=(statement&& other) noexcept {
this->~statement();
return *::new (static_cast<void*>(this)) auto(
std::move(other));
}


/**
Expand Down Expand Up @@ -318,7 +324,7 @@ class statement {

/** Check if all result rows have been processed */
bool done() const { return completed; }
operator bool() const { return !completed; }
explicit operator bool() const { return !completed; }

class row_iterator : public std::iterator<std::input_iterator_tag, size_t> {
private:
Expand Down
8 changes: 4 additions & 4 deletions value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,16 @@ template<>
std::string value::val<std::string>() const {
// Correct order to call functions according to http://www.sqlite.org/c3ref/column_blob.html
const unsigned char *text = sqlite3_value_text(handle);
int bytes = sqlite3_value_bytes(handle);
return std::string(reinterpret_cast<const char*>(text), bytes);
auto bytes = sqlite3_value_bytes(handle);
return std::string(reinterpret_cast<const char*>(text), size_t(bytes));
}

template<>
blob value::val<blob>() const {
// Correct order to call functions according to http://www.sqlite.org/c3ref/column_blob.html
const void *data = sqlite3_value_blob(handle);
int len = sqlite3_value_bytes(handle);
return blob(data, len);
auto len = sqlite3_value_bytes(handle);
return blob(data, int(len));
}

value::operator int() const { return val<int>(); }
Expand Down