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:
| Scheme | Backend |
|---|---|
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§
Structs§
- Candy
Conn - An opaque connection handle returned by
candy_connect. - Candy
Result - 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
DELETEstatement and return the number of deleted rows. - candy_
fetch_ all - Fetch every row from a
CandyResultas aVec<HashMap<String, String>>. - candy_
fetch_ one - Fetch the first (and typically only) row from a
CandyResult. - candy_
insert - Execute an
INSERTstatement 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
UPDATEstatement and return the number of affected rows.