[][src]Crate quaint

quaint

A database client abstraction for reading and writing to a SQL database in a safe manner.

Under construction and will go through several rounds of changes. Not meant for production use in the current form.

Goals

  • Query generation when the database and conditions are not known at compile time.
  • Parameterized queries.
  • A modular design, separate AST for query building and visitors for different databases.
  • Database support behind a feature flag.

Non-goals

  • Database-level type-safety in query building or being an ORM.

Databases

  • SQLite
  • PostgreSQL
  • MySQL

Methods of connecting

Quaint provides two options to connect to the underlying database.

The single connection method:

use quaint::{prelude::*, single::Quaint};

#[tokio::main]
async fn main() -> Result<(), quaint::error::Error> {
    let conn = Quaint::new("file:///tmp/example.db").await?;
    let result = conn.select(Select::default().value(1)).await?;

    assert_eq!(
        Some(1),
        result.into_iter().nth(0).and_then(|row| row[0].as_i64()),
    );

    Ok(())
}

The pooled method:

use quaint::{prelude::*, pooled::Quaint};

#[tokio::main]
async fn main() -> Result<(), quaint::error::Error> {
    let pool = Quaint::new("file:///tmp/example.db").await?;
    let conn = pool.check_out().await?;
    let result = conn.select(Select::default().value(1)).await?;

    assert_eq!(
        Some(1),
        result.into_iter().nth(0).and_then(|row| row[0].as_i64()),
    );

    Ok(())
}

Using the AST module

The crate can be used as an SQL string builder using the ast and visitor modules.

AST is generic for all databases and the visitors generate correct SQL syntax for the database.

The visitor returns the query as a string and its parameters as a vector.

let conditions = "word"
    .equals("meow")
    .and("age".less_than(10))
    .and("paw".equals("warm"));

let query = Select::from_table("naukio").so_that(conditions);
let (sql_str, params) = Sqlite::build(query);

assert_eq!(
    "SELECT `naukio`.* FROM `naukio` WHERE ((`word` = ? AND `age` < ?) AND `paw` = ?)",
    sql_str,
);

assert_eq!(
    vec![
        ParameterizedValue::from("meow"),
        ParameterizedValue::from(10),
        ParameterizedValue::from("warm"),
    ],
    params
);

Modules

ast

An abstract syntax tree for SQL queries.

connector

A set of abstractions for database connections.

error

Error module

pooled

A connection pool to a SQL database.

prelude

A "prelude" for users of the quaint crate.

single

A single connection abstraction to a SQL database.

visitor

Visitors for reading an abstract SQL syntax tree, generating the query and gathering parameters in the right order.

Macros

col

Marks a given string or a tuple as a column. Useful when using a column in calculations, e.g.

val

Marks a given string as a value. Useful when using a value in calculations, e.g.

Type Definitions

Result