-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscanall.go
58 lines (53 loc) · 1.54 KB
/
scanall.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package pgxh
import (
"github.com/apaxa-go/helper/databaseh/sqlh"
"github.com/jackc/pgx"
)
// PgxQueryer interface can hold any object that can query SQL statements.
// Currently (and is primary used for) it can hold pgx.Conn & pgx.ConnPool.
type PgxQueryer interface {
Query(sql string, args ...interface{}) (*pgx.Rows, error)
}
// ScanAll is adaptation of "github.com/apaxa-go/helper/databaseh/sqlh" StmtScanAll for "github.com/jackc/pgx".
// ScanAll performs query sql on connection conn with arguments 'args' and stores all result rows in dst.
// sql passed as-is to conn.Query so it is possible to pass prepared statement name as sql.
// ScanAll stop working on first error.
// Example:
// type Label struct {
// Id int32
// Name string
// }
//
// func (l *Label) SQLScanInterface() []interface{} {
// return []interface{}{
// &l.Id,
// &l.Name,
// }
// }
//
// type Labels []*Label
//
// func (l *Labels) SQLNewElement() sqlh.SingleScannable {
// e := &Label{}
// *l = append(*l, e)
// return e
// }
// ...
// var labels Labels
// if err := pgxh.ScanAll(conn, "SELECT id, name FROM LABELS where amount>$1", &labels, someAmount); err != nil {
// return err
// }
func ScanAll(conn PgxQueryer, sql string, dst sqlh.MultiScannable, args ...interface{}) error {
rows, err := conn.Query(sql, args...)
if err != nil {
return err
}
defer rows.Close()
for rows.Next() {
rowContainer := dst.SQLNewElement()
if err := rows.Scan(rowContainer.SQLScanInterface()...); err != nil {
return err
}
}
return rows.Err()
}