Skip to content

Commit

Permalink
rename D1 row columns field name and changed to use result of rowsArr…
Browse files Browse the repository at this point in the history
…ay.shift() directly
  • Loading branch information
syumai committed Apr 27, 2024
1 parent 640a515 commit a0eb7cd
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
6 changes: 3 additions & 3 deletions cloudflare/d1/rows.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import (
type rows struct {
rowsArray js.Value
currentRow int
// columns is cached value of Columns method.
// _columns is cached value of Columns method.
// do not use this directly.
columns []string
_columns []string
// _rowsLen is cached value of rowsLen method.
// do not use this directly.
_rowsLen int
Expand All @@ -27,7 +27,7 @@ var _ driver.Rows = (*rows)(nil)
// Columns returns column names retrieved from query result.
// If rows are empty, this returns nil.
func (r *rows) Columns() []string {
return r.columns
return r._columns
}

func (r *rows) Close() error {
Expand Down
10 changes: 4 additions & 6 deletions cloudflare/d1/stmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,22 +67,20 @@ func (s *stmt) QueryContext(_ context.Context, args []driver.NamedValue) (driver
// If there are no rows to retrieve, length is 0.
if rowsArray.Length() == 0 {
return &rows{
columns: nil,
_columns: nil,
rowsArray: rowsArray,
}, nil
}

// The first result array includes the column names.
colsArray := rowsArray.Index(0)
// First item of rowsArray is column names
colsArray := rowsArray.Call("shift")
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.
rowsArray.Call("shift")
return &rows{
columns: cols,
_columns: cols,
rowsArray: rowsArray,
}, nil
}

0 comments on commit a0eb7cd

Please sign in to comment.