diff --git a/api.go b/api.go index aa32298..31ff1cf 100644 --- a/api.go +++ b/api.go @@ -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 diff --git a/conn.go b/conn.go index 3413a23..d85bd9b 100644 --- a/conn.go +++ b/conn.go @@ -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) { diff --git a/stmt.go b/stmt.go index c683b21..b174127 100644 --- a/stmt.go +++ b/stmt.go @@ -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...) }