Struct SqlBuilder

Source
pub struct SqlBuilder { /* private fields */ }
Expand description

Main SQL builder

Implementations§

Source§

impl SqlBuilder

Source

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                              table
Source

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                                       table
Source

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", &quote("100")])
    .sql()?;

assert_eq!("SELECT 10, '100';", &sql);
// add             ^^^^^^^^^
// here             values
Source

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                 table
Source

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            table
Source

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                 table
Source

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                                ^^^^^^^
Source

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                                        ^^^^
Source

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                                        ^^^^^^^^^^
Source

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                                        ^^^^^
Source

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                                        ^^^^^^^^^^^
Source

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                                        ^^^^^
Source

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                                        ^^^^^
Source

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                                                         table
Source

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                                                                 constraint
Source

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      c2
Source

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        ^^^^^^^^
Source

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               fields
Source

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

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  field
Source

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

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                         field
Source

pub fn count_as<S, T>(&mut self, field: S, name: T) -> &mut Self
where S: ToString, T: ToString,

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                         field
Source

pub fn set<S, T>(&mut self, field: S, value: T) -> &mut Self
where 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
Source

pub fn set_str<S, T>(&mut self, field: S, value: T) -> &mut Self
where 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
Source

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                      values
Source

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                                                            query
Source

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                                                                           field
Source

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                                                              ^^^^^^^^^^^^
Source

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                                                           field
Source

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                                                                           cond
Source

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                      cond
Source

pub fn and_where_eq<S, T>(&mut self, field: S, value: T) -> &mut Self
where 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", &quote("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
Source

pub fn and_where_ne<S, T>(&mut self, field: S, value: T) -> &mut Self
where 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", &quote("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
Source

pub fn and_where_gt<S, T>(&mut self, field: S, value: T) -> &mut Self
where 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
Source

pub fn and_where_ge<S, T>(&mut self, field: S, value: T) -> &mut Self
where 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
Source

pub fn and_where_lt<S, T>(&mut self, field: S, value: T) -> &mut Self
where 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
Source

pub fn and_where_le<S, T>(&mut self, field: S, value: T) -> &mut Self
where 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
Source

pub fn and_where_like<S, T>(&mut self, field: S, mask: T) -> &mut Self
where 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
Source

pub fn and_where_like_right<S, T>(&mut self, field: S, mask: T) -> &mut Self
where 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
Source

pub fn and_where_like_left<S, T>(&mut self, field: S, mask: T) -> &mut Self
where 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
Source

pub fn and_where_like_any<S, T>(&mut self, field: S, mask: T) -> &mut Self
where 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
Source

pub fn and_where_not_like<S, T>(&mut self, field: S, mask: T) -> &mut Self
where 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
Source

pub fn and_where_not_like_right<S, T>(&mut self, field: S, mask: T) -> &mut Self
where 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
Source

pub fn and_where_not_like_left<S, T>(&mut self, field: S, mask: T) -> &mut Self
where 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
Source

pub fn and_where_not_like_any<S, T>(&mut self, field: S, mask: T) -> &mut Self
where 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
Source

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                                   field
Source

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                                   field
Source

pub fn and_where_in<S, T>(&mut self, field: S, list: &[T]) -> &mut Self
where 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
Source

pub fn and_where_in_quoted<S, T>(&mut self, field: S, list: &[T]) -> &mut Self
where S: ToString, T: ToString,

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         list
Source

pub fn and_where_not_in<S, T>(&mut self, field: S, list: &[T]) -> &mut Self
where 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
Source

pub fn and_where_not_in_quoted<S, T>( &mut self, field: S, list: &[T], ) -> &mut Self
where S: ToString, T: ToString,

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             list
Source

pub fn and_where_in_query<S, T>(&mut self, field: S, query: T) -> &mut Self
where 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
Source

pub fn and_where_not_in_query<S, T>(&mut self, field: S, query: T) -> &mut Self
where 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
Source

