Skip to content

Commit

Permalink
And or Or operation supports the none condition
Browse files Browse the repository at this point in the history
  • Loading branch information
xgfone committed Jan 20, 2024
1 parent 93e455b commit a786077
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
3 changes: 3 additions & 0 deletions op_condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ func newCondGroup(sep string) OpBuilder {
panic(fmt.Errorf("sqlx: unsupported value type %T for op '%s:%v'", _op.Val, _op.Kind, _op.Op))
}

if len(ss) == 0 {
return ""
}
return fmt.Sprintf("(%s)", strings.Join(ss, sep))
})
}
Expand Down
21 changes: 21 additions & 0 deletions op_condition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,25 @@ func TestAnd(t *testing.T) {
}
}
}

if sql := BuildOper(NewArgsBuilder(MySQL), op.And()); sql != "" {
t.Errorf("expect an empty sql, but got: %s", sql)
}

expectsql = "SELECT `c1`, `c2` FROM `table` WHERE `id`=?"
expectargs = []interface{}{1}
sql, args = Selects("c1", "c2").From("table").Where(op.And(op.Eq("id", 1), op.And())).Build()
if expectsql != sql {
t.Errorf("expect sql: %s; but got: %s;", expectsql, sql)
}

if len(args) != len(expectargs) {
t.Errorf("expect %d args, but got %d", len(expectargs), len(args))
} else {
for i, arg := range args {
if expect := expectargs[i]; expect != arg {
t.Errorf("args %d: expect '%v', but got '%v'", i, expect, arg)
}
}
}
}
4 changes: 4 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,10 @@ func isZero(v reflect.Value) bool {
}

func toslice[S1 ~[]E1, E1, E2 any](srcs S1, to func(E1) E2) (dsts []E2) {
if len(srcs) == 0 {
return
}

dsts = make([]E2, len(srcs))
for i, src := range srcs {
dsts[i] = to(src)
Expand Down

0 comments on commit a786077

Please sign in to comment.