pub struct SqlBuilder { /* private fields */ }Expand description
Main SQL builder
Implementations§
Source§impl SqlBuilder
impl SqlBuilder
Sourcepub fn select_from<S: ToString>(table: S) -> Self
pub fn select_from<S: ToString>(table: S) -> Self
Create SELECT query. You may specify comma separted list of tables.
use sql_builder::SqlBuilder;
let sql = SqlBuilder::select_from("books")
.field("title")
.field("price")
.and_where("price > 100")
.and_where_like_left("title", "Harry Potter")
.sql()?;
assert_eq!("SELECT title, price FROM books WHERE (price > 100) AND (title LIKE 'Harry Potter%');", &sql);
// add ^^^^^
// here tableSourcepub fn and_table<S: ToString>(&mut self, table: S) -> &mut Self
pub fn and_table<S: ToString>(&mut self, table: S) -> &mut Self
SELECT from additional table. Adds table name to comma separted list of tables.
use sql_builder::SqlBuilder;
let sql = SqlBuilder::select_from("books")
.and_table("newspapers")
.field("title")
.field("price")
.and_where("price > 100")
.sql()?;
assert_eq!("SELECT title, price FROM books, newspapers WHERE price > 100;", &sql);
// add ^^^^^^^^^^
// here tableSourcepub fn select_values<S: ToString>(values: &[S]) -> Self
pub fn select_values<S: ToString>(values: &[S]) -> Self
Create SELECT query without a table.
use sql_builder::{SqlBuilder, quote};
let sql = SqlBuilder::select_values(&["10", "e("100")])
.sql()?;
assert_eq!("SELECT 10, '100';", &sql);
// add ^^^^^^^^^
// here valuesSourcepub fn insert_into<S: ToString>(table: S) -> Self
pub fn insert_into<S: ToString>(table: S) -> Self
Create INSERT query.
use sql_builder::{SqlBuilder, quote};
let sql = SqlBuilder::insert_into("books")
.field("title")
.field("price")
.values(&[quote("In Search of Lost Time"), 150.to_string()])
.values(&["'Don Quixote', 200"])
.sql()?;
assert_eq!("INSERT INTO books (title, price) VALUES ('In Search of Lost Time', 150), ('Don Quixote', 200);", &sql);
// add ^^^^^
// here tableSourcepub fn update_table<S: ToString>(table: S) -> Self
pub fn update_table<S: ToString>(table: S) -> Self
Create UPDATE query.
use sql_builder::SqlBuilder;
let sql = SqlBuilder::update_table("books")
.set("price", "price + 10")
.sql()?;
assert_eq!("UPDATE books SET price = price + 10;", &sql);
// add ^^^^^
// here tableSourcepub fn delete_from<S: ToString>(table: S) -> Self
pub fn delete_from<S: ToString>(table: S) -> Self
Create DELETE query.
use sql_builder::SqlBuilder;
let sql = SqlBuilder::delete_from("books")
.and_where("price > 100")
.sql()?;
assert_eq!("DELETE FROM books WHERE price > 100;", &sql);
// add ^^^^^
// here tableSourcepub fn natural(&mut self) -> &mut Self
pub fn natural(&mut self) -> &mut Self
Use NATURAL JOIN
use sql_builder::SqlBuilder;
let sql = SqlBuilder::select_from("books")
.field("title")
.field("total")
.natural()
.join("orders")
.sql()?;
assert_eq!("SELECT title, total FROM books NATURAL JOIN orders;", &sql);
// add here ^^^^^^^Sourcepub fn left(&mut self) -> &mut Self
pub fn left(&mut self) -> &mut Self
Use LEFT JOIN
use sql_builder::SqlBuilder;
let sql = SqlBuilder::select_from("books")
.field("title")
.field("total")
.natural()
.left()
.join("orders")
.sql()?;
assert_eq!("SELECT title, total FROM books NATURAL LEFT JOIN orders;", &sql);
// add here ^^^^Sourcepub fn left_outer(&mut self) -> &mut Self
pub fn left_outer(&mut self) -> &mut Self
Use LEFT OUTER JOIN
use sql_builder::SqlBuilder;
let sql = SqlBuilder::select_from("books")
.field("title")
.field("total")
.natural()
.left_outer()
.join("orders")
.sql()?;
assert_eq!("SELECT title, total FROM books NATURAL LEFT OUTER JOIN orders;", &sql);
// add here ^^^^^^^^^^Sourcepub fn right(&mut self) -> &mut Self
pub fn right(&mut self) -> &mut Self
Use RIGHT JOIN
use sql_builder::SqlBuilder;
let sql = SqlBuilder::select_from("books")
.field("title")
.field("total")
.natural()
.right()
.join("orders")
.sql()?;
assert_eq!("SELECT title, total FROM books NATURAL RIGHT JOIN orders;", &sql);
// add here ^^^^^Sourcepub fn right_outer(&mut self) -> &mut Self
pub fn right_outer(&mut self) -> &mut Self
Use RIGHT OUTER JOIN
use sql_builder::SqlBuilder;
let sql = SqlBuilder::select_from("books")
.field("title")
.field("total")
.natural()
.right_outer()
.join("orders")
.sql()?;
assert_eq!("SELECT title, total FROM books NATURAL RIGHT OUTER JOIN orders;", &sql);
// add here ^^^^^^^^^^^Sourcepub fn inner(&mut self) -> &mut Self
pub fn inner(&mut self) -> &mut Self
Use INNER JOIN
use sql_builder::SqlBuilder;
let sql = SqlBuilder::select_from("books")
.field("title")
.field("total")
.natural()
.inner()
.join("orders")
.sql()?;
assert_eq!("SELECT title, total FROM books NATURAL INNER JOIN orders;", &sql);
// add here ^^^^^Sourcepub fn cross(&mut self) -> &mut Self
pub fn cross(&mut self) -> &mut Self
Use CROSS JOIN
use sql_builder::SqlBuilder;
let sql = SqlBuilder::select_from("books")
.field("title")
.field("total")
.natural()
.cross()
.join("orders")
.sql()?;
assert_eq!("SELECT title, total FROM books NATURAL CROSS JOIN orders;", &sql);
// add here ^^^^^Sourcepub fn join<S: ToString>(&mut self, table: S) -> &mut Self
pub fn join<S: ToString>(&mut self, table: S) -> &mut Self
Join with table.
#[macro_use] extern crate sql_builder;
use sql_builder::{SqlBuilder, SqlName};
let sql = SqlBuilder::select_from(name!("books"; "b"))
.field("b.title")
.field("s.total")
.left()
.join(name!("shops"; "s"))
.on("b.id = s.book")
.sql()?;
assert_eq!("SELECT b.title, s.total FROM books AS b LEFT JOIN shops AS s ON b.id = s.book;", &sql);
// add ^^^^^^^^^^
// here tableSourcepub fn on<S: ToString>(&mut self, constraint: S) -> &mut Self
pub fn on<S: ToString>(&mut self, constraint: S) -> &mut Self
Join constraint to the last JOIN part.
use sql_builder::SqlBuilder;
let sql = SqlBuilder::select_from("books AS b")
.field("b.title")
.field("s.total")
.join("shops AS s")
.on("b.id = s.book")
.sql()?;
assert_eq!("SELECT b.title, s.total FROM books AS b JOIN shops AS s ON b.id = s.book;", &sql);
// add ^^^^^^^^^^^^^
// here constraintSourcepub fn on_eq<S: ToString, T: ToString>(&mut self, c1: S, c2: T) -> &mut Self
pub fn on_eq<S: ToString, T: ToString>(&mut self, c1: S, c2: T) -> &mut Self
Join constraint to the last JOIN part.
use sql_builder::SqlBuilder;
let sql = SqlBuilder::select_from("books AS b")
.field("b.title")
.field("s.total")
.join("shops AS s")
.on_eq("b.id", "s.book")
.sql()?;
assert_eq!("SELECT b.title, s.total FROM books AS b JOIN shops AS s ON b.id = s.book;", &sql);
// add ^^^^ ^^^^^^
// here c1 c2Sourcepub fn distinct(&mut self) -> &mut Self
pub fn distinct(&mut self) -> &mut Self
Set DISTINCT for fields.
use sql_builder::SqlBuilder;
let sql = SqlBuilder::select_from("books")
.distinct()
.field("price")
.sql()?;
assert_eq!("SELECT DISTINCT price FROM books;", &sql);
// add here ^^^^^^^^Sourcepub fn fields<S: ToString>(&mut self, fields: &[S]) -> &mut Self
pub fn fields<S: ToString>(&mut self, fields: &[S]) -> &mut Self
Add fields.
use sql_builder::SqlBuilder;
let sql = SqlBuilder::select_from("books")
.fields(&["title", "price"])
.sql()?;
assert_eq!("SELECT title, price FROM books;", &sql);
// add ^^^^^^^^^^^^
// here fieldsSourcepub fn set_fields<S: ToString>(&mut self, fields: &[S]) -> &mut Self
pub fn set_fields<S: ToString>(&mut self, fields: &[S]) -> &mut Self
Replace fields.
use sql_builder::SqlBuilder;
// Prepare query for total count
let mut db = SqlBuilder::select_from("books");
db.field("COUNT(id)");
if let Some(filter) = &req_data.filter {
db.and_where_like_any("LOWER(title)", filter.to_lowercase());
}
if let Some(price_min) = &req_data.price_min {
db.and_where_ge("price", price_min);
}
if let Some(price_max) = &req_data.price_max {
db.and_where_le("price", price_max);
}
let sql_count = db.sql()?;
println!("Database query: total_count: {}", &sql_count);
// Prepare query for results
db.set_fields(&["id", "title", "price"]);
if let (Some(limit), Some(offset)) = (req_data.limit, req_data.offset) {
db.limit(limit).offset(offset);
}
let sql_results = db.sql()?;
println!("Database query: results: {}", &sql_results);Sourcepub fn field<S: ToString>(&mut self, field: S) -> &mut Self
pub fn field<S: ToString>(&mut self, field: S) -> &mut Self
Add field.
use sql_builder::SqlBuilder;
let sql = SqlBuilder::select_from("books")
.field("title")
.field("price")
.sql()?;
assert_eq!("SELECT title, price FROM books;", &sql);
// add ^^^^^ ^^^^^
// here field fieldSourcepub fn set_field<S: ToString>(&mut self, field: S) -> &mut Self
pub fn set_field<S: ToString>(&mut self, field: S) -> &mut Self
Replace fields with choosed one.
use sql_builder::SqlBuilder;
// Prepare query for total count
let mut db = SqlBuilder::select_from("books");
db.field("COUNT(id)");
if let Some(filter) = &req_data.filter {
db.and_where_like_any("LOWER(title)", filter.to_lowercase());
}
if let Some(price_min) = &req_data.price_min {
db.and_where_ge("price", price_min);
}
if let Some(price_max) = &req_data.price_max {
db.and_where_le("price", price_max);
}
let sql_count = db.sql()?;
println!("Database query: total_count: {}", &sql_count);
// Prepare query for results
db.set_field("id");
db.field("title");
db.field("price");
if let (Some(limit), Some(offset)) = (req_data.limit, req_data.offset) {
db.limit(limit).offset(offset);
}
let sql_results = db.sql()?;
println!("Database query: results: {}", &sql_results);Sourcepub fn count<S: ToString>(&mut self, field: S) -> &mut Self
pub fn count<S: ToString>(&mut self, field: S) -> &mut Self
Add COUNT(field).
use sql_builder::SqlBuilder;
let sql = SqlBuilder::select_from("books")
.field("title")
.count("price")
.group_by("title")
.sql()?;
assert_eq!("SELECT title, COUNT(price) FROM books GROUP BY title;", &sql);
// add ^^^^^
// here fieldSourcepub fn count_as<S, T>(&mut self, field: S, name: T) -> &mut Self
pub fn count_as<S, T>(&mut self, field: S, name: T) -> &mut Self
Add COUNT(field) AS name.
use sql_builder::SqlBuilder;
let sql = SqlBuilder::select_from("books")
.field("title")
.count_as("price", "cnt")
.group_by("title")
.sql()?;
assert_eq!("SELECT title, COUNT(price) AS cnt FROM books GROUP BY title;", &sql);
// add ^^^^^
// here fieldSourcepub fn set<S, T>(&mut self, field: S, value: T) -> &mut Self
pub fn set<S, T>(&mut self, field: S, value: T) -> &mut Self
Add SET part (for UPDATE).
use sql_builder::SqlBuilder;
let sql = SqlBuilder::update_table("books")
.set("price", "price + 10")
.sql()?;
assert_eq!("UPDATE books SET price = price + 10;", &sql);
// add ^^^^^ ^^^^^^^^^^
// here field valueSourcepub fn set_str<S, T>(&mut self, field: S, value: T) -> &mut Self
pub fn set_str<S, T>(&mut self, field: S, value: T) -> &mut Self
Add SET part with escaped string value (for UPDATE).
use sql_builder::SqlBuilder;
let sql = SqlBuilder::update_table("books")
.set_str("comment", "Don't distribute!")
.and_where_le("price", "100")
.sql()?;
assert_eq!("UPDATE books SET comment = 'Don''t distribute!' WHERE price <= 100;", &sql);
// add ^^^^^^^ ^^^^^^^^^^^^^^^^^^
// here field valueSourcepub fn values<S: ToString>(&mut self, values: &[S]) -> &mut Self
pub fn values<S: ToString>(&mut self, values: &[S]) -> &mut Self
Add VALUES part (for INSERT).
use sql_builder::{SqlBuilder, quote};
let sql = SqlBuilder::insert_into("books")
.field("title")
.field("price")
.values(&[quote("In Search of Lost Time"), 150.to_string()])
.values(&["'Don Quixote', 200"])
.sql()?;
assert_eq!("INSERT INTO books (title, price) VALUES ('In Search of Lost Time', 150), ('Don Quixote', 200);", &sql);
// add ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^
// here values valuesSourcepub fn select<S: ToString>(&mut self, query: S) -> &mut Self
pub fn select<S: ToString>(&mut self, query: S) -> &mut Self
Add SELECT part (for INSERT).
use sql_builder::SqlBuilder;
let query = SqlBuilder::select_from("warehouse")
.field("title")
.field("preliminary_price * 2")
.query()?;
assert_eq!("SELECT title, preliminary_price * 2 FROM warehouse", &query);
let sql = SqlBuilder::insert_into("books")
.field("title")
.field("price")
.select(&query)
.sql()?;
assert_eq!("INSERT INTO books (title, price) SELECT title, preliminary_price * 2 FROM warehouse;", &sql);
// add ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// here querySourcepub fn returning<S: ToString>(&mut self, field: S) -> &mut Self
pub fn returning<S: ToString>(&mut self, field: S) -> &mut Self
Add RETURNING part.
use sql_builder::SqlBuilder;
let sql = SqlBuilder::insert_into("books")
.field("title")
.field("price")
.values(&["'Don Quixote', 200"])
.returning("id")
.sql()?;
assert_eq!("INSERT INTO books (title, price) VALUES ('Don Quixote', 200) RETURNING id;", &sql);
// add ^^
// here fieldSourcepub fn returning_id(&mut self) -> &mut Self
pub fn returning_id(&mut self) -> &mut Self
Add RETURNING id.
use sql_builder::SqlBuilder;
let sql = SqlBuilder::insert_into("books")
.field("title")
.field("price")
.values(&["'Don Quixote', 200"])
.returning_id()
.sql()?;
assert_eq!("INSERT INTO books (title, price) VALUES ('Don Quixote', 200) RETURNING id;", &sql);
// add here ^^^^^^^^^^^^Sourcepub fn group_by<S: ToString>(&mut self, field: S) -> &mut Self
pub fn group_by<S: ToString>(&mut self, field: S) -> &mut Self
Add GROUP BY part.
use sql_builder::SqlBuilder;
let sql = SqlBuilder::select_from("books")
.field("price")
.field("COUNT(price) AS cnt")
.group_by("price")
.order_desc("cnt")
.sql()?;
assert_eq!("SELECT price, COUNT(price) AS cnt FROM books GROUP BY price ORDER BY cnt DESC;", &sql);
// add ^^^^^
// here fieldSourcepub fn having<S: ToString>(&mut self, cond: S) -> &mut Self
pub fn having<S: ToString>(&mut self, cond: S) -> &mut Self
Add HAVING condition.
use sql_builder::SqlBuilder;
let sql = SqlBuilder::select_from("books")
.field("price")
.field("COUNT(price) AS cnt")
.group_by("price")
.having("price > 100")
.order_desc("cnt")
.sql()?;
assert_eq!("SELECT price, COUNT(price) AS cnt FROM books GROUP BY price HAVING price > 100 ORDER BY cnt DESC;", &sql);
// add ^^^^^^^^^^^
// here condSourcepub fn and_where<S: ToString>(&mut self, cond: S) -> &mut Self
pub fn and_where<S: ToString>(&mut self, cond: S) -> &mut Self
Add WHERE condition.
use sql_builder::SqlBuilder;
let sql = SqlBuilder::select_from("books")
.field("title")
.field("price")
.and_where("price > 100")
.and_where("title LIKE 'Harry Potter%'")
.sql()?;
assert_eq!("SELECT title, price FROM books WHERE (price > 100) AND (title LIKE 'Harry Potter%');", &sql);
// add ^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^
// here cond condSourcepub fn and_where_eq<S, T>(&mut self, field: S, value: T) -> &mut Self
pub fn and_where_eq<S, T>(&mut self, field: S, value: T) -> &mut Self
Add WHERE condition for equal parts.
use sql_builder::{SqlBuilder, quote};
let sql = SqlBuilder::select_from("books")
.field("price")
.and_where_eq("title", "e("Harry Potter and the Philosopher's Stone"))
.sql()?;
assert_eq!("SELECT price FROM books WHERE title = 'Harry Potter and the Philosopher''s Stone';", &sql);
// add ^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// here field valueSourcepub fn and_where_ne<S, T>(&mut self, field: S, value: T) -> &mut Self
pub fn and_where_ne<S, T>(&mut self, field: S, value: T) -> &mut Self
Add WHERE condition for non-equal parts.
use sql_builder::{SqlBuilder, quote};
let sql = SqlBuilder::select_from("books")
.field("price")
.and_where_ne("title", "e("Harry Potter and the Philosopher's Stone"))
.sql()?;
assert_eq!("SELECT price FROM books WHERE title <> 'Harry Potter and the Philosopher''s Stone';", &sql);
// add ^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// here field valueSourcepub fn and_where_gt<S, T>(&mut self, field: S, value: T) -> &mut Self
pub fn and_where_gt<S, T>(&mut self, field: S, value: T) -> &mut Self
Add WHERE condition for field greater than value.
use sql_builder::SqlBuilder;
let sql = SqlBuilder::select_from("books")
.field("title")
.field("price")
.and_where_gt("price", 300)
.sql()?;
assert_eq!("SELECT title, price FROM books WHERE price > 300;", &sql);
// add ^^^^^ ^^^
// here field valueSourcepub fn and_where_ge<S, T>(&mut self, field: S, value: T) -> &mut Self
pub fn and_where_ge<S, T>(&mut self, field: S, value: T) -> &mut Self
Add WHERE condition for field not less than value.
use sql_builder::SqlBuilder;
let sql = SqlBuilder::select_from("books")
.field("title")
.field("price")
.and_where_ge("price", 300)
.sql()?;
assert_eq!("SELECT title, price FROM books WHERE price >= 300;", &sql);
// add ^^^^^ ^^^
// here field valueSourcepub fn and_where_lt<S, T>(&mut self, field: S, value: T) -> &mut Self
pub fn and_where_lt<S, T>(&mut self, field: S, value: T) -> &mut Self
Add WHERE condition for field less than value.
use sql_builder::SqlBuilder;
let sql = SqlBuilder::select_from("books")
.field("title")
.field("price")
.and_where_lt("price", 300)
.sql()?;
assert_eq!("SELECT title, price FROM books WHERE price < 300;", &sql);
// add ^^^^^ ^^^
// here field valueSourcepub fn and_where_le<S, T>(&mut self, field: S, value: T) -> &mut Self
pub fn and_where_le<S, T>(&mut self, field: S, value: T) -> &mut Self
Add WHERE condition for field not greater than value.
use sql_builder::SqlBuilder;
let sql = SqlBuilder::select_from("books")
.field("title")
.field("price")
.and_where_le("price", 300)
.sql()?;
assert_eq!("SELECT title, price FROM books WHERE price <= 300;", &sql);
// add ^^^^^ ^^^
// here field valueSourcepub fn and_where_like<S, T>(&mut self, field: S, mask: T) -> &mut Self
pub fn and_where_like<S, T>(&mut self, field: S, mask: T) -> &mut Self
Add WHERE LIKE condition.
use sql_builder::SqlBuilder;
let sql = SqlBuilder::select_from("books")
.field("price")
.and_where_like("title", "%Philosopher's%")
.sql()?;
assert_eq!("SELECT price FROM books WHERE title LIKE '%Philosopher''s%';", &sql);
// add ^^^^^ ^^^^^^^^^^^^^^^^
// here field maskSourcepub fn and_where_like_right<S, T>(&mut self, field: S, mask: T) -> &mut Self
pub fn and_where_like_right<S, T>(&mut self, field: S, mask: T) -> &mut Self
Add WHERE LIKE %condition.
use sql_builder::SqlBuilder;
let sql = SqlBuilder::select_from("books")
.field("price")
.and_where_like_right("title", "Stone")
.sql()?;
assert_eq!("SELECT price FROM books WHERE title LIKE '%Stone';", &sql);
// add ^^^^^ ^^^^^
// here field maskSourcepub fn and_where_like_left<S, T>(&mut self, field: S, mask: T) -> &mut Self
pub fn and_where_like_left<S, T>(&mut self, field: S, mask: T) -> &mut Self
Add WHERE LIKE condition%.
use sql_builder::SqlBuilder;
let sql = SqlBuilder::select_from("books")
.field("price")
.and_where_like_left("title", "Harry")
.sql()?;
assert_eq!("SELECT price FROM books WHERE title LIKE 'Harry%';", &sql);
// add ^^^^^ ^^^^^
// here field maskSourcepub fn and_where_like_any<S, T>(&mut self, field: S, mask: T) -> &mut Self
pub fn and_where_like_any<S, T>(&mut self, field: S, mask: T) -> &mut Self
Add WHERE LIKE %condition%.
use sql_builder::SqlBuilder;
let sql = SqlBuilder::select_from("books")
.field("price")
.and_where_like_any("title", " and ")
.sql()?;
assert_eq!("SELECT price FROM books WHERE title LIKE '% and %';", &sql);
// add ^^^^^ ^^^^^
// here field maskSourcepub fn and_where_not_like<S, T>(&mut self, field: S, mask: T) -> &mut Self
pub fn and_where_not_like<S, T>(&mut self, field: S, mask: T) -> &mut Self
Add WHERE NOT LIKE condition.
use sql_builder::SqlBuilder;
let sql = SqlBuilder::select_from("books")
.field("title")
.and_where_not_like("title", "%Alice's%")
.sql()?;
assert_eq!("SELECT title FROM books WHERE title NOT LIKE '%Alice''s%';", &sql);
// add ^^^^^ ^^^^^^^^^^
// here field maskSourcepub fn and_where_not_like_right<S, T>(&mut self, field: S, mask: T) -> &mut Self
pub fn and_where_not_like_right<S, T>(&mut self, field: S, mask: T) -> &mut Self
Add WHERE NOT LIKE %condition.
use sql_builder::SqlBuilder;
let sql = SqlBuilder::select_from("books")
.field("price")
.and_where_not_like_right("title", "Stone")
.sql()?;
assert_eq!("SELECT price FROM books WHERE title NOT LIKE '%Stone';", &sql);
// add ^^^^^ ^^^^^
// here field maskSourcepub fn and_where_not_like_left<S, T>(&mut self, field: S, mask: T) -> &mut Self
pub fn and_where_not_like_left<S, T>(&mut self, field: S, mask: T) -> &mut Self
Add WHERE NOT LIKE condition%.
use sql_builder::SqlBuilder;
let sql = SqlBuilder::select_from("books")
.field("price")
.and_where_not_like_left("title", "Harry")
.sql()?;
assert_eq!("SELECT price FROM books WHERE title NOT LIKE 'Harry%';", &sql);
// add ^^^^^ ^^^^^
// here field maskSourcepub fn and_where_not_like_any<S, T>(&mut self, field: S, mask: T) -> &mut Self
pub fn and_where_not_like_any<S, T>(&mut self, field: S, mask: T) -> &mut Self
Add WHERE NOT LIKE %condition%.
use sql_builder::SqlBuilder;
let sql = SqlBuilder::select_from("books")
.field("price")
.and_where_not_like_any("title", " and ")
.sql()?;
assert_eq!("SELECT price FROM books WHERE title NOT LIKE '% and %';", &sql);
// add ^^^^^ ^^^^^
// here field maskSourcepub fn and_where_is_null<S: ToString>(&mut self, field: S) -> &mut Self
pub fn and_where_is_null<S: ToString>(&mut self, field: S) -> &mut Self
Add WHERE IS NULL condition.
use sql_builder::SqlBuilder;
let sql = SqlBuilder::select_from("books")
.field("title")
.and_where_is_null("price")
.sql()?;
assert_eq!("SELECT title FROM books WHERE price IS NULL;", &sql);
// add ^^^^^
// here fieldSourcepub fn and_where_is_not_null<S: ToString>(&mut self, field: S) -> &mut Self
pub fn and_where_is_not_null<S: ToString>(&mut self, field: S) -> &mut Self
Add WHERE IS NOT NULL condition.
use sql_builder::SqlBuilder;
let sql = SqlBuilder::select_from("books")
.field("title")
.and_where_is_not_null("price")
.sql()?;
assert_eq!("SELECT title FROM books WHERE price IS NOT NULL;", &sql);
// add ^^^^^
// here fieldSourcepub fn and_where_in<S, T>(&mut self, field: S, list: &[T]) -> &mut Self
pub fn and_where_in<S, T>(&mut self, field: S, list: &[T]) -> &mut Self
Add WHERE field IN (list).
use sql_builder::{SqlBuilder, quote};
let sql = SqlBuilder::select_from("books")
.field("title")
.field("price")
.and_where_in("title", &[quote("G"), quote("L"), quote("t")])
.sql()?;
assert_eq!("SELECT title, price FROM books WHERE title IN ('G', 'L', 't');", &sql);
// add ^^^^^ ^^^^^^^^^^^^^
// here field listSourcepub fn and_where_in_quoted<S, T>(&mut self, field: S, list: &[T]) -> &mut Self
pub fn and_where_in_quoted<S, T>(&mut self, field: S, list: &[T]) -> &mut Self
Add WHERE field IN (string list).
use sql_builder::{SqlBuilder, quote};
let sql = SqlBuilder::select_from("books")
.field("title")
.field("price")
.and_where_in_quoted("title", &["G", "L", "t"])
.sql()?;
assert_eq!("SELECT title, price FROM books WHERE title IN ('G', 'L', 't');", &sql);
// add ^^^^^ ^^^^^^^^^^^^^
// here field listSourcepub fn and_where_not_in<S, T>(&mut self, field: S, list: &[T]) -> &mut Self
pub fn and_where_not_in<S, T>(&mut self, field: S, list: &[T]) -> &mut Self
Add WHERE field NOT IN (list).
use sql_builder::{SqlBuilder, quote};
let sql = SqlBuilder::select_from("books")
.field("title")
.field("price")
.and_where_not_in("title", &[quote("G"), quote("L"), quote("t")])
.sql()?;
assert_eq!("SELECT title, price FROM books WHERE title NOT IN ('G', 'L', 't');", &sql);
// add ^^^^^ ^^^^^^^^^^^^^
// here field listSourcepub fn and_where_not_in_quoted<S, T>(
&mut self,
field: S,
list: &[T],
) -> &mut Self
pub fn and_where_not_in_quoted<S, T>( &mut self, field: S, list: &[T], ) -> &mut Self
Add WHERE field NOT IN (string list).
use sql_builder::{SqlBuilder, quote};
let sql = SqlBuilder::select_from("books")
.field("title")
.field("price")
.and_where_not_in_quoted("title", &["G", "L", "t"])
.sql()?;
assert_eq!("SELECT title, price FROM books WHERE title NOT IN ('G', 'L', 't');", &sql);
// add ^^^^^ ^^^^^^^^^^^^^
// here field listSourcepub fn and_where_in_query<S, T>(&mut self, field: S, query: T) -> &mut Self
pub fn and_where_in_query<S, T>(&mut self, field: S, query: T) -> &mut Self
Add WHERE field IN (query).
use sql_builder::{SqlBuilder, quote};
let query = SqlBuilder::select_from("shop")
.field("title")
.and_where("sold")
.query()?;
assert_eq!("SELECT title FROM shop WHERE sold", &query);
let sql = SqlBuilder::select_from("books")
.field("title")
.field("price")
.and_where_in_query("title", &query)
.sql()?;
assert_eq!("SELECT title, price FROM books WHERE title IN (SELECT title FROM shop WHERE sold);", &sql);
// add ^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// here field querySourcepub fn and_where_not_in_query<S, T>(&mut self, field: S, query: T) -> &mut Self
pub fn and_where_not_in_query<S, T>(&mut self, field: S, query: T) -> &mut Self
Add WHERE field NOT IN (query).
use sql_builder::{SqlBuilder, quote};
let query = SqlBuilder::select_from("shop")
.field("title")
.and_where("sold")
.query()?;
assert_eq!("SELECT title FROM shop WHERE sold", &query);
let sql = SqlBuilder::select_from("books")
.field("title")
.field("price")
.and_where_not_in_query("title", &query)
.sql()?;
assert_eq!("SELECT title, price FROM books WHERE title NOT IN (SELECT title FROM shop WHERE sold);", &sql);
// add ^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// here field querySourcepub fn and_where_between<S, T, U>(
&mut self,
field: S,
min: T,
max: U,
) -> &mut Self
pub fn and_where_between<S, T, U>( &mut self, field: S, min: T, max: U, ) -> &mut Self
Add WHERE field BETWEEN values.
use sql_builder::SqlBuilder;
let sql = SqlBuilder::select_from("books")
.field("title")
.field("price")
.and_where_between("price", 10_000, 20_000)
.sql()?;
assert_eq!("SELECT title, price FROM books WHERE price BETWEEN 10000 AND 20000;", &sql);
// add ^^^^^ ^^^^^ ^^^^^
// here field min maxSourcepub fn and_where_not_between<S, T, U>(
&mut self,
field: S,
min: T,
max: U,
) -> &mut Self
pub fn and_where_not_between<S, T, U>( &mut self, field: S, min: T, max: U, ) -> &mut Self
Add WHERE field NOT BETWEEN values.
use sql_builder::SqlBuilder;
let sql = SqlBuilder::select_from("books")
.field("title")
.field("price")
.and_where_not_between("price", 10_000, 20_000)
.sql()?;
assert_eq!("SELECT title, price FROM books WHERE price NOT BETWEEN 10000 AND 20000;", &sql);
// add ^^^^^ ^^^^^ ^^^^^
// here field min maxSourcepub fn or_where<S: ToString>(&mut self, cond: S) -> &mut Self
pub fn or_where<S: ToString>(&mut self, cond: S) -> &mut Self
Add OR condition to the last WHERE condition.
use sql_builder::SqlBuilder;
let sql = SqlBuilder::select_from("books")
.field("title")
.field("price")
.and_where("price < 10")
.or_where("price > 1000")
.sql()?;
assert_eq!("SELECT title, price FROM books WHERE price < 10 OR price > 1000;", &sql);
// add ^^^^^^^^^^^^
// here condSourcepub fn or_where_eq<S, T>(&mut self, field: S, value: T) -> &mut Self
pub fn or_where_eq<S, T>(&mut self, field: S, value: T) -> &mut Self
Add OR condition of equal parts to the last WHERE condition.
use sql_builder::{SqlBuilder, quote};
let sql = SqlBuilder::select_from("books")
.field("price")
.and_where_eq("title", "e("Harry Potter and the Philosopher's Stone"))
.or_where_eq("title", "e("Harry Potter and the Chamber of Secrets"))
.sql()?;
assert_eq!("SELECT price FROM books WHERE title = 'Harry Potter and the Philosopher''s Stone' OR title = 'Harry Potter and the Chamber of Secrets';", &sql);
// add ^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// here field valueSourcepub fn or_where_ne<S, T>(&mut self, field: S, value: T) -> &mut Self
pub fn or_where_ne<S, T>(&mut self, field: S, value: T) -> &mut Self
Add OR condition of non-equal parts to the last WHERE condition.
use sql_builder::{SqlBuilder, quote};
let sql = SqlBuilder::select_from("books")
.field("price")
.or_where_ne("title", "e("Harry Potter and the Philosopher's Stone"))
.or_where_ne("title", "e("Harry Potter and the Chamber of Secrets"))
.sql()?;
assert_eq!("SELECT price FROM books WHERE title <> 'Harry Potter and the Philosopher''s Stone' OR title <> 'Harry Potter and the Chamber of Secrets';", &sql);
// add ^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// here field value field valueSourcepub fn or_where_gt<S, T>(&mut self, field: S, value: T) -> &mut Self
pub fn or_where_gt<S, T>(&mut self, field: S, value: T) -> &mut Self
Add OR condition for field greater than value to the last WHERE condition.
use sql_builder::SqlBuilder;
let sql = SqlBuilder::select_from("books")
.field("title")
.field("price")
.and_where_lt("price", 100)
.or_where_gt("price", 300)
.sql()?;
assert_eq!("SELECT title, price FROM books WHERE price < 100 OR price > 300;", &sql);
// add ^^^^^ ^^^
// here field valueSourcepub fn or_where_ge<S, T>(&mut self, field: S, value: T) -> &mut Self
pub fn or_where_ge<S, T>(&mut self, field: S, value: T) -> &mut Self
Add OR condition for field not less than value to the last WHERE condition.
use sql_builder::SqlBuilder;
let sql = SqlBuilder::select_from("books")
.field("title")
.field("price")
.or_where_lt("price", 100)
.or_where_ge("price", 300)
.sql()?;
assert_eq!("SELECT title, price FROM books WHERE price < 100 OR price >= 300;", &sql);
// add ^^^^^ ^^^
// here field valueSourcepub fn or_where_lt<S, T>(&mut self, field: S, value: T) -> &mut Self
pub fn or_where_lt<S, T>(&mut self, field: S, value: T) -> &mut Self
Add OR condition for field less than value to the last WHERE condition.
use sql_builder::SqlBuilder;
let sql = SqlBuilder::select_from("books")
.field("title")
.field("price")
.and_where_lt("price", 100)
.or_where_lt("price", 300)
.sql()?;
assert_eq!("SELECT title, price FROM books WHERE price < 100 OR price < 300;", &sql);
// add ^^^^^ ^^^
// here field valueSourcepub fn or_where_le<S, T>(&mut self, field: S, value: T) -> &mut Self
pub fn or_where_le<S, T>(&mut self, field: S, value: T) -> &mut Self
Add OR condition for field not greater than value to the last WHERE condition.
use sql_builder::SqlBuilder;
let sql = SqlBuilder::select_from("books")
.field("title")
.field("price")
.or_where_le("price", 100)
.or_where_ge("price", 300)
.sql()?;
assert_eq!("SELECT title, price FROM books WHERE price <= 100 OR price >= 300;", &sql);
// add ^^^^^ ^^^
// here field valueSourcepub fn or_where_like<S, T>(&mut self, field: S, mask: T) -> &mut Self
pub fn or_where_like<S, T>(&mut self, field: S, mask: T) -> &mut Self
Add OR LIKE condition to the last WHERE condition.
use sql_builder::SqlBuilder;
let sql = SqlBuilder::select_from("books")
.field("price")
.or_where_like("title", "%Alice's%")
.or_where_like("title", "%Philosopher's%")
.sql()?;
assert_eq!("SELECT price FROM books WHERE title LIKE '%Alice''s%' OR title LIKE '%Philosopher''s%';", &sql);
// add ^^^^^ ^^^^^^^^^^^^ ^^^^^ ^^^^^^^^^^^^^^^^^^
// here field mask field maskSourcepub fn or_where_like_right<S, T>(&mut self, field: S, mask: T) -> &mut Self
pub fn or_where_like_right<S, T>(&mut self, field: S, mask: T) -> &mut Self
Add OR LIKE condition to the last WHERE %condition.
use sql_builder::SqlBuilder;
let sql = SqlBuilder::select_from("books")
.field("price")
.or_where_like_right("title", "Alice's")
.or_where_like_right("title", "Philosopher's")
.sql()?;
assert_eq!("SELECT price FROM books WHERE title LIKE '%Alice''s' OR title LIKE '%Philosopher''s';", &sql);
// add ^^^^^ ^^^^^^^^ ^^^^^ ^^^^^^^^^^^^^^
// here field mask field maskSourcepub fn or_where_like_left<S, T>(&mut self, field: S, mask: T) -> &mut Self
pub fn or_where_like_left<S, T>(&mut self, field: S, mask: T) -> &mut Self
Add OR LIKE condition to the last WHERE condition%.
use sql_builder::SqlBuilder;
let sql = SqlBuilder::select_from("books")
.field("price")
.or_where_like_left("title", "Alice's")
.or_where_like_left("title", "Philosopher's")
.sql()?;
assert_eq!("SELECT price FROM books WHERE title LIKE 'Alice''s%' OR title LIKE 'Philosopher''s%';", &sql);
// add ^^^^^ ^^^^^^^^ ^^^^^ ^^^^^^^^^^^^^^
// here field mask field maskSourcepub fn or_where_like_any<S, T>(&mut self, field: S, mask: T) -> &mut Self
pub fn or_where_like_any<S, T>(&mut self, field: S, mask: T) -> &mut Self
Add OR LIKE condition to the last WHERE %condition%.
use sql_builder::SqlBuilder;
let sql = SqlBuilder::select_from("books")
.field("price")
.or_where_like_any("title", "Alice's")
.or_where_like_any("title", "Philosopher's")
.sql()?;
assert_eq!("SELECT price FROM books WHERE title LIKE '%Alice''s%' OR title LIKE '%Philosopher''s%';", &sql);
// add ^^^^^ ^^^^^^^^^^^^ ^^^^^ ^^^^^^^^^^^^^^^^^^
// here field mask field maskSourcepub fn or_where_not_like<S, T>(&mut self, field: S, mask: T) -> &mut Self
pub fn or_where_not_like<S, T>(&mut self, field: S, mask: T) -> &mut Self
Add OR NOT LIKE condition to the last WHERE condition.
use sql_builder::SqlBuilder;
let sql = SqlBuilder::select_from("books")
.field("title")
.and_where_not_like("title", "%Alice's%")
.or_where_not_like("title", "%Philosopher's%")
.sql()?;
assert_eq!("SELECT title FROM books WHERE title NOT LIKE '%Alice''s%' OR title NOT LIKE '%Philosopher''s%';", &sql);
// add ^^^^^ ^^^^^^^^^^^^^^^^^^
// here field maskSourcepub fn or_where_not_like_right<S, T>(&mut self, field: S, mask: T) -> &mut Self
pub fn or_where_not_like_right<S, T>(&mut self, field: S, mask: T) -> &mut Self
Add OR NOT LIKE condition to the last WHERE %condition.
use sql_builder::SqlBuilder;
let sql = SqlBuilder::select_from("books")
.field("price")
.or_where_not_like_right("title", "Alice's")
.or_where_not_like_right("title", "Philosopher's")
.sql()?;
assert_eq!("SELECT price FROM books WHERE title NOT LIKE '%Alice''s' OR title NOT LIKE '%Philosopher''s';", &sql);
// add ^^^^^ ^^^^^^^^ ^^^^^ ^^^^^^^^^^^^^^
// here field mask field maskSourcepub fn or_where_not_like_left<S, T>(&mut self, field: S, mask: T) -> &mut Self
pub fn or_where_not_like_left<S, T>(&mut self, field: S, mask: T) -> &mut Self
Add OR NOT LIKE condition to the last WHERE condition%.
use sql_builder::SqlBuilder;
let sql = SqlBuilder::select_from("books")
.field("price")
.or_where_not_like_left("title", "Alice's")
.or_where_not_like_left("title", "Philosopher's")
.sql()?;
assert_eq!("SELECT price FROM books WHERE title NOT LIKE 'Alice''s%' OR title NOT LIKE 'Philosopher''s%';", &sql);
// add ^^^^^ ^^^^^^^^ ^^^^^ ^^^^^^^^^^^^^^
// here field mask field maskSourcepub fn or_where_not_like_any<S, T>(&mut self, field: S, mask: T) -> &mut Self
pub fn or_where_not_like_any<S, T>(&mut self, field: S, mask: T) -> &mut Self
Add OR NOT LIKE condition to the last WHERE %condition%.
use sql_builder::SqlBuilder;
let sql = SqlBuilder::select_from("books")
.field("price")
.or_where_not_like_any("title", "Alice's")
.or_where_not_like_any("title", "Philosopher's")
.sql()?;
assert_eq!("SELECT price FROM books WHERE title NOT LIKE '%Alice''s%' OR title NOT LIKE '%Philosopher''s%';", &sql);
// add ^^^^^ ^^^^^^^^^^^^ ^^^^^ ^^^^^^^^^^^^^^^^^^
// here field mask field maskSourcepub fn or_where_is_null<S: ToString>(&mut self, field: S) -> &mut Self
pub fn or_where_is_null<S: ToString>(&mut self, field: S) -> &mut Self
Add OR IS NULL condition to the last WHERE condition.
use sql_builder::SqlBuilder;
let sql = SqlBuilder::select_from("books")
.field("title")
.and_where_eq("price", 0)
.or_where_is_null("price")
.sql()?;
assert_eq!("SELECT title FROM books WHERE price = 0 OR price IS NULL;", &sql);
// add ^^^^^
// here fieldSourcepub fn or_where_is_not_null<S: ToString>(&mut self, field: S) -> &mut Self
pub fn or_where_is_not_null<S: ToString>(&mut self, field: S) -> &mut Self
Add OR IS NOT NULL condition to the last WHERE condition.
use sql_builder::SqlBuilder;
let sql = SqlBuilder::select_from("books")
.field("title")
.or_where_is_not_null("title")
.or_where_is_not_null("price")
.sql()?;
assert_eq!("SELECT title FROM books WHERE title IS NOT NULL OR price IS NOT NULL;", &sql);
// add ^^^^^ ^^^^^
// here field fieldSourcepub fn or_where_in<S, T>(&mut self, field: S, list: &[T]) -> &mut Self
pub fn or_where_in<S, T>(&mut self, field: S, list: &[T]) -> &mut Self
Add OR field IN (list) to the last WHERE condition.
use sql_builder::{SqlBuilder, quote};
let sql = SqlBuilder::select_from("books")
.field("title")
.field("price")
.or_where_lt("price", 100)
.or_where_in("title", &[quote("G"), quote("L"), quote("t")])
.sql()?;
assert_eq!("SELECT title, price FROM books WHERE price < 100 OR title IN ('G', 'L', 't');", &sql);
// add ^^^^^ ^^^^^^^^^^^^^
// here field listSourcepub fn or_where_in_quoted<S, T>(&mut self, field: S, list: &[T]) -> &mut Self
pub fn or_where_in_quoted<S, T>(&mut self, field: S, list: &[T]) -> &mut Self
Add OR field IN (string list) to the last WHERE condition.
use sql_builder::{SqlBuilder, quote};
let sql = SqlBuilder::select_from("books")
.field("title")
.field("price")
.or_where_lt("price", 100)
.or_where_in_quoted("title", &["G", "L", "t"])
.sql()?;
assert_eq!("SELECT title, price FROM books WHERE price < 100 OR title IN ('G', 'L', 't');", &sql);
// add ^^^^^ ^^^^^^^^^^^^^
// here field listSourcepub fn or_where_not_in<S, T>(&mut self, field: S, list: &[T]) -> &mut Self
pub fn or_where_not_in<S, T>(&mut self, field: S, list: &[T]) -> &mut Self
Add OR field NOT IN (list) to the last WHERE condition.
use sql_builder::{SqlBuilder, quote};
let sql = SqlBuilder::select_from("books")
.field("title")
.field("price")
.or_where_lt("price", 100)
.or_where_not_in("title", &[quote("G"), quote("L"), quote("t")])
.sql()?;
assert_eq!("SELECT title, price FROM books WHERE price < 100 OR title NOT IN ('G', 'L', 't');", &sql);
// add ^^^^^ ^^^^^^^^^^^^^
// here field listSourcepub fn or_where_not_in_quoted<S, T>(
&mut self,
field: S,
list: &[T],
) -> &mut Self
pub fn or_where_not_in_quoted<S, T>( &mut self, field: S, list: &[T], ) -> &mut Self
Add OR field NOT IN (string list) to the last WHERE condition.
use sql_builder::{SqlBuilder, quote};
let sql = SqlBuilder::select_from("books")
.field("title")
.field("price")
.or_where_lt("price", 100)
.or_where_not_in_quoted("title", &["G", "L", "t"])
.sql()?;
assert_eq!("SELECT title, price FROM books WHERE price < 100 OR title NOT IN ('G', 'L', 't');", &sql);
// add ^^^^^ ^^^^^^^^^^^^^
// here field listSourcepub fn or_where_in_query<S, T>(&mut self, field: S, query: T) -> &mut Self
pub fn or_where_in_query<S, T>(&mut self, field: S, query: T) -> &mut Self
Add OR field IN (query) to the last WHERE condition.
use sql_builder::SqlBuilder;
let query = SqlBuilder::select_from("shop")
.field("title")
.and_where("sold")
.query()?;
assert_eq!("SELECT title FROM shop WHERE sold", &query);
let sql = SqlBuilder::select_from("books")
.field("title")
.field("price")
.or_where_lt("price", 100)
.or_where_in_query("title", &query)
.sql()?;
assert_eq!("SELECT title, price FROM books WHERE price < 100 OR title IN (SELECT title FROM shop WHERE sold);", &sql);
// add ^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// here field querySourcepub fn or_where_not_in_query<S, T>(&mut self, field: S, query: T) -> &mut Self
pub fn or_where_not_in_query<S, T>(&mut self, field: S, query: T) -> &mut Self
Add OR field NOT IN (query) to the last WHERE condition.
use sql_builder::SqlBuilder;
let query = SqlBuilder::select_from("shop")
.field("title")
.and_where("sold")
.query()?;
assert_eq!("SELECT title FROM shop WHERE sold", &query);
let sql = SqlBuilder::select_from("books")
.field("title")
.field("price")
.or_where_lt("price", 100)
.or_where_not_in_query("title", &query)
.sql()?;
assert_eq!("SELECT title, price FROM books WHERE price < 100 OR title NOT IN (SELECT title FROM shop WHERE sold);", &sql);
// add ^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// here field querySourcepub fn or_where_between<S, T, U>(
&mut self,
field: S,
min: T,
max: U,
) -> &mut Self
pub fn or_where_between<S, T, U>( &mut self, field: S, min: T, max: U, ) -> &mut Self
Add OR field BETWEEN values to the last WHERE condition.
use sql_builder::SqlBuilder;
let sql = SqlBuilder::select_from("books")
.field("title")
.field("price")
.or_where_between("price", 100, 200)
.or_where_between("price", 10_000, 20_000)
.sql()?;
assert_eq!("SELECT title, price FROM books WHERE price BETWEEN 100 AND 200 OR price BETWEEN 10000 AND 20000;", &sql);
// add ^^^^^ ^^^ ^^^ ^^^^^ ^^^^^ ^^^^^
// here field min max field min maxSourcepub fn or_where_not_between<S, T, U>(
&mut self,
field: S,
min: T,
max: U,
) -> &mut Self
pub fn or_where_not_between<S, T, U>( &mut self, field: S, min: T, max: U, ) -> &mut Self
Add OR field NOT BETWEEN values to the last WHERE condition.
use sql_builder::SqlBuilder;
let sql = SqlBuilder::select_from("books")
.field("title")
.field("price")
.or_where_not_between("price", 100, 200)
.or_where_not_between("price", 10_000, 20_000)
.sql()?;
assert_eq!("SELECT title, price FROM books WHERE price NOT BETWEEN 100 AND 200 OR price NOT BETWEEN 10000 AND 20000;", &sql);
// add ^^^^^ ^^^ ^^^ ^^^^^ ^^^^^ ^^^^^
// here field min max field min maxSourcepub fn union<S: ToString>(&mut self, query: S) -> &mut Self
pub fn union<S: ToString>(&mut self, query: S) -> &mut Self
Union query with subquery. ORDER BY must be in the last subquery.
use sql_builder::SqlBuilder;
let append = SqlBuilder::select_from("books")
.field("title")
.field("price")
.and_where("price < 100")
.order_asc("title")
.query()?;
let sql = SqlBuilder::select_from("books")
.field("title")
.field("price")
.and_where_like_left("title", "Harry Potter")
.order_desc("price")
.union(&append)
.sql()?;
assert_eq!("SELECT title, price FROM books WHERE title LIKE 'Harry Potter%' UNION SELECT title, price FROM books WHERE price < 100 ORDER BY title;", &sql);
// add ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// here querySourcepub fn union_all<S: ToString>(&mut self, query: S) -> &mut Self
pub fn union_all<S: ToString>(&mut self, query: S) -> &mut Self
Union query with all subquery. ORDER BY must be in the last subquery.
use sql_builder::SqlBuilder;
let append = SqlBuilder::select_values(&["'The Great Gatsby'", "124"])
.query_values()?;
let sql = SqlBuilder::select_from("books")
.field("title")
.field("price")
.and_where_like_left("title", "Harry Potter")
.order_desc("price")
.union_all(&append)
.sql()?;
assert_eq!("SELECT title, price FROM books WHERE title LIKE 'Harry Potter%' UNION ALL SELECT 'The Great Gatsby', 124;", &sql);
// add ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// here querySourcepub fn order_by<S: ToString>(&mut self, field: S, desc: bool) -> &mut Self
pub fn order_by<S: ToString>(&mut self, field: S, desc: bool) -> &mut Self
Add ORDER BY.
use sql_builder::SqlBuilder;
let sql = SqlBuilder::select_from("books")
.field("title")
.field("price")
.and_where_like_left("title", "Harry Potter")
.order_by("price", false)
.sql()?;
assert_eq!("SELECT title, price FROM books WHERE title LIKE 'Harry Potter%' ORDER BY price;", &sql);
// add ^^^^^
// here fieldSourcepub fn order_asc<S: ToString>(&mut self, field: S) -> &mut Self
pub fn order_asc<S: ToString>(&mut self, field: S) -> &mut Self
Add ORDER BY ASC.
use sql_builder::SqlBuilder;
let sql = SqlBuilder::select_from("books")
.field("title")
.field("price")
.and_where_like_left("title", "Harry Potter")
.order_asc("title")
.sql()?;
assert_eq!("SELECT title, price FROM books WHERE title LIKE 'Harry Potter%' ORDER BY title;", &sql);
// add ^^^^^
// here fieldSourcepub fn order_desc<S: ToString>(&mut self, field: S) -> &mut Self
pub fn order_desc<S: ToString>(&mut self, field: S) -> &mut Self
Add ORDER BY DESC.
use sql_builder::SqlBuilder;
let sql = SqlBuilder::select_from("books")
.field("title")
.field("price")
.and_where_like_left("title", "Harry Potter")
.order_desc("price")
.sql()?;
assert_eq!("SELECT title, price FROM books WHERE title LIKE 'Harry Potter%' ORDER BY price DESC;", &sql);
// add ^^^^^
// here fieldSourcepub fn limit<S: ToString>(&mut self, limit: S) -> &mut Self
pub fn limit<S: ToString>(&mut self, limit: S) -> &mut Self
Set LIMIT.
use sql_builder::SqlBuilder;
let sql = SqlBuilder::select_from("books")
.field("title")
.field("price")
.and_where_like_left("title", "Harry Potter")
.order_desc("price")
.limit(10)
.sql()?;
assert_eq!("SELECT title, price FROM books WHERE title LIKE 'Harry Potter%' ORDER BY price DESC LIMIT 10;", &sql);
// add ^^
// here limitSourcepub fn offset<S: ToString>(&mut self, offset: S) -> &mut Self
pub fn offset<S: ToString>(&mut self, offset: S) -> &mut Self
Set OFFSET.
use sql_builder::SqlBuilder;
let sql = SqlBuilder::select_from("books")
.field("title")
.field("price")
.and_where_like_left("title", "Harry Potter")
.order_desc("price")
.limit(10)
.offset(100)
.sql()?;
assert_eq!("SELECT title, price FROM books WHERE title LIKE 'Harry Potter%' ORDER BY price DESC LIMIT 10 OFFSET 100;", &sql);
// add ^^^
// here offsetSourcepub fn sql(&self) -> Result<String>
pub fn sql(&self) -> Result<String>
Build complete SQL command.
use sql_builder::SqlBuilder;
let sql = SqlBuilder::select_from("books").sql()?;
assert_eq!("SELECT * FROM books;", &sql);Sourcepub fn subquery(&self) -> Result<String>
pub fn subquery(&self) -> Result<String>
Build subquery SQL command.
use sql_builder::SqlBuilder;
let cat = SqlBuilder::select_from("books")
.field("CASE WHEN price < 100 THEN 'cheap' ELSE 'expensive' END AS category")
.subquery()?;
assert_eq!("(SELECT CASE WHEN price < 100 THEN 'cheap' ELSE 'expensive' END AS category FROM books)", &cat);
let sql = SqlBuilder::select_from(&cat)
.field("category")
.field("COUNT(category) AS cnt")
.group_by("category")
.order_desc("cnt")
.order_asc("category")
.sql()?;
assert_eq!("SELECT category, COUNT(category) AS cnt FROM (SELECT CASE WHEN price < 100 THEN 'cheap' ELSE 'expensive' END AS category FROM books) GROUP BY category ORDER BY cnt DESC, category;", &sql);Sourcepub fn subquery_as<S: ToString>(&self, name: S) -> Result<String>
pub fn subquery_as<S: ToString>(&self, name: S) -> Result<String>
Build named subquery SQL command.
use sql_builder::SqlBuilder;
let cat = SqlBuilder::select_from("books")
.field("CASE WHEN price < 100 THEN 'cheap' ELSE 'expensive' END")
.subquery_as("category")?;
assert_eq!("(SELECT CASE WHEN price < 100 THEN 'cheap' ELSE 'expensive' END FROM books) AS category", &cat);
// add ^^^^^^^^
// here name
let sql = SqlBuilder::select_from("books")
.field("title")
.field("price")
.field(&cat)
.sql()?;
assert_eq!("SELECT title, price, (SELECT CASE WHEN price < 100 THEN 'cheap' ELSE 'expensive' END FROM books) AS category FROM books;", &sql);Sourcepub fn query(&self) -> Result<String>
pub fn query(&self) -> Result<String>
SQL command generator for query or subquery.
use sql_builder::SqlBuilder;
let query = SqlBuilder::select_from("warehouse")
.field("title")
.field("preliminary_price * 2")
.query()?;
assert_eq!("SELECT title, preliminary_price * 2 FROM warehouse", &query);
let sql = SqlBuilder::insert_into("books")
.field("title")
.field("price")
.select(&query)
.sql()?;
assert_eq!("INSERT INTO books (title, price) SELECT title, preliminary_price * 2 FROM warehouse;", &sql);Sourcepub fn query_values(&self) -> Result<String>
pub fn query_values(&self) -> Result<String>
SQL command generator for query or subquery without a table.
use sql_builder::{SqlBuilder, quote};
let values = SqlBuilder::select_values(&["10", "e("100")])
.query_values()?;
assert_eq!("SELECT 10, '100'", &values);Trait Implementations§
Source§impl Clone for SqlBuilder
impl Clone for SqlBuilder
Source§fn clone(&self) -> SqlBuilder
fn clone(&self) -> SqlBuilder
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl Freeze for SqlBuilder
impl RefUnwindSafe for SqlBuilder
impl Send for SqlBuilder
impl Sync for SqlBuilder
impl Unpin for SqlBuilder
impl UnwindSafe for SqlBuilder
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit)