ThinSQLite++
A thin, safe and convenient modern C++ wrapper for the SQLite API.
Loading...
Searching...
No Matches
blob_iface.hpp
1/*
2 Copyright 2024 Eugene Gershnik
3
4 Use of this source code is governed by a BSD-style
5 license that can be found in the LICENSE file or at
6 https://github.com/gershnik/thinsqlitepp/blob/master/LICENSE
7*/
8
9#ifndef HEADER_SQLITEPP_BLOB_IFACE_INCLUDED
10#define HEADER_SQLITEPP_BLOB_IFACE_INCLUDED
11
12#include "handle.hpp"
13#include "exception_iface.hpp"
14#include "string_param.hpp"
15#include "span.hpp"
16
17namespace thinsqlitepp
18{
19
24
36 SQLITEPP_EXPORTED
37 class blob final : public handle<sqlite3_blob, blob>
38 {
39 public:
41 ~blob() noexcept
43
44
50 void reopen(int64_t rowid)
51 {
52 int res = sqlite3_blob_reopen(c_ptr(), rowid);
53 if (res != SQLITE_OK)
54 throw exception(res); //we do not know the db here, unfortunately
55 }
56
62 size_t bytes() const noexcept
63 {
64 int ret = sqlite3_blob_bytes(c_ptr());
65 return ret >= 0 ? size_t(ret) : 0;
66 }
67
73 void read(size_t offset, span<std::byte> dest) const
74 {
75 int res = sqlite3_blob_read(c_ptr(), dest.data(), int_size(dest.size()), int_size(offset));
76 if (res != SQLITE_OK)
77 throw exception(res); //we do not know the db here, unfortunately
78 }
79
80 #if __cpp_lib_ranges >= 201911L
81
90 template<std::ranges::contiguous_range R>
93 void read(size_t offset, R & range) const
94 {
96 auto data = std::data(range);
97 auto size = std::size(range);
98 if (size > std::numeric_limits<int>::max() / sizeof(value_type))
99 throw exception(SQLITE_TOOBIG);
100 int res = sqlite3_blob_read(c_ptr(), data, int(size * sizeof(value_type)), int_size(offset));
101 if (res != SQLITE_OK)
102 throw exception(res); //we do not know the db here, unfortunately
103 }
104
105 #endif
106
115 void write(size_t offset, span<const std::byte> src)
116 {
117 int res = sqlite3_blob_write(c_ptr(), src.data(), int_size(src.size()), int_size(offset));
118 if (res != SQLITE_OK)
119 throw exception(res); //we do not know the db here, unfortunately
120 }
121
123 void write(size_t offset, span<std::byte> src)
124 { write(offset, span<const std::byte>(src)); }
125
126 #if __cpp_lib_ranges >= 201911L
127
139 template<std::ranges::contiguous_range R>
141 void write(size_t offset, const R & range)
142 {
144 auto data = std::data(range);
145 auto size = std::size(range);
146 if (size > std::numeric_limits<int>::max() / sizeof(value_type))
147 throw exception(SQLITE_TOOBIG);
148 int res = sqlite3_blob_write(c_ptr(), data, int(size * sizeof(value_type)), int_size(offset));
149 if (res != SQLITE_OK)
150 throw exception(res); //we do not know the db here, unfortunately
151 }
152
153 #endif
154 };
155
156
158}
159
160
161#endif
sqlite3_blob_bytes
sqlite3_blob_close
sqlite3_blob_read
sqlite3_blob_reopen
sqlite3_blob_write
Access blob as a byte stream.
Definition blob_iface.hpp:38
void read(size_t offset, R &range) const
Read data from the blob.
Definition blob_iface.hpp:93
void reopen(int64_t rowid)
Move the object to a new row.
Definition blob_iface.hpp:50
void write(size_t offset, const R &range)
Write data to the blob.
Definition blob_iface.hpp:141
~blob() noexcept
Equivalent to sqlite3_blob_close.
Definition blob_iface.hpp:41
void write(size_t offset, span< const std::byte > src)
Write data to the blob.
Definition blob_iface.hpp:115
void write(size_t offset, span< std::byte > src)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition blob_iface.hpp:123
void read(size_t offset, span< std::byte > dest) const
Read data from the blob.
Definition blob_iface.hpp:73
size_t bytes() const noexcept
Returns the size of the blob.
Definition blob_iface.hpp:62
Exception used to report any SQLite errors.
Definition exception_iface.hpp:180
sqlite3_blob * c_ptr() const noexcept
Definition handle.hpp:45
T data(T... args)
std::span< T > span
Alias or reimplementation of std::span.
Definition span.hpp:36
T is_const_v
T is_trivially_copyable_v
T max(T... args)
ThinSQLite++ namespace.
Definition backup_iface.hpp:17
#define SQLITE_OK
T size(T... args)