cquill/query/ast
Types
Aggregate functions
pub type AggregateFunc {
Count
Sum
Avg
Min
Max
CountDistinct
}
Constructors
-
Count -
Sum -
Avg -
Min -
Max -
CountDistinct
Binary operators for expressions
pub type BinaryOp {
Add
Subtract
Multiply
Divide
Concat
}
Constructors
-
Add -
Subtract -
Multiply -
Divide -
Concat
Condition types for WHERE and HAVING clauses
pub type Condition {
Eq(field: String, value: Value)
NotEq(field: String, value: Value)
Gt(field: String, value: Value)
Gte(field: String, value: Value)
Lt(field: String, value: Value)
Lte(field: String, value: Value)
In(field: String, values: List(Value))
NotIn(field: String, values: List(Value))
Like(field: String, pattern: String)
NotLike(field: String, pattern: String)
ILike(field: String, pattern: String)
NotILike(field: String, pattern: String)
IsNull(field: String)
IsNotNull(field: String)
Between(field: String, low: Value, high: Value)
And(List(Condition))
Or(List(Condition))
Not(Condition)
Raw(sql: String, params: List(Value))
}
Constructors
-
Eq(field: String, value: Value)field = value
-
NotEq(field: String, value: Value)field != value
-
Gt(field: String, value: Value)field > value
-
Gte(field: String, value: Value)field >= value
-
Lt(field: String, value: Value)field < value
-
Lte(field: String, value: Value)field <= value
-
In(field: String, values: List(Value))field IN (values…)
-
NotIn(field: String, values: List(Value))field NOT IN (values…)
-
Like(field: String, pattern: String)field LIKE pattern
-
NotLike(field: String, pattern: String)field NOT LIKE pattern
-
ILike(field: String, pattern: String)field ILIKE pattern (case-insensitive LIKE)
-
NotILike(field: String, pattern: String)field NOT ILIKE pattern
-
IsNull(field: String)field IS NULL
-
IsNotNull(field: String)field IS NOT NULL
-
field BETWEEN low AND high
-
And(List(Condition))Combined conditions (all must be true)
-
Or(List(Condition))Alternative conditions (any can be true)
-
Not(Condition)Negated condition
-
Raw(sql: String, params: List(Value))Raw condition for advanced use cases
Represents a DELETE query AST. The schema parameter provides type safety.
pub type DeleteQuery(schema) {
DeleteQuery(
table: String,
schema_name: option.Option(String),
wheres: List(Where),
returning: List(String),
)
}
Constructors
-
DeleteQuery( table: String, schema_name: option.Option(String), wheres: List(Where), returning: List(String), )Arguments
- table
-
The target table
- schema_name
-
Optional schema name
- wheres
-
WHERE conditions
- returning
-
Columns to return after delete
Expression types for SELECT clauses
pub type Expr {
ColumnExpr(field: String)
QualifiedColumnExpr(table: String, field: String)
LiteralExpr(value: Value)
AggregateExpr(func: AggregateFunc, field: String)
FunctionExpr(name: String, args: List(Expr))
BinaryExpr(left: Expr, op: BinaryOp, right: Expr)
}
Constructors
-
ColumnExpr(field: String)A column reference
-
QualifiedColumnExpr(table: String, field: String)A qualified column (table.field)
-
LiteralExpr(value: Value)A literal value
-
AggregateExpr(func: AggregateFunc, field: String)An aggregate function
-
FunctionExpr(name: String, args: List(Expr))A function call
-
Binary operation
Represents an INSERT query AST. The schema parameter provides type safety.
pub type InsertQuery(schema) {
InsertQuery(
table: String,
schema_name: option.Option(String),
columns: List(String),
values: List(List(Value)),
on_conflict: option.Option(OnConflict),
returning: List(String),
)
}
Constructors
-
InsertQuery( table: String, schema_name: option.Option(String), columns: List(String), values: List(List(Value)), on_conflict: option.Option(OnConflict), returning: List(String), )Arguments
- table
-
The target table
- schema_name
-
Optional schema name (e.g., “public”)
- columns
-
Columns to insert into
- values
-
Values to insert (list of rows, each row is a list of values)
- on_conflict
-
Conflict handling strategy
- returning
-
Columns to return after insert
JOIN clause
pub type Join {
Join(
join_type: JoinType,
table: String,
table_alias: option.Option(String),
on: Condition,
)
}
Constructors
-
Join( join_type: JoinType, table: String, table_alias: option.Option(String), on: Condition, )
Types of JOIN
pub type JoinType {
InnerJoin
LeftJoin
RightJoin
FullJoin
CrossJoin
}
Constructors
-
InnerJoin -
LeftJoin -
RightJoin -
FullJoin -
CrossJoin
NULL ordering preference
pub type NullsOrder {
NullsDefault
NullsFirst
NullsLast
}
Constructors
-
NullsDefaultUse database default
-
NullsFirstNULLs come first
-
NullsLastNULLs come last
Conflict handling strategies for INSERT
pub type OnConflict {
DoNothing
DoUpdate(
conflict_columns: List(String),
update_columns: List(String),
)
DoNothingOnConstraint(constraint_name: String)
DoUpdateOnConstraint(
constraint_name: String,
update_columns: List(String),
)
}
Constructors
-
DoNothingON CONFLICT DO NOTHING
-
DoUpdate( conflict_columns: List(String), update_columns: List(String), )ON CONFLICT (columns) DO UPDATE SET …
-
DoNothingOnConstraint(constraint_name: String)ON CONFLICT ON CONSTRAINT constraint_name DO NOTHING
-
DoUpdateOnConstraint( constraint_name: String, update_columns: List(String), )ON CONFLICT ON CONSTRAINT constraint_name DO UPDATE SET …
ORDER BY clause
pub type OrderBy {
OrderBy(field: String, direction: Direction, nulls: NullsOrder)
}
Constructors
-
OrderBy(field: String, direction: Direction, nulls: NullsOrder)
The main query type representing a complete query AST. The schema parameter provides type safety by tracking which schema the query is built against.
pub type Query(schema) {
Query(
source: Source,
select: Select,
wheres: List(Where),
order_bys: List(OrderBy),
limit: option.Option(Int),
offset: option.Option(Int),
joins: List(Join),
distinct: Bool,
group_bys: List(String),
havings: List(Where),
)
}
Constructors
-
Query( source: Source, select: Select, wheres: List(Where), order_bys: List(OrderBy), limit: option.Option(Int), offset: option.Option(Int), joins: List(Join), distinct: Bool, group_bys: List(String), havings: List(Where), )Arguments
- source
-
The source table/schema being queried
- select
-
What fields to select
- wheres
-
WHERE conditions (all must match - implicit AND)
- order_bys
-
ORDER BY clauses
- limit
-
LIMIT clause
- offset
-
OFFSET clause
- joins
-
JOIN clauses
- distinct
-
DISTINCT flag
- group_bys
-
GROUP BY fields
- havings
-
HAVING conditions (for aggregate queries)
Type of query operation (useful for adapter routing)
pub type QueryOperation {
SelectOp
InsertOp
UpdateOp
DeleteOp
}
Constructors
-
SelectOp -
InsertOp -
UpdateOp -
DeleteOp
What columns to select
pub type Select {
SelectAll
SelectFields(List(String))
SelectExpr(List(SelectExpression))
}
Constructors
-
SelectAllSELECT * - all columns
-
SelectFields(List(String))SELECT specific fields by name
-
SelectExpr(List(SelectExpression))SELECT expressions (for computed columns, aggregates)
A select expression with optional alias
pub type SelectExpression {
SelectExpression(expr: Expr, alias: option.Option(String))
}
Constructors
-
SelectExpression(expr: Expr, alias: option.Option(String))
Represents the source of a query (table, subquery, etc.)
pub type Source {
TableSource(table: String, schema_name: option.Option(String))
SubquerySource(query: Query(Nil))
}
Constructors
-
TableSource(table: String, schema_name: option.Option(String))Query from a table by name
-
SubquerySource(query: Query(Nil))Query from a subquery (for future use)
Represents an UPDATE query AST. The schema parameter provides type safety.
pub type UpdateQuery(schema) {
UpdateQuery(
table: String,
schema_name: option.Option(String),
sets: List(SetClause),
wheres: List(Where),
returning: List(String),
)
}
Constructors
-
UpdateQuery( table: String, schema_name: option.Option(String), sets: List(SetClause), wheres: List(Where), returning: List(String), )Arguments
- table
-
The target table
- schema_name
-
Optional schema name
- sets
-
SET clauses: list of (column, value) pairs
- wheres
-
WHERE conditions
- returning
-
Columns to return after update
Represents a value in a query (for parameters, literals)
pub type Value {
IntValue(Int)
FloatValue(Float)
StringValue(String)
BoolValue(Bool)
NullValue
ParamValue(position: Int)
ListValue(List(Value))
}
Constructors
-
IntValue(Int)Integer value
-
FloatValue(Float)Float value
-
StringValue(String)String value
-
BoolValue(Bool)Boolean value
-
NullValueNULL value
-
ParamValue(position: Int)Positional parameter placeholder ($1, $2, etc.)
-
ListValue(List(Value))List of values (for IN clauses)
Values
pub fn new_delete(table: String) -> DeleteQuery(Nil)
Create a new empty DeleteQuery AST
pub fn new_delete_qualified(
schema_name: String,
table: String,
) -> DeleteQuery(Nil)
Create a new DeleteQuery AST with schema qualification
pub fn new_insert(table: String) -> InsertQuery(Nil)
Create a new empty InsertQuery AST
pub fn new_insert_qualified(
schema_name: String,
table: String,
) -> InsertQuery(Nil)
Create a new InsertQuery AST with schema qualification
pub fn new_qualified(
schema_name: String,
table: String,
) -> Query(Nil)
Create a new empty Query AST with schema qualification
pub fn new_update(table: String) -> UpdateQuery(Nil)
Create a new empty UpdateQuery AST
pub fn new_update_qualified(
schema_name: String,
table: String,
) -> UpdateQuery(Nil)
Create a new UpdateQuery AST with schema qualification
pub fn order_by_asc(field: String) -> OrderBy
Create a default OrderBy with ascending order
pub fn order_by_desc(field: String) -> OrderBy
Create a default OrderBy with descending order