#sql-query #query-builder #low-level #sql #build

qubl-rs

Low level And flexible query builder that gives you full control of your queries

5 releases (3 breaking)

new 0.4.0 Nov 4, 2024
0.3.1 Oct 11, 2024
0.3.0 Oct 11, 2024
0.2.0 Sep 24, 2024
0.1.0 Sep 22, 2024

#48 in Database implementations

Download history 102/week @ 2024-09-16 243/week @ 2024-09-23 38/week @ 2024-09-30 234/week @ 2024-10-07 39/week @ 2024-10-14

318 downloads per month

MIT license

47KB
1K SLoC

Sql Query Builder

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.

Examples

Initialize a query

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();
}

Build and Execute a query

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();

// ...

Examples Of Other Query Types

Insert Query


    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();

Delete Query


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();

Update Query


    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();

Disclaimer

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.

No runtime deps