-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpgxe.go
100 lines (78 loc) · 3.07 KB
/
pgxe.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package pgxe
import (
"context"
"github.com/anton7r/pgxe/internal/lexer"
"github.com/jackc/pgconn"
"github.com/jackc/pgx/v4"
"github.com/anton7r/pgx-scany/pgxscan"
"github.com/jackc/pgx/v4/pgxpool"
)
//No one likes typing context.Background() over and over again when it is not neccessary to be used
//DB stores the internal PgxPool
type DB struct {
Pool *pgxpool.Pool
}
//Select is a high-level function that is used to retrieve data from database into slices which has structs.
func (DB *DB) Select(target interface{}, query string, args ...interface{}) error {
return pgxscan.Select(context.Background(), DB.Pool, target, query, args...)
}
//Get is a high-level function that is used to retrieve data from database into a single struct
func (DB *DB) Get(target interface{}, query string, args ...interface{}) error {
return pgxscan.Get(context.Background(), DB.Pool, target, query, args...)
}
//Query is used to retrieve multiple rows from the database
func (DB *DB) Query(query string, args ...interface{}) (pgx.Rows, error) {
return DB.Pool.Query(context.Background(), query, args...)
}
//QueryRow is used to retrieve a single row from the database
func (DB *DB) QueryRow(query string, args ...interface{}) pgx.Row {
return DB.Pool.QueryRow(context.Background(), query, args...)
}
//Exec is used to run actions on database that wont return any values to the client
func (DB *DB) Exec(query string, args ...interface{}) (pgconn.CommandTag, error) {
return DB.Pool.Exec(context.Background(), query, args...)
}
//NamedSelect is a high-level function that is used to retrieve data from database into structs
func (DB *DB) NamedSelect(target interface{}, query string, arg interface{}) error {
query, err := lexer.Compile(query, arg)
if err != nil {
return err
}
return pgxscan.Select(context.Background(), DB.Pool, target, query)
}
//NamedGet is a high-level function that is used to retrieve data from database into structs
func (DB *DB) NamedGet(target interface{}, query string, arg interface{}) error {
query, err := lexer.Compile(query, arg)
if err != nil {
return err
}
return pgxscan.Get(context.Background(), DB.Pool, target, query)
}
//NamedQuery simplifies the parameters and allows the use of named parameters
func (DB *DB) NamedQuery(query string, arg interface{}) (pgx.Rows, error) {
query, err := lexer.Compile(query, arg)
if err != nil {
return nil, err
}
return DB.Pool.Query(context.Background(), query)
}
//NamedQueryRow simplifies the parameters and allows the use of named parameters
func (DB *DB) NamedQueryRow(query string, arg interface{}) (pgx.Row, error) {
query, err := lexer.Compile(query, arg)
if err != nil {
return nil, err
}
return DB.Pool.QueryRow(context.Background(), query), nil
}
//NamedExec simplifies the parameters and allows the use of named parameters
func (DB *DB) NamedExec(query string, arg interface{}) (pgconn.CommandTag, error) {
query, err := lexer.Compile(query, arg)
if err != nil {
return nil, err
}
return DB.Pool.Exec(context.Background(), query)
}
//Close closes the underlying pool
func (DB *DB) Close() {
DB.Pool.Close()
}