[go: up one dir, main page]

File: blob.cpp

package info (click to toggle)
soci 4.1.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 17,944 kB
  • sloc: ansic: 169,887; cpp: 54,198; javascript: 12,258; ada: 1,973; sh: 36; makefile: 12; xml: 2
file content (251 lines) | stat: -rw-r--r-- 6,506 bytes parent folder | download
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
//
// Copyright (C) 2004-2007 Maciej Sobczak, Stephen Hutton
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// https://www.boost.org/LICENSE_1_0.txt)
//

#define SOCI_ORACLE_SOURCE
#include "soci/oracle/soci-oracle.h"
#include "error.h"
#include "soci/statement.h"
#include <cstring>
#include <sstream>
#include <cstdio>
#include <ctime>
#include <cctype>

#ifdef _MSC_VER
#pragma warning(disable:4355)
#endif

using namespace soci;
using namespace soci::details;
using namespace soci::details::oracle;

oracle_blob_backend::oracle_blob_backend(oracle_session_backend &session)
    : session_(session), lobp_(NULL), initialized_(false)
{
    sword res = OCIDescriptorAlloc(session.envhp_,
        reinterpret_cast<dvoid**>(&lobp_), OCI_DTYPE_LOB, 0, 0);
    if (res != OCI_SUCCESS)
    {
        throw soci_error("Cannot allocate the LOB locator");
    }
}

oracle_blob_backend::~oracle_blob_backend()
{
    try
    {
        reset();
    }
    catch (const oracle_soci_error &)
    {
        // Ignore, as we shouldn't throw exceptions from the destructor
    }

    OCIDescriptorFree(lobp_, OCI_DTYPE_LOB);
}

std::size_t oracle_blob_backend::get_len()
{
    if (!initialized_)
    {
        return 0;
    }

    oraub8 len;

    sword res = OCILobGetLength2(session_.svchp_, session_.errhp_,
        lobp_, &len);

    if (res != OCI_SUCCESS)
    {
        throw_oracle_soci_error(res, session_.errhp_);
    }

    return static_cast<std::size_t>(len);
}

std::size_t oracle_blob_backend::read_from_start(void *buf, std::size_t toRead, std::size_t offset)
{
    if (offset >= get_len())
    {
        if (!initialized_ && offset == 0)
        {
            // Read-attempts (from the beginning) on uninitialized BLOBs is defined to be a no-op
            return 0;
        }

        throw soci_error("Can't read past-the-end of BLOB data.");
    }

    auto amt = static_cast<oraub8>(toRead);

    sword res = OCILobRead2(session_.svchp_, session_.errhp_, lobp_, &amt, nullptr,
        static_cast<oraub8>(offset + 1), buf, amt, OCI_ONE_PIECE, nullptr, nullptr, 0, SQLCS_IMPLICIT);
    if (res != OCI_SUCCESS)
    {
        throw_oracle_soci_error(res, session_.errhp_);
    }

    return static_cast<std::size_t>(amt);
}

std::size_t oracle_blob_backend::write_from_start(const void *buf, std::size_t toWrite, std::size_t offset)
{
    if (offset > get_len())
    {
        // If offset == length, the operation is to be understood as appending (and is therefore allowed)
        throw soci_error("Can't start writing far past-the-end of BLOB data.");
    }

    ensure_initialized();

    auto amt = static_cast<oraub8>(toWrite);

    sword res = OCILobWrite2(session_.svchp_, session_.errhp_, lobp_, &amt, nullptr,
        static_cast<oraub8>(offset + 1),
        const_cast<void*>(buf), amt, OCI_ONE_PIECE, 0, 0, 0, 0);
    if (res != OCI_SUCCESS)
    {
        throw_oracle_soci_error(res, session_.errhp_);
    }

    return static_cast<std::size_t>(amt);
}

std::size_t oracle_blob_backend::append(const void *buf, std::size_t toWrite)
{
    ensure_initialized();

    auto amt = static_cast<oraub8>(toWrite);

    sword res = OCILobWriteAppend2(session_.svchp_, session_.errhp_, lobp_,
        &amt, nullptr, const_cast<void*>(buf), amt, OCI_ONE_PIECE, 0, 0, 0, 0);
    if (res != OCI_SUCCESS)
    {
        throw_oracle_soci_error(res, session_.errhp_);
    }

    return static_cast<std::size_t>(amt);
}

void oracle_blob_backend::trim(std::size_t newLen)
{
    sword res = OCILobTrim2(session_.svchp_, session_.errhp_, lobp_,
        static_cast<oraub8>(newLen));
    if (res != OCI_SUCCESS)
    {
        throw_oracle_soci_error(res, session_.errhp_);
    }
}

oracle_blob_backend::locator_t oracle_blob_backend::get_lob_locator() const
{
    return lobp_;
}

void oracle_blob_backend::set_lob_locator(const oracle_blob_backend::locator_t locator, bool initialized)
{
    // If we select a BLOB value into a BLOB object, then the post_fetch code in
    // the standard_into_type_backend will set this object's locator to the one it is
    // already holding.
    // In this case, the locator now already points to the desired BLOB object and thus we
    // must not reset it.
    if (lobp_ != locator)
    {
        reset();

        sword res = OCILobLocatorAssign(session_.svchp_, session_.errhp_, locator, &lobp_);
        if (res != OCI_SUCCESS)
        {
            throw_oracle_soci_error(res, session_.errhp_);
        }
    }

    initialized_ = initialized;

    if (initialized)
    {
        boolean already_open = FALSE;
        sword res = OCILobIsOpen(session_.svchp_, session_.errhp_, lobp_, &already_open);

        if (res != OCI_SUCCESS)
        {
            throw_oracle_soci_error(res, session_.errhp_);
        }

        if (!already_open)
        {
            res = OCILobOpen(session_.svchp_, session_.errhp_, lobp_, OCI_LOB_READWRITE);

            if (res != OCI_SUCCESS)
            {
                throw_oracle_soci_error(res, session_.errhp_);
            }
        }
    }
}

void oracle_blob_backend::reset()
{
    if (!initialized_)
    {
        return;
    }

    boolean is_temporary = FALSE;
    sword res = OCILobIsTemporary(session_.envhp_, session_.errhp_, lobp_, &is_temporary);

    if (res != OCI_SUCCESS)
    {
        throw_oracle_soci_error(res, session_.errhp_);
    }

    if (is_temporary)
    {
        res = OCILobFreeTemporary(session_.svchp_, session_.errhp_, lobp_);
    }
    else
    {
        res = OCILobClose(session_.svchp_, session_.errhp_, lobp_);
    }

    if (res != OCI_SUCCESS)
    {
        throw_oracle_soci_error(res, session_.errhp_);
    }

    initialized_ = false;
}

void oracle_blob_backend::ensure_initialized()
{
    if (!initialized_)
    {
        // If asked to initialize explicitly, we can only create a temporary LOB
        sword res = OCILobCreateTemporary(session_.svchp_, session_.errhp_, lobp_,
                OCI_DEFAULT, SQLCS_IMPLICIT, OCI_TEMP_BLOB, FALSE, OCI_DURATION_SESSION);

        if (res != OCI_SUCCESS)
        {
            throw_oracle_soci_error(res, session_.errhp_);
        }

        res = OCILobOpen(session_.svchp_, session_.errhp_, lobp_, OCI_LOB_READWRITE);

        if (res != OCI_SUCCESS)
        {
            throw_oracle_soci_error(res, session_.errhp_);
        }

        initialized_ = true;
    }
}

details::session_backend &oracle_blob_backend::get_session_backend()
{
    return session_;
}