Query with ANDs and ORs - struggling with syntax
I want to be able to create a query that looks like this - i.e. has a first WHERE section
that must be true then an either/or but struggling to get the syntax right
SELECT * FROM user
WHERE
(date = '2022-01-01' AND area = 'south')
AND (
(
amount = 20
AND
status = 'pending'
)
OR
(
assignee = 'bob'
AND
amount = 10
)
)
I've put together an example on the playground - any pointers, greatly appreciated!
https://wirekang.github.io/kysely-playground/?p=f&i=-NQeDYTzwAp8pwmb5cWT
49 Replies
Hey š
Appreciate the playground link!
https://wirekang.github.io/kysely-playground/?p=f&i=-NQefNT9sPQKfrEtbKRq
Great, thanks again. Managed to apply that to my more complex real-world example š
We're releasing a brand new way of doing things as far as
.where
and .having
go. We'd love your feedback once it drops.With the new expression builder, the above query would look like this
or full-blown explicit mode:
This is much clearer to my eye, just one question and excuse my likely ignorance, what does
cmp
stand for/mean? I mean, I think it's obvious what it's doing but I'm struggling for what its name means"compare"
I don't love the name either. Any suggestions?
It can't be
where
since the expression builder can be used in any context
compare
is a bit too long.apply?
That sounds like a mutating method that applies something somewhere
i guess that might be dangerous/confusing given the built in
Would've
cmpr
been better?Match?
Not bad
Unknown Userā¢2y ago
Message Not Public
Sign In & Join Server To View
It's so long. All you see in the code is
compare compare compare compare
Actually not that bad in this example
If we use a long name for comare
we should use a long name for everything else in the expression builder too. I'm not sure that's a good idea. It would be weird to have bin
, fn
, val
, lit
and compare
If we use long names for all of those, it's goodbye to oneliners
The idea with short names was to keep them out of the way. The user would see the "SQL" around the typescript and know what the methods mean. No matter what names we use, it won't help the discoverability. People won't know to look for compare
any more than cmp
. People learn by looking at examples.Unknown Userā¢2y ago
Message Not Public
Sign In & Join Server To View
but with intellisense, would not matter much..and co-pilot. š¤
If kysely is first ORM for a beginner,Kysely is neither an ORM or for beginners IMO. It's a dumb query builder and you really need to know your SQL to do anything useful.
Yeah, these methods are non-sql, consumers will have to check documentation/examples regardless
dumb
in the sense that there is no extra logic on top of converting function calls to SQL. Not dumb
as in "this is dumb" šWe'd be in trouble if our sql methods were ambiguous
but with intellisense, would not matter muchThe point is not that it's long to write. It's long to read
We also need to make sure we don't step on reserved words, from the 3 built-in dialects, and from SQL spec in general.
These methods need to scream "oh this is kysely stuff"
I wish there was some widely known abbreviation for this like there are
eq
, lt
, lte
, gt
etc.
Those are also nonsense, but everyone knows what they meanI think
cmpr
is a good improvement
bin
is also open for debateYep
it's bad since it means binary
bop
- binary operation? š¤·š»āāļøbex
could be better as you suggested somewhere
Hey Bob
Longer version ropert
bino
haha
biex
, bexp
bxp
All equally nonsense, but all better than
bin
which does have a widely known (wrong) meaningMaybe we should get a tik toker to chime in
their generation makes up all sorts of abbr
Maybe we should get a tik toker to chime insus
twop
- two argument operation
"hey don't forget to add that twop!"Unknown Userā¢2y ago
Message Not Public
Sign In & Join Server To View
I think I like
bex
the most. Maybe just because I've had more time to digest that one
Do you have a favorite?bex
& bxp
are my favorites right now
bxp
is slightly more comfortable to type
on a QWERTY at leastSo
bxp
and cmpr
?
š©āāļø ?Yeah let's go for it
Any objections? @here
Unknown Userā¢2y ago
Message Not Public
Sign In & Join Server To View
Arbitrary binary expression. Like
age + 1
.Unknown Userā¢2y ago
Message Not Public
Sign In & Join Server To View
I think cmpr is an improvement. I think if there's a good description in the intellisense hover-over, users will quickly internalise the meanings.
@Igal Do you want to do the renaming in your PR?
OK
done
Arbitrary binary expression. Like age + 1.isn't
bxp
basically arit
?
the way I understood it, it was a non-specific binary expression (that can also be a comparison expression and arithmetic expression)
... now its doneisn't bxp basically aritI changed it. You can now use comparison operators there too.
So maybe we should introduce
arit
too?I'd be more inclined to getting rid of
cmpr
than adding more versions that do the same thingOK
We've released v0.26 yesterday, simplifying all of this.