Expand description
Simple Query Builder for Rust
Provides a straightforward way to build SQL queries in Rust. Conceptually, you build a list of properly termed objects in a “Query” object, and then call the sql() method on the Query object to get the SQL string.
Escape hatches are built in to allow you to use any SQL you want and have it integrated properly.
Part of the design goal is not to use attributes, macros, or other “magic” to make this work.
“Keep it simple & stupid.”
§Examples
use squeal::*;
let result = Query {
select: Some(Select::new(Columns::Star)),
from: Some("table"),
where_clause: Some(Term::Condition(
Box::new(Term::Atom("a")),
Op::O("<>"),
Box::new(Term::Atom("b")))),
group_by: None,
having: None,
order_by: None,
limit: None,
offset: None,
for_update: false,
}.sql();
assert_eq!(result, "SELECT * FROM table WHERE a <> b");Note the verbosity of the Enum scoping. This is not intentional and an artifact of this still being in early development.
Example using Q() fluent interface:
use squeal::*;
let mut qb = Q();
let result = qb.select(vec!["a", "sum(b)"])
.from("the_table")
.where_(Term::Condition(
Box::new(Term::Atom("a")),
Op::O(">"),
Box::new(Term::Atom("10"))
))
.group_by(vec!["b"])
.having(Term::Condition(
Box::new(Term::Atom("a")),
Op::O(">"),
Box::new(Term::Atom("1000")),
))
.limit(19)
.offset(10);
let q = result.build();
assert_eq!(q.sql(), "SELECT a, sum(b) FROM the_table WHERE a > 10 GROUP BY b HAVING a > 1000 LIMIT 19 OFFSET 10");
Structs§
- CreateTable is used to specify a create table query.
- The Having struct is used to specify the having clause in a query. It is used in the Query struct.
- The Insert struct is used to specify an insert query. The user is expect to construct the Insert object and then call the sql() method to get the SQL string.
- The OrderBy struct is used to specify the order by clause in a query. It is used in the Query struct. It is used to specify the columns, and optionally, whether they are ascending or descending. Each column can be ascending or descending
- The Query struct is the top-level object that represents a query. The user is expected to construct the Query object and then call the sql() method to get the SQL string.
- The QueryBuilder struct is a fluent interface for building a Query. It is not intended to be used directly, but rather through the Q() function. See the integration_test.rs for an example of usage.
- The Select struct is used to specify which columns to select. It is used in the Query struct.
- The TableBuilder struct is a fluent interface for building a Table. Tables can be built into DROP or CREATE forms.
- The Update struct is used to specify an update query. The user is expect to construct the Update object and then call the sql() method to get the SQL string.
Enums§
- The Columns enum is used to specify which columns to select.
- The Op enum is used to specify the operator in a condition. It is used in the Term struct.
- The OrderedColumn enum is used to specify the order by clause in a query. It is used in the OrderBy struct. It is used to specify the columns, and optionally, whether they are ascending or descending.
- The Term enum is used to specify a condition in a query (WHERE clause). It is used in the Query struct.
Traits§
- The Build trait is used by the XBuilder structs to build the X struct. This is a means of providing a nice factory/fluent interface.
- The Sql trait is implemented by all objects that can be used in a query. It provides a single method, sql(), that returns a String.
Functions§
- The Q function is a fluent interface for building a Query. The user is expected to construct the Query object and then call the sql() method to get the SQL string. The goal is any valid construction of a QueryBuilder is a valid Query and will, at least, syntactically, be valid SQL.