boost_sqlite 1
A sqlite C++ library
Loading...
Searching...
No Matches
blob.hpp
1// Copyright (c) 2022 Klemens D. Morgenstern
2//
3// Distributed under the Boost Software License, Version 1.0. (See accompanying
4// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5#ifndef BOOST_SQLITE_BLOB_HPP
6#define BOOST_SQLITE_BLOB_HPP
7
8#include <boost/sqlite/detail/config.hpp>
9#include <boost/sqlite/memory.hpp>
10#include <boost/sqlite/error.hpp>
11#include <type_traits>
12#include <memory>
13#include <cstring>
14
15BOOST_SQLITE_BEGIN_NAMESPACE
16
17struct connection ;
18
21{
23 const void * data() const {return data_;}
25 std::size_t size() const {return size_;}
27 blob_view(const void * data, std::size_t size) : data_(data), size_(size) {}
28
30 template<typename T>
31 explicit blob_view(const T & value,
32 typename std::enable_if<
33 std::is_pointer<decltype(std::declval<T>().data())>::value
34 && std::is_convertible<decltype(std::declval<T>().size()), std::size_t>::value
35 >::type * = nullptr) : data_(value.data()), size_(value.size()) {}
36
37 inline blob_view(const struct blob & b);
38 private:
39 const void * data_{nullptr};
40 std::size_t size_{0u};
41};
42
44enum class zero_blob : sqlite3_uint64 {};
45
47struct blob
48{
50 void * data() const {return impl_.get();}
52 std::size_t size() const {return size_;}
53
55 explicit blob(blob_view bv)
56 {
57 impl_.reset(sqlite3_malloc(bv.size()));
58 size_ = bv.size();
59 std::memcpy(impl_.get(), bv.data(), size_);
60 }
62 explicit blob(std::size_t n) : impl_(sqlite3_malloc(n)), size_(n) {}
64 void * release() && {return std::move(impl_).release(); }
65 private:
66 unique_ptr<void> impl_;
67 std::size_t size_{0u};
68};
69
70blob_view::blob_view(const blob & b) : data_(b.data()), size_(b.size()) {}
71
74{
76 blob_handle() = default;
77
79 explicit blob_handle(sqlite3_blob * blob) : blob_(blob) {}
80
83 BOOST_SQLITE_DECL
84 void reopen(sqlite3_int64 row_id, system::error_code & ec);
85 BOOST_SQLITE_DECL
86 void reopen(sqlite3_int64 row_id);
88
91 BOOST_SQLITE_DECL
92 void read_at(void *data, int len, int offset, system::error_code &ec);
93 BOOST_SQLITE_DECL
94 void read_at(void *data, int len, int offset);
96
99 BOOST_SQLITE_DECL
100 void write_at(const void *data, int len, int offset, system::error_code &ec);
101 BOOST_SQLITE_DECL
102 void write_at(const void *data, int len, int offset);
104
106 std::size_t size() const {return static_cast<std::size_t>(sqlite3_blob_bytes(blob_.get()));}
107
109 using handle_type = sqlite3_blob*;
111 handle_type handle() { return blob_.get(); }
113 handle_type release() && { return blob_.release(); }
114 private:
115 struct deleter_
116 {
117 void operator()(sqlite3_blob *impl)
118 {
119 sqlite3_blob_close(impl);
120 }
121 };
122 std::unique_ptr<sqlite3_blob, deleter_> blob_;
123};
124
127BOOST_SQLITE_DECL
128blob_handle open_blob(connection & conn,
129 cstring_ref db,
130 cstring_ref table,
131 cstring_ref column,
132 sqlite3_int64 row,
133 bool read_only,
134 system::error_code &ec,
135 error_info &ei);
136
137BOOST_SQLITE_DECL
138blob_handle open_blob(connection & conn,
139 cstring_ref db,
140 cstring_ref table,
141 cstring_ref column,
142 sqlite3_int64 row,
143 bool read_only = false);
145
146
147
148BOOST_SQLITE_END_NAMESPACE
149
150
151#endif //BOOST_SQLITE_BLOB_HPP
zero_blob
Helper type to pass a blob full of zeroes without allocating extra memory.
Definition blob.hpp:44
an object that holds a binary large object. Can be obtained by using blob_handle.
Definition blob.hpp:74
blob_handle()=default
Default constructor.
handle_type release() &&
Release the owned handle.
Definition blob.hpp:113
blob_handle(sqlite3_blob *blob)
Construct from a handle. Takes owner ship.
Definition blob.hpp:79
void read_at(void *data, int len, int offset, system::error_code &ec)
Read data from the blob.
void write_at(const void *data, int len, int offset)
Write data to the blob.
void reopen(sqlite3_int64 row_id)
Reopen on another row.
sqlite3_blob * handle_type
The handle of the blob.
Definition blob.hpp:109
void write_at(const void *data, int len, int offset, system::error_code &ec)
Write data to the blob.
handle_type handle()
Returns the handle of the blob.
Definition blob.hpp:111
std::size_t size() const
The size of the blob.
Definition blob.hpp:106
void read_at(void *data, int len, int offset)
Read data from the blob.
void reopen(sqlite3_int64 row_id, system::error_code &ec)
Reopen on another row.
a view to a binary large object
Definition blob.hpp:21
std::size_t size() const
The size of the data int he blob, in bytes.
Definition blob.hpp:25
const void * data() const
The data in the blob.
Definition blob.hpp:23
blob_view(const void *data, std::size_t size)
Construct a blob.
Definition blob.hpp:27
blob_view(const T &value, typename std::enable_if< std::is_pointer< decltype(std::declval< T >().data())>::value &&std::is_convertible< decltype(std::declval< T >().size()), std::size_t >::value >::type *=nullptr)
Construct a blob from some other blob-like structure.
Definition blob.hpp:31
An object that owns a binary large object.
Definition blob.hpp:48
blob(blob_view bv)
Create a blob from a blob_view.
Definition blob.hpp:55
void * release() &&
Release & take ownership of the blob.
Definition blob.hpp:64
blob(std::size_t n)
Create an empty blob with size n.
Definition blob.hpp:62
std::size_t size() const
The size of the data int he blob, in bytes.
Definition blob.hpp:52
void * data() const
The data in the blob.
Definition blob.hpp:50
A holder for a sqlite values used for internal APIs.
Definition value.hpp:39