summaryrefslogtreecommitdiff
path: root/src/blob.cpp
blob: 397d85b52e9fa93e0d31f2bddebe777eef26a886 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
//
// Copyright (c) 2022 Klemens Morgenstern (klemens.morgenstern@gmx.net)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//


#include <boost/sqlite/blob.hpp>
#include <boost/sqlite/connection.hpp>

BOOST_SQLITE_BEGIN_NAMESPACE

blob_handle open_blob(connection & conn,
                      cstring_ref db,
                      cstring_ref table,
                      cstring_ref column,
                      sqlite3_int64 row,
                      bool read_only,
                      system::error_code & ec,
                      error_info & ei)
{
  sqlite3_blob * bb = nullptr;
  blob_handle bh;

  int res = sqlite3_blob_open(conn.handle(), db.c_str(), table.c_str(), column.c_str(),
                              row, read_only ? 0 : 1, &bb);
  if (res != 0)
  {
      BOOST_SQLITE_ASSIGN_EC(ec, sqlite3_errcode(conn.handle()));
      ei.set_message(sqlite3_errmsg(conn.handle()));
  }
  else
      bh = blob_handle(bb);

  return bh;
}

blob_handle open_blob(connection & conn,
                      cstring_ref db,
                      cstring_ref table,
                      cstring_ref column,
                      sqlite3_int64 row,
                      bool read_only)
{
  boost::system::error_code ec;
  error_info ei;
  auto b = open_blob(conn, db, table, column, row, read_only, ec, ei);
  if (ec)
    boost::throw_exception(system::system_error(ec, ei.message()), BOOST_CURRENT_LOCATION);
  return b;
}

void blob_handle::reopen(sqlite3_int64 row_id, system::error_code & ec)
{
  int res = sqlite3_blob_reopen(blob_.get(), row_id);
  BOOST_SQLITE_ASSIGN_EC(ec, res);
}
void blob_handle::reopen(sqlite3_int64 row_id)
{
  boost::system::error_code ec;
  reopen(row_id, ec);
  if (ec)
    boost::throw_exception(system::system_error(ec), BOOST_CURRENT_LOCATION);
}

void blob_handle::read_at(void *data, int len, int offset, system::error_code &ec)
{
  int res = sqlite3_blob_read(blob_.get(), data, len, offset);
  BOOST_SQLITE_ASSIGN_EC(ec, res);
}
void blob_handle::read_at(void *data, int len, int offset)
{
  boost::system::error_code ec;
  read_at(data, len, offset, ec);
  if (ec)
    boost::throw_exception(system::system_error(ec), BOOST_CURRENT_LOCATION);
}

void blob_handle::write_at(const void *data, int len, int offset, system::error_code &ec)
{
  int res = sqlite3_blob_write(blob_.get(), data, len, offset);
  BOOST_SQLITE_ASSIGN_EC(ec, res);
}
void blob_handle::write_at(const void *data, int len, int offset)
{
  boost::system::error_code ec;
  write_at(data, len, offset, ec);
  if (ec)
    boost::throw_exception(system::system_error(ec), BOOST_CURRENT_LOCATION);
}

BOOST_SQLITE_END_NAMESPACE