forked from gobuffalo/pop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dialect.go
73 lines (64 loc) · 1.77 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
package pop
import (
"fmt"
. "github.com/markbates/pop/columns"
)
type Dialect interface {
URL() string
MigrationURL() string
Details() *ConnectionDetails
TranslateSQL(sql string) string
Create(store Store, model *Model, cols Columns) error
Update(store Store, model *Model, cols Columns) error
Destroy(store Store, model *Model) error
SelectOne(store Store, model *Model, query Query) error
SelectMany(store Store, models *Model, query Query) error
CreateDB() error
DropDB() error
}
func genericCreate(store Store, model *Model, cols Columns) error {
var id int64
w := cols.Writeable()
query := fmt.Sprintf("INSERT INTO %s (%s) VALUES (%s)", model.TableName(), w.String(), w.SymbolizedString())
if Debug {
Log(query)
}
res, err := store.NamedExec(query, model.Value)
if err != nil {
return err
}
id, err = res.LastInsertId()
if err == nil {
model.setID(int(id))
}
return err
}
func genericUpdate(store Store, model *Model, cols Columns) error {
stmt := fmt.Sprintf("UPDATE %s SET %s where id = %d", model.TableName(), cols.Writeable().UpdateString(), model.ID())
if Debug {
Log(stmt)
}
_, err := store.NamedExec(stmt, model.Value)
return err
}
func genericDestroy(store Store, model *Model) error {
stmt := fmt.Sprintf("DELETE FROM %s WHERE id = %d", model.TableName(), model.ID())
return genericExec(store, stmt)
}
func genericExec(store Store, stmt string) error {
if Debug {
Log(stmt)
}
_, err := store.Exec(stmt)
return err
}
func genericSelectOne(store Store, model *Model, query Query) error {
sql, args := query.ToSQL(model)
err := store.Get(model.Value, sql, args...)
return err
}
func genericSelectMany(store Store, models *Model, query Query) error {
sql, args := query.ToSQL(models)
err := store.Select(models.Value, sql, args...)
return err
}