1 unstable release

new 0.2.2 Apr 26, 2025

#1708 in Database interfaces

Apache-2.0

1MB
19K SLoC

Sqlint is no longer maintained in this repository


lib.rs:

sqlint

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

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
  • Microsoft SQL Server

Methods of connecting

Sqlint provides two options to connect to the underlying database.

The single connection method:

use sqlint::{prelude::*, single::Sqlint};

#[tokio::main]
async fn main() -> Result<(), sqlint::error::Error> {
    let conn = Sqlint::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 sqlint::{prelude::*, pooled::Sqlint};

#[tokio::main]
async fn main() -> Result<(), sqlint::error::Error> {
    let pool = Sqlint::builder("file:///tmp/example.db")?.build();
    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![
        Value::from("meow"),
        Value::from(10),
        Value::from("warm"),
    ],
    params
);

Dependencies

~3–25MB
~402K SLoC