70 lines
1.6 KiB
Go
Raw Normal View History

package d1
import (
"context"
"database/sql/driver"
"errors"
2023-01-09 23:10:30 +09:00
"github.com/syumai/workers/internal/jsutil"
"syscall/js"
)
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 []drier.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) {
argValues := make([]any, len(args))
for i, arg := range args {
argValues[i] = arg.Value
}
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 {
argValues[i] = arg
}
resultPromise := s.stmtObj.Call("bind", argValues...).Call("all")
rowsObj, err := jsutil.AwaitPromise(resultPromise)
if err != nil {
return nil, err
}
return &rows{
rowsObj: rowsObj,
}, nil
}