database/query
A query builder for Postgres, it’s not fully built out yet and currently only performs the most basic queries.
Types
A query is a struct that contains the schema to query from, the fields to select, the where clause, and the bindings for the query.
Normally you won’t interact with this struct directly, instead you’ll use
the functions below to build up a query and then pass it to the associated
functions to get the SQL string. These are already wrapped in the top level
database
module.
pub type Query(a) {
Query(
from: Schema(a),
select: List(String),
where: List(String),
bindings: List(pgo.Value),
)
}
Constructors
-
Query( from: Schema(a), select: List(String), where: List(String), bindings: List(pgo.Value), )
A where clause is a list of tuples where the first element is the field name and the second is the value to bind to that field. It will automatically generate the correct number of placeholders for the values and append them to the bindings list for PGO.
pub type Where =
List(#(String, List(pgo.Value)))
Functions
pub fn build(query: Query(a)) -> String
Builds the SQL string for the query. This is the same as SELECT ... FROM ... WHERE ...
in SQL.
pub fn delete(query: Query(a)) -> String
Builds the SQL string for a delete query. This is the same as DELETE FROM ... WHERE ...
in SQL. It will return the deleted rows.
pub fn from(schema: Schema(a)) -> Query(a)
Create a new query from a schema. This is the starting point for building a query and the schema’s decoder is also used as the returned value.
pub fn insert(schema: Schema(a)) -> String
Builds the SQL string for an insert query. This is the same as INSERT INTO ... (field1, field2, ...) VALUES (value1, value2, ...)
in SQL. It will ignore
the primary key field in the schema but may supporrt that in the future.
pub fn select(query: Query(a), fields: List(String)) -> Query(a)
Adds fields to the select clause of the query. This is the same as
SELECT field1, field2, ...
in SQL.
pub fn update(query: Query(a), fields: List(#(String, Value))) -> String
Builds the SQL string for an update query. This is the same as UPDATE ... SET field1 = value1, field2 = value2, ...
in SQL. It will return
the updated rows.