forked from revel/modules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transaction.go
104 lines (86 loc) · 2.32 KB
/
transaction.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
101
102
103
104
package gorp
import (
"database/sql"
sq "github.com/Masterminds/squirrel"
gorpa "github.com/go-gorp/gorp"
)
type (
// This is a small wrapped around gorp.Transaction so you can make use of the builder statements as well.
Transaction struct {
Map *gorpa.Transaction
dbgorp *DbGorp
}
)
func (txn *Transaction) Rollback() (err error) {
return txn.Map.Rollback()
}
func (txn *Transaction) Commit() (err error) {
return txn.Map.Commit()
}
func (txn *Transaction) Select(i interface{}, builder sq.SelectBuilder) (l []interface{}, err error) {
query, args, err := builder.ToSql()
if err == nil {
list, err := txn.Map.Select(i, query, args...)
if err != nil && gorpa.NonFatalError(err) {
return list, nil
}
if err == sql.ErrNoRows {
err = nil
}
return list, err
}
return
}
func (txn *Transaction) SelectOne(i interface{}, builder sq.SelectBuilder) (err error) {
query, args, err := builder.ToSql()
if err == nil {
err = txn.Map.SelectOne(i, query, args...)
if err != nil && gorpa.NonFatalError(err) {
return nil
}
}
return
}
func (txn *Transaction) SelectInt(builder sq.SelectBuilder) (i int64, err error) {
query, args, err := builder.ToSql()
if err == nil {
i, err = txn.Map.SelectInt(query, args...)
}
return
}
func (txn *Transaction) ExecUpdate(builder sq.UpdateBuilder) (r sql.Result, err error) {
query, args, err := builder.ToSql()
if err == nil {
r, err = txn.Map.Exec(query, args...)
}
return
}
func (txn *Transaction) ExecInsert(builder sq.InsertBuilder) (r sql.Result, err error) {
query, args, err := builder.ToSql()
if err == nil {
r, err = txn.Map.Exec(query, args...)
}
return
}
// Shifted some common functions up a level
func (txn *Transaction) Insert(list ...interface{}) error {
return txn.Map.Insert(list...)
}
func (txn *Transaction) Update(list ...interface{}) (int64, error) {
return txn.Map.Update(list...)
}
func (txn *Transaction) Get(i interface{}, keys ...interface{}) (interface{}, error) {
return txn.Map.Get(i, keys...)
}
func (txn *Transaction) Delete(i ...interface{}) (int64, error) {
return txn.Map.Delete(i...)
}
func (txn *Transaction) GetMap() DbGeneric {
return txn.Map
}
func (txn *Transaction) Builder() sq.StatementBuilderType {
return txn.dbgorp.SqlStatementBuilder
}
func (txn *Transaction) Schema() (result string) {
return txn.dbgorp.Schema()
}