forked from gobuffalo/pop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
postgresql.go
133 lines (114 loc) · 3.03 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
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"
)
type PostgreSQL struct {
translateCache map[string]string
mu sync.Mutex
ConnectionDetails *ConnectionDetails
}
func (p *PostgreSQL) Details() *ConnectionDetails {
return p.ConnectionDetails
}
func (p *PostgreSQL) Create(store 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 := store.PrepareNamed(query)
if err != nil {
return err
}
err = stmt.Get(&id, model.Value)
if err == nil {
model.setID(id.ID)
}
return err
}
func (p *PostgreSQL) Update(store Store, model *Model, cols Columns) error {
return genericUpdate(store, model, cols)
}
func (p *PostgreSQL) Destroy(store Store, model *Model) error {
return genericDestroy(store, model)
}
func (p *PostgreSQL) SelectOne(store Store, model *Model, query Query) error {
return genericSelectOne(store, model, query)
}
func (p *PostgreSQL) SelectMany(store Store, models *Model, query Query) error {
return genericSelectMany(store, models, query)
}
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)
return clam.RunAndListen(cmd, func(s string) {
fmt.Println(s)
})
}
func (p *PostgreSQL) DropDB() error {
deets := p.ConnectionDetails
cmd := exec.Command("dropdb", "-e", "-h", deets.Host, "-p", deets.Port, "-U", deets.User, deets.Database)
return clam.RunAndListen(cmd, func(s string) {
fmt.Println(s)
})
}
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 {
deets.Parse("5432")
cd := &PostgreSQL{
ConnectionDetails: deets,
translateCache: map[string]string{},
mu: sync.Mutex{},
}
return cd
}