Skip to content

Commit

Permalink
Added conversion from column_value to span<byte>
Browse files Browse the repository at this point in the history
  • Loading branch information
snej committed May 15, 2024
1 parent 7ca844e commit d889411
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
13 changes: 6 additions & 7 deletions include/sqnice/query.hh
Original file line number Diff line number Diff line change
Expand Up @@ -450,12 +450,6 @@ namespace sqnice {
/** Represents a single column of a query row; returned by `query::row[]`. */
class column_value : sqnice::noncopyable {
public:
/// Gets the value as type `T`.
template <typename T> T get() const noexcept;

/// Implicit conversion to type `T`, for assignment or passing as a parameter.
template <typename T> operator T() const noexcept {return get<T>();} // NOLINT(*-explicit-constructor)

/// The data type of the column value.
data_type type() const noexcept;
bool not_null() const noexcept {return type() != data_type::null;}
Expand All @@ -464,7 +458,10 @@ namespace sqnice {
/// The length in bytes of a text or blob value.
size_t size_bytes() const noexcept;

// The following are just the specializations of get<T>() ...
/// Implicit conversion to type `T`, for assignment or passing as a parameter.
template <typename T> operator T() const noexcept {return get<T>();} // NOLINT(*-explicit-constructor)

// `get()` returns the value as any of various types, depending on what it's assigned to.

template <std::signed_integral T>
T get() const noexcept {
Expand Down Expand Up @@ -492,8 +489,10 @@ namespace sqnice {
template<std::same_as<std::string> T> T get() const noexcept {
return std::string(get<std::string_view>());
}
template<std::same_as<std::span<const std::byte>> T> T get() const noexcept;
template<std::same_as<const void*> T> T get() const noexcept;
template<std::same_as<blob> T> T get() const noexcept;

template<std::same_as<null_type> T> T get() const noexcept {return ignore;}

template <typename U>
Expand Down
14 changes: 11 additions & 3 deletions src/query.cc
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ namespace sqnice {
statement::statement(statement const& other) noexcept
:checking(other)
,impl_(other.impl_)
{
{
clear_bindings();
}

Expand Down Expand Up @@ -263,7 +263,7 @@ namespace sqnice {
int rc;
if (value.data)
rc = sqlite3_bind_blob64(stmt(), idx, value.data, value.size,
copy ? SQLITE_TRANSIENT : SQLITE_STATIC);
copy ? SQLITE_TRANSIENT : SQLITE_STATIC);
else
rc = sqlite3_bind_zeroblob64(stmt(), idx, value.size);
return check_bind(rc, idx);
Expand Down Expand Up @@ -318,7 +318,7 @@ namespace sqnice {
return rc;
}


#pragma mark - QUERY:


Expand Down Expand Up @@ -398,6 +398,14 @@ namespace sqnice {
return sqlite3_column_blob(stmt_, idx_);
}

template<> span<const std::byte> column_value::get() const noexcept {
// It's important to make the calls in this order,
// so we get the size of the blob value, not the string value.
auto data = sqlite3_column_blob(stmt_, idx_);
auto size = sqlite3_column_bytes(stmt_, idx_);
return {(const byte*)data, size_t(size)};
}

template<> blob column_value::get() const noexcept {
// It's important to make the calls in this order,
// so we get the size of the blob value, not the string value.
Expand Down

0 comments on commit d889411

Please sign in to comment.