-
Notifications
You must be signed in to change notification settings - Fork 22
/
row.go
174 lines (157 loc) · 4.89 KB
/
row.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package fixtures
import (
"fmt"
"sort"
"strings"
"time"
)
const (
onInsertNow = "ON_INSERT_NOW()"
onUpdateNow = "ON_UPDATE_NOW()"
postgresDriver = "postgres"
)
// Row represents a single database row
type Row struct {
Table string
PK map[string]interface{}
Fields map[string]interface{}
insertColumnLength int
updateColumnLength int
pkColumns []string
pkValues []interface{}
insertColumns []string
updateColumns []string
insertValues []interface{}
updateValues []interface{}
}
// Init loads internal struct variables
func (row *Row) Init() {
// Initial values
row.insertColumnLength = len(row.PK) + len(row.Fields)
row.updateColumnLength = len(row.PK) + len(row.Fields)
row.pkColumns = make([]string, 0)
row.pkValues = make([]interface{}, 0)
row.insertColumns = make([]string, 0)
row.updateColumns = make([]string, 0)
row.insertValues = make([]interface{}, 0)
row.updateValues = make([]interface{}, 0)
// Get and sort map keys
var i int
pkKeys := make([]string, len(row.PK))
i = 0
for pkKey := range row.PK {
pkKeys[i] = pkKey
i++
}
sort.Strings(pkKeys)
fieldKeys := make([]string, len(row.Fields))
i = 0
for fieldKey := range row.Fields {
fieldKeys[i] = fieldKey
i++
}
sort.Strings(fieldKeys)
// Primary keys
for _, pkKey := range pkKeys {
row.pkColumns = append(row.pkColumns, pkKey)
row.pkValues = append(row.pkValues, row.PK[pkKey])
row.insertColumns = append(row.insertColumns, pkKey)
row.updateColumns = append(row.updateColumns, pkKey)
row.insertValues = append(row.insertValues, row.PK[pkKey])
row.updateValues = append(row.updateValues, row.PK[pkKey])
}
// Rest of the fields
for _, fieldKey := range fieldKeys {
sv, ok := row.Fields[fieldKey].(string)
if ok && sv == onInsertNow {
row.insertColumns = append(row.insertColumns, fieldKey)
row.insertValues = append(row.insertValues, time.Now())
row.updateColumnLength--
continue
}
if ok && sv == onUpdateNow {
row.updateColumns = append(row.updateColumns, fieldKey)
row.updateValues = append(row.updateValues, time.Now())
row.insertColumnLength--
continue
}
row.insertColumns = append(row.insertColumns, fieldKey)
row.updateColumns = append(row.updateColumns, fieldKey)
row.insertValues = append(row.insertValues, row.Fields[fieldKey])
row.updateValues = append(row.updateValues, row.Fields[fieldKey])
}
}
// GetInsertColumnsLength returns number of columns for INSERT query
func (row *Row) GetInsertColumnsLength() int {
return row.insertColumnLength
}
// GetUpdateColumnsLength returns number of columns for UDPATE query
func (row *Row) GetUpdateColumnsLength() int {
return row.updateColumnLength
}
// GetInsertColumns returns a slice of column names for INSERT query
func (row *Row) GetInsertColumns() []string {
escapedColumns := make([]string, len(row.insertColumns))
for i, insertColumn := range row.insertColumns {
escapedColumns[i] = fmt.Sprintf("\"%s\"", insertColumn)
}
return escapedColumns
}
// GetUpdateColumns returns a slice of column names for UPDATE query
func (row *Row) GetUpdateColumns() []string {
escapedColumns := make([]string, len(row.updateColumns))
for i, updateColumn := range row.updateColumns {
escapedColumns[i] = fmt.Sprintf("\"%s\"", updateColumn)
}
return escapedColumns
}
// GetInsertValues returns a slice of values for INSERT query
func (row *Row) GetInsertValues() []interface{} {
return row.insertValues
}
// GetUpdateValues returns a slice of values for UPDATE query
func (row *Row) GetUpdateValues() []interface{} {
return row.updateValues
}
// GetInsertPlaceholders returns a slice of placeholders for INSERT query
func (row *Row) GetInsertPlaceholders(driver string) []string {
placeholders := make([]string, row.GetInsertColumnsLength())
for i := 0; i < row.GetInsertColumnsLength(); i++ {
if driver == postgresDriver {
placeholders[i] = fmt.Sprintf("$%d", i+1)
} else {
placeholders[i] = "?"
}
}
return placeholders
}
// GetUpdatePlaceholders returns a slice of placeholders for UPDATE query
func (row *Row) GetUpdatePlaceholders(driver string) []string {
placeholders := make([]string, row.GetUpdateColumnsLength())
for i, c := range row.GetUpdateColumns() {
if driver == postgresDriver {
placeholders[i] = fmt.Sprintf("%s = $%d", c, i+1)
} else {
placeholders[i] = fmt.Sprintf("%s = ?", c)
}
}
return placeholders
}
// GetWhere returns a where condition based on primary key with placeholders
func (row *Row) GetWhere(driver string, i int) string {
wheres := make([]string, len(row.PK))
j := i
for _, c := range row.pkColumns {
if driver == postgresDriver {
wheres[i-j] = fmt.Sprintf("%s = $%d", c, i+1)
} else {
wheres[i-j] = fmt.Sprintf("%s = ?", c)
}
i++
}
return strings.Join(wheres, " AND ")
}
// GetPKValues returns a slice of primary key values
func (row *Row) GetPKValues() []interface{} {
return row.pkValues
}