-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
191 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Binary file not shown.
This file contains 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,59 @@ | ||
// Package blob provides an alternative interface to incremental BLOB I/O. | ||
package blob | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/ncruces/go-sqlite3" | ||
) | ||
|
||
// Register registers the blob_open SQL function. | ||
func Register(db *sqlite3.Conn) { | ||
db.CreateFunction("blob_open", -1, | ||
sqlite3.DETERMINISTIC|sqlite3.DIRECTONLY, openBlob) | ||
} | ||
|
||
func openBlob(ctx sqlite3.Context, arg ...sqlite3.Value) { | ||
if len(arg) < 6 { | ||
ctx.ResultError(errors.New("wrong number of arguments to function blob_open()")) | ||
return | ||
} | ||
|
||
row := arg[3].Int64() | ||
|
||
var err error | ||
blob, ok := ctx.GetAuxData(0).(*sqlite3.Blob) | ||
if ok { | ||
err = blob.Reopen(row) | ||
if errors.Is(err, sqlite3.MISUSE) { | ||
// Blob was closed (db, table or column changed). | ||
ok = false | ||
} | ||
} | ||
|
||
if !ok { | ||
db := arg[0].Text() | ||
table := arg[1].Text() | ||
column := arg[2].Text() | ||
write := arg[4].Bool() | ||
blob, err = ctx.Conn().OpenBlob(db, table, column, row, write) | ||
} | ||
if err != nil { | ||
ctx.ResultError(err) | ||
return | ||
} | ||
|
||
fn := arg[5].Pointer().(OpenCallback) | ||
err = fn(blob, arg[6:]...) | ||
if err != nil { | ||
ctx.ResultError(err) | ||
return | ||
} | ||
|
||
// This ensures the blob is closed if db, table or column change. | ||
ctx.SetAuxData(0, blob) | ||
ctx.SetAuxData(1, blob) | ||
ctx.SetAuxData(2, blob) | ||
} | ||
|
||
type OpenCallback func(*sqlite3.Blob, ...sqlite3.Value) error |
This file contains 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,61 @@ | ||
package blob_test | ||
|
||
import ( | ||
"io" | ||
"log" | ||
"os" | ||
|
||
"github.com/ncruces/go-sqlite3" | ||
"github.com/ncruces/go-sqlite3/driver" | ||
_ "github.com/ncruces/go-sqlite3/embed" | ||
"github.com/ncruces/go-sqlite3/ext/blob" | ||
) | ||
|
||
func Example() { | ||
// Open the database, registering the extension. | ||
db, err := driver.Open(":memory:", func(conn *sqlite3.Conn) error { | ||
blob.Register(conn) | ||
return nil | ||
}) | ||
|
||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
defer os.Remove("demo.db") | ||
defer db.Close() | ||
|
||
_, err = db.Exec(`CREATE TABLE IF NOT EXISTS test (col)`) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
const message = "Hello BLOB!" | ||
|
||
// Create the BLOB. | ||
_, err = db.Exec(`INSERT INTO test VALUES (?)`, sqlite3.ZeroBlob(len(message))) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
// Write the BLOB. | ||
_, err = db.Exec(`SELECT blob_open('main', 'test', 'col', last_insert_rowid(), true, ?)`, | ||
sqlite3.Pointer[blob.OpenCallback](func(blob *sqlite3.Blob, _ ...sqlite3.Value) error { | ||
_, err = io.WriteString(blob, message) | ||
return err | ||
})) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
// Read the BLOB. | ||
_, err = db.Exec(`SELECT blob_open('main', 'test', 'col', rowid, false, ?) FROM test`, | ||
sqlite3.Pointer[blob.OpenCallback](func(blob *sqlite3.Blob, _ ...sqlite3.Value) error { | ||
_, err = io.Copy(os.Stdout, blob) | ||
return err | ||
})) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
// Output: | ||
// Hello BLOB! | ||
} |
This file contains 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,14 @@ | ||
package sqlite3 | ||
|
||
// Pointer returns a pointer to a value | ||
// that can be used as an argument to | ||
// [database/sql.DB.Exec] and similar methods. | ||
// | ||
// https://www.sqlite.org/bindptr.html | ||
func Pointer[T any](val T) any { | ||
return pointer[T]{val} | ||
} | ||
|
||
type pointer[T any] struct{ val T } | ||
|
||
func (p pointer[T]) Value() any { return p.val } |
This file contains 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
This file contains 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
This file contains 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
This file contains 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