File: SQLiteBlobInputStream.cpp

package info (click to toggle)
ugene 51.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 125,712 kB
  • sloc: cpp: 637,632; xml: 212,751; ansic: 81,474; javascript: 5,416; sh: 1,204; python: 771; makefile: 36
file content (119 lines) | stat: -rw-r--r-- 3,784 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
/**
 * UGENE - Integrated Bioinformatics Tools.
 * Copyright (C) 2008-2024 UniPro <ugene@unipro.ru>
 * http://ugene.net
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301, USA.
 */

#include <sqlite3.h>

#include <U2Core/U2DbiUtils.h>
#include <U2Core/U2SafePoints.h>

#include "SQLiteBlobInputStream.h"

namespace U2 {

/************************************************************************/
/* SQLiteBlobStream */
/************************************************************************/
const int SQLiteBlobStream::READ_ONLY = 0;
const int SQLiteBlobStream::READ_WRITE = 1;

SQLiteBlobStream::SQLiteBlobStream()
    : handle(nullptr), size(0), offset(0) {
}

SQLiteBlobStream::~SQLiteBlobStream() {
    releaseHandle();
}

void SQLiteBlobStream::init(int accessType, DbRef* db, const QByteArray& tableId, const QByteArray& columnId, const U2DataId& rowId, U2OpStatus& os) {
    SAFE_POINT_EXT(db != nullptr, os.setError("NULL db ref"), );
    SAFE_POINT_EXT(db->handle != nullptr, os.setError("NULL db handle"), );

    int status = sqlite3_blob_open(
        db->handle,
        "main",
        tableId.constData(),
        columnId.constData(),
        U2DbiUtils::toDbiId(rowId),
        accessType,
        &handle);
    if (SQLITE_OK != status) {
        os.setError(sqlite3_errmsg(db->handle));
    } else {
        size = sqlite3_blob_bytes(handle);
    }
}

void SQLiteBlobStream::releaseHandle() {
    if (handle != nullptr) {
        sqlite3_blob_close(handle);
        handle = nullptr;
    }
}

/************************************************************************/
/* SQLiteBlobInputStream */
/************************************************************************/
SQLiteBlobInputStream::SQLiteBlobInputStream(DbRef* db, const QByteArray& tableId, const QByteArray& columnId, const U2DataId& rowId, U2OpStatus& os)
    : InputStream(), SQLiteBlobStream() {
    init(SQLiteBlobStream::READ_ONLY, db, tableId, columnId, rowId, os);
}

qint64 SQLiteBlobInputStream::available() {
    return size - offset;
}

void SQLiteBlobInputStream::close() {
    releaseHandle();
}

int SQLiteBlobInputStream::read(char* buffer, int length, U2OpStatus& os) {
    SAFE_POINT_EXT(handle != nullptr, os.setError("blob handle is not opened"), 0);
    int targetLength = (offset + length < size) ? length : (size - offset);
    if (0 == targetLength) {
        return -1;
    }

    int status = sqlite3_blob_read(handle, (void*)buffer, targetLength, offset);
    if (SQLITE_OK != status) {
        os.setError(QObject::tr("Can not read data. The database is closed or the data were changed."));
        return 0;
    }
    offset += targetLength;
    return targetLength;
}

qint64 SQLiteBlobInputStream::skip(qint64 n, U2OpStatus& os) {
    SAFE_POINT_EXT(handle != nullptr, os.setError("blob handle is not opened"), 0);
    if (offset + n >= size) {
        int oldOffset = offset;
        offset = size;
        return size - oldOffset;
    }
    if (offset + n < 0) {
        int oldOffset = offset;
        offset = 0;
        return -oldOffset;
    }
    offset += n;
    return n;
}

}  // namespace U2