/
mysql.go
128 lines (110 loc) · 2.82 KB
/
mysql.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
package repo
import (
"log"
"math/rand"
_ "github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx"
)
type MySQLRepo struct {
db *sqlx.DB
}
func (repo *MySQLRepo) Init() bool {
var err error
if repo.db, err = sqlx.Connect("mysql", "root@/test"); err != nil {
panic(err)
}
repo.db.SetMaxOpenConns(8)
repo.db.SetMaxIdleConns(8)
return true
}
func (repo *MySQLRepo) Seed(itemsInDataSet int) bool {
log.Printf("Seeding data to Mysql")
if _, err := repo.db.Exec(`DROP TABLE IF EXISTS items`); err != nil {
panic(err)
}
sqlStmt := `
CREATE TABLE items (
id INTEGER NOT NULL PRIMARY KEY,
name VARCHAR(255),
year INTEGER,
description TEXT,
FULLTEXT (description),
INDEX (year),
INDEX (name)
)
`
if _, err := repo.db.Exec(sqlStmt); err != nil {
panic(err)
}
tx, err := repo.db.Begin()
if err != nil {
panic(err)
}
stmt, err := tx.Prepare("INSERT INTO items(id, name,year,description) VALUES (?,?,?,?)")
if err != nil {
panic(err)
}
defer stmt.Close()
for i := 0; i < itemsInDataSet; i++ {
it := newItem(i)
if _, err = stmt.Exec(i, it.Name, it.Year, it.Description); err != nil {
panic(err)
}
}
tx.Commit()
return true
}
func (repo *MySQLRepo) QueryFullText(textQuery func() string, N int, limit int) (ret []*Item) {
stmt, err := repo.db.Preparex("select * from items where MATCH (description) AGAINST (? IN BOOLEAN MODE) LIMIT ?")
if err != nil {
log.Fatal(err)
}
defer stmt.Close()
ret = make([]*Item, 0, limit*N)
for i := 0; i < N; i++ {
rows, err := stmt.Queryx(textQuery(), limit)
if err != nil {
panic(err)
}
for rows.Next() {
it := &Item{}
if err = rows.StructScan(it); err != nil {
panic(err)
}
ret = append(ret, it)
}
rows.Close()
}
return ret
}
func (repo *MySQLRepo) QueryByID(N int, onlyQuery bool) (it *Item) {
stmt, err := repo.db.Preparex("select id,name,year,description from items where id = ?")
if err != nil {
log.Fatal(err)
}
return sqlFetchOne(stmt, N, onlyQuery, rand.Int()%itemsInDataSet)
}
func (repo *MySQLRepo) Query2Cond(N int, onlyQuery bool, limit int) (ret []*Item) {
stmt, err := repo.db.Preparex("select id,name,year,description from items where year > ? and name = ? limit ?")
if err != nil {
log.Fatal(err)
}
return sqlFetchAll(stmt, N, onlyQuery, limit, 2010, randString(), limit)
}
func (repo *MySQLRepo) Query1Cond(N int, onlyQuery bool, limit int) (ret []*Item) {
stmt, err := repo.db.Preparex("select id,name,year,description from items where year > ? limit ?")
if err != nil {
log.Fatal(err)
}
return sqlFetchAll(stmt, N, onlyQuery, limit, 2010, limit)
}
func (repo *MySQLRepo) Update(N int) {
stmt, err := repo.db.Preparex("UPDATE items SET name=?,year=?,description=? WHERE id=?")
if err != nil {
log.Fatal(err)
}
sqlUpdate(stmt, 1)
}
func init() {
registerRepo("mysql", &MySQLRepo{})
}