Crate squeal

source ·
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: Select::new(Columns::Star),
     from: "table".to_string(),
     where_clause: Some(Term::Condition(
       Box::new(Term::Atom("a".to_string())),
     Op::O("<>".to_string()),
     Box::new(Term::Atom("b".to_string())))),
     group_by: None,
    having: None,
    order_by: None,
    limit: None,
    offset: None,
}.sql();

assert_eq!(result, "SELECT * FROM table WHERE a <> b");

Note the verbosity of the to_string and Enum scoping. This is not intentional and an artifact of this still being in early development.

Structs

  • The Having struct is used to specify the having clause in a query. It is used in the Query struct.
  • 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 Select struct is used to specify which columns to select. It is used in the Query struct.

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 Sql trait is implemented by all objects that can be used in a query. It provides a single method, sql(), that returns a String.