pub fn and_where_between<S, T, U>( &mut self, field: S, min: T, max: U, ) -> &mut Self
where S: ToString, T: ToString, U: ToString,

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       max
Source

pub fn and_where_not_between<S, T, U>( &mut self, field: S, min: T, max: U, ) -> &mut Self
where S: ToString, T: ToString, U: ToString,

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       max
Source

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                                                            cond
Source

pub fn or_where_eq<S, T>(&mut self, field: S, value: T) -> &mut Self
where 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", &quote("Harry Potter and the Philosopher's Stone"))
    .or_where_eq("title", &quote("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
Source

pub fn or_where_ne<S, T>(&mut self, field: S, value: T) -> &mut Self
where 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", &quote("Harry Potter and the Philosopher's Stone"))
    .or_where_ne("title", &quote("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
Source

pub fn or_where_gt<S, T>(&mut self, field: S, value: T) -> &mut Self
where 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
Source

pub fn or_where_ge<S, T>(&mut self, field: S, value: T) -> &mut Self
where 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
Source

pub fn or_where_lt<S, T>(&mut self, field: S, value: T) -> &mut Self
where 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
Source

pub fn or_where_le<S, T>(&mut self, field: S, value: T) -> &mut Self
where 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
Source

pub fn or_where_like<S, T>(&mut self, field: S, mask: T) -> &mut Self
where 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
Source

pub fn or_where_like_right<S, T>(&mut self, field: S, mask: T) -> &mut Self
where 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
Source

pub fn or_where_like_left<S, T>(&mut self, field: S, mask: T) -> &mut Self
where 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
Source

pub fn or_where_like_any<S, T>(&mut self, field: S, mask: T) -> &mut Self
where 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
Source

pub fn or_where_not_like<S, T>(&mut self, field: S, mask: T) -> &mut Self
where 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
Source

pub fn or_where_not_like_right<S, T>(&mut self, field: S, mask: T) -> &mut Self
where 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
Source

pub fn or_where_not_like_left<S, T>(&mut self, field: S, mask: T) -> &mut Self
where 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
Source

pub fn or_where_not_like_any<S, T>(&mut self, field: S, mask: T) -> &mut Self
where 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
Source

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                                                field
Source

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                field
Source

pub fn or_where_in<S, T>(&mut self, field: S, list: &[T]) -> &mut Self
where 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
Source

pub fn or_where_in_quoted<S, T>(&mut self, field: S, list: &[T]) -> &mut Self
where S: ToString, T: ToString,

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         list
Source

pub fn or_where_not_in<S, T>(&mut self, field: S, list: &[T]) -> &mut Self
where 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
Source

pub fn or_where_not_in_quoted<S, T>( &mut self, field: S, list: &[T], ) -> &mut Self
where S: ToString, T: ToString,

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             list
Source

pub fn or_where_in_query<S, T>(&mut self, field: S, query: T) -> &mut Self
where 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
Source

pub fn or_where_not_in_query<S, T>(&mut self, field: S, query: T) -> &mut Self
where 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
Source

pub fn or_where_between<S, T, U>( &mut self, field: S, min: T, max: U, ) -> &mut Self
where S: ToString, T: ToString, U: ToString,

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       max
Source

pub fn or_where_not_between<S, T, U>( &mut self, field: S, min: T, max: U, ) -> &mut Self
where S: ToString, T: ToString, U: ToString,

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       max
Source

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                                                                                                        query
Source

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                                                                                           query
Source

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                                                                              field
Source

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                                                                              field
Source

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                                                                              field
Source

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                                                                                              limit
Source

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                                                                                                        offset
Source

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

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

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

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

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", &quote("100")])
    .query_values()?;

assert_eq!("SELECT 10, '100'", &values);

Trait Implementations§

Source§

impl Clone for SqlBuilder

Source§

fn clone(&self) -> SqlBuilder

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.