-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtx.go
More file actions
76 lines (67 loc) · 1.67 KB
/
Copy pathtx.go
File metadata and controls
76 lines (67 loc) · 1.67 KB
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
package dotpgx
import (
"github.com/jackc/pgx"
)
// Tx is transaction
type Tx struct {
Ptx *pgx.Tx
qm queryMap
}
// Begin a transaction
func (db *DB) Begin() (tx *Tx, err error) {
ptx, err := db.Pool.Begin()
if err != nil {
return
}
tx = &Tx{
Ptx: ptx,
qm: db.qm,
}
return
}
// Rollback the transaction
func (tx *Tx) Rollback() error {
return tx.Ptx.Rollback()
}
// Commit the transaction
func (tx *Tx) Commit() error {
return tx.Ptx.Commit()
}
// Prepare a sql statement identified by name.
func (tx *Tx) Prepare(name string) (*pgx.PreparedStatement, error) {
q, err := tx.qm.getQuery(name)
if err != nil {
return nil, err
}
q.ps, err = tx.Ptx.Prepare(name, q.getSQL())
if err != nil {
return nil, err
}
return q.ps, nil
}
// Query runs the sql indentified by name. Return a row set.
func (tx *Tx) Query(name string, args ...interface{}) (*pgx.Rows, error) {
q, err := tx.qm.getQuery(name)
if err != nil {
return nil, err
}
return tx.Ptx.Query(q.getSQL(), args...)
}
// QueryRow runs the sql identified by name. It returns a single row.
// Not that an error is only returned if the query is not defined.
// A query error is defered untill row.Scan is run. See pgx docs for more info.
func (tx *Tx) QueryRow(name string, args ...interface{}) (*pgx.Row, error) {
q, err := tx.qm.getQuery(name)
if err != nil {
return nil, err
}
return tx.Ptx.QueryRow(q.getSQL(), args...), nil
}
// Exec runs the sql identified by name. Returns the result of the exec or an error.
func (tx *Tx) Exec(name string, args ...interface{}) (pgx.CommandTag, error) {
q, err := tx.qm.getQuery(name)
if err != nil {
return "", err
}
return tx.Ptx.Exec(q.getSQL(), args...)
}