Skip to main content
Hint

This article is generated by AI translation.

Rule-Based Arguments

Use @{...} with the Rule mechanism to elegantly handle common dynamic command scenarios.

Only add name when it is not empty
select * from users where id > :id @{and, name = :name}

AND Rule

AND rule
select * from users where id > :id @{and, name = :name}
  • When the named argument name is not null/empty, and name = ? is appended to the WHERE clause.
  • Final SQL:
    • name empty: select * from users where id > ?
    • name present: select * from users where id > ? and name = ?

OR Rule

OR rule
select * from users where id > :id @{or, name = :name}
  • When name is not null/empty, or name = ? is appended to the WHERE clause.
  • Final SQL:
    • name empty: select * from users where id > ?
    • name present: select * from users where id > ? or name = ?

SET Rule

SET rule
update users set modify_time = ? @{set, status = :status} where id > :id
  • When status is not null/empty, status = ? is appended to the SET clause.
  • Final SQL:
    • status empty: update users set modify_time = ? where id > ?
    • status present: update users set modify_time = ?, status = ? where id > ?

IN Rule

IN rule
select * from users where status = true @{in,and id in :ids}
  • If ids has 3 elements, the IN rule appends and id in (?, ?, ?); the number of placeholders matches the element count.
  • Final SQL:
    • ids empty: select * from users where status = true
    • ids present: select * from users where status = true and id in (?, ?, ?)
About rules
  • dbVisitor ships with many built-in rules for different needs.
  • For details, see here.