-
Notifications
You must be signed in to change notification settings - Fork 33
/
funcmap.go
155 lines (141 loc) · 3.31 KB
/
funcmap.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
package main
import (
"fmt"
"text/template"
)
var tmplFuncMap = template.FuncMap{
"createInsertSQL": createInsertSQL,
"createInsertParams": createInsertParams,
"createInsertScan": createInsertScan,
"createSelectByPkSQL": createSelectByPkSQL,
"createSelectByPkFuncParams": createSelectByPkFuncParams,
"createSelectByPkSQLParams": createSelectByPkSQLParams,
"createSelectByPkScan": createSelectByPkScan,
}
func createSelectByPkSQL(st *Struct) string {
var sql string
var colNames []string
var pkNames []string
for _, c := range st.Table.Columns {
if c.IsPrimaryKey {
pkNames = append(pkNames, c.Name)
}
colNames = append(colNames, c.Name)
}
sql = "SELECT " + flatten(colNames, ", ") + " FROM " + st.Table.Name + " WHERE "
for i, c := range pkNames {
placeHolder := i + 1
if i == 0 {
sql = sql + c + fmt.Sprintf(" = $%d", placeHolder)
} else {
sql = sql + " AND " + c + fmt.Sprintf(" = $%d", placeHolder)
}
}
return sql
}
func createSelectByPkScan(st *Struct) string {
var s []string
for _, f := range st.Fields {
s = append(s, fmt.Sprintf("&r.%s", f.Name))
}
return flatten(s, ", ")
}
func createSelectByPkSQLParams(st *Struct) string {
var fs []string
for i, f := range st.Fields {
if f.Column.IsPrimaryKey {
fs = append(fs, fmt.Sprintf("pk%d", i))
}
}
return flatten(fs, ", ")
}
func createSelectByPkFuncParams(st *Struct) string {
var fs []string
for i, f := range st.Fields {
if f.Column.IsPrimaryKey {
fs = append(fs, fmt.Sprintf("pk%d ", i)+f.Type)
}
}
return flatten(fs, ", ")
}
func createInsertScan(st *Struct) string {
var fs []string
for _, f := range st.Fields {
if f.Column.IsPrimaryKey && st.Table.AutoGenPk {
fs = append(fs, "&r."+f.Name)
}
}
return flatten(fs, ", ")
}
func createInsertParams(st *Struct) string {
var fs []string
for _, f := range st.Fields {
if f.Column.IsPrimaryKey && st.Table.AutoGenPk {
continue
} else {
fs = append(fs, "&r."+f.Name)
}
}
return flatten(fs, ", ")
}
func flatten(elems []string, sep string) string {
var str string
for i, e := range elems {
if i == 0 {
str = str + e
} else {
str = str + sep + e
}
}
return str
}
func placeholders(l []string) string {
var ph string
var j int
for i := range l {
j = i + 1
if i == 0 {
ph = ph + fmt.Sprintf("$%d", j)
} else {
ph = ph + fmt.Sprintf(", $%d", j)
}
}
return ph
}
func createInsertSQL(st *Struct) string {
var sql string
sql = "INSERT INTO " + st.Table.Name + " ("
if len(st.Table.Columns) == 1 && st.Table.Columns[0].IsPrimaryKey && st.Table.AutoGenPk {
sql = sql + st.Table.Columns[0].Name + ") VALUES (DEFAULT)"
} else {
var colNames []string
for _, c := range st.Table.Columns {
if c.IsPrimaryKey && st.Table.AutoGenPk {
continue
} else {
colNames = append(colNames, c.Name)
}
}
sql = sql + flatten(colNames, ", ") + ") VALUES ("
var fieldNames []string
for _, f := range st.Fields {
if f.Column.IsPrimaryKey && st.Table.AutoGenPk {
continue
} else {
fieldNames = append(fieldNames, f.Name)
}
}
sql = sql + placeholders(fieldNames) + ")"
}
if st.Table.AutoGenPk {
sql = sql + " RETURNING "
for i, c := range st.Table.PrimaryKeys {
if i == 0 {
sql = sql + c.Name
} else {
sql = sql + ", " + c.Name
}
}
}
return sql
}