cquill/query

Values

pub fn and(conditions: List(ast.Condition)) -> ast.Condition

Combine conditions with AND

pub fn between(field: String, low: a, high: a) -> ast.Condition

Create a BETWEEN condition: field BETWEEN low AND high

pub fn bool_value(value: Bool) -> ast.Value

Create a boolean value

pub fn condition_count(query: ast.Query(s)) -> Int

Count the number of WHERE conditions.

pub fn cross_join(
  query: ast.Query(s),
  table: String,
) -> ast.Query(s)

Add a CROSS JOIN to the query.

pub const desc: ast.Direction
pub fn distinct(query: ast.Query(s)) -> ast.Query(s)

Add DISTINCT to the query.

pub fn eq(field: String, value: a) -> ast.Condition

Create an equality condition: field = value

pub fn eq_bool(field: String, value: Bool) -> ast.Condition

Create an equality condition with explicit boolean value

pub fn eq_int(field: String, value: Int) -> ast.Condition

Create an equality condition with explicit integer value

pub fn eq_string(field: String, value: String) -> ast.Condition

Create an equality condition with explicit string value

pub fn float_value(value: Float) -> ast.Value

Create a float value

pub fn from(schema: schema.Schema) -> ast.Query(schema.Schema)

Create a new query from a schema. This is the primary entry point for building queries.

Example

let query = query.from(user_schema)
pub fn from_qualified(
  schema_name: String,
  table: String,
) -> ast.Query(Nil)

Create a query from a qualified table name (schema.table).

pub fn from_subquery(subquery: ast.Query(a)) -> ast.Query(Nil)

Create a query from a subquery.

pub fn from_table(table: String) -> ast.Query(Nil)

Create a query from a table name directly (without schema metadata). Useful for simple queries or testing.

pub fn full_join(
  query: ast.Query(s),
  table: String,
  on on: ast.Condition,
) -> ast.Query(s)

Add a FULL OUTER JOIN to the query.

pub fn get_conditions(query: ast.Query(s)) -> List(ast.Condition)

Get all WHERE conditions from the query.

pub fn get_joins(query: ast.Query(s)) -> List(ast.Join)

Get all JOIN clauses.

pub fn get_limit(query: ast.Query(s)) -> option.Option(Int)

Get the LIMIT value.

pub fn get_offset(query: ast.Query(s)) -> option.Option(Int)

Get the OFFSET value.

pub fn get_order_bys(query: ast.Query(s)) -> List(ast.OrderBy)

Get all ORDER BY clauses from the query.

pub fn get_select(query: ast.Query(s)) -> ast.Select

Get the SELECT clause.

pub fn get_source(query: ast.Query(s)) -> ast.Source

Get the source of the query.

pub fn group_by(
  query: ast.Query(s),
  field: String,
) -> ast.Query(s)

Add a GROUP BY field.

pub fn group_by_fields(
  query: ast.Query(s),
  fields: List(String),
) -> ast.Query(s)

Add multiple GROUP BY fields.

pub fn gt(field: String, value: a) -> ast.Condition

Create a greater-than condition: field > value

pub fn gt_int(field: String, value: Int) -> ast.Condition

Create a greater-than condition with explicit integer value

pub fn gte(field: String, value: a) -> ast.Condition

Create a greater-than-or-equal condition: field >= value

pub fn gte_int(field: String, value: Int) -> ast.Condition

Create a greater-than-or-equal condition with explicit integer value

pub fn has_conditions(query: ast.Query(s)) -> Bool

Check if the query has any WHERE conditions.

pub fn has_order_by(query: ast.Query(s)) -> Bool

Check if the query has any ORDER BY clauses.

pub fn has_pagination(query: ast.Query(s)) -> Bool

Check if the query has pagination.

pub fn having(
  query: ast.Query(s),
  condition: ast.Condition,
) -> ast.Query(s)

Add a HAVING condition.

pub fn in_ints(field: String, values: List(Int)) -> ast.Condition

Create an IN condition with explicit integer values

pub fn in_strings(
  field: String,
  values: List(String),
) -> ast.Condition

Create an IN condition with explicit string values

pub fn int_value(value: Int) -> ast.Value

Create an integer value

pub fn is_distinct(query: ast.Query(s)) -> Bool

Check if the query is DISTINCT.

pub fn is_in(field: String, values: List(a)) -> ast.Condition

Create an IN condition: field IN (values…)

pub fn is_not_in(field: String, values: List(a)) -> ast.Condition

Create a NOT IN condition: field NOT IN (values…)

pub fn is_not_null(field: String) -> ast.Condition

Create an IS NOT NULL condition

pub fn is_null(field: String) -> ast.Condition

Create an IS NULL condition

pub fn join(
  query: ast.Query(s),
  table: String,
  on on: ast.Condition,
) -> ast.Query(s)

Add an INNER JOIN to the query.

pub fn join_as(
  query: ast.Query(s),
  table: String,
  alias alias: String,
  on on: ast.Condition,
) -> ast.Query(s)

