-
Notifications
You must be signed in to change notification settings - Fork 0
/
connection.go
149 lines (136 loc) · 3.88 KB
/
connection.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
package goeloquent
import (
"database/sql"
"errors"
_ "reflect"
_ "time"
)
type Connection struct {
DB *sql.DB
Config *DBConfig
ConnectionName string
}
type IConnection interface {
Insert(query string, bindings []interface{}) (result sql.Result, err error)
Select(query string, bindings []interface{}, dest interface{}, mapping map[string]interface{}) (result sql.Result, err error)
Update(query string, bindings []interface{}) (result sql.Result, err error)
Delete(query string, bindings []interface{}) (result sql.Result, err error)
AffectingStatement(query string, bindings []interface{}) (result sql.Result, err error)
Statement(query string, bindings []interface{}) (sql.Result, error)
Table(tableName string) *Builder
}
type Preparer interface {
Prepare(query string) (*sql.Stmt, error)
}
type Execer interface {
Exec(query string, args ...interface{}) (sql.Result, error)
}
type ITransaction interface {
BeginTransaction() (*Transaction, error)
Transaction(closure TxClosure) (interface{}, error)
}
func (c *Connection) Select(query string, bindings []interface{}, dest interface{}, mapping map[string]interface{}) (result sql.Result, err error) {
var stmt *sql.Stmt
var rows *sql.Rows
stmt, err = c.DB.Prepare(query)
if err != nil {
return
}
defer stmt.Close()
rows, err = stmt.Query(bindings...)
if err != nil {
return
}
defer rows.Close()
return ScanAll(rows, dest, mapping), nil
}
func (c *Connection) BeginTransaction() (*Transaction, error) {
begin, err := c.DB.Begin()
if err != nil {
return nil, errors.New(err.Error())
}
tx := &Transaction{
Tx: begin,
Config: c.Config,
ConnectionName: c.ConnectionName,
}
return tx, nil
}
func (c *Connection) Transaction(closure TxClosure) (res interface{}, err error) {
begin, err := c.DB.Begin()
if err != nil {
return nil, err
}
defer func() {
if result := recover(); result != nil {
if e, ok := result.(error); ok {
err = e
} else {
err = errors.New("error occurred during transaction")
}
_ = begin.Rollback()
} else {
err = begin.Commit()
}
}()
tx := &Transaction{
Tx: begin,
Config: c.Config,
ConnectionName: c.ConnectionName,
}
return closure(tx)
}
func (c *Connection) Insert(query string, bindings []interface{}) (result sql.Result, err error) {
return c.AffectingStatement(query, bindings)
}
func (c *Connection) Update(query string, bindings []interface{}) (result sql.Result, err error) {
return c.AffectingStatement(query, bindings)
}
func (c *Connection) Delete(query string, bindings []interface{}) (result sql.Result, err error) {
return c.AffectingStatement(query, bindings)
}
func (c *Connection) AffectingStatement(query string, bindings []interface{}) (result sql.Result, err error) {
stmt, errP := c.DB.Prepare(query)
if errP != nil {
err = errP
return
}
defer stmt.Close()
result, err = stmt.Exec(bindings...)
if err != nil {
return
}
return
}
func (c *Connection) Table(tableName string) *Builder {
builder := NewBuilder(c)
builder.Grammar = &MysqlGrammar{}
builder.Grammar.SetTablePrefix(c.Config.Prefix)
builder.Grammar.SetBuilder(builder)
builder.From(tableName)
return builder
}
func (c *Connection) Model(modelPointer interface{}) *Builder {
builder := NewBuilder(c)
builder.Grammar = &MysqlGrammar{}
builder.Grammar.SetTablePrefix(c.Config.Prefix)
builder.Grammar.SetBuilder(builder)
builder.SetModel(modelPointer)
return builder
}
func (c *Connection) Statement(query string, bindings []interface{}) (sql.Result, error) {
return c.AffectingStatement(query, bindings)
}
func (c *Connection) GetDB() *sql.DB {
return c.DB
}
func (c *Connection) Query() *Builder {
builder := NewBuilder(c)
builder.Grammar = &MysqlGrammar{}
builder.Grammar.SetBuilder(builder)
builder.Grammar.SetTablePrefix(c.Config.Prefix)
return builder
}
func (c *Connection) GetConfig() *DBConfig {
return c.Config
}