jsstuff / xql

SQL builder and utilities library for node.js (runs in browser as well).
The Unlicense
20 stars 2 forks source link

Compound where clauses #10

Closed illogikal closed 3 years ago

illogikal commented 3 years ago

QUERY I WANT SELECT * FROM foos WHERE ((foos.first_field LIKE '%%') OR (foos.other_field LIKE '%%' ) )

My xql code:

xql.SELECT('*').FROM('foos').WHERE('first_field', 'LIKE', '%%')
            .OR_WHERE('other_field', 'LIKE', '%%')

my current xql query gives me this: SELECT * FROM "foos" WHERE "first_field" LIKE '%%' OR "other_field" LIKE '%%'

How do I wrap the parentheses around compound where clauses? I want to do this so I can make further conditions in the query.

kobalicek commented 3 years ago

Can you describe what further conditions do you mean?

With xql you can do something like this:

// AND(...) / OR(...) can be used at any time to combine boolean expressions.
return SELECT()
  .FROM("tasks")
  .WHERE(
    OR(AND(COLUMN("duration").GE(10),
           COLUMN("duration").LE(20))
       ,
       COLUMN("duration").EQ(0)
    )
  );

This way it should always be properly parenthesized

illogikal commented 3 years ago

Uncaught (in promise) TypeError: import_xql.default.COLUMN(...).LIKE is not a function

xql.COLUMN("icon_title").EQ("") works xql.COLUMN("icon_title").LIKE("") doesnt' work Uncaught (in promise) TypeError: import_xql.default.COLUMN(...).LIKE is not a function

with sqlite

illogikal commented 3 years ago

Thanks, I got it now.

illogikal commented 3 years ago

Can you join mulitple clauses like this?

return SELECT()
  .FROM("tasks")
  .WHERE(
    OR(AND(COLUMN("duration").GE(10),
           COLUMN("duration").LE(20))
       ,
       COLUMN("duration").LT(-3),
       COLUMN("duration").GT(1),
       COLUMN("duration").EQ(2)
    )
  );