Merge pull request #128 from ben-krieger/d1-blob

Support []byte arguments to D1 Query/Exec
This commit is contained in:
syumai 2024-11-08 01:17:38 +09:00 committed by GitHub
commit f0f5c6ff9e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 29 additions and 6 deletions

View File

@ -7,6 +7,8 @@ import (
"math" "math"
"sync" "sync"
"syscall/js" "syscall/js"
"github.com/syumai/workers/internal/jsutil"
) )
type rows struct { type rows struct {
@ -61,9 +63,14 @@ func convertRowColumnValueToAny(v js.Value) (driver.Value, error) {
case js.TypeString: case js.TypeString:
return v.String(), nil return v.String(), nil
case js.TypeObject: case js.TypeObject:
// TODO: handle BLOB type (ArrayBuffer). // handle BLOB type (ArrayBuffer).
// see: https://developers.cloudflare.com/d1/platform/client-api/#type-conversion src := jsutil.Uint8ArrayClass.New(v)
return nil, errors.New("d1: row column value type object is not currently supported") dst := make([]byte, src.Length())
n := js.CopyBytesToGo(dst, src)
if n != len(dst) {
return nil, errors.New("incomplete copy from Uint8Array")
}
return dst[:n], nil
} }
return nil, errors.New("d1: unexpected row column value type") return nil, errors.New("d1: unexpected row column value type")
} }

View File

@ -34,12 +34,20 @@ func (s *stmt) Exec([]driver.Value) (driver.Result, error) {
} }
// ExecContext executes prepared statement. // ExecContext executes prepared statement.
// Given []drier.NamedValue's `Name` field will be ignored because Cloudflare D1 client doesn't support it. // Given []driver.NamedValue's `Name` field will be ignored because Cloudflare D1 client doesn't support it.
func (s *stmt) ExecContext(_ context.Context, args []driver.NamedValue) (driver.Result, error) { func (s *stmt) ExecContext(_ context.Context, args []driver.NamedValue) (driver.Result, error) {
argValues := make([]any, len(args)) argValues := make([]any, len(args))
for i, arg := range args { for i, arg := range args {
if src, ok := arg.Value.([]byte); ok {
dst := jsutil.Uint8ArrayClass.New(len(src))
if n := js.CopyBytesToJS(dst, src); n != len(src) {
return nil, errors.New("incomplete copy into Uint8Array")
}
argValues[i] = dst
} else {
argValues[i] = arg.Value argValues[i] = arg.Value
} }
}
resultPromise := s.stmtObj.Call("bind", argValues...).Call("run") resultPromise := s.stmtObj.Call("bind", argValues...).Call("run")
resultObj, err := jsutil.AwaitPromise(resultPromise) resultObj, err := jsutil.AwaitPromise(resultPromise)
if err != nil { if err != nil {
@ -57,8 +65,16 @@ func (s *stmt) Query([]driver.Value) (driver.Rows, error) {
func (s *stmt) QueryContext(_ context.Context, args []driver.NamedValue) (driver.Rows, error) { func (s *stmt) QueryContext(_ context.Context, args []driver.NamedValue) (driver.Rows, error) {
argValues := make([]any, len(args)) argValues := make([]any, len(args))
for i, arg := range args { for i, arg := range args {
if src, ok := arg.Value.([]byte); ok {
dst := jsutil.Uint8ArrayClass.New(len(src))
if n := js.CopyBytesToJS(dst, src); n != len(src) {
return nil, errors.New("incomplete copy into Uint8Array")
}
argValues[i] = dst
} else {
argValues[i] = arg.Value argValues[i] = arg.Value
} }
}
resultPromise := s.stmtObj.Call("bind", argValues...).Call("raw", map[string]any{"columnNames": true}) resultPromise := s.stmtObj.Call("bind", argValues...).Call("raw", map[string]any{"columnNames": true})
rowsArray, err := jsutil.AwaitPromise(resultPromise) rowsArray, err := jsutil.AwaitPromise(resultPromise)
if err != nil { if err != nil {