ThinSQLite++
A thin, safe and convenient modern C++ wrapper for 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/main/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
36 class blob final : public handle<sqlite3_blob, blob>
37 {
38 public:
40 ~blob() noexcept
42
43
49 void reopen(int64_t rowid)
50 {
51 int res = sqlite3_blob_reopen(c_ptr(), rowid);
52 if (res != SQLITE_OK)
53 throw exception(res); //we do not know the db here, unfortunately
54 }
55
61 size_t bytes() const noexcept
62 {
63 int ret = sqlite3_blob_bytes(c_ptr());
64 return ret >= 0 ? ret : 0;
65 }
66
72 void read(size_t offset, span<std::byte> dest) const
73 {
74 int res = sqlite3_blob_read(c_ptr(), dest.data(), int_size(dest.size()), int_size(offset));
75 if (res != SQLITE_OK)
76 throw exception(res); //we do not know the db here, unfortunately
77 }
78
79 #if __cpp_lib_ranges >= 201911L
80
89 template<std::ranges::contiguous_range R>
92 void read(size_t offset, R & range) const
93 {
95 auto data = std::data(range);
96 auto size = std::size(range);
97 if (size > std::numeric_limits<int>::max() / sizeof(value_type))
98 throw exception(SQLITE_TOOBIG);
99 int res = sqlite3_blob_read(c_ptr(), data, int(size * sizeof(value_type)), int_size(offset));
100 if (res != SQLITE_OK)
101 throw exception(res); //we do not know the db here, unfortunately
102 }
103
104 #endif
105
114 void write(size_t offset, span<const std::byte> src)
115 {
116 int res = sqlite3_blob_write(c_ptr(), src.data(), int_size(src.size()), int_size(offset));
117 if (res != SQLITE_OK)
118 throw exception(res); //we do not know the db here, unfortunately
119 }
120
122 void write(size_t offset, span<std::byte> src)
123 { write(offset, span<const std::byte>(src)); }
124
125 #if __cpp_lib_ranges >= 201911L
126
138 template<std::ranges::contiguous_range R>
140 void write(size_t offset, R range) const
141 {
143 auto data = std::data(range);
144 auto size = std::size(range);
145 if (size > std::numeric_limits<int>::max() / sizeof(value_type))
146 throw exception(SQLITE_TOOBIG);
147 int res = sqlite3_blob_write(c_ptr(), data, int(size * sizeof(value_type)), int_size(offset));
148 if (res != SQLITE_OK)
149 throw exception(res); //we do not know the db here, unfortunately
150 }
151
152 #endif
153 };
154
155
157}
158
159
160#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:37
void read(size_t offset, R &range) const
Read data from the blob.
Definition blob_iface.hpp:92
void reopen(int64_t rowid)
Move the object to a new row.
Definition blob_iface.hpp:49
void write(size_t offset, R range) const
Write data to the blob.
Definition blob_iface.hpp:140
~blob() noexcept
Equivalent to sqlite3_blob_close.
Definition blob_iface.hpp:40
void write(size_t offset, span< const std::byte > src)
Write data to the blob.
Definition blob_iface.hpp:114
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:122
void read(size_t offset, span< std::byte > dest) const
Read data from the blob.
Definition blob_iface.hpp:72
size_t bytes() const noexcept
Returns the size of the blob.
Definition blob_iface.hpp:61
Exception used to report any SQLite errors.
Definition exception_iface.hpp:166
Base functionality for all fake wrapper classes
Definition handle.hpp:27
sqlite3_blob * c_ptr() const noexcept
Definition handle.hpp:45
T data(T... args)
T is_const_v
T is_trivially_copyable_v
ThinSQLite++ namespace.
Definition backup_iface.hpp:17
#define SQLITE_OK
T size(T... args)