-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconn.go
194 lines (168 loc) · 4.57 KB
/
conn.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package sol
import (
"database/sql"
"log"
"github.com/aodin/sol/dialect"
)
// Conn is the common database connection interface. It can perform
// queries and dialect specific compilation. Since transactions also
// implement this interface, it is highly recommended to pass the Conn
// interface to functions that do not need to modify transactional state.
type Conn interface {
Begin() (TX, error)
Close() error
Query(stmt Executable, dest ...interface{}) error
String(stmt Executable) string
}
// TX is the interface for a transaction. In addition to standard COMMIT
// and ROLLBACK behavior, the interface includes a generic Close method
// that can be used with defer. Close will rollback the transaction
// unless the IsSuccessful method has been called.
type TX interface {
Conn
Commit() error
IsSuccessful()
Rollback() error
}
// Connection is an alias for Conn
type Connection interface {
Conn
}
// Transaction is an alias for TX
type Transaction interface {
TX
}
// DB is a database connection pool. Most functions should use the
// Conn interface instead of this type.
type DB struct {
*sql.DB
dialect dialect.Dialect
panicky bool
}
var _ Conn = &DB{}
// Begin will start a new transaction on the current connection pool
func (c *DB) Begin() (TX, error) {
tx, err := c.DB.Begin()
if c.panicky && err != nil {
log.Panic(err)
}
return &transaction{Tx: tx, dialect: c.dialect, panicky: c.panicky}, err
}
// Close will make the current connection pool unusable
func (c *DB) Close() error {
err := c.DB.Close()
if c.panicky && err != nil {
log.Panic(err)
}
return err
}
// Dialect returns the current connection pool's dialect, e.g. sqlite3
func (c *DB) Dialect() dialect.Dialect {
return c.dialect
}
// Query executes an Executable statement
func (c *DB) Query(stmt Executable, dest ...interface{}) error {
err := perform(c.DB, c.dialect, stmt, dest...)
if c.panicky && err != nil && err != sql.ErrNoRows {
log.Panic(err)
}
return err
}
// String returns the compiled Executable using the DB's dialect.
// If an error is encountered during compilation, it will return the
// error instead.
func (c *DB) String(stmt Executable) string {
compiled, err := stmt.Compile(c.dialect, Params())
if err != nil {
return err.Error()
}
return compiled
}
// PanicOnError will create a new connection that will panic on any error
func (c DB) PanicOnError() *DB {
c.panicky = true
return &c
}
// Must is an alias for PanicOnError
func (c DB) Must() *DB {
return c.PanicOnError()
}
// Open connects to the database using the given driver and credentials.
// It returns a database connection pool and an error if one occurred.
func Open(driver, credentials string) (*DB, error) {
db, err := sql.Open(driver, credentials)
if err != nil {
return nil, err
}
// Get the dialect
d, err := dialect.Get(driver)
if err != nil {
return nil, err
}
return &DB{DB: db, dialect: d}, nil
}
type transaction struct {
*sql.Tx
dialect dialect.Dialect
successful bool
panicky bool
}
var _ Conn = &transaction{}
var _ TX = &transaction{}
// Begin simply returns the transaction itself
// TODO database/sql does not support nested transactions, more detail
// here: https://github.com/golang/go/issues/7898
func (tx *transaction) Begin() (TX, error) {
return tx, nil
}
// Close will commit the transaction unless it has failed
func (tx *transaction) Close() (err error) {
if tx.successful {
err = tx.Tx.Commit()
} else {
err = tx.Tx.Rollback()
}
if tx.panicky && err != nil {
log.Panic(err)
}
return
}
// Commit will attempt to commit the transaction
func (tx *transaction) Commit() error {
err := tx.Tx.Commit()
if tx.panicky && err != nil {
log.Panic(err)
}
return err
}
// IsSuccessful will mark the transaction as successful, changing
// the behavior of Close()
func (tx *transaction) IsSuccessful() {
tx.successful = true
}
// Query executes an Executable statement
func (tx *transaction) Query(stmt Executable, dest ...interface{}) error {
err := perform(tx.Tx, tx.dialect, stmt, dest...)
if tx.panicky && err != nil && err != sql.ErrNoRows {
log.Panic(err)
}
return err
}
// Rollback will attempt to roll back the transaction
func (tx *transaction) Rollback() error {
err := tx.Tx.Rollback()
if tx.panicky && err != nil {
log.Panic(err)
}
return err
}
// String returns the compiled Executable using the transaction's dialect.
// If an error is encountered during compilation, it will return the
// error instead.
func (tx *transaction) String(stmt Executable) string {
compiled, err := stmt.Compile(tx.dialect, Params())
if err != nil {
return err.Error()
}
return compiled
}