Skip to content

Commit f3d6514

Browse files
authored
Use sql.Named to clarify the blobio example (#281)
I find the example SQL queries a bit difficult to read at the callsite with the magic numbers and ? placeholders. I think sql.Named makes it more obvious to the reader what the different parts of the SQL query represent.
1 parent 93f711c commit f3d6514

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

ext/blobio/blob_test.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package blobio_test
22

33
import (
4+
"database/sql"
45
"io"
56
"log"
67
"os"
@@ -34,7 +35,8 @@ func Example() {
3435
const message = "Hello BLOB!"
3536

3637
// Create the BLOB.
37-
r, err := db.Exec(`INSERT INTO test VALUES (?)`, sqlite3.ZeroBlob(len(message)))
38+
r, err := db.Exec(`INSERT INTO test VALUES (:data)`,
39+
sql.Named("data", sqlite3.ZeroBlob(len(message))))
3840
if err != nil {
3941
log.Fatal(err)
4042
}
@@ -45,15 +47,19 @@ func Example() {
4547
}
4648

4749
// Write the BLOB.
48-
_, err = db.Exec(`SELECT writeblob('main', 'test', 'col', ?, 0, ?)`,
49-
id, message)
50+
_, err = db.Exec(`SELECT writeblob('main', 'test', 'col', :rowid, :offset, :message)`,
51+
sql.Named("rowid", id),
52+
sql.Named("offset", 0),
53+
sql.Named("message", message))
5054
if err != nil {
5155
log.Fatal(err)
5256
}
5357

5458
// Read the BLOB.
55-
_, err = db.Exec(`SELECT readblob('main', 'test', 'col', ?, 0, ?)`,
56-
id, sqlite3.Pointer(os.Stdout))
59+
_, err = db.Exec(`SELECT readblob('main', 'test', 'col', :rowid, :offset, :writer)`,
60+
sql.Named("rowid", id),
61+
sql.Named("offset", 0),
62+
sql.Named("writer", sqlite3.Pointer(os.Stdout)))
5763
if err != nil {
5864
log.Fatal(err)
5965
}

0 commit comments

Comments
 (0)