-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathselect_test.go
192 lines (173 loc) · 4.53 KB
/
select_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
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
package gomysql
import (
"encoding/json"
"fmt"
"log"
"os"
"testing"
"time"
)
type Paipan struct {
ID int64 `db:"id"`
Name string `db:"name"`
Gender bool `db:"gender"`
Mark string `db:"mark"`
Data json.RawMessage `db:"data"`
LifeStyleId int `db:"life_style_id"`
Created int64 `db:"created"`
}
type Category struct {
ID int64 `db:"id"`
Uids []int64 `db:"uids"`
Subcate []int64 `db:"subcate"`
}
var schema = `
CREATE TABLE person (
first_name text,
last_name text,
email text
);
CREATE TABLE place (
country text,
city text NULL,
telcode integer
)`
func TestSelect(t *testing.T) {
conf := Sqlconfig{
UserName: "test",
Password: "123456",
Port: 3306,
DbName: "test",
Host: "192.168.101.4",
MultiStatements: true,
}
db, err := conf.NewMysqlDb()
if err != nil {
t.Fatal(err)
}
// db.Query(schema)
// db.Insert("INSERT INTO person (first_name, last_name, email) VALUES (?, ?, ?)", "Jason", "Moiron", "[email protected]")
// db.Insert("INSERT INTO person (first_name, last_name, email) VALUES (?, ?, ?)", "John", "Doe", "[email protected]")
persons := &Person{}
res := db.Select(&persons, "select * from person limit 1")
if res.Err != nil {
t.Fatal(err)
}
t.Log(persons)
// 建表
}
func TestSelect1(t *testing.T) {
conf := Sqlconfig{
UserName: "test",
Password: "123456",
Port: 3306,
DbName: "test",
Host: "192.168.101.4",
MultiStatements: true,
}
db, err := conf.NewMysqlDb()
if err != nil {
t.Fatal(err)
}
// db.Query(schema)
// db.Insert("INSERT INTO person (first_name, last_name, email) VALUES (?, ?, ?)", "Jason", "Moiron", "[email protected]")
// db.Insert("INSERT INTO person (first_name, last_name, email) VALUES (?, ?, ?)", "John", "Doe", "[email protected]")
persons := make([]Person, 0)
res := db.Select(&persons, "select * from person limit 1")
if res.Err != nil {
t.Fatal(err)
}
t.Log(persons)
// 建表
}
const Num = 1000
func TestMysql8(t *testing.T) {
start := time.Now()
conf := &Sqlconfig{
Host: "127.0.0.1",
UserName: "cander",
Password: "123456",
DbName: "test",
Port: 3306,
MaxOpenConns: 100,
MaxIdleConns: 10,
ReadTimeout: 100 * time.Second,
WriteTimeout: 100 * time.Second,
WriteLogWhenFailed: true,
ConnMaxLifetime: 30 * time.Second,
// 删改查失败写入的文件
LogFile: ".failedlinux.sql",
}
ch := make(chan int, Num)
db, err := conf.NewMysqlDb()
if err != nil {
os.Exit(1)
}
for i := 0; i < Num; i++ {
go func(i int) {
db.Insert("insert into test(name, age) values(?,?)", fmt.Sprintf("test%d", i), i)
ch <- 1
}(i)
}
for i := 0; i < Num; i++ {
<-ch
}
rows, err := db.GetRowsIn("select id from test where age in (?)", []string{"1", "2", "3", "4", "5"})
if err != nil {
os.Exit(1)
}
for rows.Next() {
var id int64
rows.Scan(&id)
}
rows.Close()
log.Println("mysql8:", time.Since(start).Seconds())
}
func TestSelect2(t *testing.T) {
conf := Sqlconfig{
UserName: "test",
Password: "123456",
Port: 3306,
DbName: "test",
Host: "192.168.101.4",
MultiStatements: true,
}
db, err := conf.NewMysqlDb()
if err != nil {
t.Fatal(err)
}
// db.Query(schema)
// db.Insert("INSERT INTO person (first_name, last_name, email) VALUES (?, ?, ?)", "Jason", "Moiron", "[email protected]")
// db.Insert("INSERT INTO person (first_name, last_name, email) VALUES (?, ?, ?)", "John", "Doe", "[email protected]")
persons := make([]*Person, 0)
res := db.Select(&persons, "select * from person limit 1")
if res.Err != nil {
t.Fatal(err)
}
t.Log(*persons[0])
// 建表
}
func TestSelect3(t *testing.T) {
conf := Sqlconfig{
UserName: "test",
Password: "123456",
Port: 3306,
DbName: "test",
Host: "192.168.101.4",
MultiStatements: true,
}
db, err := conf.NewMysqlDb()
if err != nil {
t.Fatal(err)
}
// db.Query(schema)
// db.Insert("INSERT INTO person (first_name, last_name, email) VALUES (?, ?, ?)", "Jason", "Moiron", "[email protected]")
// db.Insert("INSERT INTO person (first_name, last_name, email) VALUES (?, ?, ?)", "John", "Doe", "[email protected]")
persons := Person{}
res := db.Select(&persons, "select * from person limit 1")
if res.Err != nil {
t.Fatal(err)
}
t.Log(persons)
// 建表
}