-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add Blob I/O #1083
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Add Blob I/O #1083
Changes from 12 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
c357e0c
Add Blob I/O
joriszwart 6188ff5
Merge branch 'mattn:master' into master
joriszwart 88d6cfd
Blob I/O: early return
joriszwart c6a9868
Blob I/O: use raw connection
joriszwart 2866235
Blob I/O: doc
joriszwart 867ec21
Blob I/O: unabbreviate offs → offset
joriszwart 5583431
Blob I/O: use keys in struct init
joriszwart 606d2d2
Blob I/O: clean up connection
joriszwart 79df247
Blob I/O: move test data / connection
joriszwart dff49f9
Merge branch 'mattn:master' into master
joriszwart 01dbf4b
Blob I/O: add write and seek
joriszwart 893e77d
Blob I/O: format error
joriszwart 7df73e0
Blob I/O: limit and expose blob size
joriszwart e519482
Blob I/O: correct usage of raw connection
joriszwart 67dc006
Blob I/O: close db
joriszwart 7f0a6fb
Blob I/O: use vfsdb
joriszwart 5b53687
Blob I/O: insufficient space errors for Write, raw connection callback
joriszwart ef6fa5a
Blob I/O: helpful error
joriszwart fe0ed2e
Blob I/O: adhere to io.Writer
joriszwart cb853d0
Blob I/O: use :memory: for compatibility (SQLite <3.37.0)
joriszwart 7d10860
Blob I/O: add extra checks after review
joriszwart 3f8fe99
Blob I/O: fix TODO
joriszwart File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
// Copyright (C) 2022 Yasuhiro Matsumoto <[email protected]>. | ||
// | ||
// Use of this source code is governed by an MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package sqlite3 | ||
|
||
/* | ||
#ifndef USE_LIBSQLITE3 | ||
#include "sqlite3-binding.h" | ||
#else | ||
#include <sqlite3.h> | ||
#endif | ||
#include <stdlib.h> | ||
*/ | ||
import "C" | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"io" | ||
"math" | ||
"runtime" | ||
"unsafe" | ||
) | ||
|
||
// SQLiteBlob implements the SQLite Blob I/O interface. | ||
type SQLiteBlob struct { | ||
conn *SQLiteConn | ||
blob *C.sqlite3_blob | ||
size int | ||
offset int | ||
} | ||
|
||
// Blob opens a blob. | ||
// | ||
// See https://www.sqlite.org/c3ref/blob_open.html for usage. | ||
// | ||
// Should only be used with conn.Raw. | ||
func (conn *SQLiteConn) Blob(database, table, column string, rowid int64, flags int) (*SQLiteBlob, error) { | ||
databaseptr := C.CString(database) | ||
defer C.free(unsafe.Pointer(databaseptr)) | ||
|
||
tableptr := C.CString(table) | ||
defer C.free(unsafe.Pointer(tableptr)) | ||
|
||
columnptr := C.CString(column) | ||
defer C.free(unsafe.Pointer(columnptr)) | ||
|
||
var blob *C.sqlite3_blob | ||
ret := C.sqlite3_blob_open(conn.db, databaseptr, tableptr, columnptr, C.longlong(rowid), C.int(flags), &blob) | ||
|
||
if ret != C.SQLITE_OK { | ||
return nil, conn.lastError() | ||
} | ||
|
||
size := int(C.sqlite3_blob_bytes(blob)) | ||
bb := &SQLiteBlob{conn: conn, blob: blob, size: size, offset: 0} | ||
|
||
runtime.SetFinalizer(bb, (*SQLiteBlob).Close) | ||
|
||
return bb, nil | ||
} | ||
|
||
// Read implements the io.Reader interface. | ||
func (s *SQLiteBlob) Read(b []byte) (n int, err error) { | ||
if s.offset >= s.size { | ||
return 0, io.EOF | ||
} | ||
|
||
n = s.size - s.offset | ||
if len(b) < n { | ||
n = len(b) | ||
} | ||
|
||
p := &b[0] | ||
rittneje marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ret := C.sqlite3_blob_read(s.blob, unsafe.Pointer(p), C.int(n), C.int(s.offset)) | ||
if ret != C.SQLITE_OK { | ||
return 0, s.conn.lastError() | ||
} | ||
|
||
s.offset += n | ||
|
||
return n, nil | ||
} | ||
|
||
// Write implements the io.Writer interface. | ||
func (s *SQLiteBlob) Write(b []byte) (n int, err error) { | ||
if s.offset >= s.size { | ||
return 0, io.EOF | ||
joriszwart marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
n = s.size - s.offset | ||
joriszwart marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if len(b) < n { | ||
n = len(b) | ||
} | ||
|
||
p := &b[0] | ||
rittneje marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ret := C.sqlite3_blob_write(s.blob, unsafe.Pointer(p), C.int(n), C.int(s.offset)) | ||
if ret != C.SQLITE_OK { | ||
return 0, s.conn.lastError() | ||
} | ||
|
||
s.offset += n | ||
|
||
return n, nil | ||
} | ||
|
||
// Seek implements the io.Seeker interface. | ||
func (s *SQLiteBlob) Seek(offset int64, whence int) (int64, error) { | ||
if offset > math.MaxInt32 { | ||
return 0, errors.New("sqlite3.SQLiteBlob.Seek: invalid position") | ||
joriszwart marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
var abs int64 | ||
switch whence { | ||
case io.SeekStart: | ||
abs = offset | ||
case io.SeekCurrent: | ||
abs = int64(s.offset) + offset | ||
case io.SeekEnd: | ||
abs = int64(s.size) + offset | ||
default: | ||
return 0, fmt.Errorf("sqlite3.SQLiteBlob.Seek: invalid whence %d", whence) | ||
} | ||
|
||
if abs < 0 { | ||
return 0, errors.New("sqlite.SQLiteBlob.Seek: negative position") | ||
} | ||
|
||
s.offset = int(abs) | ||
joriszwart marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return abs, nil | ||
} | ||
|
||
// Close implements the io.Closer interface. | ||
func (s *SQLiteBlob) Close() error { | ||
ret := C.sqlite3_blob_close(s.blob) | ||
|
||
s.blob = nil | ||
runtime.SetFinalizer(s, nil) | ||
|
||
if ret != C.SQLITE_OK { | ||
return s.conn.lastError() | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.