Skip to main content

Crate candybase

Crate candybase 

Source
Expand description

§Candybase — Procedural Database Access for Rust

Candy brings the simplicity of PHP’s mysqli_* functions to Rust — flat, procedural functions you can call one after another, with no builders, no traits, and no boilerplate. Safety is preserved internally through Result<T, CandyError>.

§Quick Start

Add the crate to Cargo.toml:

[dependencies]
candybase = { version = "0.1", features = ["sqlite"] }

Then write flat, immediate code:

use candybase::*;

fn main() -> Result<(), CandyError> {
    let conn = candy_connect("localhost", "user", "pass", "dbname")?;

    // SELECT
    let res   = candy_query(&conn, "SELECT * FROM users")?;
    let users = candy_fetch_all(res)?;
    println!("{:?}", users);

    // INSERT / UPDATE / DELETE
    candy_insert(&conn, "INSERT INTO users (name) VALUES ('Alice')")?;
    candy_update(&conn, "UPDATE users SET name='Bob' WHERE id=1")?;
    candy_delete(&conn, "DELETE FROM users WHERE id=2")?;

    // Atomic transaction
    candy_transaction(&conn, vec![
        "INSERT INTO orders (user_id) VALUES (1)",
        "UPDATE stock SET qty = qty - 1 WHERE id = 5",
    ])?;

    candy_close(conn)?;
    Ok(())
}

§Backend Selection

Candy detects which backend to use from the URL scheme or the CANDY_DB_URL environment variable:

SchemeBackend
mysql://…MySQL / MariaDB
postgres://…PostgreSQL
sqlite://…SQLite
(no env var)SQLite :memory:

§Feature Flags

Enable only the backends you need to keep compile times short:

candybase = { version = "0.1", features = ["mysql", "postgres", "sqlite"] }

The default feature set is ["sqlite"].

Re-exports§

pub use error::CandyError;

Modules§

error
Error Types
mysql
MySQL Backend
postgres
PostgreSQL Backend
sqlite
SQLite Backend

Structs§

CandyConn
An opaque connection handle returned by candy_connect.
CandyResult
An opaque result handle returned by candy_query.

Functions§

candy_close
Close the database connection and release all associated resources.
candy_connect
Establish a database connection.
candy_connect_url
Establish a database connection from a full URL string.
candy_delete
Execute a DELETE statement and return the number of deleted rows.
candy_fetch_all
Fetch every row from a CandyResult as a Vec<HashMap<String, String>>.
candy_fetch_one
Fetch the first (and typically only) row from a CandyResult.
candy_insert
Execute an INSERT statement and return the number of inserted rows.
candy_query
Execute a SQL query and return a buffered result handle.
candy_transaction
Execute multiple SQL statements atomically.
candy_update
Execute an UPDATE statement and return the number of affected rows.