forked from gobuffalo/pop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dialect.go
163 lines (148 loc) · 3.82 KB
/
dialect.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
package pop
import (
"encoding/gob"
"fmt"
"io"
"io/ioutil"
"github.com/gobuffalo/fizz"
"github.com/gobuffalo/pop/columns"
"github.com/gobuffalo/uuid"
"github.com/jmoiron/sqlx"
"github.com/pkg/errors"
)
func init() {
gob.Register(uuid.UUID{})
}
type dialect interface {
Name() string
URL() string
MigrationURL() string
Details() *ConnectionDetails
TranslateSQL(string) string
Create(store, *Model, columns.Columns) error
Update(store, *Model, columns.Columns) error
Destroy(store, *Model) error
SelectOne(store, *Model, Query) error
SelectMany(store, *Model, Query) error
CreateDB() error
DropDB() error
DumpSchema(io.Writer) error
LoadSchema(io.Reader) error
FizzTranslator() fizz.Translator
Lock(func() error) error
TruncateAll(*Connection) error
}
func genericCreate(s store, model *Model, cols columns.Columns) error {
keyType := model.PrimaryKeyType()
switch keyType {
case "int", "int64":
var id int64
w := cols.Writeable()
query := fmt.Sprintf("INSERT INTO %s (%s) VALUES (%s)", model.TableName(), w.String(), w.SymbolizedString())
Log(query)
res, err := s.NamedExec(query, model.Value)
if err != nil {
return errors.WithStack(err)
}
id, err = res.LastInsertId()
if err == nil {
model.setID(id)
}
if err != nil {
return errors.WithStack(err)
}
return nil
case "UUID", "string":
if keyType == "UUID" {
if model.ID() == emptyUUID {
u, err := uuid.NewV4()
if err != nil {
return errors.WithStack(err)
}
model.setID(u)
}
} else if model.ID() == "" {
return fmt.Errorf("missing ID value")
}
w := cols.Writeable()
w.Add("id")
query := fmt.Sprintf("INSERT INTO %s (%s) VALUES (%s)", model.TableName(), w.String(), w.SymbolizedString())
Log(query)
stmt, err := s.PrepareNamed(query)
if err != nil {
return errors.WithStack(err)
}
_, err = stmt.Exec(model.Value)
if err != nil {
return errors.WithStack(err)
}
return nil
}
return errors.Errorf("can not use %s as a primary key type!", keyType)
}
func genericUpdate(s store, model *Model, cols columns.Columns) error {
stmt := fmt.Sprintf("UPDATE %s SET %s where %s", model.TableName(), cols.Writeable().UpdateString(), model.whereID())
Log(stmt)
_, err := s.NamedExec(stmt, model.Value)
if err != nil {
return errors.WithStack(err)
}
return nil
}
func genericDestroy(s store, model *Model) error {
stmt := fmt.Sprintf("DELETE FROM %s WHERE %s", model.TableName(), model.whereID())
err := genericExec(s, stmt)
if err != nil {
return errors.WithStack(err)
}
return nil
}
func genericExec(s store, stmt string) error {
Log(stmt)
_, err := s.Exec(stmt)
if err != nil {
return errors.WithStack(err)
}
return nil
}
func genericSelectOne(s store, model *Model, query Query) error {
sql, args := query.ToSQL(model)
Log(sql, args...)
err := s.Get(model.Value, sql, args...)
if err != nil {
return errors.WithStack(err)
}
return nil
}
func genericSelectMany(s store, models *Model, query Query) error {
sql, args := query.ToSQL(models)
Log(sql, args...)
err := s.Select(models.Value, sql, args...)
if err != nil {
return errors.WithStack(err)
}
return nil
}
func genericLoadSchema(deets *ConnectionDetails, migrationURL string, r io.Reader) error {
// Open DB connection on the target DB
db, err := sqlx.Open(deets.Dialect, migrationURL)
if err != nil {
return errors.WithMessage(err, fmt.Sprintf("unable to load schema for %s", deets.Database))
}
defer db.Close()
// Get reader contents
contents, err := ioutil.ReadAll(r)
if err != nil {
return err
}
if len(contents) == 0 {
fmt.Printf("schema is empty for %s, skipping\n", deets.Database)
return nil
}
_, err = db.Exec(string(contents))
if err != nil {
return errors.WithMessage(err, fmt.Sprintf("unable to load schema for %s", deets.Database))
}
fmt.Printf("loaded schema for %s\n", deets.Database)
return nil
}