forked from gobuffalo/pop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
postgresql.go
136 lines (117 loc) · 3.42 KB
/
postgresql.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
package pop
import (
"fmt"
"os/exec"
"strconv"
"sync"
_ "github.com/lib/pq"
"github.com/markbates/going/clam"
. "github.com/markbates/pop/columns"
"github.com/markbates/pop/fizz"
"github.com/markbates/pop/fizz/translators"
"github.com/pkg/errors"
)
type postgresql struct {
translateCache map[string]string
mu sync.Mutex
ConnectionDetails *ConnectionDetails
}
func (p *postgresql) Details() *ConnectionDetails {
return p.ConnectionDetails
}
func (p *postgresql) Create(s store, model *Model, cols Columns) error {
cols.Remove("id")
id := struct {
ID int `db:"id"`
}{}
w := cols.Writeable()
query := fmt.Sprintf("INSERT INTO %s (%s) VALUES (%s) returning id", model.TableName(), w.String(), w.SymbolizedString())
Log(query)
stmt, err := s.PrepareNamed(query)
if err != nil {
return errors.Wrapf(err, "postgres error preparing insert statement %s", query)
}
err = stmt.Get(&id, model.Value)
if err != nil {
return errors.Wrap(err, "postgres error inserting record")
}
model.setID(id.ID)
return nil
}
func (p *postgresql) Update(s store, model *Model, cols Columns) error {
return errors.Wrap(genericUpdate(s, model, cols), "postgres update")
}
func (p *postgresql) Destroy(s store, model *Model) error {
return errors.Wrap(genericDestroy(s, model), "postgres destroy")
}
func (p *postgresql) SelectOne(s store, model *Model, query Query) error {
return errors.Wrap(genericSelectOne(s, model, query), "postgres select one")
}
func (p *postgresql) SelectMany(s store, models *Model, query Query) error {
return errors.Wrap(genericSelectMany(s, models, query), "postgres select many")
}
func (p *postgresql) CreateDB() error {
// createdb -h db -p 5432 -U postgres enterprise_development
deets := p.ConnectionDetails
cmd := exec.Command("createdb", "-e", "-h", deets.Host, "-p", deets.Port, "-U", deets.User, deets.Database)
err := clam.RunAndListen(cmd, func(s string) {
fmt.Println(s)
})
return errors.Wrapf(err, "error creating PostgreSQL database %s", deets.Database)
}
func (p *postgresql) DropDB() error {
deets := p.ConnectionDetails
cmd := exec.Command("dropdb", "-e", "-h", deets.Host, "-p", deets.Port, "-U", deets.User, deets.Database)
err := clam.RunAndListen(cmd, func(s string) {
fmt.Println(s)
})
return errors.Wrapf(err, "error dropping PostgreSQL database %s", deets.Database)
}
func (m *postgresql) URL() string {
c := m.ConnectionDetails
if c.URL != "" {
return c.URL
}
s := "postgres://%s:%s@%s:%s/%s?sslmode=disable"
return fmt.Sprintf(s, c.User, c.Password, c.Host, c.Port, c.Database)
}
func (m *postgresql) MigrationURL() string {
return m.URL()
}
func (p *postgresql) TranslateSQL(sql string) string {
defer p.mu.Unlock()
p.mu.Lock()
if csql, ok := p.translateCache[sql]; ok {
return csql
}
curr := 1
out := make([]byte, 0, len(sql))
for i := 0; i < len(sql); i++ {
if sql[i] == '?' {
str := "$" + strconv.Itoa(curr)
for _, char := range str {
out = append(out, byte(char))
}
curr += 1
} else {
out = append(out, sql[i])
}
}
csql := string(out)
p.translateCache[sql] = csql
return csql
}
func (p *postgresql) FizzTranslator() fizz.Translator {
return translators.NewPostgres()
}
func (p *postgresql) Lock(fn func() error) error {
return fn()
}
func newPostgreSQL(deets *ConnectionDetails) dialect {
cd := &postgresql{
ConnectionDetails: deets,
translateCache: map[string]string{},
mu: sync.Mutex{},
}
return cd
}