[−][src]Struct sql_builder::SqlBuilder
Main SQL builder
Implementations
impl SqlBuilder
[src]
pub fn select_from<S: ToString>(table: S) -> Self
[src]
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 table
pub fn select_values<S: ToString>(values: &[S]) -> Self
[src]
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 values
pub fn insert_into<S: ToString>(table: S) -> Self
[src]
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 table
pub fn update_table<S: ToString>(table: S) -> Self
[src]
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 table
pub fn delete_from<S: ToString>(table: S) -> Self
[src]
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 table
pub fn natural(&mut self) -> &mut Self
[src]
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 ^^^^^^^
pub fn left(&mut self) -> &mut Self
[src]
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 ^^^^
pub fn left_outer(&mut self) -> &mut Self
[src]
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 ^^^^^^^^^^
pub fn right(&mut self) -> &mut Self
[src]
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 ^^^^^
pub fn right_outer(&mut self) -> &mut Self
[src]
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 ^^^^^^^^^^^
pub fn inner(&mut self) -> &mut Self
[src]
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 ^^^^^
pub fn cross(&mut self) -> &mut Self
[src]
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 ^^^^^
pub fn join<S: ToString>(&mut self, table: S) -> &mut Self
[src]
Join with table.
use sql_builder::SqlBuilder; let sql = SqlBuilder::select_from("books AS b") .field("b.title") .field("s.total") .left() .join("shops AS 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 table
pub fn on<S: ToString>(&mut self, constraint: S) -> &mut Self
[src]
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 constraint
pub fn distinct(&mut self) -> &mut Self
[src]
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 ^^^^^^^^
pub fn fields<S: ToString>(&mut self, fields: &[S]) -> &mut Self
[src]
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 fields
pub fn set_fields<S: ToString>(&mut self, fields: &[S]) -> &mut Self
[src]
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);
pub fn field<S: ToString>(&mut self, field: S) -> &mut Self
[src]
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 field
pub fn set_field<S: ToString>(&mut self, field: S) -> &mut Self
[src]
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);
pub fn set<S, T>(&mut self, field: S, value: T) -> &mut Self where
S: ToString,
T: ToString,
[src]
S: ToString,
T: ToString,
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 value
pub fn set_str<S, T>(&mut self, field: S, value: T) -> &mut Self where
S: ToString,
T: ToString,
[src]
S: ToString,
T: ToString,
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 value
pub fn values<S: ToString>(&mut self, values: &[S]) -> &mut Self
[src]
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 values
pub fn select<S: ToString>(&mut self, query: S) -> &mut Self
[src]
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 query
pub fn returning<S: ToString>(&mut self, field: S) -> &mut Self
[src]
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 field
pub fn returning_id(&mut self) -> &mut Self
[src]
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 ^^^^^^^^^^^^
pub fn group_by<S: ToString>(&mut self, field: S) -> &mut Self
[src]
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 field
pub fn having<S: ToString>(&mut self, cond: S) -> &mut Self
[src]
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 cond
pub fn and_where<S: ToString>(&mut self, cond: S) -> &mut Self
[src]
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 cond
pub fn and_where_eq<S, T>(&mut self, field: S, value: T) -> &mut Self where
S: ToString,
T: ToString,
[src]
S: ToString,
T: ToString,
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 value
pub fn and_where_ne<S, T>(&mut self, field: S, value: T) -> &mut Self where
S: ToString,
T: ToString,
[src]
S: ToString,
T: ToString,
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 value
pub fn and_where_gt<S, T>(&mut self, field: S, value: T) -> &mut Self where
S: ToString,
T: ToString,
[src]
S: ToString,
T: ToString,
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 value
pub fn and_where_ge<S, T>(&mut self, field: S, value: T) -> &mut Self where
S: ToString,
T: ToString,
[src]
S: ToString,
T: ToString,
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 value
pub fn and_where_lt<S, T>(&mut self, field: S, value: T) -> &mut Self where
S: ToString,
T: ToString,
[src]
S: ToString,
T: ToString,
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 value
pub fn and_where_le<S, T>(&mut self, field: S, value: T) -> &mut Self where
S: ToString,
T: ToString,
[src]
S: ToString,
T: ToString,
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 value
pub fn and_where_like<S, T>(&mut self, field: S, mask: T) -> &mut Self where
S: ToString,
T: ToString,
[src]
S: ToString,
T: ToString,
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 mask
pub fn and_where_like_right<S, T>(&mut self, field: S, mask: T) -> &mut Self where
S: ToString,
T: ToString,
[src]
S: ToString,
T: ToString,
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 mask
pub fn and_where_like_left<S, T>(&mut self, field: S, mask: T) -> &mut Self where
S: ToString,
T: ToString,
[src]
S: ToString,
T: ToString,
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 mask
pub fn and_where_like_any<S, T>(&mut self, field: S, mask: T) -> &mut Self where
S: ToString,
T: ToString,
[src]
S: ToString,
T: ToString,
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 mask
pub fn and_where_not_like<S, T>(&mut self, field: S, mask: T) -> &mut Self where
S: ToString,
T: ToString,
[src]
S: ToString,
T: ToString,
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 mask
pub fn and_where_not_like_right<S, T>(&mut self, field: S, mask: T) -> &mut Self where
S: ToString,
T: ToString,
[src]
S: ToString,
T: ToString,
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 mask
pub fn and_where_not_like_left<S, T>(&mut self, field: S, mask: T) -> &mut Self where
S: ToString,
T: ToString,
[src]
S: ToString,
T: ToString,
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 mask
pub fn and_where_not_like_any<S, T>(&mut self, field: S, mask: T) -> &mut Self where
S: ToString,
T: ToString,
[src]
S: ToString,
T: ToString,
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 mask
pub fn and_where_is_null<S: ToString>(&mut self, field: S) -> &mut Self
[src]
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 field
pub fn and_where_is_not_null<S: ToString>(&mut self, field: S) -> &mut Self
[src]
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 field
pub fn and_where_in<S, T>(&mut self, field: S, list: &[T]) -> &mut Self where
S: ToString,
T: ToString,
[src]
S: ToString,
T: ToString,
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 list
pub fn and_where_not_in<S, T>(&mut self, field: S, list: &[T]) -> &mut Self where
S: ToString,
T: ToString,
[src]
S: ToString,
T: ToString,
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 list
pub fn and_where_in_query<S, T>(&mut self, field: S, query: T) -> &mut Self where
S: ToString,
T: ToString,
[src]
S: ToString,
T: ToString,
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 query
pub fn and_where_not_in_query<S, T>(&mut self, field: S, query: T) -> &mut Self where
S: ToString,
T: ToString,
[src]
S: ToString,
T: ToString,
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 query
pub fn or_where<S: ToString>(&mut self, cond: S) -> &mut Self
[src]
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 cond
pub fn or_where_eq<S, T>(&mut self, field: S, value: T) -> &mut Self where
S: ToString,
T: ToString,
[src]
S: ToString,
T: ToString,
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 value
pub fn or_where_ne<S, T>(&mut self, field: S, value: T) -> &mut Self where
S: ToString,
T: ToString,
[src]
S: ToString,
T: ToString,
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 value
pub fn or_where_gt<S, T>(&mut self, field: S, value: T) -> &mut Self where
S: ToString,
T: ToString,
[src]
S: ToString,
T: ToString,
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 value
pub fn or_where_ge<S, T>(&mut self, field: S, value: T) -> &mut Self where
S: ToString,
T: ToString,
[src]
S: ToString,
T: ToString,
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 value
pub fn or_where_lt<S, T>(&mut self, field: S, value: T) -> &mut Self where
S: ToString,
T: ToString,
[src]
S: ToString,
T: ToString,
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 value
pub fn or_where_le<S, T>(&mut self, field: S, value: T) -> &mut Self where
S: ToString,
T: ToString,
[src]
S: ToString,
T: ToString,
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 value
pub fn or_where_like<S, T>(&mut self, field: S, mask: T) -> &mut Self where
S: ToString,
T: ToString,
[src]
S: ToString,
T: ToString,
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 mask
pub fn or_where_like_right<S, T>(&mut self, field: S, mask: T) -> &mut Self where
S: ToString,
T: ToString,
[src]
S: ToString,
T: ToString,
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 mask
pub fn or_where_like_left<S, T>(&mut self, field: S, mask: T) -> &mut Self where
S: ToString,
T: ToString,
[src]
S: ToString,
T: ToString,
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 mask
pub fn or_where_like_any<S, T>(&mut self, field: S, mask: T) -> &mut Self where
S: ToString,
T: ToString,
[src]
S: ToString,
T: ToString,
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 mask
pub fn or_where_not_like<S, T>(&mut self, field: S, mask: T) -> &mut Self where
S: ToString,
T: ToString,
[src]
S: ToString,
T: ToString,
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 mask
pub fn or_where_not_like_right<S, T>(&mut self, field: S, mask: T) -> &mut Self where
S: ToString,
T: ToString,
[src]
S: ToString,
T: ToString,
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 mask
pub fn or_where_not_like_left<S, T>(&mut self, field: S, mask: T) -> &mut Self where
S: ToString,
T: ToString,
[src]
S: ToString,
T: ToString,
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 mask
pub fn or_where_not_like_any<S, T>(&mut self, field: S, mask: T) -> &mut Self where
S: ToString,
T: ToString,
[src]
S: ToString,
T: ToString,
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 mask
pub fn or_where_is_null<S: ToString>(&mut self, field: S) -> &mut Self
[src]
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 field
pub fn or_where_is_not_null<S: ToString>(&mut self, field: S) -> &mut Self
[src]
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 field
pub fn or_where_in<S, T>(&mut self, field: S, list: &[T]) -> &mut Self where
S: ToString,
T: ToString,
[src]
S: ToString,
T: ToString,
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 list
pub fn or_where_not_in<S, T>(&mut self, field: S, list: &[T]) -> &mut Self where
S: ToString,
T: ToString,
[src]
S: ToString,
T: ToString,
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 list
pub fn or_where_in_query<S, T>(&mut self, field: S, query: T) -> &mut Self where
S: ToString,
T: ToString,
[src]
S: ToString,
T: ToString,
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 query
pub fn or_where_not_in_query<S, T>(&mut self, field: S, query: T) -> &mut Self where
S: ToString,
T: ToString,
[src]
S: ToString,
T: ToString,
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 query
pub fn union<S: ToString>(&mut self, query: S) -> &mut Self
[src]
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 query
pub fn union_all<S: ToString>(&mut self, query: S) -> &mut Self
[src]
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 query
pub fn order_by<S: ToString>(&mut self, field: S, desc: bool) -> &mut Self
[src]
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 field
pub fn order_asc<S: ToString>(&mut self, field: S) -> &mut Self
[src]
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 field
pub fn order_desc<S: ToString>(&mut self, field: S) -> &mut Self
[src]
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 field
pub fn limit<S: ToString>(&mut self, limit: S) -> &mut Self
[src]
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 limit
pub fn offset<S: ToString>(&mut self, offset: S) -> &mut Self
[src]
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 offset
pub fn sql(&self) -> Result<String, Box<dyn Error + Send + Sync>>
[src]
Build complete SQL command.
use sql_builder::SqlBuilder; let sql = SqlBuilder::select_from("books").sql()?; assert_eq!("SELECT * FROM books;", &sql);
pub fn subquery(&self) -> Result<String, Box<dyn Error + Send + Sync>>
[src]
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);
pub fn subquery_as<S: ToString>(
&self,
name: S
) -> Result<String, Box<dyn Error + Send + Sync>>
[src]
&self,
name: S
) -> Result<String, Box<dyn Error + Send + Sync>>
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);
pub fn query(&self) -> Result<String, Box<dyn Error + Send + Sync>>
[src]
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);
pub fn query_values(&self) -> Result<String, Box<dyn Error + Send + Sync>>
[src]
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
impl Clone for SqlBuilder
[src]
fn clone(&self) -> SqlBuilder
[src]
fn clone_from(&mut self, source: &Self)
1.0.0[src]
Auto Trait Implementations
impl RefUnwindSafe for SqlBuilder
impl Send for SqlBuilder
impl Sync for SqlBuilder
impl Unpin for SqlBuilder
impl UnwindSafe for SqlBuilder
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
fn to_owned(&self) -> T
[src]
fn clone_into(&self, target: &mut T)
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,