ThinSQLite++
A thin, safe and convenient modern C++ wrapper for the SQLite API.
Toggle main menu visibility
Loading...
Searching...
No Matches
impl
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
17
namespace
thinsqlitepp
18
{
19
24
36
SQLITEPP_EXPORTED
37
class
blob
final :
public
handle
<sqlite3_blob, blob>
38
{
39
public
:
41
~blob
() noexcept
42
{
sqlite3_blob_close
(
c_ptr
()); }
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>
91
requires
(
std::is_trivially_copyable_v<std::ranges::range_value_t<R>
> &&
92
!
std::is_const_v<std::remove_reference_t<std::ranges::range_reference_t<R>
>>)
93
void
read
(
size_t
offset, R & range)
const
94
{
95
using
value_type =
std::remove_reference_t<std::ranges::range_reference_t<R>
>;
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>
140
requires
(
std::is_trivially_copyable_v<std::ranges::range_value_t<R>
>)
141
void
write
(
size_t
offset,
const
R & range)
142
{
143
using
value_type =
std::remove_reference_t<std::ranges::range_reference_t<R>
>;
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_bytes
sqlite3_blob_close
sqlite3_blob_close
sqlite3_blob_read
sqlite3_blob_read
sqlite3_blob_reopen
sqlite3_blob_reopen
sqlite3_blob_write
sqlite3_blob_write
thinsqlitepp::blob
Access blob as a byte stream.
Definition
blob_iface.hpp:38
thinsqlitepp::blob::read
void read(size_t offset, R &range) const
Read data from the blob.
Definition
blob_iface.hpp:93
thinsqlitepp::blob::reopen
void reopen(int64_t rowid)
Move the object to a new row.
Definition
blob_iface.hpp:50
thinsqlitepp::blob::write
void write(size_t offset, const R &range)
Write data to the blob.
Definition
blob_iface.hpp:141
thinsqlitepp::blob::~blob
~blob() noexcept
Equivalent to sqlite3_blob_close.
Definition
blob_iface.hpp:41
thinsqlitepp::blob::write
void write(size_t offset, span< const std::byte > src)
Write data to the blob.
Definition
blob_iface.hpp:115
thinsqlitepp::blob::write
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
thinsqlitepp::blob::read
void read(size_t offset, span< std::byte > dest) const
Read data from the blob.
Definition
blob_iface.hpp:73
thinsqlitepp::blob::bytes
size_t bytes() const noexcept
Returns the size of the blob.
Definition
blob_iface.hpp:62
thinsqlitepp::exception
Exception used to report any SQLite errors.
Definition
exception_iface.hpp:180
thinsqlitepp::handle< sqlite3_blob, blob >::c_ptr
sqlite3_blob * c_ptr() const noexcept
Definition
handle.hpp:45
thinsqlitepp::handle< sqlite3_blob, blob >::handle
handle()=delete
std::span::data
T data(T... args)
thinsqlitepp::span
std::span< T > span
Alias or reimplementation of std::span.
Definition
span.hpp:36
std::is_const_v
T is_const_v
std::is_trivially_copyable_v
T is_trivially_copyable_v
std::numeric_limits::max
T max(T... args)
thinsqlitepp
ThinSQLite++ namespace.
Definition
backup_iface.hpp:17
std::remove_reference_t
SQLITE_OK
#define SQLITE_OK
std::span::size
T size(T... args)
Generated by
1.17.0