Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[+] add multi row support, closes #54 #104

Merged
merged 1 commit into from
Sep 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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