-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathapi.go
215 lines (197 loc) · 4.7 KB
/
api.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// Package db implements golang package db functionality for lua.
package db
import (
"context"
"database/sql"
"fmt"
"sync"
lua "github.com/yuin/gopher-lua"
)
const (
// max open connections
MaxOpenConns = 1
)
type luaDB interface {
constructor(*dbConfig) (luaDB, error)
getDB() *sql.DB
closeDB() error
getTXOptions() *sql.TxOptions
}
type dbConfig struct {
connString string
sharedMode bool
maxOpenConns int
readOnly bool
}
var (
knownDrivers = make(map[string]luaDB, 0)
knownDriversLock = &sync.Mutex{}
sharedDB = make(map[string]luaDB, 0)
sharedDBLock = &sync.Mutex{}
)
// RegisterDriver register sql driver
func RegisterDriver(driver string, i luaDB) {
knownDriversLock.Lock()
defer knownDriversLock.Unlock()
knownDrivers[driver] = i
}
func checkDB(L *lua.LState, n int) luaDB {
ud := L.CheckUserData(n)
if v, ok := ud.Value.(luaDB); ok {
return v
}
L.ArgError(n, "database expected")
return nil
}
// Open lua db.open(driver, connection_string, config) returns (db_ud, err)
// config table:
// {
// shared=false,
// max_connections=X,
// read_only=false
// }
func Open(L *lua.LState) int {
knownDriversLock.Lock()
defer knownDriversLock.Unlock()
driver := L.CheckString(1)
connString := L.CheckString(2)
db, ok := knownDrivers[driver]
if !ok {
L.Push(lua.LNil)
L.Push(lua.LString(fmt.Sprintf("unknown driver: %s", driver)))
return 2
}
// parse config
config := &dbConfig{connString: connString, maxOpenConns: MaxOpenConns}
if L.GetTop() > 2 {
configLua := L.CheckTable(3)
configLua.ForEach(func(k lua.LValue, v lua.LValue) {
if k.String() == `shared` {
if val, ok := v.(lua.LBool); ok {
config.sharedMode = bool(val)
} else {
L.ArgError(3, "shared must be bool")
}
}
if k.String() == `max_connections` {
if val, ok := v.(lua.LNumber); ok {
config.maxOpenConns = int(val)
} else {
L.ArgError(3, "max_connections must be number")
}
}
if k.String() == `read_only` {
if val, ok := v.(lua.LBool); ok {
config.readOnly = bool(val)
} else {
L.ArgError(3, "read_only must be bool")
}
}
})
}
dbIface, err := db.constructor(config)
if err != nil {
L.Push(lua.LNil)
L.Push(lua.LString(err.Error()))
return 2
}
ud := L.NewUserData()
ud.Value = dbIface
L.SetMetatable(ud, L.GetTypeMetatable(`db_ud`))
L.Push(ud)
return 1
}
// Query lua db_ud:query(query) returns ({rows = {}, columns = {}}, err)
func Query(L *lua.LState) int {
dbInterface := checkDB(L, 1)
query := L.CheckString(2)
sqlDB := dbInterface.getDB()
opts := dbInterface.getTXOptions()
tx, err := sqlDB.BeginTx(context.Background(), opts)
if err != nil {
L.Push(lua.LNil)
L.Push(lua.LString(err.Error()))
return 2
}
defer tx.Commit()
sqlRows, err := tx.Query(query)
if err != nil {
L.Push(lua.LNil)
L.Push(lua.LString(err.Error()))
return 2
}
defer sqlRows.Close()
rows, columns, err := parseRows(sqlRows, L)
if err != nil {
L.Push(lua.LNil)
L.Push(lua.LString(err.Error()))
return 2
}
result := L.NewTable()
result.RawSetString(`rows`, rows)
result.RawSetString(`columns`, columns)
L.Push(result)
return 1
}
// Exec lua db_ud:exec(query) returns ({rows_affected=number, last_insert_id=number}, err)
func Exec(L *lua.LState) int {
dbInterface := checkDB(L, 1)
query := L.CheckString(2)
sqlDB := dbInterface.getDB()
opts := dbInterface.getTXOptions()
tx, err := sqlDB.BeginTx(context.Background(), opts)
if err != nil {
L.Push(lua.LNil)
L.Push(lua.LString(err.Error()))
return 2
}
defer tx.Commit()
sqlResult, err := tx.Exec(query)
if err != nil {
L.Push(lua.LNil)
L.Push(lua.LString(err.Error()))
return 2
}
result := L.NewTable()
if id, err := sqlResult.LastInsertId(); err == nil {
result.RawSetString(`last_insert_id`, lua.LNumber(id))
}
if aff, err := sqlResult.RowsAffected(); err == nil {
result.RawSetString(`rows_affected`, lua.LNumber(aff))
}
L.Push(result)
return 1
}
// Command lua db_ud:command(query) returns ({rows = {}, columns = {}}, err)
func Command(L *lua.LState) int {
dbInterface := checkDB(L, 1)
query := L.CheckString(2)
sqlDB := dbInterface.getDB()
sqlRows, err := sqlDB.Query(query)
if err != nil {
L.Push(lua.LNil)
L.Push(lua.LString(err.Error()))
return 2
}
defer sqlRows.Close()
rows, columns, err := parseRows(sqlRows, L)
if err != nil {
L.Push(lua.LNil)
L.Push(lua.LString(err.Error()))
return 2
}
result := L.NewTable()
result.RawSetString(`rows`, rows)
result.RawSetString(`columns`, columns)
L.Push(result)
return 1
}
// Close lua db_ud:close() returns err
func Close(L *lua.LState) int {
dbIface := checkDB(L, 1)
if err := dbIface.closeDB(); err != nil {
L.Push(lua.LString(err.Error()))
return 1
}
return 0
}