/
utils.go
88 lines (75 loc) · 1.47 KB
/
utils.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
package main
import (
"fmt"
"strings"
sqlx "github.com/Code-Hex/sqlx-transactionmanager"
osqlx "github.com/jmoiron/sqlx"
)
var (
Postgres bool
Mysql bool
Sqlite bool
)
type Schema struct {
create string
drop string
}
func (s Schema) Postgres() (string, string) {
return s.create, s.drop
}
func (s Schema) MySQL() (string, string) {
return strings.Replace(s.create, `"`, "`", -1), s.drop
}
func (s Schema) Sqlite3() (string, string) {
return strings.Replace(s.create, `now()`, `CURRENT_TIMESTAMP`, -1), s.drop
}
var defaultSchema = Schema{
create: `
CREATE TABLE person (
first_name text,
last_name text,
email text,
added_at timestamp default now()
);
CREATE TABLE place (
country text,
city text NULL,
telcode integer
);
`,
drop: `
drop table person;
drop table place;
`,
}
func MultiExec(e osqlx.Execer, query string) {
stmts := strings.Split(query, ";\n")
if len(strings.Trim(stmts[len(stmts)-1], " \n\t\r")) == 0 {
stmts = stmts[:len(stmts)-1]
}
for _, s := range stmts {
_, err := e.Exec(s)
if err != nil {
fmt.Println(err, s)
}
}
}
func RunWithSchema(schema Schema, db *sqlx.DB, run func(db *sqlx.DB)) {
runner := func(create, drop string) {
defer func() { MultiExec(db, drop) }()
MultiExec(db, create)
run(db)
}
if Postgres {
create, drop := schema.Postgres()
runner(create, drop)
}
if Sqlite {
create, drop := schema.Sqlite3()
runner(create, drop)
}
if Mysql {
create, drop := schema.MySQL()
runner(create, drop)
}
}