NaturalQueryLib
Welcome to NaturalQueryLib, a highly flexible SQL query builder written in Rust! This library allows you to build and execute SQL queries with ease, while supporting dynamic parameters and JSON encoding for database compatibility.
🚀 Features
- Supports SELECT, INSERT, UPDATE, and DELETE queries.
- Fluent API for building complex SQL queries.
- JSON support via
serde_jsonandsqlx. - Asynchronous execution with
sqlxconnection pools. - Lightweight and developer-friendly.
📦 Installation
Add the following to your Cargo.toml:
= "0.1.0"
= { = "0.8", = ["runtime-tokio-native-tls", "json"] }
= "0.1"
= { = "1.0", = ["derive"] }
= "1.0"
🛠️ Usage
Basic Setup
First, configure your SQLx connection pool:
use ;
use Query;
async
Examples
1️⃣ SELECT Query
let select_query = select
.from
.columns
.where_clause
.order_by
.limit
.build;
println!;
// Output: SELECT id, name, email FROM users WHERE age > 18 ORDER BY name ASC LIMIT 5
2️⃣ INSERT Query
let insert_query = insert_into
.columns
.values
.build;
println!;
// Output: INSERT INTO users (name, email, age) VALUES (?, ?, ?)
3️⃣ UPDATE Query
let update_query = update
.set
.where_clause
.build;
println!;
// Output: UPDATE users SET name = ?, email = ? WHERE id = 1
4️⃣ DELETE Query
let delete_query = delete_from
.where_clause
.build;
println!;
// Output: DELETE FROM users WHERE id = 1
5️⃣ JOIN Query
let join_query = select
.from
.join
.columns
.build;
println!;
// Output: SELECT users.id, users.name, orders.total FROM users INNER JOIN orders ON users.id = orders.user_id
Asynchronous Execution
Execute a Query
let rows_affected = update
.set
.where_clause
.execute
.await?;
println!;
Fetch Results
let users: = select
.from
.columns
.where_clause
.fetch_all
.await?;
for user in users
🎨 Stickers for Clarity
| Query Type | Example Output |
|---|---|
| SELECT | SELECT id, name FROM users WHERE active = true LIMIT 5 |
| INSERT | INSERT INTO users (name, email) VALUES (?, ?) |
| UPDATE | UPDATE users SET name = ?, email = ? WHERE id = 1 |
| DELETE | DELETE FROM users WHERE id = 1 |
| JOIN | SELECT users.id, orders.total FROM users INNER JOIN orders ON users.id = orders.user_id |
🧩 Contributing
Contributions are welcome! Please fork the repository and submit a pull request for any improvements or bug fixes.
- Fork the project.
- Create your feature branch (
git checkout -b feature/YourFeature). - Commit your changes (
git commit -m 'Add YourFeature'). - Push to the branch (
git push origin feature/YourFeature). - Open a pull request.
📜 License
This project is licensed under the MIT License. See the LICENSE file for details.
💬 Feedback
If you have any feedback, questions, or suggestions, feel free to reach out via GitHub Issues.