ColumnBlob API, error handling.

This commit is contained in:
Nuno Cruces
2023-01-21 12:09:54 +00:00
parent 5643077254
commit 51bb4f6fb2
3 changed files with 30 additions and 18 deletions

2
api.go
View File

@@ -28,6 +28,7 @@ func newConn(module api.Module) *Conn {
malloc: getFun("malloc"),
free: getFun("free"),
destructor: uint64(destructor),
errcode: getFun("sqlite3_errcode"),
errstr: getFun("sqlite3_errstr"),
errmsg: getFun("sqlite3_errmsg"),
erroff: getFun("sqlite3_error_offset"),
@@ -59,6 +60,7 @@ type sqliteAPI struct {
malloc api.Function
free api.Function
destructor uint64
errcode api.Function
errstr api.Function
errmsg api.Function
erroff api.Function

28
conn.go
View File

@@ -141,31 +141,33 @@ func (c *Conn) error(rc uint64) error {
return nil
}
serr := Error{
err := Error{
Code: ErrorCode(rc),
ExtendedCode: ExtendedErrorCode(rc),
}
if err.Code == NOMEM || err.ExtendedCode == IOERR_NOMEM {
panic(oomErr)
}
var r []uint64
// string
r, _ = c.api.errstr.Call(c.ctx, rc)
if r != nil {
serr.str = c.getString(uint32(r[0]), 512)
}
// message
// Do this first, sqlite3_errmsg is guaranteed to never change the value of the error code.
r, _ = c.api.errmsg.Call(c.ctx, uint64(c.handle))
if r != nil {
serr.msg = c.getString(uint32(r[0]), 512)
err.msg = c.getString(uint32(r[0]), 512)
}
switch serr.msg {
case "not an error", serr.str:
serr.msg = ""
r, _ = c.api.errstr.Call(c.ctx, rc)
if r != nil {
err.str = c.getString(uint32(r[0]), 512)
}
return &serr
if err.msg == err.str {
err.msg = ""
}
return &err
}
func (c *Conn) free(ptr uint32) {

18
stmt.go
View File

@@ -149,7 +149,11 @@ func (s *Stmt) ColumnText(col int) string {
ptr := uint32(r[0])
if ptr == 0 {
// handle error
r, err = s.c.api.errcode.Call(s.c.ctx, uint64(s.handle))
if err != nil {
panic(err)
}
s.err = s.c.error(r[0])
return ""
}
@@ -166,7 +170,7 @@ func (s *Stmt) ColumnText(col int) string {
return string(mem)
}
func (s *Stmt) ColumnBlob(col int, buf []byte) int {
func (s *Stmt) ColumnBlob(col int, buf []byte) []byte {
r, err := s.c.api.columnBlob.Call(s.c.ctx,
uint64(s.handle), uint64(col))
if err != nil {
@@ -175,8 +179,12 @@ func (s *Stmt) ColumnBlob(col int, buf []byte) int {
ptr := uint32(r[0])
if ptr == 0 {
// handle error
return 0
r, err = s.c.api.errcode.Call(s.c.ctx, uint64(s.handle))
if err != nil {
panic(err)
}
s.err = s.c.error(r[0])
return nil
}
r, err = s.c.api.columnBytes.Call(s.c.ctx,
@@ -189,5 +197,5 @@ func (s *Stmt) ColumnBlob(col int, buf []byte) int {
if !ok {
panic(rangeErr)
}
return copy(mem, buf)
return append(buf[0:0], mem...)
}