🛠️ SQL Utility
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
- Dynamic Query Building: Construct complex SQL queries at runtime.
- Type Safety: Leverages Rust's type system and
serde_jsonfor safe value handling. - Flexible API: Low-level builders (
build_*) and high-level helpers (make_*). - Minimal Dependencies: Only
serde_json.
📦 Installation
Add the following to your Cargo.toml file:
[]
= "0.1.7"
= "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
use build_select_sql;
let sql = build_select_sql;
assert_eq!;
🔹 Using make_query
make_query builds clauses from slices of strings. All parameters except tables are Option.
Columns, tables, and order items are joined with , (comma with spaces); WHERE conditions are joined with AND.
use make_query;
// Full example
let sql = make_query;
assert_eq!;
// Minimal — select all rows
let sql = make_query;
assert_eq!;
Fetch Single Row
make_fetch is a convenience wrapper around build_select_sql that always adds LIMIT 1 and never adds OFFSET or ORDER BY. Use it when you only need one matching row.
use make_fetch;
// Select all columns, first matching row
let sql = make_fetch;
assert_eq!;
// Select specific columns with multiple WHERE conditions
let sql = make_fetch;
assert_eq!;
// No WHERE — first row of the table
let sql = make_fetch;
assert_eq!;
Insert Queries
Two functions are available for INSERT statements.
🔹 Using build_insert_sql
Accepts a HashMap<K, serde_json::Value> where K can be String or &str.
use HashMap;
use json;
use build_insert_sql;
let mut data = new;
data.insert;
data.insert;
data.insert;
data.insert;
let sql = build_insert_sql;
// INSERT INTO "users" ("name", "age", "is_admin", "deleted_at") VALUES ('John Doe', 30, false, NULL)
// Note: column order depends on HashMap iteration order.
🔹 Using make_insert (high-level)
make_insert is a convenience wrapper around build_insert_sql with the same signature. Use it for a consistent naming style across make_* helpers.
use HashMap;
use json;
use make_insert;
let mut data = new;
data.insert;
data.insert;
let sql = make_insert;
// INSERT INTO "users" ("name", "active") VALUES ('Alice', true)
// Note: column order depends on HashMap iteration order.
Supported value types
serde_json::Value |
SQL output |
|---|---|
String |
'escaped string' |
Number |
numeric literal |
Bool |
true / false |
Null |
NULL |
Array / Object |
JSON-serialised string |
Update Queries
Two functions are available for UPDATE statements.
🔹 Using build_update_sql (low-level)
Accepts the same HashMap<K, serde_json::Value> as build_insert_sql. The WHERE clause is a raw pre-built string.
use HashMap;
use json;
use build_update_sql;
let mut data = new;
data.insert;
data.insert;
let sql = build_update_sql;
// UPDATE "users" SET "name" = 'Jane Doe' , "updated_at" = '2024-01-01' WHERE id = 1
// Without WHERE — updates all rows
let mut data = new;
data.insert;
let sql = build_update_sql;
// UPDATE "users" SET "active" = false
🔹 Using make_update (high-level)
make_update builds the WHERE clause from a slice of condition strings (joined with AND), mirroring make_query and make_delete.
use HashMap;
use json;
use make_update;
// Single condition
let mut data = new;
data.insert;
let sql = make_update;
assert_eq!;
// Multiple conditions
let mut data = new;
data.insert;
let sql = make_update;
assert_eq!;
// No WHERE clause
let mut data = new;
data.insert;
let sql = make_update;
assert_eq!;
Delete Queries
Two functions are available for DELETE statements.
🔹 Using build_delete_sql (low-level)
use build_delete_sql;
// With WHERE clause
let sql = build_delete_sql;
assert_eq!;
// Without WHERE — deletes all rows
let sql = build_delete_sql;
assert_eq!;
🔹 Using make_delete (high-level)
make_delete builds the WHERE clause from a slice of condition strings (joined with AND), mirroring make_query.
use make_delete;
// Single condition
let sql = make_delete;
assert_eq!;
// Multiple conditions
let sql = make_delete;
assert_eq!;
// No WHERE clause
let sql = make_delete;
assert_eq!;
🛠 Helper Functions
These functions build individual SQL clauses from string slices.
| Function | Joins with | Description |
|---|---|---|
make_column(&[&str]) |
, |
Joins column names. |
make_table(&[&str]) |
, |
Joins table names. |
make_where(&[&str]) |
AND |
Joins WHERE conditions. |
make_order(&[&str]) |
, |
Joins ORDER BY expressions. |
quote(&str) |
— | Wraps in single quotes, escapes internal ' → ''. |
quote_identifier(&str) |
— | Wraps in double quotes, escapes internal " → "". |
Example
use ;
let cond = make_where;
assert_eq!;
assert_eq!;
assert_eq!;
📄 License
This project is licensed under the MIT License - see the LICENSE file for details.
Made with ❤️ in Rust.