aboutsummaryrefslogtreecommitdiff
path: root/odb/sqlite/stream.cxx
blob: 6d966f769e7f24faa588a46c0bcdd6b96284baac (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// file      : odb/sqlite/stream.cxx
// copyright : Copyright (c) 2005-2018 Code Synthesis Tools CC
// license   : GNU GPL v2; see accompanying LICENSE file

#include <sqlite3.h>

#if SQLITE_VERSION_NUMBER >= 3004000

#include <odb/sqlite/stream.hxx>

#include <stdexcept> // invalid_argument

#include <odb/sqlite/error.hxx>
#include <odb/sqlite/database.hxx>
#include <odb/sqlite/transaction.hxx>

using namespace std;

namespace odb
{
  namespace sqlite
  {
    stream::
    stream (const char* db,
            const char* table,
            const char* column,
            long long rowid,
            bool rw)
        : active_object (transaction::current ().connection ())
    {
      int e (sqlite3_blob_open (conn_.handle (),
                                db,
                                table,
                                column,
                                static_cast<sqlite_int64> (rowid),
                                rw,
                                &h_));

      if (e != SQLITE_OK)
        translate_error (e, conn_);

      list_add (); // Add ourselves to the active objects list.
    }

    size_t stream::
    size () const
    {
      return static_cast<size_t> (sqlite3_blob_bytes (h_));
    }

    void stream::
    read (void* buf, size_t n, size_t o)
    {
      int e (sqlite3_blob_read (
               h_, buf, static_cast<int> (n), static_cast<int> (o)));

      if (e != SQLITE_OK)
      {
        if (e == SQLITE_ERROR)
          throw invalid_argument ("read past end");
        else
          translate_error (e, conn_);
      }
    }

    void stream::
    write (const void* buf, size_t n, size_t o)
    {
      int e (sqlite3_blob_write (
               h_, buf, static_cast<int> (n), static_cast<int> (o)));

      if (e != SQLITE_OK)
      {
        if (e == SQLITE_ERROR)
          throw invalid_argument ("write past end");
        else
          translate_error (e, conn_);
      }
    }

    void stream::
    close (bool check)
    {
      if (h_ != 0)
      {
        list_remove ();

        int e (sqlite3_blob_close (h_));
        h_ = 0; // No use trying again.

        if (check && e != SQLITE_OK)
          translate_error (e, conn_);
      }
    }

#if SQLITE_VERSION_NUMBER >= 3007004
    void stream::
    reopen (long long rowid)
    {
      int e (sqlite3_blob_reopen (h_, rowid));

      if (e != SQLITE_OK)
        // According to the SQLite documentation, the handle is now
        // considered aborted, which means we still need to close it.
        //
        translate_error (e, conn_);
    }
#endif

    void stream::
    clear ()
    {
      // This call can be part of the stack unwinding, so don't check
      // for the errors.
      //
      close (false);
    }
  }
}

#endif // SQLITE_VERSION_NUMBER >= 3004000