-
Notifications
You must be signed in to change notification settings - Fork 1
/
transaction.go
81 lines (61 loc) · 1.78 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
package dbr
import (
"database/sql"
"time"
)
type Transaction struct {
commited bool
dbr *Dbr
db *db
startTime time.Time
Duration time.Duration
}
func newTransaction(dbr *Dbr, db *db, startTime time.Time) *Transaction {
return &Transaction{dbr: dbr, db: db, startTime: startTime}
}
func (tx *Transaction) Commit() error {
defer func() {
tx.Duration = time.Since(tx.startTime)
}()
err := tx.db.database.(*sql.Tx).Commit()
if err != nil {
return err
}
tx.commited = true
return nil
}
func (tx *Transaction) Rollback() error {
defer func() {
tx.Duration = time.Since(tx.startTime)
}()
return tx.db.database.(*sql.Tx).Rollback()
}
func (tx *Transaction) RollbackUnlessCommit() error {
if !tx.commited {
return tx.db.database.(*sql.Tx).Rollback()
}
return nil
}
func (tx *Transaction) Select(column ...interface{}) *StmtSelect {
columns := newColumns(tx.db, false)
columns.list = column
return newStmtSelect(tx.dbr, tx.dbr.Connections.Write, &StmtWith{}, columns)
}
func (tx *Transaction) Insert() *StmtInsert {
return newStmtInsert(tx.dbr, tx.dbr.Connections.Write, &StmtWith{})
}
func (tx *Transaction) Update(table interface{}) *StmtUpdate {
return newStmtUpdate(tx.dbr, tx.dbr.Connections.Write, &StmtWith{}, table)
}
func (tx *Transaction) Delete() *StmtDelete {
return newStmtDelete(tx.dbr, tx.dbr.Connections.Write, &StmtWith{})
}
func (tx *Transaction) Execute(query string) *StmtExecute {
return newStmtExecute(tx.dbr, tx.dbr.Connections.Write, query)
}
func (tx *Transaction) With(name string, builder Builder) *StmtWith {
return newStmtWith(tx.dbr, tx.dbr.Connections, name, false, builder)
}
func (tx *Transaction) WithRecursive(name string, builder Builder) *StmtWith {
return newStmtWith(tx.dbr, tx.dbr.Connections, name, true, builder)
}