Skip to content

Commit

Permalink
feat: warn when there are args but no placeholders
Browse files Browse the repository at this point in the history
  • Loading branch information
vmihailenco committed Oct 18, 2021
1 parent 225258e commit 06dde21
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 5 deletions.
3 changes: 3 additions & 0 deletions internal/dbtest/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,9 @@ func TestQuery(t *testing.T) {
func(db *bun.DB) schema.QueryAppender {
return db.NewInsert().Model(new(SoftDelete2)).On("CONFLICT DO NOTHING")
},
func(db *bun.DB) schema.QueryAppender {
return db.NewInsert().Model(&Model{}).Returning("")
},
}

timeRE := regexp.MustCompile(`'\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d+(\+\d{2}:\d{2})?'`)
Expand Down
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mysql5-96
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
INSERT INTO `models` (`id`, `str`) VALUES (DEFAULT, '')
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mysql8-96
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
INSERT INTO `models` (`id`, `str`) VALUES (DEFAULT, '')
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-pg-96
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
INSERT INTO "models" ("id", "str") VALUES (DEFAULT, '')
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-pgx-96
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
INSERT INTO "models" ("id", "str") VALUES (DEFAULT, '')
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-sqlite-96
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
INSERT INTO "models" ("str") VALUES ('')
8 changes: 5 additions & 3 deletions query_base.go
Original file line number Diff line number Diff line change
Expand Up @@ -819,9 +819,11 @@ func (q *returningQuery) addReturningField(field *schema.Field) {

func (q *returningQuery) hasReturning() bool {
if len(q.returning) == 1 {
switch q.returning[0].Query {
case "null", "NULL":
return false
if ret := q.returning[0]; len(ret.Args) == 0 {
switch ret.Query {
case "", "null", "NULL":
return false
}
}
}
return len(q.returning) > 0 || len(q.returningFields) > 0
Expand Down
2 changes: 1 addition & 1 deletion query_insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func (q *InsertQuery) WhereOr(query string, args ...interface{}) *InsertQuery {

// Returning adds a RETURNING clause to the query.
//
// To suppress the auto-generated RETURNING clause, use `Returning("NULL")`.
// To suppress the auto-generated RETURNING clause, use `Returning("")`.
func (q *InsertQuery) Returning(query string, args ...interface{}) *InsertQuery {
q.addReturning(schema.SafeQuery(query, args))
return q
Expand Down
13 changes: 12 additions & 1 deletion schema/sqlfmt.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package schema

import (
"strings"

"github.com/uptrace/bun/internal"
)

type QueryAppender interface {
AppendQuery(fmter Formatter, b []byte) ([]byte, error)
}
Expand Down Expand Up @@ -42,8 +48,13 @@ var _ QueryAppender = QueryWithArgs{}
func SafeQuery(query string, args []interface{}) QueryWithArgs {
if args == nil {
args = make([]interface{}, 0)
} else if len(query) > 0 && strings.IndexByte(query, '?') == -1 {
internal.Warn.Printf("query %q has args %v, but no placeholders", query, args)
}
return QueryWithArgs{
Query: query,
Args: args,
}
return QueryWithArgs{Query: query, Args: args}
}

func UnsafeIdent(ident string) QueryWithArgs {
Expand Down

0 comments on commit 06dde21

Please sign in to comment.