forked from gobuffalo/pop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dialect.go
128 lines (118 loc) · 2.94 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
package pop
import (
"encoding/gob"
"fmt"
"io"
"github.com/gobuffalo/pop/columns"
"github.com/gobuffalo/pop/fizz"
"github.com/gobuffalo/uuid"
"github.com/pkg/errors"
)
func init() {
gob.Register(uuid.UUID{})
}
type dialect interface {
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":
if model.ID() == emptyUUID {
u, err := uuid.NewV4()
if err != nil {
return errors.WithStack(err)
}
model.setID(u)
}
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
}