forked from go-llsqlite/crawshaw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
blob.go
165 lines (150 loc) · 4.09 KB
/
blob.go
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
// Copyright (c) 2018 David Crawshaw <[email protected]>
//
// Permission to use, copy, modify, and distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
package sqlite
// #include <blocking_step.h>
// #include <sqlite3.h>
// #include <stdlib.h>
// #include <stdint.h>
import "C"
import (
"errors"
"fmt"
"io"
"unsafe"
)
var cmain = C.CString("main")
var ctemp = C.CString("temp")
// OpenBlob opens a blob in a particular {database,table,column,row}.
//
// https://www.sqlite.org/c3ref/blob_open.html
func (conn *Conn) OpenBlob(dbn, table, column string, row int64, write bool) (*Blob, error) {
var cdb *C.char
switch dbn {
case "", "main":
cdb = cmain
case "temp":
cdb = ctemp
default:
cdb = C.CString(dbn)
defer C.free(unsafe.Pointer(cdb))
}
var flags C.int
if write {
flags = 1
}
ctable := C.CString(table)
ccolumn := C.CString(column)
defer func() {
C.free(unsafe.Pointer(ctable))
C.free(unsafe.Pointer(ccolumn))
}()
blob := &Blob{conn: conn}
for {
conn.count++
if err := conn.interrupted("Conn.OpenBlob", ""); err != nil {
return nil, err
}
switch res := C.sqlite3_blob_open(conn.conn, cdb, ctable, ccolumn,
C.sqlite3_int64(row), flags, &blob.blob); res {
case C.SQLITE_LOCKED_SHAREDCACHE:
if res := C.wait_for_unlock_notify(
conn.conn, conn.unlockNote); res != C.SQLITE_OK {
return nil, conn.reserr("Conn.OpenBlob(Wait)", "", res)
}
// loop
case C.SQLITE_OK:
blob.size = int64(C.sqlite3_blob_bytes(blob.blob))
return blob, nil
default:
return nil, conn.extreserr("Conn.OpenBlob", "", res)
}
}
}
// Blob provides streaming access to SQLite blobs.
type Blob struct {
conn *Conn
blob *C.sqlite3_blob
size int64
}
func (blob *Blob) Reopen(rowid int64) (err error) {
rc := C.sqlite3_blob_reopen(blob.blob, C.sqlite3_int64(rowid))
err = blob.conn.reserr("Blob.Reopen", "", rc)
if err != nil {
return
}
blob.setSize()
return
}
func (blob *Blob) setSize() {
blob.size = int64(C.sqlite3_blob_bytes(blob.blob))
}
// https://www.sqlite.org/c3ref/blob_read.html
func (blob *Blob) ReadAt(p []byte, off int64) (n int, err error) {
if blob.blob == nil {
return 0, ErrBlobClosed
}
if off < 0 {
err = fmt.Errorf("bad offset %v", off)
return
}
if off >= blob.size {
err = io.EOF
return
}
if err := blob.conn.interrupted("Blob.ReadAt", ""); err != nil {
return 0, err
}
if int64(len(p)) > blob.size-off {
p = p[:blob.size-off]
}
lenp := C.int(len(p))
res := C.sqlite3_blob_read(blob.blob, unsafe.Pointer(&p[0]), lenp, C.int(off))
if err := blob.conn.reserr("Blob.ReadAt", "", res); err != nil {
return 0, err
}
n = len(p)
if off+int64(len(p)) >= blob.size {
err = io.EOF
}
return
}
// https://www.sqlite.org/c3ref/blob_write.html
func (blob *Blob) WriteAt(p []byte, off int64) (n int, err error) {
if blob.blob == nil {
return 0, ErrBlobClosed
}
if err := blob.conn.interrupted("Blob.WriteAt", ""); err != nil {
return 0, err
}
lenp := C.int(len(p))
res := C.sqlite3_blob_write(blob.blob, unsafe.Pointer(&p[0]), lenp, C.int(off))
if err := blob.conn.reserr("Blob.WriteAt", "", res); err != nil {
return 0, err
}
return len(p), nil
}
// Size returns the total size of a blob.
func (blob *Blob) Size() int64 {
return blob.size
}
// https://www.sqlite.org/c3ref/blob_close.html
func (blob *Blob) Close() error {
if blob.blob == nil {
return ErrBlobClosed
}
err := blob.conn.reserr("Blob.Close", "", C.sqlite3_blob_close(blob.blob))
blob.blob = nil
return err
}
var ErrBlobClosed = errors.New("blob closed")