-
Notifications
You must be signed in to change notification settings - Fork 2
/
clientconnect.go
70 lines (60 loc) · 1.37 KB
/
clientconnect.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
package gluasql_mysql
import (
"database/sql"
"fmt"
"net/url"
util "github.com/tongson/LadyLua/external/gluasql/util"
"github.com/yuin/gopher-lua"
)
func clientConnectMethod(L *lua.LState) int {
client := checkClient(L)
tb := util.GetValue(L, 2)
options, ok := tb.(map[string]interface{})
if tb == nil || !ok {
L.ArgError(2, "options excepted")
return 0
}
host, _ := options["host"].(string)
if host == "" {
host = "127.0.0.1"
}
port, _ := options["port"].(int)
if port == 0 {
port = 3306
}
database, _ := options["database"].(string)
user, _ := options["user"].(string)
password, _ := options["password"].(string)
charset, _ := options["charset"].(string)
// current support tcp connection only
dsn := fmt.Sprintf("tcp(%s:%d)/%s", host, port, database)
if user != "" {
if password != "" {
dsn = fmt.Sprintf("%s:%s@", user, password) + dsn
} else {
dsn = fmt.Sprintf("%s@", user) + dsn
}
}
query := url.Values{}
if charset != "" {
query.Set("charset", charset)
}
if client.Timeout > 0 {
stimeout := client.Timeout.String()
query.Set("readTimeout", stimeout)
query.Set("writeTimeout", stimeout)
}
s := query.Encode()
if s != "" {
dsn += "?" + s
}
var err error
client.DB, err = sql.Open("mysql", dsn)
if err != nil {
L.Push(lua.LBool(false))
L.Push(lua.LString(err.Error()))
return 2
}
L.Push(lua.LBool(true))
return 1
}