-
Notifications
You must be signed in to change notification settings - Fork 52
/
postgres.go
76 lines (68 loc) · 1.61 KB
/
postgres.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
package hood
import (
"fmt"
_ "github.com/lib/pq"
"strings"
"time"
)
func init() {
RegisterDialect("postgres", NewPostgres())
}
type postgres struct {
base
}
func NewPostgres() Dialect {
d := &postgres{}
d.base.Dialect = d
return d
}
func (d *postgres) SqlType(f interface{}, size int) string {
switch f.(type) {
case Id:
return "bigserial"
case time.Time, Created, Updated:
return "timestamp with time zone"
case bool:
return "boolean"
case int, int8, int16, int32, uint, uint8, uint16, uint32:
return "integer"
case int64, uint64:
return "bigint"
case float32, float64:
return "double precision"
case []byte:
return "bytea"
case string:
if size > 0 && size < 65532 {
return fmt.Sprintf("varchar(%d)", size)
}
return "text"
}
panic("invalid sql type")
}
func (d *postgres) Insert(hood *Hood, model *Model) (Id, error) {
sql, args := d.Dialect.InsertSql(model)
var id int64
err := hood.QueryRow(sql, args...).Scan(&id)
return Id(id), err
}
func (d *postgres) InsertSql(model *Model) (string, []interface{}) {
m := 0
columns, markers, values := columnsMarkersAndValuesForModel(d.Dialect, model, &m)
quotedColumns := make([]string, 0, len(columns))
for _, c := range columns {
quotedColumns = append(quotedColumns, d.Dialect.Quote(c))
}
sql := fmt.Sprintf(
"INSERT INTO %v (%v) VALUES (%v) RETURNING %v",
d.Dialect.Quote(model.Table),
strings.Join(quotedColumns, ", "),
strings.Join(markers, ", "),
d.Dialect.Quote(model.Pk.Name),
)
return sql, values
}
func (d *postgres) KeywordAutoIncrement() string {
// postgres has not auto increment keyword, uses SERIAL type
return ""
}