Towards JSON.

This commit is contained in:
Nuno Cruces
2023-10-13 17:06:05 +01:00
parent f6d77f3cf4
commit eec45ea684
12 changed files with 272 additions and 10 deletions

View File

@@ -1,6 +1,7 @@
package sqlite3
import (
"encoding/json"
"errors"
"math"
"time"
@@ -138,6 +139,31 @@ func (c Context) resultRFC3339Nano(value time.Time) {
uint64(c.api.destructor), _UTF8)
}
// ResultJSON sets the result of the function to the JSON encoding of value.
//
// https://www.sqlite.org/c3ref/result_blob.html
func (c Context) ResultJSON(value any) {
data, err := json.Marshal(value)
if err != nil {
c.ResultError(err)
}
ptr := c.newBytes(data)
c.call(c.api.resultText,
uint64(c.handle), uint64(ptr), uint64(len(data)),
uint64(c.api.destructor))
}
// ResultValue sets the result of the function a copy of [Value].
//
// https://www.sqlite.org/c3ref/result_blob.html
func (c Context) ResultValue(value Value) {
if value.sqlite != c.sqlite {
c.ResultError(MISUSE)
}
c.call(c.api.resultValue,
uint64(c.handle), uint64(value.handle))
}
// ResultError sets the result of the function an error.
//
// https://www.sqlite.org/c3ref/result_blob.html