Skip to content

Commit

Permalink
Merge pull request #112 from zztkm/fix#106
Browse files Browse the repository at this point in the history
fix D1 driver implementation using stmt.raw({ columnNames: true })
  • Loading branch information
syumai committed Apr 24, 2024
2 parents 73526ab + 33abcb5 commit 617355c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 26 deletions.
28 changes: 6 additions & 22 deletions cloudflare/d1/rows.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,14 @@ import (
"math"
"sync"
"syscall/js"

"github.com/syumai/workers/internal/jsutil"
)

type rows struct {
rowsObj js.Value
currentRow int
// columns is cached value of Columns method.
// do not use this directly.
_columns []string
onceColumns sync.Once
columns []string
// _rowsLen is cached value of rowsLen method.
// do not use this directly.
_rowsLen int
Expand All @@ -27,23 +24,10 @@ type rows struct {

var _ driver.Rows = (*rows)(nil)

// Columns returns column names retrieved from query result object's keys.
// Columns returns column names retrieved from query result.
// If rows are empty, this returns nil.
func (r *rows) Columns() []string {
r.onceColumns.Do(func() {
if r.rowsObj.Length() == 0 {
// return nothing when row count is zero.
return
}
colsArray := jsutil.ObjectClass.Call("keys", r.rowsObj.Index(0))
colsLen := colsArray.Length()
cols := make([]string, colsLen)
for i := 0; i < colsLen; i++ {
cols[i] = colsArray.Index(i).String()
}
r._columns = cols
})
return r._columns
return r.columns
}

func (r *rows) Close() error {
Expand Down Expand Up @@ -91,9 +75,9 @@ func (r *rows) Next(dest []driver.Value) error {
return io.EOF
}
rowObj := r.rowsObj.Index(r.currentRow)
cols := r.Columns()
for i, col := range cols {
v, err := convertRowColumnValueToAny(rowObj.Get(col))
rowObjLen := rowObj.Length()
for i := 0; i < rowObjLen; i++ {
v, err := convertRowColumnValueToAny(rowObj.Index(i))
if err != nil {
return err
}
Expand Down
23 changes: 19 additions & 4 deletions cloudflare/d1/stmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,30 @@ func (s *stmt) QueryContext(_ context.Context, args []driver.NamedValue) (driver
for i, arg := range args {
argValues[i] = arg.Value
}
resultPromise := s.stmtObj.Call("bind", argValues...).Call("all")
resultPromise := s.stmtObj.Call("bind", argValues...).Call("raw", map[string]any{"columnNames": true})
rowsObj, err := jsutil.AwaitPromise(resultPromise)
if err != nil {
return nil, err
}
if !rowsObj.Get("success").Bool() {
return nil, errors.New("d1: failed to query")
// If there are no rows to retrieve, length is 0.
if rowsObj.Length() == 0 {
return &rows{
columns: nil,
rowsObj: rowsObj,
}, nil
}

// The first result array includes the column names.
colsArray := rowsObj.Index(0)
colsLen := colsArray.Length()
cols := make([]string, colsLen)
for i := 0; i < colsLen; i++ {
cols[i] = colsArray.Index(i).String()
}
// Remove the first result array from the rowsObj.
rowsObj.Call("shift")
return &rows{
rowsObj: rowsObj.Get("results"),
columns: cols,
rowsObj: rowsObj,
}, nil
}

0 comments on commit 617355c

Please sign in to comment.