pub struct QueryBuilder<'a> {
pub query: String,
pub table: String,
pub qtype: QueryType,
pub list: Vec<KeywordList>,
pub hq: Option<[&'a str; 26]>,
}
Expand description
Struct that benefits to build queries for interactions with rdbms’s.
Fields§
§query: String
§table: String
§qtype: QueryType
§list: Vec<KeywordList>
§hq: Option<[&'a str; 26]>
Implementations§
Source§impl<'a> QueryBuilder<'a>
Implementations For QueryBuilder.
impl<'a> QueryBuilder<'a>
Implementations For QueryBuilder.
Sourcepub fn select(fields: Vec<&str>) -> Result<Self, Error>
pub fn select(fields: Vec<&str>) -> Result<Self, Error>
Select constructor. Use it if you want to build a Select Query.
use qubl::{QueryBuilder, ValueType};
fn main(){
let query = QueryBuilder::select(vec!["*"]).unwrap();
}
Sourcepub fn delete() -> Result<Self, Error>
pub fn delete() -> Result<Self, Error>
Delete constructor. Use it if you want to build a Delete Query.
use qubl::{QueryBuilder, ValueType};
fn main(){
let query = QueryBuilder::delete().unwrap();
}
Sourcepub fn update() -> Result<Self, Error>
pub fn update() -> Result<Self, Error>
Update constructor. Use it if you want to build a Update Query.
use qubl::{QueryBuilder, ValueType};
fn main(){
let query = QueryBuilder::update().unwrap();
}
Sourcepub fn insert(columns: Vec<&str>, values: Vec<ValueType>) -> Result<Self, Error>
pub fn insert(columns: Vec<&str>, values: Vec<ValueType>) -> Result<Self, Error>
Insert constructor. Use it if you want to build a Insert Query.
use qubl::{QueryBuilder, ValueType};
fn main(){
let fields = vec!["id", "age", "name"];
let values = vec![ValueType::Int32(5), ValueType::Int64(25), ValueType::String("necdet".to_string())]
let query = QueryBuilder::insert(fields, values).unwrap();
}
Sourcepub fn table(&mut self, table: &str) -> &mut Self
pub fn table(&mut self, table: &str) -> &mut Self
define the table. It should came after the constructors.
use qubl::{QueryBuilder, ValueType};
fn main(){
let query = QueryBuilder::select(vec!["*"]).unwrap().table("users");
}
Sourcepub fn count(condition: &str, _as: Option<&str>) -> Self
pub fn count(condition: &str, _as: Option<&str>) -> Self
Count constructor. Use it if you want to learn to length of a table.
use qubl::{QueryBuilder, ValueType};
fn main(){
let query = QueryBuilder::count("*", Some("length")).table("users");
}
Sourcepub fn where_(
&mut self,
column: &str,
mark: &str,
value: ValueType,
) -> &mut Self
pub fn where_( &mut self, column: &str, mark: &str, value: ValueType, ) -> &mut Self
add the “WHERE” keyword with it’s synthax.
use qubl::{QueryBuilder, ValueType};
fn main(){
let query = QueryBuilder::select(vec!["*"]).unwrap().table("users").where_("id", "=", ValueType::Int32(5)).finish();
assert_eq!(query, "SELECT * FROM users WHERE id = 5;")
}
Sourcepub fn where_in(&mut self, column: &str, ins: &Vec<ValueType>) -> &mut Self
pub fn where_in(&mut self, column: &str, ins: &Vec<ValueType>) -> &mut Self
It adds the “IN” keyword with it’s synthax. Don’t use “.where_cond()” method if you use it.
use qubl::{QueryBuilder, ValueType};
fn main(){
let ins = vec![ValueType::Int16(1), ValueType::Int64(5), ValueType::Int32(10)];
let query = QueryBuilder::select(vec!["*"]).unwrap().table("users").where_in("id", &ins).finish();
assert_eq!(query, "SELECT * FROM users WHERE id IN (1, 5, 10);")
}
Sourcepub fn where_not_in(&mut self, column: &str, ins: &Vec<ValueType>) -> &mut Self
pub fn where_not_in(&mut self, column: &str, ins: &Vec<ValueType>) -> &mut Self
It adds the “NOT IN” keyword with it’s synthax. Don’t use “.where_cond()” method if you use it.
use qubl::{QueryBuilder, ValueType};
fn main(){
let ins = vec![ValueType::Int16(1), ValueType::Int64(5), ValueType::Int32(10)];
let query = QueryBuilder::select(vec!["*"]).unwrap().table("users").where_not_in("id", &ins).finish();
assert_eq!(query, "SELECT * FROM users WHERE id NOT IN (1, 5, 10);")
}
Sourcepub fn where_in_custom(&mut self, column: &str, query: &str) -> &mut Self
pub fn where_in_custom(&mut self, column: &str, query: &str) -> &mut Self
It adds the “IN” keyword with it’s synthax and an empty condition, use it if you want to give more complex condition to “IN” keyword. Don’t use “.where_cond()” with it.
use qubl::{QueryBuilder, ValueType};
fn main(){
let ins = vec![ValueType::Int16(1), ValueType::Int64(5), ValueType::Int32(10)];
let query = QueryBuilder::select(vec!["*"]).unwrap().table("users").where_in_custom("id", "1, 5, 10").finish();
assert_eq!(query, "SELECT * FROM users WHERE id IN (1, 5, 10);")
}
Sourcepub fn where_not_in_custom(&mut self, column: &str, query: &str) -> &mut Self
pub fn where_not_in_custom(&mut self, column: &str, query: &str) -> &mut Self
It adds the “NOT IN” keyword with it’s synthax and an empty condition, use it if you want to give more complex condition to “NOT IN” keyword. Don’t use “.where_cond()” with it.
use qubl::{QueryBuilder, ValueType};
fn main(){
let ins = vec![ValueType::Int16(1), ValueType::Int64(5), ValueType::Int32(10)];
let query = QueryBuilder::select(vec!["*"]).unwrap().table("users").where_not_in_custom("id", "1, 5, 10").finish();
assert_eq!(query, "SELECT * FROM users WHERE id NOT IN (1, 5, 10);")
}
Sourcepub fn and_in(&mut self, column: &str, ins: &Vec<ValueType>) -> &mut Self
pub fn and_in(&mut self, column: &str, ins: &Vec<ValueType>) -> &mut Self
It adds the “IN” keyword with it’s synthax, with ‘AND’ keyword except ‘WHERE’.
use qubl::{QueryBuilder, ValueType};
fn main(){
let ins = vec![ValueType::Int16(1), ValueType::Int64(5), ValueType::Int32(10)];
let query = QueryBuilder::select(vec!["*"]).unwrap()
.table("users")
.where_("class", "=", ValueType::String("10/c".to_string()))
.and_in("id", &ins)
.finish();
assert_eq!(query, "SELECT * FROM users WHERE class = '10/c' AND id IN (1, 5, 10);")
}
Sourcepub fn and_not_in(&mut self, column: &str, ins: &Vec<ValueType>) -> &mut Self
pub fn and_not_in(&mut self, column: &str, ins: &Vec<ValueType>) -> &mut Self
It adds the “NOT IN” keyword with it’s synthax, with ‘AND’ keyword except ‘WHERE’.
use qubl::{QueryBuilder, ValueType};
fn main(){
let ins = vec![ValueType::Int16(1), ValueType::Int64(5), ValueType::Int32(10)];
let query = QueryBuilder::select(vec!["*"]).unwrap()
.table("users")
.where_("class", "=", ValueType::String("10/c".to_string()))
.and_not_in("id", &ins)
.finish();
assert_eq!(query, "SELECT * FROM users WHERE class = '10/c' AND id NOT IN (1, 5, 10);")
}
Sourcepub fn and_in_custom(&mut self, column: &str, query: &str) -> &mut Self
pub fn and_in_custom(&mut self, column: &str, query: &str) -> &mut Self
It adds the “IN” keyword with it’s synthax, with ‘AND’ keyword except ‘WHERE’.
use qubl::{QueryBuilder, ValueType};
fn main(){
let ins = vec![ValueType::Int16(1), ValueType::Int64(5), ValueType::Int32(10)];
let query = QueryBuilder::select(vec!["*"]).unwrap()
.table("users")
.where_("class", "=", ValueType::String("10/c".to_string()))
.and_in_custom("id", "1, 5, 10")
.finish();
assert_eq!(query, "SELECT * FROM users WHERE class = '10/c' AND id IN (1, 5, 10);")
}
Sourcepub fn and_not_in_custom(&mut self, column: &str, query: &str) -> &mut Self
pub fn and_not_in_custom(&mut self, column: &str, query: &str) -> &mut Self
It adds the “IN” keyword with it’s synthax, with ‘AND’ keyword except ‘WHERE’.
use qubl::{QueryBuilder, ValueType};
fn main(){
let ins = vec![ValueType::Int16(1), ValueType::Int64(5), ValueType::Int32(10)];
let query = QueryBuilder::select(vec!["*"]).unwrap()
.table("users")
.where_("class", "=", ValueType::String("10/c".to_string()))
.and_not_in_custom("id", "1, 5, 10")
.finish();
assert_eq!(query, "SELECT * FROM users WHERE class = '10/c' AND id NOT IN (1, 5, 10);")
}
Sourcepub fn or_in(&mut self, column: &str, ins: &Vec<ValueType>) -> &mut Self
pub fn or_in(&mut self, column: &str, ins: &Vec<ValueType>) -> &mut Self
It adds the “IN” keyword with it’s synthax, with ‘OR’ keyword except ‘WHERE’.
use qubl::{QueryBuilder, ValueType};
fn main(){
let ins = vec![ValueType::Int16(1), ValueType::Int64(5), ValueType::Int32(10)];
let query = QueryBuilder::select(vec!["*"]).unwrap()
.table("users")
.where_("class", "=", ValueType::String("10/c".to_string()))
.or_in("id", &ins)
.finish();
assert_eq!(query, "SELECT * FROM users WHERE class = '10/c' OR id IN (1, 5, 10);")
}
Sourcepub fn or_not_in(&mut self, column: &str, ins: &Vec<ValueType>) -> &mut Self
pub fn or_not_in(&mut self, column: &str, ins: &Vec<ValueType>) -> &mut Self
It adds the “NOT IN” keyword with it’s synthax, with ‘OR’ keyword except ‘WHERE’.
use qubl::{QueryBuilder, ValueType};
fn main(){
let ins = vec![ValueType::Int16(1), ValueType::Int64(5), ValueType::Int32(10)];
let query = QueryBuilder::select(vec!["*"]).unwrap()
.table("users")
.where_("class", "=", ValueType::String("10/c".to_string()))
.or_not_in("id", &ins)
.finish();
assert_eq!(query, "SELECT * FROM users WHERE class = '10/c' OR id NOT IN (1, 5, 10);")
}
Sourcepub fn or_in_custom(&mut self, column: &str, query: &str) -> &mut Self
pub fn or_in_custom(&mut self, column: &str, query: &str) -> &mut Self
It adds the “IN” keyword with it’s synthax, with ‘AND’ keyword except ‘WHERE’.
use qubl::{QueryBuilder, ValueType};
fn main(){
let ins = vec![ValueType::Int16(1), ValueType::Int64(5), ValueType::Int32(10)];
let query = QueryBuilder::select(vec!["*"]).unwrap()
.table("users")
.where_("class", "=", ValueType::String("10/c".to_string()))
.or_in_custom("id", "1, 5, 10")
.finish();
assert_eq!(query, "SELECT * FROM users WHERE class = '10/c' OR id IN (1, 5, 10);")
}
Sourcepub fn or_not_in_custom(&mut self, column: &str, query: &str) -> &mut Self
pub fn or_not_in_custom(&mut self, column: &str, query: &str) -> &mut Self
It adds the “IN” keyword with it’s synthax, with ‘AND’ keyword except ‘WHERE’.
use qubl::{QueryBuilder, ValueType};
fn main(){
let ins = vec![ValueType::Int16(1), ValueType::Int64(5), ValueType::Int32(10)];
let query = QueryBuilder::select(vec!["*"]).unwrap()
.table("users")
.where_("class", "=", ValueType::String("10/c".to_string()))
.or_not_in_custom("id", "1, 5, 10")
.finish();
assert_eq!(query, "SELECT * FROM users WHERE class = '10/c' OR id NOT IN (1, 5, 10);")
}
Sourcepub fn open_parenthesis(&mut self, parenthesis_type: BracketType) -> &mut Self
pub fn open_parenthesis(&mut self, parenthesis_type: BracketType) -> &mut Self
it opens a parenthesis without declaring it’s first parameter. It adds this string to the query: “… (WHERE / AND / OR) (” It’s suitable for more custom approach with parenthesis, if you only want to use WHERE, AND & OR queries, we suggest you to check .open_parenthesis_with()
method.
use qubl::{QueryBuilder, ValueType};
fn main {
let query = QueryBuilder::select(vec!["*"]).unwrap()
.table("users")
.where_("grades", ">", ValueType::Int32(80))
.open_parenthesis(BracketType::And)
.and("height", ">", ValueType::Int32(170))
.or("weight", ">", ValueType::Int32(60))
.close_parenthesis() // you have to close the parenthesis, otherwise it'll panic.
.finish();
assert_eq!(query, "SELECT * FROM users WHERE grades > 80 AND ( AND height > 170 OR weight > 60);");
}
Sourcepub fn open_parenthesis_with(
&mut self,
parenthesis_type: BracketType,
column: &str,
mark: &str,
value: ValueType,
) -> &mut Self
pub fn open_parenthesis_with( &mut self, parenthesis_type: BracketType, column: &str, mark: &str, value: ValueType, ) -> &mut Self
it opens a parenthesis with declaring it’s first parameter, it suits very well the most common use cases of parenthesis.
use qubl::{QueryBuilder, ValueType};
fn main {
let query = QueryBuilder::select(vec!["*"]).unwrap()
.table("users")
.where_("grades", ">", ValueType::Int32(80))
.open_parenthesis_with(BracketType::And, "height", ">", ValueType::Int32(170))
.open_parenthesis_with(BracketType::Or, "weight", ">", ValueType::Int32(50))
.and("weight", "<", ValueType::Int32(70))
.close_parenthesis()
.close_parenthesis()
.finish();
assert_eq!(query, "SELECT * FROM users WHERE grades > 80 AND (height > 170 OR (weight > 50 AND weight < 70));");
}
Sourcepub fn close_parenthesis(&mut self) -> &mut Self
pub fn close_parenthesis(&mut self) -> &mut Self
Parenthesis closer for open parenthesis. The combined amount of .open_parenthesis()
and .open_parenthesis_with()
must match to be the query produced correctly. It’ll panic if there is no call of these methods or query don’t have “(” character.
Sourcepub fn time_zone(&mut self, timezone: Timezone) -> &mut Self
pub fn time_zone(&mut self, timezone: Timezone) -> &mut Self
it benefits to set timezone when you make your query. It’s very flexible, always put on very beginning of the query, you can use it later than any other method.
Sourcepub fn global_time_zone(&mut self, timezone: Timezone) -> &mut Self
pub fn global_time_zone(&mut self, timezone: Timezone) -> &mut Self
it benefits to set global timezone when you make your query. It’s very flexible, always put on very beginning of the query, you can use it later than any other method.
Sourcepub fn or(&mut self, column: &str, mark: &str, value: ValueType) -> &mut Self
pub fn or(&mut self, column: &str, mark: &str, value: ValueType) -> &mut Self
It adds the “OR” keyword with it’s synthax. Warning: It’s not ready yet to chaining “AND” and “OR” keywords, for now, applying that kind of complex query use “.append_custom()” method instead.
use qubl::{QueryBuilder, ValueType};
fn main(){
let ins = vec![ValueType::Int16(1), ValueType::Int64(5), ValueType::Int32(10)];
let query = QueryBuilder::select(vec!["*"]).unwrap().table("users").where_("id", "=", ValueType::Int32(10)).or("name", "=", ValueType::String("necdet".to_string())).finish();
assert_eq!(query, "SELECT * FROM users WHERE id = 10 OR name = 'necdet';")
}
Sourcepub fn set(&mut self, column: &str, value: ValueType) -> &mut Self
pub fn set(&mut self, column: &str, value: ValueType) -> &mut Self
It adds the “SET” keyword with it’s synthax.
use qubl::{QueryBuilder, ValueType};
fn main(){
let query = QueryBuilder::update().unwrap()
.table("users")
.set("name", ValueType::String("arda".to_string()))
.where_("id", "=", ValueType::Int32(1))
.finish();
assert_eq!(query, "UPDATE users SET name = 'arda' WHERE id = 1;")
}
Sourcepub fn and(&mut self, column: &str, mark: &str, value: ValueType) -> &mut Self
pub fn and(&mut self, column: &str, mark: &str, value: ValueType) -> &mut Self
It adds the “AND” keyword with it’s synthax. Warning: It’s not ready yet to chaining “OR” and “AND” keywords, for now, applying that kind of complex query use “.append_custom()” method instead.
use qubl::{QueryBuilder, ValueType};
fn main(){
let ins = vec![ValueType::Int16(1), ValueType::Int64(5), ValueType::Int32(10)];
let query = QueryBuilder::select(vec!["*"]).unwrap().table("users").where_("id", "=", ValueType::Int32(10)).and("name", "=", ValueType::String("necdet".to_string())).finish();
assert_eq!(query, "SELECT * FROM users WHERE id = 10 AND name = 'necdet';")
}
Sourcepub fn offset(&mut self, offset: i32) -> &mut Self
pub fn offset(&mut self, offset: i32) -> &mut Self
It adds the “OFFSET” keyword with it’s synthax. Be careful about it’s alignment with “LIMIT” keyword.
use qubl::{QueryBuilder, ValueType};
fn main(){
let ins = vec![ValueType::Int16(1), ValueType::Int64(5), ValueType::Int32(10)];
let query = QueryBuilder::select(vec!["*"]).unwrap().table("users").where_("id", "=", ValueType::Int32(10)).limit(5).offset(0).finish();
assert_eq!(query, "SELECT * FROM users WHERE id = 10 LIMIT 5 OFFSET 0;")
}
Sourcepub fn limit(&mut self, limit: i32) -> &mut Self
pub fn limit(&mut self, limit: i32) -> &mut Self
It adds the “LIMIT” keyword with it’s synthax.
use qubl::{QueryBuilder, ValueType};
fn main(){
let ins = vec![ValueType::Int16(1), ValueType::Int64(5), ValueType::Int32(10)];
let query = QueryBuilder::select(vec!["*"]).unwrap().table("users").where_("id", "=", ValueType::Int32(10)).limit(5).offset(0).finish();
assert_eq!(query, "SELECT * FROM users WHERE id = 10 LIMIT 5 OFFSET 0;")
}
Sourcepub fn like(&mut self, columns: Vec<&str>, operand: &str) -> &mut Self
pub fn like(&mut self, columns: Vec<&str>, operand: &str) -> &mut Self
It adds the “LIKE” keyword with it’s synthax.
use qubl::{QueryBuilder, ValueType};
fn main(){
let query = QueryBuilder::select(vec!["*"]).unwrap()
.table("blogs")
.like(vec!["description", "title"], "qubl is awesome!")
.finish();
assert_eq!(query, "SELECT * FROM blogs WHERE description LIKE '%qubl is awesome!%' OR title LIKE '%qubl is awesome!%';")
}
// it has more niche and different usages, for them check the tests.
Sourcepub fn order_by(&mut self, column: &str, ordering: &str) -> &mut Self
pub fn order_by(&mut self, column: &str, ordering: &str) -> &mut Self
It adds the “ORDER BY” keyword with it’s synthax. It only accepts “ASC”, “DESC”, “asc”, “desc” values.
use qubl::{QueryBuilder, ValueType};
fn main(){
let query = QueryBuilder::select(vec!["*"]).unwrap()
.table("users")
.where_("age", ">", ValueType::Int32(25))
.order_by("id", "ASC")
.limit(5)
.offset(0)
.finish();
assert_eq!(query, "SELECT * FROM users WHERE age > 25 ORDER BY id ASC LIMIT 5 OFFSET 0;")
}
Sourcepub fn order_random(&mut self) -> &mut Self
pub fn order_random(&mut self) -> &mut Self
A practical method that adds a query for shuffling the lines.
use qubl::{QueryBuilder, ValueType};
fn main(){
let query = QueryBuilder::select(vec!["*"]).unwrap()
.table("users")
.where_("age", ">", ValueType::Int32(25))
.order_random()
.limit(5)
.offset(0)
.finish();
assert_eq!(query, "SELECT * FROM users WHERE age > 25 ORDER BY RAND() LIMIT 5 OFFSET 0;")
}
Sourcepub fn order_by_field(&mut self, column: &str, ordering: Vec<&str>) -> &mut Self
pub fn order_by_field(&mut self, column: &str, ordering: Vec<&str>) -> &mut Self
Adds “FIELD()” function with it’s synthax. It’s used on ordering depending on strings.
use qubl::{QueryBuilder, ValueType};
fn main(){
let query = QueryBuilder::select(vec!["*"]).unwrap()
.table("users")
.where_("age", ">", ValueType::Int32(25))
.order_by_field("role", vec!["admin", "member", "observer"])
.limit(5)
.offset(0)
.finish();
assert_eq!(query, "SELECT * FROM users WHERE age > 25 ORDER BY FIELD(role, 'admin', 'member', 'observer') LIMIT 5 OFFSET 0;")
}
Sourcepub fn group_by(&mut self, column: &str) -> &mut Self
pub fn group_by(&mut self, column: &str) -> &mut Self
It adds the “GROUP BY” keyword with it’s Synthax.
pub fn having( &mut self, column: &str, mark: &str, value: ValueType, ) -> &mut Self
Sourcepub fn inner_join(
&mut self,
table: &str,
left: &str,
mark: &str,
right: &str,
) -> &mut Self
pub fn inner_join( &mut self, table: &str, left: &str, mark: &str, right: &str, ) -> &mut Self
it adds the INNER JOIN
keyword with it’s synthax.
use qubl::{QueryBuilder, ValueType};
fn main(){
let query = QueryBuilder::select(vec!["*"]).unwrap()
.table("students s")
.inner_join("grades g", "s.id", "=", "g.student_id")
.where_("id", "=", ValueType::Int32(10))
.finish();
assert_eq!(query, "SELECT * FROM students s INNER JOIN grades g ON s.id = g.student_id WHERE id = 10;");
}
Sourcepub fn left_join(
&mut self,
table: &str,
left: &str,
mark: &str,
right: &str,
) -> &mut Self
pub fn left_join( &mut self, table: &str, left: &str, mark: &str, right: &str, ) -> &mut Self
it adds the LEFT JOIN
keyword with it’s synthax.
use qubl::{QueryBuilder, ValueType};
fn main(){
let query = QueryBuilder::select(vec!["*"]).unwrap()
.table("students s")
.left_join("grades g", "s.id", "=", "g.student_id")
.where_("id", "=", ValueType::Int32(10))
.finish();
assert_eq!(query, "SELECT * FROM students s LEFT JOIN grades g ON s.id = g.student_id WHERE id = 10;");
}
Sourcepub fn right_join(
&mut self,
table: &str,
left: &str,
mark: &str,
right: &str,
) -> &mut Self
pub fn right_join( &mut self, table: &str, left: &str, mark: &str, right: &str, ) -> &mut Self
it adds the RIGHT JOIN
keyword with it’s synthax.
use qubl::{QueryBuilder, ValueType};
fn main(){
let query = QueryBuilder::select(vec!["*"]).unwrap()
.table("students s")
.right_join("grades g", "s.id", "=", "g.student_id")
.where_("id", "=", ValueType::Int32(10))
.finish();
assert_eq!(query, "SELECT * FROM students s RIGHT JOIN grades g ON s.id = g.student_id WHERE id = 10;");
}
Sourcepub fn cross_join(&mut self, table: &str) -> &mut Self
pub fn cross_join(&mut self, table: &str) -> &mut Self
it adds the CROSS JOIN
keyword with it’s synthax.
use qubl::{QueryBuilder, ValueType};
fn main(){
let query = QueryBuilder::select(vec!["*"]).unwrap()
.table("students s")
.cross_join("grades g")
.where_("id", "=", ValueType::Int32(10))
.finish();
assert_eq!(query, "SELECT * FROM students s RIGHT JOIN grades g ON s.id = g.student_id WHERE id = 10;");
}
Sourcepub fn natural_join(&mut self, table: &str) -> &mut Self
pub fn natural_join(&mut self, table: &str) -> &mut Self
it adds the NATURAL JOIN
keyword with it’s synthax.
use qubl::{QueryBuilder, ValueType};
fn main(){
let query = QueryBuilder::select(vec!["*"]).unwrap()
.table("students s")
.natural_join("grades g")
.where_("id", "=", ValueType::Int32(10))
.finish();
assert_eq!(query, "SELECT * FROM students s RIGHT JOIN grades g ON s.id = g.student_id WHERE id = 10;");
}
Sourcepub fn union(&mut self, others: Vec<QueryBuilder<'_>>) -> &mut Self
pub fn union(&mut self, others: Vec<QueryBuilder<'_>>) -> &mut Self
it adds the UNION
keyword and its synthax. You can pass multiple queries to union with:
use qubl::{QueryBuilder, ValueType};
fn main(){
let mut union_1 = QueryBuilder::select(vec!["name", "age", "id"]).unwrap();
union_1.table("users").where_("age", ">", ValueType::Int32(7));
let union_2 = QueryBuilder::select(vec!["name", "age", "id"]).unwrap()
.table("users")
.where_("age", "<", ValueType::Int32(15))
.union(vec![union_1])
.finish();
assert_eq!(union_2, "(SELECT name, age, id FROM users WHERE age < 15) UNION (SELECT name, age, id FROM users WHERE age > 7);");
}
Sourcepub fn union_all(&mut self, others: Vec<QueryBuilder<'_>>) -> &mut Self
pub fn union_all(&mut self, others: Vec<QueryBuilder<'_>>) -> &mut Self
it adds the UNION
keyword and its synthax. You can pass multiple queries to union with:
use qubl::{QueryBuilder, ValueType};
fn main(){
let mut union_1 = QueryBuilder::select(vec!["id", "title", "description", "published"]).unwrap();
union_1.table("blogs").like(vec!["title"], "text");
let mut union_2 = QueryBuilder::select(vec!["id", "title", "description", "published"]).unwrap();
union_2.table("blogs").like(vec!["description"], "some text");
let union_3 = QueryBuilder::select(vec!["id", "title", "description", "published"]).unwrap()
.table("blogs")
.where_("published", "=", ValueType::Boolean(true))
.union_all(vec![union_1, union_2])
.finish();
assert_eq!(union_3, "(SELECT id, title, description, published FROM blogs WHERE published = true) UNION ALL (SELECT id, title, description, published FROM blogs WHERE title LIKE '%text%') UNION ALL (SELECT id, title, description, published FROM blogs WHERE description LIKE '%some text%');");
}
Sourcepub fn append_custom(&mut self, query: &str) -> &mut Self
pub fn append_custom(&mut self, query: &str) -> &mut Self
A wildcard method that gives you the chance to write a part of your query. Warning, it does not add any keyword to builder, i’ll encourage to add proper keyword to it with .append_keyword()
method for your custom query, otherwise you should continue building your query by yourself with that function, or you’ve to be prepared to encounter bugs.
use qubl::{QueryBuilder, ValueType};
fn main(){
let query = QueryBuilder::select(vec!["*"]).unwrap()
.table("users")
.append_custom("WHERE age > 25 ORDER BY FIELD(role, 'admin', 'member', 'observer') LIMIT 5 OFFSET 0")
.finish();
assert_eq!(query, "SELECT * FROM users WHERE age > 25 ORDER BY FIELD(role, 'admin', 'member', 'observer') LIMIT 5 OFFSET 0;")
}
Sourcepub fn append_keyword(&mut self, keyword: KeywordList) -> &mut Self
pub fn append_keyword(&mut self, keyword: KeywordList) -> &mut Self
A wildcard method that benefits you to append a keyword to the keyword list, so the QueryBuilder can build your queries properly, later than you appended your custom string to your query. It should be used with .append_custom()
method.
use qubl::{QueryBuilder, ValueType, KeywordList};
fn main(){
let query = QueryBuilder::select(vec!["*"]).unwrap()
.table("users")
.append_custom("WHERE age > 25 ORDER BY FIELD(role, 'admin', 'member', 'observer') LIMIT 5 OFFSET 0")
.append_keyword(KeywordList::Where)
.append_keyword(KeywordList::Field)
.append_keyword(KeywordList::Limit)
.append_keyword(KeywordList::Offset)
.finish();
assert_eq!(query, "SELECT * FROM users WHERE age > 25 ORDER BY FIELD(role, 'admin', 'member', 'observer') LIMIT 5 OFFSET 0;")
}
Sourcepub fn json_extract(
&mut self,
haystack: &str,
needle: &str,
_as: Option<&str>,
) -> &mut Self
pub fn json_extract( &mut self, haystack: &str, needle: &str, _as: Option<&str>, ) -> &mut Self
It applies “JSON_EXTRACT()” mysql function with it’s Synthax. If you encounter any syntactic bugs or deficiencies about that function, please report it via opening an issue.
use qubl::{QueryBuilder, ValueType};
fn main(){
let query = QueryBuilder::select(["*"].to_vec()).unwrap()
.json_extract("articles", "[0]", Some("blog1"))
.json_extract("articles", "[1]", Some("blog2"))
.json_extract("articles", "[2]", Some("blog3"))
.table("users")
.where_("published", "=", ValueType::Int32(1))
.finish();
assert_eq!(query, "SELECT JSON_EXTRACT(articles, '$[0]') AS blog1, JSON_EXTRACT(articles, '$[1]') AS blog2, JSON_EXTRACT(articles, '$[2]') AS blog3 FROM users WHERE published = 1;")
}
Sourcepub fn json_contains(
&mut self,
column: &str,
needle: JsonValue<'_>,
path: Option<&str>,
) -> &mut Self
pub fn json_contains( &mut self, column: &str, needle: JsonValue<'_>, path: Option<&str>, ) -> &mut Self
It applies “JSON_CONTAINS()” mysql function with it’s Synthax. If you encounter any syntactic bugs or deficiencies about that function, please report it via opening an issue.
use qubl::{QueryBuilder, ValueType, JsonValue};
fn main(){
let value = ValueType::String("blablabla.jpg".to_string());
let prop = vec![("name", &value)];
let object = JsonValue::MysqlJsonObject(&prop);
let query = QueryBuilder::select(["*"].to_vec()).unwrap()
.table("users")
.where_("pic", "=", ValueType::String("".to_string()))
.json_contains("pic", object, Some(".name"))
.finish();
assert_eq!(query, "SELECT * FROM users WHERE JSON_CONTAINS(pic, JSON_OBJECT('name', 'blablabla.jpg'), '$.name');")
}
Sourcepub fn not_json_contains(
&mut self,
column: &str,
needle: JsonValue<'_>,
path: Option<&str>,
) -> &mut Self
pub fn not_json_contains( &mut self, column: &str, needle: JsonValue<'_>, path: Option<&str>, ) -> &mut Self
It applies “NOT JSON_CONTAINS()” mysql function with it’s Synthax. If you encounter any syntactic bugs or deficiencies about that function, please report it via opening an issue.
use qubl::{QueryBuilder, ValueType, JsonValue};
fn main(){
let value = ValueType::String("blablabla.jpg".to_string());
let prop = vec![("name", &value)];
let object = JsonValue::MysqlJsonObject(&prop);
let query = QueryBuilder::select(["*"].to_vec()).unwrap()
.table("users")
.where_("pic", "=", ValueType::String("".to_string()))
.not_json_contains("pic", object, Some(".name"))
.finish();
assert_eq!(query, "SELECT * FROM users WHERE NOT JSON_CONTAINS(pic, JSON_OBJECT('name', 'blablabla.jpg'), '$.name');")
}
Sourcepub fn json_array_append(
&mut self,
column: &str,
path: Option<&str>,
object: JsonValue<'_>,
) -> &mut Self
pub fn json_array_append( &mut self, column: &str, path: Option<&str>, object: JsonValue<'_>, ) -> &mut Self
it adds JSON_ARRAY_APPEND()
mysql function with it’s synthax. It’s intended to used with only update constructor, don’t use it with any other kind of query.
use qubl::{QueryBuilder, ValueType, JsonValue};
fn main () {
let lesson = ("lesson", &ValueType::String("math".to_string()));
let point = ("point", &ValueType::Int32(100));
let values = vec![lesson, point];
let object = JsonValue::MysqlJsonObject(&values);
let query = QueryBuilder::update().unwrap()
.table("users")
.json_array_append("points", Some(""), object.clone())
.where_("id", "=", ValueType::Int8(1))
.finish();
assert_eq!("UPDATE users SET points = JSON_ARRAY_APPEND(points, '$', JSON_OBJECT('lesson', 'math', 'point', 100)) WHERE id = 1;", query);
}
Sourcepub fn json_remove(&mut self, column: &str, paths: Vec<&str>) -> &mut Self
pub fn json_remove(&mut self, column: &str, paths: Vec<&str>) -> &mut Self
it adds “JSON_REMOVE()” function with it’s synthax. You cannot pass empty strings to paths.
use qubl::{QueryBuilder, ValueType};
fn main () {
let query = QueryBuilder::update().unwrap()
.table("blogs")
.json_remove("likes", vec!["[10]"])
.where_("blog_id", "=", ValueType::Int32(20))
.finish();
assert_eq!(query, "UPDATE blogs SET likes = JSON_REMOVE(likes, '$[10]') WHERE blog_id = 20;");
}
Sourcepub fn json_set(
&mut self,
column: &str,
path: &str,
value: JsonValue<'_>,
) -> &mut Self
pub fn json_set( &mut self, column: &str, path: &str, value: JsonValue<'_>, ) -> &mut Self
It adds JSON_SET()
function with it’s synthax. It updates values with the specified path.
use qubl::{QueryBuilder, ValueType, JsonValue};
fn main () {
let lesson = ("lesson", &ValueType::String("math".to_string()));
let point = ("point", &ValueType::Int32(100));
let values = vec![lesson, point];
let object = JsonValue::MysqlJsonObject(&values);
let query = QueryBuilder::update().unwrap()
.table("users")
.json_set("points", "[0]", object)
.where_("id", "=", ValueType::Int32(1))
.finish();
assert_eq!("UPDATE users SET points = JSON_SET(points, '$[0]', JSON_OBJECT('lesson', 'math', 'point', 100)) WHERE id = 1;", query);
}
Sourcepub fn json_replace(
&mut self,
column: &str,
path: &str,
value: JsonValue<'_>,
) -> &mut Self
pub fn json_replace( &mut self, column: &str, path: &str, value: JsonValue<'_>, ) -> &mut Self
It adds JSON_REPLACE()
function with it’s synthax. It updates values with the specified path.
use qubl::{QueryBuilder, ValueType, JsonValue};
fn main () {
let value = ValueType::Int32(100);
let value = JsonValue::Initial(&value);
let query = QueryBuilder::update().unwrap()
.table("users")
.json_replace("points", "[0].point", value)
.where_("id", "=", ValueType::Int32(1))
.finish();
assert_eq!("UPDATE users SET points = JSON_REPLACE(points, '$[0].point', 100) WHERE id = 1;", query)
}
Trait Implementations§
Source§impl<'a> Clone for QueryBuilder<'a>
impl<'a> Clone for QueryBuilder<'a>
Source§fn clone(&self) -> QueryBuilder<'a>
fn clone(&self) -> QueryBuilder<'a>
1.0.0 · Source§const fn clone_from(&mut self, source: &Self)
const fn clone_from(&mut self, source: &Self)
source
. Read more