103 lines
2.6 KiB
Go
Raw Permalink Normal View History

package d1
import (
"context"
"database/sql/driver"
"errors"
2023-01-09 23:10:30 +09:00
"syscall/js"
2023-01-09 23:12:49 +09:00
"github.com/syumai/workers/internal/jsutil"
)
2023-01-09 23:10:30 +09:00
type stmt struct {
stmtObj js.Value
}
var (
_ driver.Stmt = (*stmt)(nil)
_ driver.StmtExecContext = (*stmt)(nil)
_ driver.StmtQueryContext = (*stmt)(nil)
)
func (s *stmt) Close() error {
2023-01-09 23:10:30 +09:00
// do nothing
return nil
}
2023-01-09 23:10:30 +09:00
// NumInput is not supported and always returns -1.
func (s *stmt) NumInput() int {
2023-01-09 23:10:30 +09:00
return -1
}
2023-01-09 23:10:30 +09:00
func (s *stmt) Exec([]driver.Value) (driver.Result, error) {
return nil, errors.New("d1: Exec is deprecated and not implemented")
}
2023-01-09 23:10:30 +09:00
// ExecContext executes prepared statement.
// Given []driver.NamedValue's `Name` field will be ignored because Cloudflare D1 client doesn't support it.
2023-01-09 23:10:30 +09:00
func (s *stmt) ExecContext(_ context.Context, args []driver.NamedValue) (driver.Result, error) {
argValues := make([]any, len(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
}
2023-01-09 23:10:30 +09:00
}
resultPromise := s.stmtObj.Call("bind", argValues...).Call("run")
resultObj, err := jsutil.AwaitPromise(resultPromise)
if err != nil {
return nil, err
}
return &result{
resultObj: resultObj,
}, nil
}
2023-01-09 23:10:30 +09:00
func (s *stmt) Query([]driver.Value) (driver.Rows, error) {
return nil, errors.New("d1: Query is deprecated and not implemented")
}
2023-01-09 23:10:30 +09:00
func (s *stmt) QueryContext(_ context.Context, args []driver.NamedValue) (driver.Rows, error) {
argValues := make([]any, len(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
}
2023-01-09 23:10:30 +09:00
}
2024-04-25 07:59:07 +09:00
resultPromise := s.stmtObj.Call("bind", argValues...).Call("raw", map[string]any{"columnNames": true})
2024-04-27 15:34:02 +09:00
rowsArray, err := jsutil.AwaitPromise(resultPromise)
2023-01-09 23:10:30 +09:00
if err != nil {
return nil, err
}
// If there are no rows to retrieve, length is 0.
2024-04-27 15:34:02 +09:00
if rowsArray.Length() == 0 {
return &rows{
_columns: nil,
2024-04-27 15:34:02 +09:00
rowsArray: rowsArray,
}, nil
2023-02-06 02:49:21 +09:00
}
2024-04-25 07:59:07 +09:00
// First item of rowsArray is column names
colsArray := rowsArray.Call("shift")
2024-04-25 07:59:07 +09:00
colsLen := colsArray.Length()
cols := make([]string, colsLen)
for i := 0; i < colsLen; i++ {
cols[i] = colsArray.Index(i).String()
}
return &rows{
_columns: cols,
2024-04-27 15:34:02 +09:00
rowsArray: rowsArray,
2024-04-25 07:59:07 +09:00
}, nil
}