SqlAnd

class SqlAnd(*terms)

Combines terms with AND. Created via SQL.and_expr(*terms). Inherits from SqlObj (via SqlBoolOp).

Parameters:

terms – Zero or more initial SqlTerm or SqlBoolOp instances.

add(expr, op=None, value=None)

Add a term. If expr is a string with op and value, it is wrapped in a SqlTerm. See SqlTerm for the full list of supported parameter variants.

Parameters:
  • expr – A string (column name or literal expression), SqlTerm, or SqlBoolOp.

  • op – Optional operator string.

  • value – Optional comparison value.

expr = (SQL.and_expr()
        .add('status', '=', 'active')
        .add('age', '>', 18))
# status = %s
# AND age > %s
# args: ['active', 18]

expr.add(SQL.or_expr().add('a', '=', 1).add('b', '=', 2))
# status = %s
# AND age > %s
# AND (a = %s
#      OR b = %s)
# args: ['active', 18, 1, 2]
extend(other)

Append all terms from another boolean expression.

Parameters:

other – A SqlAnd or SqlOr instance whose terms are appended.

expr1 = SQL.and_expr().add('a', '=', 1)
expr2 = SQL.and_expr().add('b', '=', 2)
expr1.extend(expr2)
# a = %s
# AND b = %s
# args: [1, 2]
is_empty()

Returns True if all terms are empty.

Return type:

bool

has_sql()

Returns True if any term produces SQL output.

Return type:

bool

SqlOr

class SqlOr(*terms)

Combines terms with OR. Created via SQL.or_expr(*terms). Same interface as SqlAnd. Inherits from SqlObj (via SqlBoolOp).

Parameters:

terms – Zero or more initial SqlTerm or SqlBoolOp instances.

expr = (SQL.or_expr()
        .add('role', '=', 'admin')
        .add('role', '=', 'superuser'))
# role = %s
# OR role = %s
# args: ['admin', 'superuser']