You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
if the sql is dynamic , how to use sqlc ?
for example:
/* name: ListAuthors :many */
SELECT * FROM authors where id = ? and name = ? and age = ? and school = ? and article_count = ?
ORDER BY name;
There is a multi query , if id and age is not set , the where condition can be omitted ,like this
SELECT * FROM authors where age = ? and school = ? and article_count = ?
You can build advanced queries by using boolean operator in your WHERE clause:
-- name: ListAuthors :manySELECT*FROM authors
WHERE (@name::text=''OR name = @name)
AND (@birth::date='0001-01-01'OR birth = @birth)
AND (@bio::text=''OR bio ILIKE '%'|| @bio ||'%');
The conditions are evaluated as follow:
(@name::text = '' OR name = @name) returns true if @name is an empty string or evaluates to name = @name
AND (@birth::date = '0001-01-01' OR birth = @birth) is evaluated regardless of the result of the first condition, it returns true if @birth is the zero value of time.Time or evalues to birth = @birth
AND (@bio::text = '' OR bio ILIKE '%' || @bio || '%') is evaluated regardless of the result of the second condition, it returns true if @birth if an empty string or evaluates to bio ILIKE '%' || @bio || '%'
You can add as many conditions as you want by following this pattern and you can try it in the playground.
Reacted by Jeffrey Lo, Stas Karkavin, Steve Dignam, Alexandre Rodrigues, James Qualls, Juan Esteban Londoño Tabares, Luis Guzmán, Yann Coleu, ANiWarlock and Haseeb Majid
This only works for non-nullable parameters, or for values where the zero-value is enough. how about the case for boolean?
SELECT * FROM users WHERE (@active::boolean IS NULL OR active = @active)
(optional filtering by active parameter)
you can't use the zero-value of boolean (false).
ofc this can also be true for strings. where string can be NULL, empty string or values string.
Even for queries with LIMIT = $1
you can workaround it using some super high number, but that is just a hack.
you should be able to pass NULL so it falls back to LIMIT ALL.
Yes, it is problematic if you need to use the zero value, there is a discussion on how to fix this #451. Meanwhile, you can use a second boolean to indicate the status of your filter:
-- name: ListUsers :manySELECT*FROM users
WHERE (NOT @is_active::booleanOR active = @active)
(NOT @is_name::booleanOR name = @name);
Activity
aitva commentedon Jun 28, 2021
Hi @whistlesun,
You can build advanced queries by using boolean operator in your
WHEREclause:The conditions are evaluated as follow:
(@name::text = '' OR name = @name)returns true if@nameis an empty string or evaluates toname = @nameAND (@birth::date = '0001-01-01' OR birth = @birth)is evaluated regardless of the result of the first condition, it returns true if@birthis the zero value oftime.Timeor evalues tobirth = @birthAND (@bio::text = '' OR bio ILIKE '%' || @bio || '%')is evaluated regardless of the result of the second condition, it returns true if@birthif an empty string or evaluates tobio ILIKE '%' || @bio || '%'You can add as many conditions as you want by following this pattern and you can try it in the playground.
mrsufgi commentedon Jun 29, 2021
This only works for non-nullable parameters, or for values where the zero-value is enough. how about the case for
boolean?(optional filtering by
activeparameter)you can't use the zero-value of boolean (false).
ofc this can also be true for strings. where string can be NULL, empty string or values string.
Even for queries with LIMIT = $1
you can workaround it using some super high number, but that is just a hack.
you should be able to pass NULL so it falls back to LIMIT ALL.
aitva commentedon Jun 29, 2021
Yes, it is problematic if you need to use the zero value, there is a discussion on how to fix this #451. Meanwhile, you can use a second
booleanto indicate the status of your filter:Calling the generated code from Go:
aitva commentedon Jun 29, 2021
For queries with a
LIMITclause you can useNULLIF:mrsufgi commentedon Jun 29, 2021
@aitva seems like a viable workaround, I'm pretty sure there is an issue with NULLIF flawlessly but looks OK as a bandaid.
whistlesun commentedon Jul 5, 2021
tks
flymedllva commentedon Nov 28, 2021
@aitva And how to live with MySQL?
jalen-qian commentedon Jan 21, 2022
There is the same problem on mysql.