2026-06-11 12:33:30 +01:00
|
|
|
//go:build go1.27 || goexperiment.jsonv2
|
2025-09-23 13:36:31 +01:00
|
|
|
|
|
|
|
|
package sqlite3
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json/v2"
|
|
|
|
|
"strconv"
|
|
|
|
|
|
2026-06-16 20:32:43 +00:00
|
|
|
"github.com/hanzoai/sqlite3/internal/errutil"
|
|
|
|
|
"github.com/hanzoai/sqlite3/internal/sqlite3_wrap"
|
|
|
|
|
"github.com/hanzoai/sqlite3/internal/util"
|
2025-09-23 13:36:31 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// JSON returns a value that can be used as an argument to
|
|
|
|
|
// [database/sql.DB.Exec], [database/sql.Row.Scan] and similar methods to
|
|
|
|
|
// store value as JSON, or decode JSON into value.
|
|
|
|
|
// JSON should NOT be used with [Stmt.BindJSON], [Stmt.ColumnJSON],
|
|
|
|
|
// [Value.JSON], or [Context.ResultJSON].
|
|
|
|
|
func JSON(value any) any {
|
|
|
|
|
return util.JSON{Value: value}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ResultJSON sets the result of the function to the JSON encoding of value.
|
|
|
|
|
//
|
|
|
|
|
// https://sqlite.org/c3ref/result_blob.html
|
|
|
|
|
func (ctx Context) ResultJSON(value any) {
|
2026-03-15 11:10:12 +00:00
|
|
|
w := bytesWriter{wrp: ctx.c.wrp}
|
2025-09-23 13:36:31 +01:00
|
|
|
if err := json.MarshalWrite(&w, value); err != nil {
|
2026-03-15 11:10:12 +00:00
|
|
|
ctx.c.wrp.Free(w.ptr)
|
2025-09-23 13:36:31 +01:00
|
|
|
ctx.ResultError(err)
|
|
|
|
|
return // notest
|
|
|
|
|
}
|
2026-03-15 11:10:12 +00:00
|
|
|
ctx.c.wrp.Xsqlite3_result_text_go(
|
|
|
|
|
int32(ctx.handle), int32(w.ptr), int64(len(w.buf)))
|
2025-09-23 13:36:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// BindJSON binds the JSON encoding of value to the prepared statement.
|
|
|
|
|
// The leftmost SQL parameter has an index of 1.
|
|
|
|
|
//
|
|
|
|
|
// https://sqlite.org/c3ref/bind_blob.html
|
|
|
|
|
func (s *Stmt) BindJSON(param int, value any) error {
|
2026-03-15 11:10:12 +00:00
|
|
|
w := bytesWriter{wrp: s.c.wrp}
|
2025-09-23 13:36:31 +01:00
|
|
|
if err := json.MarshalWrite(&w, value); err != nil {
|
2026-03-15 11:10:12 +00:00
|
|
|
s.c.wrp.Free(w.ptr)
|
2026-01-24 12:38:16 +00:00
|
|
|
return err // notest
|
2025-09-23 13:36:31 +01:00
|
|
|
}
|
2026-03-15 11:10:12 +00:00
|
|
|
rc := res_t(s.c.wrp.Xsqlite3_bind_text_go(
|
2026-03-12 20:53:33 +00:00
|
|
|
int32(s.handle), int32(param),
|
2026-03-15 11:10:12 +00:00
|
|
|
int32(w.ptr), int64(len(w.buf))))
|
2025-09-23 13:36:31 +01:00
|
|
|
return s.c.error(rc)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ColumnJSON parses the JSON-encoded value of the result column
|
|
|
|
|
// and stores it in the value pointed to by ptr.
|
|
|
|
|
// The leftmost column of the result set has the index 0.
|
|
|
|
|
//
|
|
|
|
|
// https://sqlite.org/c3ref/column_blob.html
|
|
|
|
|
func (s *Stmt) ColumnJSON(col int, ptr any) error {
|
|
|
|
|
var data []byte
|
|
|
|
|
switch s.ColumnType(col) {
|
|
|
|
|
case NULL:
|
|
|
|
|
data = []byte("null")
|
|
|
|
|
case TEXT:
|
|
|
|
|
data = s.ColumnRawText(col)
|
|
|
|
|
case BLOB:
|
|
|
|
|
data = s.ColumnRawBlob(col)
|
|
|
|
|
case INTEGER:
|
|
|
|
|
data = strconv.AppendInt(nil, s.ColumnInt64(col), 10)
|
|
|
|
|
case FLOAT:
|
|
|
|
|
data = util.AppendNumber(nil, s.ColumnFloat(col))
|
|
|
|
|
default:
|
2026-03-12 20:53:33 +00:00
|
|
|
panic(errutil.AssertErr())
|
2025-09-23 13:36:31 +01:00
|
|
|
}
|
|
|
|
|
return json.Unmarshal(data, ptr)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// JSON parses a JSON-encoded value
|
|
|
|
|
// and stores the result in the value pointed to by ptr.
|
|
|
|
|
func (v Value) JSON(ptr any) error {
|
|
|
|
|
var data []byte
|
|
|
|
|
switch v.Type() {
|
|
|
|
|
case NULL:
|
|
|
|
|
data = []byte("null")
|
|
|
|
|
case TEXT:
|
|
|
|
|
data = v.RawText()
|
|
|
|
|
case BLOB:
|
|
|
|
|
data = v.RawBlob()
|
|
|
|
|
case INTEGER:
|
|
|
|
|
data = strconv.AppendInt(nil, v.Int64(), 10)
|
|
|
|
|
case FLOAT:
|
|
|
|
|
data = util.AppendNumber(nil, v.Float())
|
|
|
|
|
default:
|
2026-03-12 20:53:33 +00:00
|
|
|
panic(errutil.AssertErr())
|
2025-09-23 13:36:31 +01:00
|
|
|
}
|
|
|
|
|
return json.Unmarshal(data, ptr)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type bytesWriter struct {
|
2026-03-15 11:10:12 +00:00
|
|
|
wrp *sqlite3_wrap.Wrapper
|
2025-09-23 13:36:31 +01:00
|
|
|
buf []byte
|
|
|
|
|
ptr ptr_t
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (b *bytesWriter) Write(p []byte) (n int, err error) {
|
2026-01-24 12:38:16 +00:00
|
|
|
if len(p) > cap(b.buf)-len(b.buf)-1 {
|
|
|
|
|
want := int64(len(b.buf)) + int64(len(p)) + 1
|
2025-09-23 13:36:31 +01:00
|
|
|
grow := int64(cap(b.buf))
|
|
|
|
|
grow += grow >> 1
|
|
|
|
|
want = max(want, grow)
|
2026-03-15 11:10:12 +00:00
|
|
|
b.ptr = b.wrp.Realloc(b.ptr, want)
|
2026-03-18 17:48:31 +00:00
|
|
|
b.buf = b.wrp.Bytes(b.ptr, want)[:len(b.buf)]
|
2025-09-23 13:36:31 +01:00
|
|
|
}
|
|
|
|
|
b.buf = append(b.buf, p...)
|
2026-01-24 12:38:16 +00:00
|
|
|
_ = append(b.buf, 0)
|
2025-09-23 13:36:31 +01:00
|
|
|
return len(p), nil
|
|
|
|
|
}
|