sqlutil 0.1.0

A utility library for building SQL queries
Documentation
# 🛠️ SQL Utility

[![Rust](https://img.shields.io/badge/rust-1.70%2B-orange.svg)](https://www.rust-lang.org)
[![License](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE)

**SQL Utility** is a lightweight, flexible Rust library designed to simplify the dynamic construction of SQL queries. It provides a robust set of helper functions to programmatically generate `SELECT`, `INSERT`, `UPDATE`, and `DELETE` statements with ease.

---

## 📖 Table of Contents

- [Features]#-features
- [Installation]#-installation
- [Usage]#-usage
  - [Select Queries]#select-queries
  - [Insert Queries]#insert-queries
  - [Update Queries]#update-queries
  - [Delete Queries]#delete-queries
- [Helper Functions]#-helper-functions

---

## ✨ Features

- **Dynamic Query Building**: Construct complex SQL queries at runtime.
- **Type Safety**: Leverages Rust's type system and `serde_json` for safe value handling.
- **Flexible API**: Low-level builders and high-level helpers.
- **Zero Dependencies**: (Well, almost - just `serde_json`!).

## 📦 Installation

Add the following to your `Cargo.toml` file:

```toml
[dependencies]
sqlutil = "0.1.0"
serde_json = "1.0"
```

## 🚀 Usage

### Select Queries

Build `SELECT` queries using either the granular `build_select_sql` or the concise `make_query` helper.

#### 🔹 Using `build_select_sql`

```rust
use sqlutil::build_select_sql;

let sql = build_select_sql(
    "users",                 // table
    Some("id, name, email"), // columns
    Some("active = 1"),      // where clause
    Some("created_at DESC"), // order by
    Some(0),                 // offset
    Some(10),                // limit
);

assert_eq!(sql, "SELECT id, name, email FROM users WHERE active = 1 ORDER BY created_at DESC OFFSET 0 LIMIT 10");
```

#### 🔹 Using `make_query`

The `make_query` function simplifies constructing clauses from arrays of strings.

```rust
use sqlutil::make_query;

let sql = make_query(
    &["users"],                     // tables
    &["id", "name"],                // columns
    &["age > 18", "active = 1"],    // where conditions (joined by AND)
    &["name ASC"],                  // order by
    0,                              // offset
    20                              // limit
);

assert_eq!(sql, "SELECT id, name FROM users WHERE age > 18 AND active = 1 ORDER BY name ASC OFFSET 0 LIMIT 20");
```

### Insert Queries

Generate `INSERT` statements using `build_insert_sql`. It accepts a `HashMap<String, serde_json::Value>` to map column names to values.

```rust
use std::collections::HashMap;
use serde_json::json;
use sqlutil::build_insert_sql;

let mut data = HashMap::new();
data.insert("name".to_string(), json!("John Doe"));
data.insert("age".to_string(), json!(30));
data.insert("is_admin".to_string(), json!(false));

let sql = build_insert_sql("users", data);

// Output: INSERT INTO users (name, age, is_admin) VALUES ('John Doe', 30, false)
// Note: The order of columns depends on the HashMap iteration order.
```

### Update Queries

Generate `UPDATE` statements with `build_update_sql`.

```rust
use std::collections::HashMap;
use serde_json::json;
use sqlutil::build_update_sql;

let mut data = HashMap::new();
data.insert("name".to_string(), json!("Jane Doe"));
data.insert("updated_at".to_string(), json!("2023-01-01"));

let sql = build_update_sql(
    "users", 
    data, 
    Some("id = 1") // where clause
);

// Output: UPDATE users SET name = 'Jane Doe' , updated_at = '2023-01-01' WHERE id = 1
```

### Delete Queries

Generate `DELETE` statements with `build_delete_sql`.

```rust
use sqlutil::build_delete_sql;

let sql = build_delete_sql(
    "users", 
    Some("id = 1") // where clause
);

assert_eq!(sql, "DELETE FROM users WHERE id = 1");
```

## 🛠 Helper Functions

The library includes several utility functions to help you manually construct SQL parts:

| Function | Description |
|----------|-------------|
| `make_column(&[&str])` | Joins columns with commas. |
| `make_table(&[&str])` | Joins tables with commas. |
| `make_where(&[&str])` | Joins conditions with `AND`. |
| `make_order(&[&str])` | Joins order clauses with commas. |
| `quote(&str)` | Escapes single quotes and wraps the string in single quotes. |

**Example:**

```rust
use sqlutil::{make_where, quote};

let where_clause = make_where(&["status = 'active'", "count > 0"]);
assert_eq!(where_clause, "status = 'active' AND count > 0");

let quoted = quote("O'Reilly");
assert_eq!(quoted, "'O''Reilly'");
```

---

Made with ❤️ in Rust.