Skip to content

Commit 069bc9c

Browse files
committed
DB: Make use of the QueryBuilder
1 parent d020f43 commit 069bc9c

File tree

1 file changed

+6
-116
lines changed

1 file changed

+6
-116
lines changed

database/db.go

Lines changed: 6 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"net"
2424
"net/url"
2525
"strconv"
26-
"strings"
2726
"sync"
2827
"time"
2928
)
@@ -229,143 +228,34 @@ func (db *DB) GetAddr() string {
229228

230229
// BuildDeleteStmt returns a DELETE statement for the given struct.
231230
func (db *DB) BuildDeleteStmt(from interface{}) string {
232-
return fmt.Sprintf(
233-
`DELETE FROM "%s" WHERE id IN (?)`,
234-
TableName(from),
235-
)
231+
return NewQB(from).Delete()
236232
}
237233

238234
// BuildInsertStmt returns an INSERT INTO statement for the given struct.
239235
func (db *DB) BuildInsertStmt(into interface{}) (string, int) {
240-
columns := db.columnMap.Columns(into)
241-
242-
return fmt.Sprintf(
243-
`INSERT INTO "%s" ("%s") VALUES (%s)`,
244-
TableName(into),
245-
strings.Join(columns, `", "`),
246-
fmt.Sprintf(":%s", strings.Join(columns, ", :")),
247-
), len(columns)
236+
return NewQB(into).Insert(db)
248237
}
249238

250239
// BuildInsertIgnoreStmt returns an INSERT statement for the specified struct for
251240
// which the database ignores rows that have already been inserted.
252241
func (db *DB) BuildInsertIgnoreStmt(into interface{}) (string, int) {
253-
table := TableName(into)
254-
columns := db.columnMap.Columns(into)
255-
var clause string
256-
257-
switch db.DriverName() {
258-
case MySQL:
259-
// MySQL treats UPDATE id = id as a no-op.
260-
clause = fmt.Sprintf(`ON DUPLICATE KEY UPDATE "%s" = "%s"`, columns[0], columns[0])
261-
case PostgreSQL:
262-
var constraint string
263-
if constrainter, ok := into.(PgsqlOnConflictConstrainter); ok {
264-
constraint = constrainter.PgsqlOnConflictConstraint()
265-
} else {
266-
constraint = "pk_" + table
267-
}
268-
269-
clause = fmt.Sprintf("ON CONFLICT ON CONSTRAINT %s DO NOTHING", constraint)
270-
}
271-
272-
return fmt.Sprintf(
273-
`INSERT INTO "%s" ("%s") VALUES (%s) %s`,
274-
table,
275-
strings.Join(columns, `", "`),
276-
fmt.Sprintf(":%s", strings.Join(columns, ", :")),
277-
clause,
278-
), len(columns)
242+
return NewQB(into).InsertIgnore(db)
279243
}
280244

281245
// BuildSelectStmt returns a SELECT query that creates the FROM part from the given table struct
282246
// and the column list from the specified columns struct.
283247
func (db *DB) BuildSelectStmt(table interface{}, columns interface{}) string {
284-
q := fmt.Sprintf(
285-
`SELECT "%s" FROM "%s"`,
286-
strings.Join(db.columnMap.Columns(columns), `", "`),
287-
TableName(table),
288-
)
289-
290-
if scoper, ok := table.(Scoper); ok {
291-
where, _ := db.BuildWhere(scoper.Scope())
292-
q += ` WHERE ` + where
293-
}
294-
295-
return q
248+
return NewQB(table).SetColumns(db.columnMap.Columns(columns)...).Select(db)
296249
}
297250

298251
// BuildUpdateStmt returns an UPDATE statement for the given struct.
299252
func (db *DB) BuildUpdateStmt(update interface{}) (string, int) {
300-
columns := db.columnMap.Columns(update)
301-
set := make([]string, 0, len(columns))
302-
303-
for _, col := range columns {
304-
set = append(set, fmt.Sprintf(`"%s" = :%s`, col, col))
305-
}
306-
307-
return fmt.Sprintf(
308-
`UPDATE "%s" SET %s WHERE id = :id`,
309-
TableName(update),
310-
strings.Join(set, ", "),
311-
), len(columns) + 1 // +1 because of WHERE id = :id
253+
return NewQB(update).Update(db)
312254
}
313255

314256
// BuildUpsertStmt returns an upsert statement for the given struct.
315257
func (db *DB) BuildUpsertStmt(subject interface{}) (stmt string, placeholders int) {
316-
insertColumns := db.columnMap.Columns(subject)
317-
table := TableName(subject)
318-
var updateColumns []string
319-
320-
if upserter, ok := subject.(Upserter); ok {
321-
updateColumns = db.columnMap.Columns(upserter.Upsert())
322-
} else {
323-
updateColumns = insertColumns
324-
}
325-
326-
var clause, setFormat string
327-
switch db.DriverName() {
328-
case MySQL:
329-
clause = "ON DUPLICATE KEY UPDATE"
330-
setFormat = `"%[1]s" = VALUES("%[1]s")`
331-
case PostgreSQL:
332-
var constraint string
333-
if constrainter, ok := subject.(PgsqlOnConflictConstrainter); ok {
334-
constraint = constrainter.PgsqlOnConflictConstraint()
335-
} else {
336-
constraint = "pk_" + table
337-
}
338-
339-
clause = fmt.Sprintf("ON CONFLICT ON CONSTRAINT %s DO UPDATE SET", constraint)
340-
setFormat = `"%[1]s" = EXCLUDED."%[1]s"`
341-
}
342-
343-
set := make([]string, 0, len(updateColumns))
344-
345-
for _, col := range updateColumns {
346-
set = append(set, fmt.Sprintf(setFormat, col))
347-
}
348-
349-
return fmt.Sprintf(
350-
`INSERT INTO "%s" ("%s") VALUES (%s) %s %s`,
351-
table,
352-
strings.Join(insertColumns, `", "`),
353-
fmt.Sprintf(":%s", strings.Join(insertColumns, ",:")),
354-
clause,
355-
strings.Join(set, ","),
356-
), len(insertColumns)
357-
}
358-
359-
// BuildWhere returns a WHERE clause with named placeholder conditions built from the specified struct
360-
// combined with the AND operator.
361-
func (db *DB) BuildWhere(subject interface{}) (string, int) {
362-
columns := db.columnMap.Columns(subject)
363-
where := make([]string, 0, len(columns))
364-
for _, col := range columns {
365-
where = append(where, fmt.Sprintf(`"%s" = :%s`, col, col))
366-
}
367-
368-
return strings.Join(where, ` AND `), len(columns)
258+
return NewQB(subject).Upsert(db)
369259
}
370260

371261
// OnSuccess is a callback for successful (bulk) DML operations.

0 commit comments

Comments
 (0)