-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqldata_test.go
119 lines (97 loc) · 2.82 KB
/
sqldata_test.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
package sqldata
import (
"context"
"flag"
"github.com/BurntSushi/toml"
"testing"
)
var sqlHand SQLData
func initConfig() {}
func TestInitConfig(t *testing.T) {
if sqlHand == nil {
var sysconfig Config
var configPath string
flag.StringVar(&configPath, "config", "my.conf", "server config.")
flag.Parse()
configPath = "my.conf"
if _, err := toml.DecodeFile(configPath, &sysconfig); err != nil {
t.Fatalf("decode err:%v", err)
}
t.Log("connetct db is ok")
newSql := NewFactory(&sysconfig)
ctx := context.Background()
sqlHand = newSql.New(ctx)
}
}
func TestMysqlFetchMap(t *testing.T) {
initConfig()
condition := 2
datas, err := sqlHand.FetchMapFromSql("SELECT * FROM infos where id=?", condition)
if err != nil {
t.Fatalf("get error. [err:%v]", err)
}
for pkey, val := range datas {
t.Log("%v,%v", pkey, val)
}
datasa, _ := sqlHand.FetchMapFromSql("desc infos")
for dkey, dval := range datasa {
t.Log("==========desc infos->%#v:%#v", dkey, dval)
}
t.Logf("TestMysqlFetchMap : %v", datas)
}
func TestPrepareInsert(t *testing.T) {
initConfig()
lastId, err := sqlHand.PrepareInsert("INSERT INTO `infos` (`name`, `age`) VALUES (?,?),(?,?)",
"肖2", 30, "肖2", 30)
if err != nil {
t.Fatalf("get error. [err:%v]", err)
}
t.Logf("PrepareInsert insert_id : %v", lastId)
}
func TestPrepareOpAffected(t *testing.T) {
initConfig()
affect, err := sqlHand.PrepareOpAffected("UPDATE `my`.`infos` SET `name`=? WHERE `id`=?",
"xiaojh12", 26)
if err != nil {
t.Fatalf("get error. [err:%v]", err)
}
t.Logf("PrepareOpAffected op : %v", affect)
}
func TestOpAffected(t *testing.T) {
initConfig()
affect, err := sqlHand.OpAffected("UPDATE `my`.`infos` SET `name`=? WHERE `id`=?",
"xiaojh22", 22)
if err != nil {
t.Fatalf("get error. [err:%v]", err)
}
t.Logf("OpAffected op : %v", affect)
}
func TestGetDb(t *testing.T) {
initConfig()
row := sqlHand.GetDb().QueryRow("SELECT name FROM infos where id=?", 2)
var name string
err := row.Scan(&name)
if err != nil {
t.Fatalf("get error. [err:%v]", err)
}
t.Logf("TestGetDb op : %v", name)
}
func TestInterfaceCommit(t *testing.T) {
initConfig()
sqlHand.Begin()
stmt, err := sqlHand.TxPrepare("UPDATE `my`.`infos` SET `name`=? WHERE `id`=?")
result, err := sqlHand.TxExec(stmt, "hahha", 2)
stmt, err = sqlHand.TxPrepare("INSERT INTO `infos` (`name`, `age`) VALUES (?,?)")
result, err = sqlHand.TxExec(stmt, "肖commit1", 30)
lastid, err := result.LastInsertId()
t.Logf("======InterfaceCommit result 3 op : %v", lastid)
stmt, err = sqlHand.TxPrepare("INSERT INTO `infos` (`name`, `age`) VALUES (?,?)")
result, err = sqlHand.TxExec(stmt, 3)
t.Logf("======InterfaceCommit result 4 op : %v", err)
err = sqlHand.Commit()
if err != nil {
sqlHand.Rollback()
t.Fatalf("InterfaceCommit error. [err:%v]", err)
}
t.Logf("InterfaceCommit op is ok")
}