forked from gobuffalo/pop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dialect.go
103 lines (93 loc) · 2.86 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
package pop
import (
"encoding/gob"
"fmt"
"io"
. "github.com/markbates/pop/columns"
"github.com/markbates/pop/fizz"
"github.com/pkg/errors"
uuid "github.com/satori/go.uuid"
)
func init() {
gob.Register(uuid.UUID{})
}
type dialect interface {
URL() string
MigrationURL() string
Details() *ConnectionDetails
TranslateSQL(string) string
Create(store, *Model, Columns) error
Update(store, *Model, 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) error {
keyType := model.PrimaryKeyType()
switch keyType {
case "int":
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.Wrapf(err, "create: couldn't execute named statement %s", query)
}
id, err = res.LastInsertId()
if err == nil {
model.setID(int(id))
}
return errors.Wrap(err, "couldn't get the last inserted id")
case "UUID":
model.setID(uuid.NewV4())
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)
return err
}
return errors.Errorf("can not use %s as a primary key type!", keyType)
}
func genericUpdate(s store, model *Model, cols 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)
return errors.Wrapf(err, "update: couldn't execute named statement %s", stmt)
}
func genericDestroy(s store, model *Model) error {
stmt := fmt.Sprintf("DELETE FROM %s WHERE %s", model.TableName(), model.whereID())
return errors.Wrapf(genericExec(s, stmt), "destroy: couldn't execute named statement %s", stmt)
}
func genericExec(s store, stmt string) error {
Log(stmt)
_, err := s.Exec(stmt)
return errors.Wrapf(err, "couldn't execute statement %s", stmt)
}
func genericSelectOne(s store, model *Model, query Query) error {
sql, args := query.ToSQL(model)
Log(sql, args...)
err := s.Get(model.Value, sql, args...)
return errors.Wrapf(err, "couldn't select one %s %+v", sql, args)
}
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.Wrapf(err, "couldn't select many %s %+v", sql, args)
}
return nil
}