Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gosqlite
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
gosqlite
Commits
b253c526
Commit
b253c526
authored
May 09, 2013
by
gwenn
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Make Blob implement io.Seeker (like in tclsqlite3).
parent
77b73a52
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
35 additions
and
27 deletions
+35
-27
blob.go
blob.go
+35
-27
No files found.
blob.go
View file @
b253c526
...
...
@@ -20,16 +20,15 @@ import (
// io.ReadCloser adapter to BLOB
// (See http://sqlite.org/c3ref/blob.html)
type
BlobReader
struct
{
c
*
Conn
bl
*
C
.
sqlite3_blob
size
int
ReadO
ffset
int
c
*
Conn
bl
*
C
.
sqlite3_blob
size
int
o
ffset
int
}
// io.ReadWriteCloser adapter to BLOB
type
BlobReadWriter
struct
{
BlobReader
WriteOffset
int
}
// Zeroblobs are used to reserve space for a BLOB that is later written.
...
...
@@ -53,7 +52,7 @@ func (c *Conn) NewBlobReadWriter(db, table, column string, row int64) (*BlobRead
if
err
!=
nil
{
return
nil
,
err
}
return
&
BlobReadWriter
{
BlobReader
{
c
,
bl
,
-
1
,
0
}
,
0
},
nil
return
&
BlobReadWriter
{
BlobReader
{
c
,
bl
,
-
1
,
0
}},
nil
}
func
(
c
*
Conn
)
blob_open
(
db
,
table
,
column
string
,
row
int64
,
write
bool
)
(
*
C
.
sqlite3_blob
,
error
)
{
...
...
@@ -101,22 +100,41 @@ func (r *BlobReader) Read(v []byte) (int, error) {
if
err
!=
nil
{
return
0
,
err
}
if
r
.
ReadO
ffset
>=
size
{
if
r
.
o
ffset
>=
size
{
return
0
,
io
.
EOF
}
if
len
(
v
)
>
(
size
-
r
.
ReadO
ffset
)
{
v
=
v
[
0
:
size
-
r
.
ReadO
ffset
]
if
len
(
v
)
>
(
size
-
r
.
o
ffset
)
{
v
=
v
[
0
:
size
-
r
.
o
ffset
]
}
p
:=
&
v
[
0
]
n
:=
len
(
v
)
rv
:=
C
.
sqlite3_blob_read
(
r
.
bl
,
unsafe
.
Pointer
(
p
),
C
.
int
(
n
),
C
.
int
(
r
.
ReadO
ffset
))
rv
:=
C
.
sqlite3_blob_read
(
r
.
bl
,
unsafe
.
Pointer
(
p
),
C
.
int
(
n
),
C
.
int
(
r
.
o
ffset
))
if
rv
!=
C
.
SQLITE_OK
{
return
0
,
r
.
c
.
error
(
rv
,
"BlobReader.Read"
)
}
r
.
ReadO
ffset
+=
n
r
.
o
ffset
+=
n
return
n
,
nil
}
// Seek sets the offset for the next Read or Write to offset.
func
(
r
*
BlobReader
)
Seek
(
offset
int64
,
whence
int
)
(
int64
,
error
)
{
switch
whence
{
case
0
:
// SEEK_SET
r
.
offset
=
int
(
offset
)
case
1
:
// SEEK_CUR
r
.
offset
+=
int
(
offset
)
case
2
:
// SEEK_END
size
,
err
:=
r
.
Size
()
if
err
!=
nil
{
return
0
,
err
}
r
.
offset
=
size
+
int
(
offset
)
default
:
return
0
,
r
.
c
.
specificError
(
"Bad seekMode"
)
}
return
int64
(
r
.
offset
),
nil
}
// Size returns the size of an opened BLOB.
// (See http://sqlite.org/c3ref/blob_bytes.html)
func
(
r
*
BlobReader
)
Size
()
(
int
,
error
)
{
...
...
@@ -139,21 +157,21 @@ func (w *BlobReadWriter) Write(v []byte) (int, error) {
if
err
!=
nil
{
return
0
,
err
}
if
w
.
WriteO
ffset
>=
size
{
if
w
.
o
ffset
>=
size
{
return
0
,
io
.
EOF
}
/* Write must return a non-nil error if it returns n < len(v) */
if
len
(
v
)
>
(
size
-
w
.
WriteO
ffset
)
{
v
=
v
[
0
:
size
-
w
.
WriteO
ffset
]
if
len
(
v
)
>
(
size
-
w
.
o
ffset
)
{
v
=
v
[
0
:
size
-
w
.
o
ffset
]
err
=
io
.
EOF
}
p
:=
&
v
[
0
]
n
:=
len
(
v
)
rv
:=
C
.
sqlite3_blob_write
(
w
.
bl
,
unsafe
.
Pointer
(
p
),
C
.
int
(
n
),
C
.
int
(
w
.
WriteO
ffset
))
rv
:=
C
.
sqlite3_blob_write
(
w
.
bl
,
unsafe
.
Pointer
(
p
),
C
.
int
(
n
),
C
.
int
(
w
.
o
ffset
))
if
rv
!=
C
.
SQLITE_OK
{
return
0
,
w
.
c
.
error
(
rv
,
"BlobReadWiter.Write"
)
}
w
.
WriteO
ffset
+=
n
w
.
o
ffset
+=
n
return
n
,
err
}
...
...
@@ -165,16 +183,6 @@ func (r *BlobReader) Reopen(rowid int64) error {
return
r
.
c
.
error
(
rv
,
fmt
.
Sprintf
(
"BlobReader.Reopen(%d)"
,
rowid
))
}
r
.
size
=
-
1
r
.
ReadOffset
=
0
return
nil
}
// Reopen moves a BLOB handle to a new row.
// (See http://sqlite.org/c3ref/blob_reopen.html)
func
(
w
*
BlobReadWriter
)
Reopen
(
rowid
int64
)
error
{
if
err
:=
w
.
BlobReader
.
Reopen
(
rowid
);
err
!=
nil
{
return
err
}
w
.
WriteOffset
=
0
r
.
offset
=
0
return
nil
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment