forked from gobuffalo/pop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysql.go
92 lines (73 loc) · 2.04 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
package pop
import (
"fmt"
"os/exec"
_ "github.com/go-sql-driver/mysql"
"github.com/markbates/going/clam"
. "github.com/markbates/pop/columns"
"github.com/markbates/pop/fizz"
"github.com/markbates/pop/fizz/translators"
)
type mysql struct {
ConnectionDetails *ConnectionDetails
}
func (m *mysql) Details() *ConnectionDetails {
return m.ConnectionDetails
}
func (m *mysql) URL() string {
c := m.ConnectionDetails
if c.URL != "" {
return c.URL
}
s := "%s:%s@(%s:%s)/%s?parseTime=true&multiStatements=true&readTimeout=1s"
return fmt.Sprintf(s, c.User, c.Password, c.Host, c.Port, c.Database)
}
func (m *mysql) MigrationURL() string {
return m.URL()
}
func (m *mysql) Create(s store, model *Model, cols Columns) error {
return genericCreate(s, model, cols)
}
func (m *mysql) Update(s store, model *Model, cols Columns) error {
return genericUpdate(s, model, cols)
}
func (m *mysql) Destroy(s store, model *Model) error {
return genericDestroy(s, model)
}
func (m *mysql) SelectOne(s store, model *Model, query Query) error {
return genericSelectOne(s, model, query)
}
func (m *mysql) SelectMany(s store, models *Model, query Query) error {
return genericSelectMany(s, models, query)
}
func (m *mysql) CreateDB() error {
c := m.ConnectionDetails
cmd := exec.Command("mysql", "-u", c.User, "-p"+c.Password, "-e", fmt.Sprintf("create database %s", c.Database))
return clam.RunAndListen(cmd, func(s string) {
fmt.Println(s)
})
}
func (m *mysql) DropDB() error {
c := m.ConnectionDetails
cmd := exec.Command("mysql", "-u", c.User, "-p"+c.Password, "-e", fmt.Sprintf("drop database %s", c.Database))
return clam.RunAndListen(cmd, func(s string) {
fmt.Println(s)
})
}
func (m *mysql) TranslateSQL(sql string) string {
return sql
}
func (m *mysql) FizzTranslator() fizz.Translator {
t := translators.NewMySQL(m.URL(), m.Details().Database)
return t
}
func (m *mysql) Lock(fn func() error) error {
return fn()
}
func newMySQL(deets *ConnectionDetails) dialect {
deets.Parse("3306")
cd := &mysql{
ConnectionDetails: deets,
}
return cd
}