-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathquery_sql_and_execute.go
209 lines (178 loc) · 7.06 KB
/
query_sql_and_execute.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package lore
import (
"errors"
"github.com/Masterminds/squirrel"
"github.com/jmoiron/sqlx"
)
/*
SelectModelByPrimaryKey builds and executes a SQL statement on the db similar to the following SQL,
scanning the result into resultSinglePtr. This is essentially a convenience wrapper around
BuildSqlSelectStar and ExecuteThenParseSingle.
`SELECT * FROM <table> WHERE <primary field key> = <primary field value> LIMIT 1;`
*/
func SelectModelByPrimaryKey(mi ModelInterface, db *sqlx.DB, resultSinglePtr interface{}) (found bool, err error) {
// Ensure non-empty primary field key.
if mi.DbPrimaryFieldKey() == "" {
return false, errors.New(_ERR_EMPTY_PRIMARY_KEY)
}
// Build query.
q := NewQuery(mi)
q.SetSqlBuilder(
q.BuildSqlSelectStar().
Where(squirrel.Eq{
mi.DbPrimaryFieldKey(): mi.DbPrimaryFieldValue(),
}).
Limit(1),
)
// Execute ParseSingle query to return single, if found.
return q.ExecuteThenParseSingle(db, resultSinglePtr)
}
/*
SelectModelWhere builds and executes a SQL statement on the db similar to the following SQL,
scanning the result into resultSinglePtr. This is essentially a convenience wrapper around
BuildSqlSelectStar and ExecuteThenParseSingle, applying the WHERE clause accordingly.
Note that where is a pointer; if no where is desired, pass nil instead. When desired, this is
typically something like a squirrel.Eq instance, etc. For your convenience, you can use lore.Where
to build a single Where-ish object around a squirrel.Eq/squirrel.Gt/etc.
`SELECT * FROM <table> WHERE <where conditions> LIMIT 1;`
*/
func SelectModelWhere(mi ModelInterface, db *sqlx.DB, where *sqlPart, resultSinglePtr interface{}) (found bool, err error) {
// Build query.
q := NewQuery(mi)
qSqlBuilder := q.BuildSqlSelectStar()
// Add where if non-nil.
if where != nil {
qSqlBuilder = qSqlBuilder.Where(*where)
}
q.SetSqlBuilder(qSqlBuilder)
// Execute ParseSingle query.
return q.ExecuteThenParseSingle(db, resultSinglePtr)
}
/*
SelectModelsWhere is analogous to SelectModelWhere, but wraps ExecuteThenParseList instead of
ExecuteThenParseSingle, and allows appying a LIMIT clause too.
Note that limit is a pointer here - if nil is supplied, no limit is set on the SQL statement;
otherwise, the underlying limit uint64 is applied.
`SELECT * FROM <table> WHERE <where conditions> LIMIT <limit>;`
*/
func SelectModelsWhere(mi ModelInterface, db *sqlx.DB, where *sqlPart, limit *uint64, resultListPtr interface{}) (numRowsAffected uint64, err error) {
// Build query.
q := NewQuery(mi)
qSqlBuilder := q.BuildSqlSelectStar()
// Add where if non-nil.
if where != nil {
qSqlBuilder = qSqlBuilder.Where(*where)
}
// Add limit if non-nil.
if limit != nil {
qSqlBuilder = qSqlBuilder.Limit(*limit)
}
q.SetSqlBuilder(qSqlBuilder)
// Execute ParseList query.
return q.ExecuteThenParseList(db, resultListPtr)
}
/*
InsertNewModel builds and executes a SQL statement on the db similar to the following SQL, scanning
the result into resultSinglePtr. This is essentially a convenience wrapper around
BuildSqlInsertModel and ExecuteThenParseSingle.
`INSERT INTO <table> (<columns from DbFieldMap>) VALUES (<values from DbFieldMap>) RETURNING * ;`
*/
func InsertNewModel(mi ModelInterface, db *sqlx.DB, resultSinglePtr interface{}) error {
// Build query.
q := NewQuery(mi)
qSqlBuilder, err := q.BuildSqlInsertModel()
if err != nil {
return err
}
q.SetSqlBuilder(
qSqlBuilder.Suffix(RETURNING_STAR),
)
// Execute ParseSingle query.
_, err = q.ExecuteThenParseSingle(db, resultSinglePtr)
return err
}
/*
UpdateModelByPrimaryKey builds and executes a SQL statement on the db similar to the following SQL,
scanning the result into resultSinglePtr. This is essentially a convenience wrapper around
BuildSqlUpdateModelByPrimaryKey and ExecuteThenParseSingle.
`UPDATE <table> SET <columns and values from DbFieldMap>
WHERE <primary field key> = <primary field value> RETURNING * ;`
*/
func UpdateModelByPrimaryKey(mi ModelInterface, db *sqlx.DB, resultSinglePtr interface{}) (found bool, err error) {
// Build query.
q := NewQuery(mi)
qSqlBuilder, err := q.BuildSqlUpdateModelByPrimaryKey()
if err != nil {
return false, err
}
q.SetSqlBuilder(
qSqlBuilder.Suffix(RETURNING_STAR),
)
// Execute ParseSingle query.
return q.ExecuteThenParseSingle(db, resultSinglePtr)
}
/*
UpdateSetMapWhere builds and executes a SQL statement on the db similar to the following SQL,
scanning the result into resultListPtr. This is essentially a convenience wrapper around
BuildSqlUpdateSetMap and ExecuteThenParseList, applying the WHERE clause accordingly.
Note that where is a pointer; if no where is desired, pass nil instead. When desired, this is
typically something like a squirrel.Eq instance, etc. For your convenience, you can use lore.Where
to build a single Where-ish object around a squirrel.Eq/squirrel.Gt/etc.
`UPDATE <table> SET <columns and values from map> WHERE <where conditions> RETURNING * ;`
*/
func UpdateSetMapWhere(mi ModelInterface, db *sqlx.DB, m map[string]interface{}, where *sqlPart, resultListPtr interface{}) (numRowsAffected uint64, err error) {
// Build query.
q := NewQuery(mi)
qSqlBuilder := q.BuildSqlUpdateSetMap(m)
// Add where if non-nil.
if where != nil {
qSqlBuilder = qSqlBuilder.Where(*where)
}
q.SetSqlBuilder(
qSqlBuilder.Suffix(RETURNING_STAR),
)
// Execute ParseList query.
return q.ExecuteThenParseList(db, resultListPtr)
}
/*
DeleteModelByPrimaryKey builds and executes a SQL statement on the db similar to the following SQL,
scanning the result into resultSinglePtr. This is essentially a convenience wrapper around
BuildSqlDeleteModelByPrimaryKey and ExecuteThenParseSingle.
`DELETE FROM <table> WHERE <primary field key> = <primary field value> RETURNING * ;`
*/
func DeleteModelByPrimaryKey(mi ModelInterface, db *sqlx.DB, resultSinglePtr interface{}) (found bool, err error) {
// Build query.
q := NewQuery(mi)
qSqlBuilder, err := q.BuildSqlDeleteModelByPrimaryKey()
if err != nil {
return false, err
}
q.SetSqlBuilder(
qSqlBuilder.Suffix(RETURNING_STAR),
)
// Execute ParseSingle query.
return q.ExecuteThenParseSingle(db, resultSinglePtr)
}
/*
DeleteModelsWhere builds and executes a SQL statement on the db similar to the following SQL,
scanning the result into resultListPtr. This is essentially a convenience wrapper around
BuildSqlDelete and ExecuteThenParseList, applying the WHERE clause accordingly.
Note that where is a pointer; if no where is desired, pass nil instead. When desired, this is
typically something like a squirrel.Eq instance, etc. For your convenience, you can use lore.Where
to build a single Where-ish object around a squirrel.Eq/squirrel.Gt/etc.
`DELETE FROM <table> WHERE <where conditions> RETURNING * ;`
*/
func DeleteModelsWhere(mi ModelInterface, db *sqlx.DB, where *sqlPart, resultListPtr interface{}) (numRowsAffected uint64, err error) {
// Build query.
q := NewQuery(mi)
qSqlBuilder := q.BuildSqlDelete()
// Add where if non-nil.
if where != nil {
qSqlBuilder = qSqlBuilder.Where(*where)
}
q.SetSqlBuilder(
qSqlBuilder.Suffix(RETURNING_STAR),
)
// Execute ParseList query.
return q.ExecuteThenParseList(db, resultListPtr)
}