Add an INNER JOIN with an alias.

pub fn left_join(
  query: ast.Query(s),
  table: String,
  on on: ast.Condition,
) -> ast.Query(s)

Add a LEFT JOIN to the query.

pub fn left_join_as(
  query: ast.Query(s),
  table: String,
  alias alias: String,
  on on: ast.Condition,
) -> ast.Query(s)

Add a LEFT JOIN with an alias.

pub fn like(field: String, pattern: String) -> ast.Condition

Create a LIKE condition: field LIKE pattern

pub fn limit(query: ast.Query(s), count: Int) -> ast.Query(s)

Set the LIMIT for the query.

Example

user_query
|> query.limit(10)
pub fn list_value(values: List(ast.Value)) -> ast.Value

Create a list value

pub fn lt(field: String, value: a) -> ast.Condition

Create a less-than condition: field < value

pub fn lt_int(field: String, value: Int) -> ast.Condition

Create a less-than condition with explicit integer value

pub fn lte(field: String, value: a) -> ast.Condition

Create a less-than-or-equal condition: field <= value

pub fn lte_int(field: String, value: Int) -> ast.Condition

Create a less-than-or-equal condition with explicit integer value

pub fn no_pagination(query: ast.Query(s)) -> ast.Query(s)

Remove LIMIT and OFFSET.

pub fn not(condition: ast.Condition) -> ast.Condition

Negate a condition

pub fn not_eq(field: String, value: a) -> ast.Condition

Create a not-equal condition: field != value

pub fn not_like(field: String, pattern: String) -> ast.Condition

Create a NOT LIKE condition: field NOT LIKE pattern

pub fn null_value() -> ast.Value

Create a null value

pub const nulls_default: ast.NullsOrder
pub const nulls_first: ast.NullsOrder
pub const nulls_last: ast.NullsOrder
pub fn offset(query: ast.Query(s), count: Int) -> ast.Query(s)

Set the OFFSET for the query.

Example

user_query
|> query.offset(20)
|> query.limit(10)
pub fn or(conditions: List(ast.Condition)) -> ast.Condition

Combine conditions with OR

pub fn or_where(
  query: ast.Query(s),
  condition: ast.Condition,
) -> ast.Query(s)

Add an OR condition to the query. This wraps existing conditions and the new one in an OR.

Example

user_query
|> query.where(query.eq("role", "admin"))
|> query.or_where(query.eq("role", "moderator"))
pub fn order_by(
  query: ast.Query(s),
  field: String,
  direction: ast.Direction,
) -> ast.Query(s)

Add an ORDER BY clause to the query.

Example

user_query
|> query.order_by("created_at", ast.Desc)
pub fn order_by_asc(
  query: ast.Query(s),
  field: String,
) -> ast.Query(s)

Add an ORDER BY ASC clause.

pub fn order_by_clear(query: ast.Query(s)) -> ast.Query(s)

Clear all ORDER BY clauses.

pub fn order_by_desc(
  query: ast.Query(s),
  field: String,
) -> ast.Query(s)

Add an ORDER BY DESC clause.

pub fn order_by_with_nulls(
  query: ast.Query(s),
  field: String,
  direction: ast.Direction,
  nulls: ast.NullsOrder,
) -> ast.Query(s)

Add an ORDER BY clause with null ordering.

pub fn paginate(
  query: ast.Query(s),
  page page: Int,
  per_page per_page: Int,
) -> ast.Query(s)

Set both LIMIT and OFFSET for pagination.

Example

// Page 3 with 10 items per page
user_query
|> query.paginate(page: 3, per_page: 10)
pub fn right_join(
  query: ast.Query(s),
  table: String,
  on on: ast.Condition,
) -> ast.Query(s)

Add a RIGHT JOIN to the query.

pub fn select(
  query: ast.Query(s),
  fields: List(String),
) -> ast.Query(s)

Select specific fields by name.

Example

user_query
|> query.select(["id", "email", "name"])
pub fn select_all(query: ast.Query(s)) -> ast.Query(s)

Select all fields (SELECT *). This is the default behavior.

pub fn select_expr(
  query: ast.Query(s),
  expressions: List(ast.SelectExpression),
) -> ast.Query(s)

Select with expressions (for computed columns, aggregates).

pub fn string_value(value: String) -> ast.Value

Create a string value

pub fn to_debug_string(query: ast.Query(s)) -> String

Convert a query to a debug string representation. This is for debugging and testing, NOT for SQL generation.

pub fn where(
  query: ast.Query(s),
  condition: ast.Condition,
) -> ast.Query(s)

Add a WHERE condition to the query. Multiple where clauses are combined with AND.

Example

user_query
|> query.where(query.eq("active", True))
|> query.where(query.gt("age", 18))
pub fn where_clear(query: ast.Query(s)) -> ast.Query(s)

Clear all WHERE conditions.

pub fn where_replace(
  query: ast.Query(s),
  condition: ast.Condition,
) -> ast.Query(s)

Replace all WHERE conditions with the given condition.

Search Document