Question
I can't understand why the following code isn't working. Please help me figure it out.
=: setList(table: 'main_table'; field: 'status' = 5; where: cond`'date' < $#nd && 'status' != 5`; log: true)
Answer
The fact is that the cond
syntactic construction does not work in the where parameters of functions. In where, a special qrow
construction can be used, which allows you to set various search conditions for the database.
=: select(table: $#ntn; field: 'id'; where: qrow`'field_1' > 100 || 'field_2' = true`)
qrow
supports operators:
&&
— AND
||
— OR
()
— grouping
Comparison operators are similar to the operators of the rest of the system.
Name parameters are specified in single quotes 'field_1'
In the comparison 'field_1' > 100
, one name parameter must be used.
Name parameters can be used on both sides of the comparison 'field_1' > 'field_2'
— select rows where field_1
> field_2
.
Example with grouping:
=: select(table: $#ntn; field: 'id'; where: qrow`'(field_1' > 100 && 'field_3' = true) || 'field_2' = true`)
field_1
is greater than 100 and field_3
= true or field_2 equals true
At the same level, operators must be the same!
Together with qrow, several where
can be used, they work in AND
mode:
=: select(table: $#ntn; field: 'id'; where: qrow`'(field_1' > 100 && 'field_3' = true) || 'field_2' = true`; where: 'filed_4' = false)
(field_1
is greater than 100 and field_3
= true or field_2
equals true) and filed_4
= false
If you use id
in comparisons in AND
mode, the query will execute much faster if you put this condition in a separate where first
=: select(table: $#ntn; field: 'id'; where: 'id' != #id; where: qrow`'(field_1' > 100 && 'field_3' = true) || 'field_2' = true`)
id
is not equal to the current row id and (field_1
is greater than 100 and field_3 = true
or field_2
equals true)
In your case, the code with qrow will look like this:
=: setList(table: 'main_table'; field: 'status' = 5; where: qrow`'date' < $#nd && 'status' != 5`; log: true)
But for such simple conditions, it is better to use several where parameters:
=: setList(table: 'main_table'; field: 'status' = 5; where: 'date' < $#nd; where: 'status' != 5; log: true)