For my current project I’m using pgxpool to query the database using this select
query " query(ctx, SELECT * FROM invoices WHERE user_id = $1
,id)".
This function returns a type pgx.Rows which holds the values of the query and an error type.
If an error exists, then value of the pgx.Rows isn’t returned.
However, this isn’t the case since the function pgx.Query() doesn’t returns an error for no rows.
In this case the function doesn’t return an error nor a value of type pg.Rows.
Only pgx.QueryRow() along with pgxscan.ScanOne() can return a NoRowsError for you.
The problem is pgx.ScanOne() only returns one row where I want to return multiple.
From what I gathered online I’m to supposed to use Rows.Next() to check if the next row exist otherwise it return false when there are no rows. However, when the query does return rows I’m not sure what I"m supposed to do.
My current implementation
func ReadInvoicesByUserID(id int) ([]*Invoice, fields.GrammarError) {
ctx, db := bikeshop.Connect()
defer db.Close()
var tempInv Invoice
var invoices []*Invoice
_, fieldErr := ReadInvoices()
if fieldErr.ErrMsgs != nil &&
strings.Contains(fieldErr.ErrMsgs[0], "failed to connect to `user=username") {
fmt.Printf("ReadInvoicesByUserID funct: error username doesn't exist")
return nil, fieldErr
}
rows, err := db.Query(ctx, `SELECT * FROM invoices WHERE user_id = $1`, id)
if !rows.Next() {
// if no rows were found thats an NoRowsError
if rows.Err() == nil {
fmt.Println("Found an Error Iterating in Getting All the Invoices for the Specified User")
fieldErr.AddMsg(fields.ResourceNotFound, "no rows in result set")
return nil, fieldErr
}
return nil, fieldErr
}
// err := pgxscan.ScanAll(&invs, rows)
// var err error
for rows.Next() {
fmt.Println("Processing Query in ReadInvoicesbyUserID")
err = pgxscan.ScanOne(&tempInv, rows)
if errors.Is(err, pgx.ErrNoRows) {
fmt.Println("Found an Error Iterating in Getting All the Invoices for the Specified User")
break
}
invoices = append(invoices, &tempInv)
}
if rows.Err() != nil {
fmt.Println("Found an Error Iterating in Getting All the Invoices for the Specified User")
}
return invoices, fieldErr
}
refs
pgx_v5
pgxscan
pg_v5 pgxpool
pgx_v5 implementation for rows