Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions gen/sqlx.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,25 @@ type sqlxContext struct {
}

func (ctx *sqlxContext) Build(w io.Writer) error {
const (
constbindOption = "CONSTBIND"
bindOption = "BIND"
)

var fixedMethods []*Method = nil
for i, method := range ctx.Methods {
if l := len(method.Out); l == 0 || !checkErrorType(method.Out[l-1]) {
return fmt.Errorf("checkErrorType: no 'error' found in method %s returned values",
quote(method.Ident))
}

// Check for conflicting CONSTBIND and BIND options
opts := method.SqlxOptions()
if hasOption(opts, constbindOption) && hasOption(opts, bindOption) {
return fmt.Errorf("method %s: CONSTBIND and BIND options are mutually exclusive, please use only one of them",
quote(method.Ident))
}

if method.SingleScan() != "" {
if len(method.Out) != 1 {
return fmt.Errorf("%s method expects only error returned value when `scan(expr)` option has been specified",
Expand Down
14 changes: 14 additions & 0 deletions gen/sqlx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,4 +205,18 @@ func TestBuildSqlx(t *testing.T) {
return
}
})
t.Run("fail_constbind_bind_conflict", func(t *testing.T) {
builder, ok := newBuilder(t)
if !ok {
return
}
if err := runTest(genFile, builder); err == nil {
t.Errorf("build: expects errors, got nil")
return
} else if !strings.Contains(err.Error(),
"CONSTBIND and BIND options are mutually exclusive") {
t.Errorf("build: expects ConstBindBindConflict error, got => %s", err)
return
}
})
}
7 changes: 7 additions & 0 deletions gen/testdata/sqlx/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,10 @@ type User struct {
Name string
Age int
}

//go:generate defc [mode] [output] [features...] TestBuildSqlx/fail_constbind_bind_conflict
type FailConstBindBindConflict interface {
// GetUser query constbind bind
// SELECT * FROM user WHERE username = ${user.Name} AND age > ${user.Age};
GetUser(ctx context.Context, user *User) (*User, error)
}
2 changes: 1 addition & 1 deletion runtime/version.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package defc

const Version = "v1.44.1"
const Version = "v1.44.2"
Loading