Skip to content

Commit c9b3645

Browse files
committed
Avoid copy, alloc.
1 parent 2204b96 commit c9b3645

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

context.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,12 +177,15 @@ func (ctx Context) ResultPointer(ptr any) {
177177
//
178178
// https://sqlite.org/c3ref/result_blob.html
179179
func (ctx Context) ResultJSON(value any) {
180-
data, err := json.Marshal(value)
180+
err := json.NewEncoder(callbackWriter(func(p []byte) (int, error) {
181+
ctx.ResultRawText(p[:len(p)-1]) // remove the newline
182+
return 0, nil
183+
})).Encode(value)
184+
181185
if err != nil {
182186
ctx.ResultError(err)
183187
return // notest
184188
}
185-
ctx.ResultRawText(data)
186189
}
187190

188191
// ResultValue sets the result of the function to a copy of [Value].

stmt.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -367,11 +367,9 @@ func (s *Stmt) BindPointer(param int, ptr any) error {
367367
//
368368
// https://sqlite.org/c3ref/bind_blob.html
369369
func (s *Stmt) BindJSON(param int, value any) error {
370-
data, err := json.Marshal(value)
371-
if err != nil {
372-
return err
373-
}
374-
return s.BindRawText(param, data)
370+
return json.NewEncoder(callbackWriter(func(p []byte) (int, error) {
371+
return 0, s.BindRawText(param, p[:len(p)-1]) // remove the newline
372+
})).Encode(value)
375373
}
376374

377375
// BindValue binds a copy of value to the prepared statement.
@@ -751,3 +749,7 @@ func (s *Stmt) columns(count int64) ([]byte, ptr_t, error) {
751749

752750
return util.View(s.c.mod, typePtr, count), dataPtr, nil
753751
}
752+
753+
type callbackWriter func(p []byte) (int, error)
754+
755+
func (fn callbackWriter) Write(p []byte) (int, error) { return fn(p) }

0 commit comments

Comments
 (0)