candybase 0.1.0

A procedural database access library for Rust — as simple as PHP's mysqli_* functions, with Rust's safety.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
# 🍬 Candybase — Procedural Database Access for Rust

[![Crates.io](https://img.shields.io/crates/v/candybase.svg)](https://crates.io/crates/candybase)
[![Docs.rs](https://docs.rs/candybase/badge.svg)](https://docs.rs/candybase)
[![License: MIT OR Apache-2.0](https://img.shields.io/badge/license-MIT%20-blue.svg)](LICENSE)

> **As simple as PHP's `mysqli_*` functions. As safe as Rust.**

<center>
<img src="./candy_banner.svg"/>
</center>

---

## The Idea 

Most Rust database crates require you to learn builders, traits, generics, and async runtimes before you can run a single query. Candy takes the opposite approach: **flat functions, immediate results, no ceremony**.

If you have ever written PHP like this:

```php
$conn = mysqli_connect("localhost", "root", "secret", "shop");
$res  = mysqli_query($conn, "SELECT * FROM products");
$rows = mysqli_fetch_all($res, MYSQLI_ASSOC);
```

Then Candy will feel familiar:

```rust
use candybase::*;

let conn  = candy_connect("localhost", "root", "secret", "shop")?;
let res   = candy_query(&conn, "SELECT * FROM products")?;
let rows  = candy_fetch_all(res)?;
```

### Design Goals

| Goal | How Candy achieves it |
|------|----------------------|
| **Simplicity** | Flat procedural functions, no builders, no traits |
| **Safety** | Every function returns `Result<T, CandyError>` |
| **Universality** | MySQL, PostgreSQL, and SQLite through one API |
| **Framework-agnostic** | Works in Axum, Rocket, Yew, or plain `main()` |
| **Familiar** | Mirrors the mental model of `mysqli_*` |

---

## Installation

Add Candy to `Cargo.toml` with the backend(s) you need:

```toml
[dependencies]
# SQLite only (default, no server required)
candybase = "0.1"

# MySQL
candybase = { version = "0.1", features = ["mysql"] }

# PostgreSQL
candybase = { version = "0.1", features = ["postgres"] }

# All three backends
candybase = { version = "0.1", features = ["all"] }
```

### Feature Flags

| Flag | Backend | External dependency? |
|------|---------|---------------------|
| `sqlite` (default) | SQLite | No — bundled via `rusqlite` |
| `mysql` | MySQL / MariaDB | Requires a running server |
| `postgres` | PostgreSQL | Requires a running server |
| `all` | All three ||

---

## Quick Start

### SQLite (no server needed)

```rust
use candybase::*;

fn main() -> Result<(), CandyError> {
    // Connect to an in-memory database
    let conn = candy_connect("", "", "", ":memory:")?;

    // Create a table
    candy_query(&conn, "
        CREATE TABLE users (
            id   INTEGER PRIMARY KEY AUTOINCREMENT,
            name TEXT    NOT NULL,
            age  INTEGER
        )
    ")?;

    // Insert rows
    candy_insert(&conn, "INSERT INTO users (name, age) VALUES ('Alice', 30)")?;
    candy_insert(&conn, "INSERT INTO users (name, age) VALUES ('Bob',   25)")?;

    // Fetch all rows
    let res   = candy_query(&conn, "SELECT * FROM users")?;
    let users = candy_fetch_all(res)?;

    for u in &users {
        println!("id={} name={} age={}", u["id"], u["name"], u["age"]);
    }

    // Fetch a single row
    let res   = candy_query(&conn, "SELECT * FROM users WHERE name='Alice'")?;
    let alice = candy_fetch_one(res)?;
    println!("Alice's age: {}", alice["age"]);

    // Update and delete
    candy_update(&conn, "UPDATE users SET age = 31 WHERE name='Alice'")?;
    candy_delete(&conn, "DELETE FROM users WHERE name='Bob'")?;

    // Atomic transaction
    candy_transaction(&conn, vec![
        "INSERT INTO users (name, age) VALUES ('Carol', 35)",
        "UPDATE users SET age = age + 1 WHERE name = 'Alice'",
    ])?;

    // Close (optional — Drop cleans up too)
    candy_close(conn)?;
    Ok(())
}
```

### MySQL

```rust
use candybase::*;

fn main() -> Result<(), CandyError> {
    let conn = candy_connect("localhost", "root", "secret", "mydb")?;
    // ... same API as SQLite above ...
    candy_close(conn)
}
```

### PostgreSQL

```rust
use candybase::*;

fn main() -> Result<(), CandyError> {
    let conn = candy_connect("localhost", "postgres", "secret", "mydb")?;
    // ... same API ...
    candy_close(conn)
}
```

---

## Environment Variable

Set `CANDY_DB_URL` and call `candy_connect` with empty strings:

```bash
export CANDY_DB_URL="mysql://root:secret@localhost/shop"
```

```rust
// Reads CANDY_DB_URL automatically
let conn = candy_connect("", "", "", "")?;
```

Or use `candy_connect_url` directly:

```rust
let conn = candy_connect_url("sqlite:///path/to/mydb.sqlite")?;
let conn = candy_connect_url("mysql://user:pass@host/db")?;
let conn = candy_connect_url("postgres://user:pass@host/db")?;
```

---

## API Reference

### `candy_connect`

```rust
pub fn candy_connect(host: &str, user: &str, pass: &str, db: &str)
    -> Result<CandyConn, CandyError>
```

Opens a connection. Backend is inferred from `CANDY_DB_URL`, the `host` scheme, or the enabled feature flags. Returns a `CandyConn` handle.

---

### `candy_connect_url`

```rust
pub fn candy_connect_url(url: &str) -> Result<CandyConn, CandyError>
```

Opens a connection from a full URL string. URL scheme selects the backend:

| URL prefix | Backend |
|------------|---------|
| `mysql://` | MySQL |
| `postgres://` or `postgresql://` | PostgreSQL |
| `sqlite://` | SQLite |

---

### `candy_query`

```rust
pub fn candy_query(conn: &CandyConn, sql: &str) -> Result<CandyResult, CandyError>
```

Executes a SQL statement and returns a buffered `CandyResult`. Intended for `SELECT` statements. All rows are fetched into memory.

---

### `candy_fetch_all`

```rust
pub fn candy_fetch_all(res: CandyResult)
    -> Result<Vec<HashMap<String, String>>, CandyError>
```

Consumes a `CandyResult` and returns every row as a `Vec<HashMap<String, String>>`. All column values are strings; `NULL` becomes `"NULL"`.

---

### `candy_fetch_one`

```rust
pub fn candy_fetch_one(res: CandyResult)
    -> Result<HashMap<String, String>, CandyError>
```

Returns only the first row. Returns `CandyError::Fetch` if the result set is empty.

---

### `candy_insert`

```rust
pub fn candy_insert(conn: &CandyConn, sql: &str) -> Result<u64, CandyError>
```

Executes an `INSERT` statement. Returns the number of rows inserted.

---

### `candy_update`

```rust
pub fn candy_update(conn: &CandyConn, sql: &str) -> Result<u64, CandyError>
```

Executes an `UPDATE` statement. Returns the number of rows affected.

---

### `candy_delete`

```rust
pub fn candy_delete(conn: &CandyConn, sql: &str) -> Result<u64, CandyError>
```

Executes a `DELETE` statement. Returns the number of rows deleted.

---

### `candy_transaction`

```rust
pub fn candy_transaction(conn: &CandyConn, queries: Vec<&str>)
    -> Result<(), CandyError>
```

Executes a list of SQL statements as a single atomic transaction. Automatically rolls back if any statement fails and returns `CandyError::Transaction`.

---

### `candy_close`

```rust
pub fn candy_close(conn: CandyConn) -> Result<(), CandyError>
```

Closes the connection and releases resources. The `CandyConn` is consumed. Dropping a `CandyConn` without calling `candy_close` is also safe.

---

## Error Handling

```rust
use candybase::{candy_connect, CandyError};

match candy_connect("localhost", "root", "wrong_password", "db") {
    Ok(conn)                       => { /* connected */ }
    Err(CandyError::Connection(m)) => eprintln!("Cannot connect: {}", m),
    Err(CandyError::Query(m))      => eprintln!("Query failed: {}", m),
    Err(CandyError::Fetch(m))      => eprintln!("Fetch failed: {}", m),
    Err(CandyError::Transaction(m))=> eprintln!("TX failed: {}", m),
    Err(e)                         => eprintln!("Error: {}", e),
}
```

| Variant | When raised |
|---------|-------------|
| `CandyError::Connection` | Cannot reach the server / bad credentials |
| `CandyError::Query` | SQL statement rejected by the database |
| `CandyError::Fetch` | Row decoding failed / empty result set |
| `CandyError::Transaction` | A statement inside a transaction failed |
| `CandyError::UrlParse` | Unrecognised URL scheme |
| `CandyError::Internal` | Unexpected internal error |

---

## Running the Examples

```bash
# SQLite (no server needed)
cargo run --example sqlite_demo --features sqlite

# MySQL (requires a running server)
export CANDY_DB_URL="mysql://root:secret@localhost/test"
cargo run --example mysql_demo --features mysql

# PostgreSQL (requires a running server)
export CANDY_DB_URL="postgres://postgres:secret@localhost/test"
cargo run --example postgres_demo --features postgres
```

---

## Running Tests

```bash
# SQLite unit tests (always available)
cargo test --features sqlite

# All backends (requires live MySQL + PostgreSQL)
MYSQL_URL="mysql://root:secret@localhost/test" \
PG_URL="postgres://postgres:secret@localhost/test" \
cargo test --features all
```

---

## Using with Web Frameworks

### Axum

```rust
use axum::{Router, routing::get, Json};
use candybase::*;
use std::collections::HashMap;

async fn list_users() -> Json<Vec<HashMap<String, String>>> {
    let conn  = candy_connect("localhost", "root", "secret", "shop").unwrap();
    let res   = candy_query(&conn, "SELECT * FROM users").unwrap();
    let users = candy_fetch_all(res).unwrap();
    Json(users)
}
```

### Rocket

```rust
use rocket::get;
use candybase::*;

#[get("/users")]
fn users() -> String {
    let conn  = candy_connect("localhost", "root", "secret", "shop").unwrap();
    let res   = candy_query(&conn, "SELECT * FROM users").unwrap();
    let rows  = candy_fetch_all(res).unwrap();
    format!("{:?}", rows)
}
```

---

## Comparison with Other Crates

| Crate | Style | Backends | Learning curve |
|-------|-------|----------|----------------|
| **candy** | Procedural functions | MySQL, PG, SQLite | Minimal |
| sqlx | Async, macro-heavy | MySQL, PG, SQLite | Steep |
| diesel | ORM, schema-first | MySQL, PG, SQLite | Very steep |
| rusqlite | Low-level | SQLite only | Moderate |
| tokio-postgres | Async, low-level | PG only | Steep |

Candy is not a replacement for sqlx or Diesel in production systems that need async, connection pooling, migrations, or type-safe queries. It is the right choice when you want to **get something working quickly** with the least possible friction.

---

## Roadmap

- [ ] Async support (`candy_async` companion crate)
- [ ] Prepared statement API (`candy_prepare` / `candy_execute`)
- [ ] Connection pool helpers
- [ ] Named parameter binding (`?name` style)
- [ ] `serde` deserialization into typed structs

---
Licensed Under [MIT License](License)