Crates.io | qubl-rs |
lib.rs | qubl-rs |
version | 0.5.0 |
source | src |
created_at | 2024-09-22 18:17:46.051862 |
updated_at | 2024-11-07 08:18:55.278072 |
description | Low level And flexible query builder that gives you full control of your queries. |
homepage | |
repository | https://github.com/Necoo33/qubl-rs |
max_upload_size | |
id | 1383156 |
size | 47,656 |
Low level And flexible query builder that gives you full control of your queries, it's created for especially mysql but probably it's compatible for postgresql with many ways as well. It also includes sanitization for both column and value inputs.
That builder enforces you to start your sql query from ground up and build it both imperative and declaratively, however you need.
It currently supports most basic types of queries: SELECT
, INSERT
, DELETE
, UPDATE
.
It also supports this operators for now: WHERE
, AND
, OR
, SET
, LIMIT
, OFFSET
, ORDER BY
, LIKE
.
For initializing a query, each query type has it's own constructor function. For example, if you want to start a select query, you have to initialize it with corresponding constructor function, such as: select()
, delete()
, update()
, insert()
use qubl::{QueryBuilder, ValueType};
fn main(){
let fields = vec!["name", "surname", "id", "age"];
let select_query = QueryBuilder::select(fields).unwrap();
let fields = vec!["name", "surname", "age"];
let values = vec![("necdet", ValueType::String), ("etiman", ValueType::String), ("21", ValueType::Integer)];
let insert_query = QueryBuilder::insert(fields, values).unwrap();
let delete_query = QueryBuilder::delete().unwrap();
let update_query = QueryBuilder::update().unwrap();
}
Then build your query both imperative or declarative way.
Imperative way:
// ...
select_query.table("products");
// do something:
select_query.where_cond("price", "<", ("250", ValueType::Integer));
select_query.and("price", ">", ("50", ValueType::Integer));
select_query.limit(10);
select_query.offset(0);
// don't forget to finish the query and assign it's value to a variable.
let finish_select_query = select_query.finish();
// ...
Or, do it declarative way:
// ...
let finish_the_select_query = select_query.table("products").where_cond("price", "<", ("250", ValueType::Integer)).and("price", ">", ("50", ValueType::Integer)).limit(10).offset(0).finish();
// ...
let columns = vec!["id", "name", "surname", "age", "password", "email", "grade", "passed"];
let values = vec![("1", ValueType::Integer), ("necdet arda", ValueType::String), ("etiman", ValueType::String),
("25", ValueType::Integer), ("123456", ValueType::String), ("arda_etiman_799@windowslive.com", ValueType::String),
("75.65", ValueType::Float), ("true", ValueType::Boolean)];
let insert_query = QueryBuilder::insert(columns, values).unwrap().table("users").finish();
let mut delete_query = QueryBuilder::delete().unwrap();
delete_query.table("users");
delete_query.where_cond("age", "<", ("25", ValueType::Integer));
let delete_query = delete_query.finish();
let mut update_query = QueryBuilder::update().unwrap();
update_query = update_query.table("users")
.set("name", ("necdet", ValueType::String))
.set("passed", ("true", ValueType::Boolean))
.where_cond("id", "=", ("1", ValueType::Integer));
let finish_update_query = update_query.finish();
But don't forget, this liblary is just builds that queries, not executes it. For that, you have to use a database driver or orm.