This repository has been archived by the owner on Jul 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
mapper.tmpl.go
160 lines (140 loc) · 4.29 KB
/
mapper.tmpl.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package tablestruct
var supportTemplate = `
// generated mechanically by tablestruct, do not edit!!
package {{.Package}}
type Scanner interface {
Scan(...interface{}) error
}
`
var mapperTemplate = `
// generated mechanically by tablestruct, do not edit!!
package {{.Package}}
import (
{{range .Imports}}{{.}}
{{end}}
)
{{range .TableMaps}}
type {{.MapperType}} struct {
{{range .MapperFields}}{{.}}
{{end}}
}
func New{{.MapperType}} (db *sql.DB) *{{.MapperType}} {
m := &{{.MapperType}}{
db: db,
sql: make(map[string]string),
stmt: make(map[string]*sql.Stmt),
}
m.prepareStatements()
return m
}
func ({{.VarName}} {{.MapperType}}) prepareStatements() {
var rawSql = map[string]string{
{{if .Mapper.PrimaryKey}}
"Get": "SELECT {{.ColumnList}} FROM {{.Table}} WHERE {{.Mapper.PrimaryKey.Column}} = $1",
"Update": "UPDATE {{.Table}} SET {{.Mapper.UpdateList}} WHERE {{.Mapper.PrimaryKey.Column}} = ${{len .Mapper.Columns | add 1}}",
"Insert": "INSERT INTO {{.Table}} VALUES ({{.Mapper.InsertList}}) RETURNING {{.Mapper.PrimaryKey.Column}}",
"Delete": "DELETE FROM {{.Table}} WHERE {{.Mapper.PrimaryKey.Column}} = $1",
{{end}}
"All": "SELECT {{.ColumnList}} FROM {{.Table}}",
}
for k, v := range rawSql {
stmt, err := {{.VarName}}.db.Prepare(v)
if err != nil {
// TODO(paulsmith): return error instead.
log.Printf("SQL: %q", v)
log.Fatalf("preparing %s SQL: %v", k, err)
}
{{.VarName}}.stmt[k] = stmt
{{.VarName}}.sql[k] = v
}
}
func ({{.VarName}} {{.MapperType}}) loadObj(scanner Scanner) (obj *{{.StructType}}, err error) {
obj = new({{.StructType}})
dest := []interface{}{
{{range .Fields}}&obj.{{.}},
{{end}}
}
err = scanner.Scan(dest...)
return
}
func ({{.VarName}} {{.MapperType}}) Get(key int64) (*{{.StructType}}, error) {
row := {{.VarName}}.stmt["Get"].QueryRow(key)
return {{.VarName}}.loadObj(row)
}
{{if .Mapper.PrimaryKey}}
func ({{.VarName}} {{.MapperType}}) Update(obj *{{.StructType}}) error {
args := []interface{}{
{{range .Fields}}obj.{{.}},
{{end}}
obj.{{.Mapper.PrimaryKey.Field}},
}
_, err := {{.VarName}}.stmt["Update"].Exec(args...)
return err
}
func ({{.VarName}} {{.MapperType}}) insert(obj *{{.StructType}}, stmt *sql.Stmt) error {
args := []interface{}{
{{range .Mapper.InsertFields}}obj.{{.}},
{{end}}
}
row := stmt.QueryRow(args...)
err := row.Scan(&obj.{{.Mapper.PrimaryKey.Field}})
return err
}
func ({{.VarName}} {{.MapperType}}) Insert(obj *{{.StructType}}) error {
return {{.VarName}}.insert(obj, {{.VarName}}.stmt["Insert"])
}
func ({{.VarName}} {{.MapperType}}) InsertMany(objs []*{{.StructType}}) error {
tx, err := {{.VarName}}.db.Begin()
if err != nil {
return err
}
stmt := tx.Stmt({{.VarName}}.stmt["Insert"])
for _, obj := range objs {
err := {{.VarName}}.insert(obj, stmt)
if err != nil {
return err
}
}
return tx.Commit()
}
{{end}}
func ({{.VarName}} {{.MapperType}}) loadManyObjs(rows *sql.Rows) ([]*{{.StructType}}, error) {
var objs []*{{.StructType}}
for rows.Next() {
obj, err := {{.VarName}}.loadObj(rows)
if err != nil {
return nil, err
}
objs = append(objs, obj)
}
if err := rows.Err(); err != nil {
return nil, err
}
return objs, nil
}
func ({{.VarName}} {{.MapperType}}) FindWhere(where string) ([]*{{.StructType}}, error) {
sql := "SELECT {{.ColumnList}} FROM {{.Table}} WHERE " + where
rows, err := {{.VarName}}.db.Query(sql)
if err != nil {
return nil, err
}
return {{.VarName}}.loadManyObjs(rows)
}
func ({{.VarName}} {{.MapperType}}) All() ([]*{{.StructType}}, error) {
rows, err := {{.VarName}}.stmt["All"].Query()
if err != nil {
return nil, err
}
return {{.VarName}}.loadManyObjs(rows)
}
{{if .Mapper.PrimaryKey}}
func ({{.VarName}} {{.MapperType}}) Delete(obj *{{.StructType}}) error {
_, err := {{.VarName}}.stmt["Delete"].Exec(obj.{{.Mapper.PrimaryKey.Field}})
return err
}
{{end}}
func ({{.VarName}} {{.MapperType}}) Table() string {
return "{{.Table}}"
}
{{end}}
`