API Reference
This section contains the auto-generated API documentation.
sqlo - A modern, type-safe, and extensible SQL query builder for Python.
- class sqlo.Q[source]
Bases:
objectQuery Builder Factory.
- classmethod set_debug(debug=True)[source]
Enable or disable debug mode (prints SQL to stdout).
- Parameters:
debug (bool)
- class sqlo.Raw(sql, params=None)[source]
Bases:
ExpressionRaw SQL fragment.
WARNING: Raw SQL bypasses parameter binding and can be vulnerable to SQL injection. Only use Raw() with trusted input or when you need to reference columns/functions.
For dynamic user input, always use parameterized queries instead.
- class sqlo.Func(name, *args)[source]
Bases:
ExpressionRepresents a SQL function call.
This class allows you to wrap any SQL function (e.g., COUNT, MAX, AVG, custom functions) and use it within your queries. It supports arguments and aliasing.
- Parameters:
name (str) – The name of the SQL function (e.g., “COUNT”).
*args (Any) – Arguments to pass to the function.
Example
>>> f = Func("COUNT", "*") >>> f.as_("total")
- class sqlo.Condition(column=None, value=None, operator='=')[source]
Bases:
ExpressionRepresents a SQL condition for use in WHERE or HAVING clauses.
This class allows you to build simple or complex conditions with support for compact operator syntax (e.g., “age>”) and standard syntax (e.g., “age >”). Conditions can be combined using bitwise operators (& and |) or static methods.
- Parameters:
Examples
>>> # Simple condition >>> c = Condition("age>=", 18) >>> sql, params = c.build() >>> >>> # IN operator >>> c = Condition("status", ["pending", "done"], operator="IN") >>> >>> # IS NULL >>> c = Condition("deleted_at", operator="IS NULL") >>> >>> # Static factory methods >>> c = Condition.null("deleted_at") >>> c = Condition.in_("status", ["pending", "done"]) >>> c = Condition.exists(subquery) >>> >>> # Combining conditions >>> c1 = Condition("age>=", 18) >>> c2 = Condition("country", "US") >>> combined = c1 & c2 # AND >>> combined = c1 \| c2 # OR
- static and_(*conditions)[source]
Combine multiple conditions with AND logic.
- Parameters:
*conditions (Condition) – Variable number of Condition objects
- Returns:
Combined Condition object
- Return type:
Example
>>> c1 = Condition("age>=", 18) >>> c2 = Condition("country", "US") >>> c3 = Condition("verified", True) >>> combined = Condition.and_(c1, c2, c3) >>> # SQL: (`age` >= ? AND `country` = ? AND `verified` = ?)
- build(dialect=None)[source]
Build the condition and return (sql, params) tuple.
- Parameters:
dialect – Optional dialect to use for quoting. Defaults to MySQLDialect.
- Returns:
Tuple of (sql_string, params_tuple)
- Return type:
Example
>>> c = Condition("age>=", 18) >>> sql, params = c.build() >>> assert sql == "`age` >= ?" >>> assert params == (18,)
- static exists(subquery)[source]
Create an EXISTS condition with a subquery.
- Parameters:
subquery (SelectQuery) – SelectQuery object
- Returns:
Condition object
- Return type:
Example
>>> subquery = Q.select("1").from_("orders").where(Condition("user_id", 123)) >>> c = Condition.exists(subquery) >>> sql, params = c.build() >>> # SQL: EXISTS (SELECT 1 FROM `orders` WHERE `user_id` = ?)
- static in_(column, values)[source]
Create an IN condition.
- Parameters:
- Returns:
Condition object
- Return type:
Example
>>> c = Condition.in_("status", ["pending", "done"]) >>> sql, params = c.build() >>> # SQL: `status` IN (?, ?)
- static not_exists(subquery)[source]
Create a NOT EXISTS condition with a subquery.
- Parameters:
subquery (SelectQuery) – SelectQuery object
- Returns:
Condition object
- Return type:
Example
>>> subquery = Q.select("1").from_("orders").where(Condition("user_id", 123)) >>> c = Condition.not_exists(subquery) >>> sql, params = c.build() >>> # SQL: NOT EXISTS (SELECT 1 FROM `orders` WHERE `user_id` = ?)
- static not_in(column, values)[source]
Create a NOT IN condition.
- Parameters:
- Returns:
Condition object
- Return type:
Example
>>> c = Condition.not_in("status", ["canceled", "failed"]) >>> sql, params = c.build() >>> # SQL: `status` NOT IN (?, ?)
- static not_null(column)[source]
Create an IS NOT NULL condition.
- Parameters:
column (str) – Column name to check for NOT NULL
- Returns:
Condition object
- Return type:
Example
>>> c = Condition.not_null("email") >>> sql, params = c.build() >>> # SQL: `email` IS NOT NULL
- static null(column)[source]
Create an IS NULL condition.
- Parameters:
column (str) – Column name to check for NULL
- Returns:
Condition object
- Return type:
Example
>>> c = Condition.null("deleted_at") >>> sql, params = c.build() >>> # SQL: `deleted_at` IS NULL
- static or_(*conditions)[source]
Combine multiple conditions with OR logic.
- Parameters:
*conditions (Condition) – Variable number of Condition objects
- Returns:
ComplexCondition object
- Return type:
Example
>>> c1 = Condition("status", "pending") >>> c2 = Condition("status", "processing") >>> combined = Condition.or_(c1, c2) >>> # SQL: (`status` = ? OR `status` = ?)
- class sqlo.JSON(column)[source]
Bases:
ExpressionRepresents a JSON column.
Example
>>> JSON("data").extract("name")
- Parameters:
column (str)
- class sqlo.JSONPath(column, path)[source]
Bases:
ExpressionRepresents a JSON path extraction.
- class sqlo.Window[source]
Bases:
objectRepresents a SQL window specification for window functions.
Example
>>> Window.partition_by("department").order_by("-salary") >>> Window.order_by("date").rows_between("UNBOUNDED PRECEDING", "CURRENT ROW")
- class sqlo.query.select.SelectQuery(*columns, dialect=None, debug=False)[source]
Bases:
WhereClauseMixin,Query,Generic[T]- from_(table, alias=None)[source]
- Parameters:
table (str | SelectQuery)
alias (str | None)
- Return type:
- inner_join(table, on=None)[source]
- Parameters:
- Return type:
SelectQuery[T]
- left_join(table, on=None)[source]
- Parameters:
- Return type:
SelectQuery[T]
- right_join(table, on=None)[source]
- Parameters:
- Return type:
SelectQuery[T]
- cross_join(table)[source]
- Parameters:
table (str)
- Return type:
SelectQuery[T]
- where(column, value=None, operator='=')[source]
- Parameters:
- Return type:
SelectQuery[T]
- where_in(column, values)[source]
Add an IN WHERE condition.
- Parameters:
column (str)
values (list[Any] | SelectQuery)
- Return type:
SelectQuery[T]
- where_not_in(column, values)[source]
Add a NOT IN WHERE condition.
- Parameters:
column (str)
values (list[Any] | SelectQuery)
- Return type:
SelectQuery[T]
- or_where(column, value=None, operator='=')[source]
Add an OR WHERE condition.
- Parameters:
- Return type:
SelectQuery[T]
- where_null(column)[source]
Add an IS NULL WHERE condition.
- Parameters:
column (str)
- Return type:
SelectQuery[T]
- where_not_null(column)[source]
Add an IS NOT NULL WHERE condition.
- Parameters:
column (str)
- Return type:
SelectQuery[T]
- where_between(column, value1, value2)[source]
Add a BETWEEN WHERE condition.
- Parameters:
- Return type:
SelectQuery[T]
- where_not_between(column, value1, value2)[source]
Add a NOT BETWEEN WHERE condition.
- Parameters:
- Return type:
SelectQuery[T]
- where_like(column, pattern)[source]
Add a LIKE WHERE condition.
- Parameters:
- Return type:
SelectQuery[T]
- where_not_like(column, pattern)[source]
Add a NOT LIKE WHERE condition.
- Parameters:
- Return type:
SelectQuery[T]
- order_by(*columns)[source]
- Parameters:
- Return type:
SelectQuery[T]
- limit(limit)[source]
- Parameters:
limit (int)
- Return type:
SelectQuery[T]
- offset(offset)[source]
- Parameters:
offset (int)
- Return type:
SelectQuery[T]
- group_by(*columns)[source]
- Parameters:
columns (str)
- Return type:
SelectQuery[T]
- when(condition, callback)[source]
- Parameters:
condition (Any)
callback (Callable[[SelectQuery[T]], None])
- Return type:
SelectQuery[T]
- optimizer_hint(hint)[source]
Add an optimizer hint (MySQL 8.0+).
- Parameters:
hint (str)
- Return type:
- union(query)[source]
- Parameters:
query (SelectQuery)
- Return type:
SelectQuery[T]
- union_all(query)[source]
- Parameters:
query (SelectQuery)
- Return type:
SelectQuery[T]
- class sqlo.query.insert.InsertQuery(table, dialect=None, debug=False)[source]
Bases:
Query- Parameters:
table (str)
- from_select(columns, select_query)[source]
Insert data from a SELECT query.
- Parameters:
select_query (SelectQuery)
- Return type:
- class sqlo.query.update.UpdateQuery(table, dialect=None, debug=False)[source]
Bases:
WhereClauseMixin,Query- Parameters:
table (str)
- join(table, on=None, join_type='INNER')[source]
Add a JOIN clause (MySQL multi-table UPDATE).
- Parameters:
- Return type:
- allow_all_rows()[source]
Allow UPDATE without WHERE clause (updates all rows).
This is a safety feature to prevent accidental mass updates. You must call this method explicitly if you want to update all rows.
- Returns:
Self for method chaining
- Return type:
Example
>>> Q.update("users").set({"active": False}).allow_all_rows().build()
- class sqlo.query.delete.DeleteQuery(table, dialect=None, debug=False)[source]
Bases:
WhereClauseMixin,Query- Parameters:
table (str)
- join(table, on=None, join_type='INNER')[source]
Add a JOIN clause (MySQL multi-table DELETE).
- Parameters:
- Return type:
- allow_all_rows()[source]
Allow DELETE without WHERE clause (deletes all rows).
This is a safety feature to prevent accidental mass deletions. You must call this method explicitly if you want to delete all rows.
- Returns:
Self for method chaining
- Return type:
Example
>>> Q.delete_from("temp_table").allow_all_rows().build()
- class sqlo.expressions.Expression[source]
Bases:
objectBase class for SQL expressions.
- class sqlo.expressions.Raw(sql, params=None)[source]
Bases:
ExpressionRaw SQL fragment.
WARNING: Raw SQL bypasses parameter binding and can be vulnerable to SQL injection. Only use Raw() with trusted input or when you need to reference columns/functions.
For dynamic user input, always use parameterized queries instead.
- class sqlo.expressions.Func(name, *args)[source]
Bases:
ExpressionRepresents a SQL function call.
This class allows you to wrap any SQL function (e.g., COUNT, MAX, AVG, custom functions) and use it within your queries. It supports arguments and aliasing.
- Parameters:
name (str) – The name of the SQL function (e.g., “COUNT”).
*args (Any) – Arguments to pass to the function.
Example
>>> f = Func("COUNT", "*") >>> f.as_("total")
- class sqlo.expressions.FunctionFactory[source]
Bases:
objectFactory for creating SQL function expressions.
- class sqlo.expressions.Condition(column=None, value=None, operator='=')[source]
Bases:
ExpressionRepresents a SQL condition for use in WHERE or HAVING clauses.
This class allows you to build simple or complex conditions with support for compact operator syntax (e.g., “age>”) and standard syntax (e.g., “age >”). Conditions can be combined using bitwise operators (& and |) or static methods.
- Parameters:
Examples
>>> # Simple condition >>> c = Condition("age>=", 18) >>> sql, params = c.build() >>> >>> # IN operator >>> c = Condition("status", ["pending", "done"], operator="IN") >>> >>> # IS NULL >>> c = Condition("deleted_at", operator="IS NULL") >>> >>> # Static factory methods >>> c = Condition.null("deleted_at") >>> c = Condition.in_("status", ["pending", "done"]) >>> c = Condition.exists(subquery) >>> >>> # Combining conditions >>> c1 = Condition("age>=", 18) >>> c2 = Condition("country", "US") >>> combined = c1 & c2 # AND >>> combined = c1 \| c2 # OR
- build(dialect=None)[source]
Build the condition and return (sql, params) tuple.
- Parameters:
dialect – Optional dialect to use for quoting. Defaults to MySQLDialect.
- Returns:
Tuple of (sql_string, params_tuple)
- Return type:
Example
>>> c = Condition("age>=", 18) >>> sql, params = c.build() >>> assert sql == "`age` >= ?" >>> assert params == (18,)
- static null(column)[source]
Create an IS NULL condition.
- Parameters:
column (str) – Column name to check for NULL
- Returns:
Condition object
- Return type:
Example
>>> c = Condition.null("deleted_at") >>> sql, params = c.build() >>> # SQL: `deleted_at` IS NULL
- static not_null(column)[source]
Create an IS NOT NULL condition.
- Parameters:
column (str) – Column name to check for NOT NULL
- Returns:
Condition object
- Return type:
Example
>>> c = Condition.not_null("email") >>> sql, params = c.build() >>> # SQL: `email` IS NOT NULL
- static in_(column, values)[source]
Create an IN condition.
- Parameters:
- Returns:
Condition object
- Return type:
Example
>>> c = Condition.in_("status", ["pending", "done"]) >>> sql, params = c.build() >>> # SQL: `status` IN (?, ?)
- static not_in(column, values)[source]
Create a NOT IN condition.
- Parameters:
- Returns:
Condition object
- Return type:
Example
>>> c = Condition.not_in("status", ["canceled", "failed"]) >>> sql, params = c.build() >>> # SQL: `status` NOT IN (?, ?)
- static exists(subquery)[source]
Create an EXISTS condition with a subquery.
- Parameters:
subquery (SelectQuery) – SelectQuery object
- Returns:
Condition object
- Return type:
Example
>>> subquery = Q.select("1").from_("orders").where(Condition("user_id", 123)) >>> c = Condition.exists(subquery) >>> sql, params = c.build() >>> # SQL: EXISTS (SELECT 1 FROM `orders` WHERE `user_id` = ?)
- static not_exists(subquery)[source]
Create a NOT EXISTS condition with a subquery.
- Parameters:
subquery (SelectQuery) – SelectQuery object
- Returns:
Condition object
- Return type:
Example
>>> subquery = Q.select("1").from_("orders").where(Condition("user_id", 123)) >>> c = Condition.not_exists(subquery) >>> sql, params = c.build() >>> # SQL: NOT EXISTS (SELECT 1 FROM `orders` WHERE `user_id` = ?)
- static and_(*conditions)[source]
Combine multiple conditions with AND logic.
- Parameters:
*conditions (Condition) – Variable number of Condition objects
- Returns:
Combined Condition object
- Return type:
Example
>>> c1 = Condition("age>=", 18) >>> c2 = Condition("country", "US") >>> c3 = Condition("verified", True) >>> combined = Condition.and_(c1, c2, c3) >>> # SQL: (`age` >= ? AND `country` = ? AND `verified` = ?)
- static or_(*conditions)[source]
Combine multiple conditions with OR logic.
- Parameters:
*conditions (Condition) – Variable number of Condition objects
- Returns:
ComplexCondition object
- Return type:
Example
>>> c1 = Condition("status", "pending") >>> c2 = Condition("status", "processing") >>> combined = Condition.or_(c1, c2) >>> # SQL: (`status` = ? OR `status` = ?)
- class sqlo.expressions.ComplexCondition(operator, left, right)[source]
Bases:
ExpressionRepresents a complex SQL condition combining multiple conditions with AND/OR logic.
This class is typically created automatically when using bitwise operators (& or |) on Condition objects, or when using Condition.and_() / Condition.or_() static methods. It properly handles operator precedence and nested conditions.
- Parameters:
operator (str) – The logical operator (“AND” or “OR”).
left (Union[Condition, ComplexCondition]) – The left-hand condition.
right (Union[Condition, ComplexCondition]) – The right-hand condition.
Examples
>>> c1 = Condition("age>=", 18) >>> c2 = Condition("country", "US") >>> c3 = Condition("verified", True) >>> >>> # Creates ComplexCondition automatically >>> complex = (c1 & c2) \| c3 >>> # Represents: (age >= 18 AND country = 'US') OR verified = True
- build(dialect=None)[source]
Build the complex condition and return (sql, params) tuple.
- Parameters:
dialect – Optional dialect to use for quoting. Defaults to MySQLDialect.
- Returns:
Tuple of (sql_string, params_tuple)
- Return type:
Example
>>> c1 = Condition("age>=", 18) >>> c2 = Condition("country", "US") >>> complex = c1 \| c2 >>> sql, params = complex.build() >>> # SQL: (`age` >= ? OR `country` = ?)
- class sqlo.expressions.JSONPath(column, path)[source]
Bases:
ExpressionRepresents a JSON path extraction.