Skip to content

Commit 8a7a943

Browse files
committed
sqlite: support sql.RawBytes (return []byte in driver.Value)
The https://pkg.go.dev/database/sql/driver#Value type can be either string or []byte, but []byte permits database/sql scanning into *sql.RawBytes without allocating. We were previously returning strings all the time if that's what SQLite thought it was. (But Go doesn't distinguish between "TEXT" and "BLOB" ... either are just some bytes) This effectively moves any allocation (from []byte to string or []byte to clone of []byte) later, letting it be managed by database/sql. │ before │ after │ │ sec/op │ sec/op vs base │ QueryRows100MixedTypes-8 37.61µ ± 1% 36.60µ ± 0% -2.67% (p=0.000 n=10) │ before │ after │ │ B/op │ B/op vs base │ QueryRows100MixedTypes-8 4.281Ki ± 0% 2.719Ki ± 0% -36.50% (p=0.000 n=10) │ before │ after │ │ allocs/op │ allocs/op vs base │ QueryRows100MixedTypes-8 207.0 ± 0% 107.0 ± 0% -48.31% (p=0.000 n=10) Signed-off-by: Brad Fitzpatrick <[email protected]>
1 parent 0461cba commit 8a7a943

File tree

3 files changed

+6
-4
lines changed

3 files changed

+6
-4
lines changed

cgosqlite/cgosqlite.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ func (stmt *Stmt) ColumnText(col int) string {
378378
}
379379

380380
func (stmt *Stmt) ColumnBlob(col int) []byte {
381-
res := C.sqlite3_column_blob(stmt.stmt.ptr(), C.int(col))
381+
res := C.ts_sqlite3_column_blob(stmt.stmt.int(), C.int(col))
382382
if res == nil {
383383
return nil
384384
}

cgosqlite/cgosqlite.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,10 @@ static const unsigned char *ts_sqlite3_column_text(handle_sqlite3_stmt stmt, int
106106
return sqlite3_column_text((sqlite3_stmt*)(stmt), iCol);
107107
}
108108

109+
static const unsigned char *ts_sqlite3_column_blob(handle_sqlite3_stmt stmt, int iCol) {
110+
return sqlite3_column_blob((sqlite3_stmt*)(stmt), iCol);
111+
}
112+
109113
static int ts_sqlite3_column_type(handle_sqlite3_stmt stmt, int iCol) {
110114
return sqlite3_column_type((sqlite3_stmt*)(stmt), iCol);
111115
}

sqlite.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -739,9 +739,7 @@ func (r *rows) Next(dest []driver.Value) error {
739739
}
740740
case sqliteh.SQLITE_FLOAT:
741741
dest[i] = r.stmt.stmt.ColumnDouble(i)
742-
case sqliteh.SQLITE_TEXT:
743-
dest[i] = r.stmt.stmt.ColumnText(i)
744-
case sqliteh.SQLITE_BLOB:
742+
case sqliteh.SQLITE_BLOB, sqliteh.SQLITE_TEXT:
745743
dest[i] = r.stmt.stmt.ColumnBlob(i)
746744
case sqliteh.SQLITE_NULL:
747745
dest[i] = nil

0 commit comments

Comments
 (0)