elgris / sqrl

Fluent SQL generation for golang
MIT License
279 stars 38 forks source link

How to create dynamic WHERE clause #14

Open PepijnK opened 7 years ago

PepijnK commented 7 years ago

Hi,

Is there support for dynamic where clauses? I could not find any documentation about how to do that. In my case I need to add AND'd conditions with nested OR conditions conditionally, to get something like: WHERE x=? AND (foo>=? OR bar> ?) AND (bar> ?)

I've tried to create an []sq.Sqlizer and add the OR-expressions in there, then feed that to SelectBuilder.Where() but it fails with the message

converting Exec argument # 1's type: unsupported type []sqrl.Sqlizer, a slice

Ideally there is some kind of ConditionBuilder for this I guess.

nuqz commented 7 years ago

@PepijnK Hi, take a look at Or type and it's conj type. According to example you can try something like:

statement.
  Where(sq.Eq{"x": xval}).
  Where(sq.Or{
    sq.GtOrEq{"foo": fooval},
    sq.Gt{"bar": barval1},
  }).
  Where(sq.Gt{"bar": barval2})