Crate typed_qb[][src]

Expand description

typed-qb is a compile-time, typed, query builder. The query is transformed into an SQL query string at compile time. If code compiles and the schema in the code matches the database, it should be (almost) impossible to write queries that produce errors.

Make sure to enable the generic_associated_types feature and include the prelude:

#![feature(generic_associated_types)]
use typed_qb::prelude::*;

Use the tables macro to generate table definitions:

typed_qb::tables! {
    CREATE TABLE Users (
        Id INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
        Name VARCHAR(64) NOT NULL,
        PRIMARY KEY(Id)
    );
}

This will generate a table definition Users implementing Table. To construct queries, call the methods on this trait:

let query = Users::query(|user| data! {
    id: user.id,
    name: user.name,
});

Pass the query to Database::typed_query to execute the query:

let opts = mysql::OptsBuilder::new()
    .user(Some("..."))
    .pass(Some("..."))
    .db_name(Some("..."));
let pool = mysql::Pool::new(opts)?;

let mut conn = pool.get_conn()?;
let results = conn.typed_query(query)?;

Modules

DELETE FROM queries

INSERT queries

SELECT queries

UPDATE queries

Macros

Generates a SelectedData anonymous struct from a list of expressions. Each expression must be separated by a comma (,). Fields can be specified in a struct-like syntax:

Parses SQL syntax into a Value.

Lifts the if and match statements to the top, duplicating code. For example, the following code:

Generate table definitions from CREATE TABLE statements

Structs

Enums

Traits

Represents a table. All fields of the table can be accessed via struct fields. You should not implement this trait. TableReferences are passed to the closures provided to the various methods on the Table trait.

Derive Macros