Skip to content

Commit

Permalink
[+] add multi row support, closes #54
Browse files Browse the repository at this point in the history
  • Loading branch information
pashagolub committed Sep 23, 2022
1 parent 38d7bf4 commit bcf86bc
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 2 deletions.
11 changes: 10 additions & 1 deletion rows.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ func (r *Rows) RowError(row int, err error) *Rows {
// return the same instance to perform subsequent actions.
// Note that the number of values must match the number
// of columns
func (r *Rows) AddRow(values ...interface{}) *Rows {
func (r *Rows) AddRow(values ...any) *Rows {
if len(values) != len(r.defs) {
panic("Expected number of values to match number of columns")
}
Expand All @@ -236,6 +236,15 @@ func (r *Rows) AddRow(values ...interface{}) *Rows {
return r
}

// AddRows adds multiple rows composed from any slice and
// returns the same instance to perform subsequent actions.
func (r *Rows) AddRows(values ...[]any) *Rows {
for _, value := range values {
r.AddRow(value...)
}
return r
}

// AddCommandTag will add a command tag to the result set
func (r *Rows) AddCommandTag(tag pgconn.CommandTag) *Rows {
r.commandTag = tag
Expand Down
84 changes: 83 additions & 1 deletion rows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,94 @@ func TestExplicitTypeCasting(t *testing.T) {
}
}

func TestAddRows(t *testing.T) {
t.Parallel()
mock, err := NewConn()
if err != nil {
fmt.Println("failed to open sqlmock database:", err)
}
defer mock.Close(context.Background())

values := [][]any{
{
1, "John",
},
{
2, "Jane",
},
{
3, "Peter",
},
{
4, "Emily",
},
}

rows := NewRows([]string{"id", "name"}).AddRows(values...)
mock.ExpectQuery("SELECT").WillReturnRows(rows).RowsWillBeClosed()

rs, _ := mock.Query(context.Background(), "SELECT")
defer rs.Close()

for rs.Next() {
var id int
var name string
_ = rs.Scan(&id, &name)
fmt.Println("scanned id:", id, "and name:", name)
}

if rs.Err() != nil {
fmt.Println("got rows error:", rs.Err())
}
// Output: scanned id: 1 and title: John
// scanned id: 2 and title: Jane
// scanned id: 3 and title: Peter
// scanned id: 4 and title: Emily
}

func ExampleRows_AddRows() {
mock, err := NewConn()
if err != nil {
fmt.Println("failed to open sqlmock database:", err)
}
defer mock.Close(context.Background())

values := [][]any{
{
1, "one",
},
{
2, "two",
},
}

rows := NewRows([]string{"id", "title"}).AddRows(values...)

mock.ExpectQuery("SELECT").WillReturnRows(rows)

rs, _ := mock.Query(context.Background(), "SELECT")
defer rs.Close()

for rs.Next() {
var id int
var title string
_ = rs.Scan(&id, &title)
fmt.Println("scanned id:", id, "and title:", title)
}

if rs.Err() != nil {
fmt.Println("got rows error:", rs.Err())
}
// Output: scanned id: 1 and title: one
// scanned id: 2 and title: two
}

func ExampleRows() {
mock, err := NewConn()
if err != nil {
fmt.Println("failed to open pgxmock database:", err)
}
// defer mock.Close(context.Background())
defer mock.Close(context.Background())

rows := NewRows([]string{"id", "title"}).
AddRow(1, "one").
Expand Down

0 comments on commit bcf86bc

Please sign in to comment.