Struct Sqlite3Builder

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

Main Sqlite3 builder

Implementations§

Source§

impl Sqlite3Builder

Source

pub fn select_from<S: ToString>(table: S) -> Self

Create SELECT query. You may specify comma separted list of tables.

extern crate sqlite3builder;

use sqlite3builder::Sqlite3Builder;

let sql = Sqlite3Builder::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 select_values<S: ToString>(values: &[S]) -> Self

Create SELECT query without a table.

extern crate sqlite3builder;

use sqlite3builder::{Sqlite3Builder, quote};

let sql = Sqlite3Builder::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.

extern crate sqlite3builder;

use sqlite3builder::{Sqlite3Builder, quote};

let sql = Sqlite3Builder::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.

extern crate sqlite3builder;

use sqlite3builder::Sqlite3Builder;

let sql = Sqlite3Builder::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.

extern crate sqlite3builder;

use sqlite3builder::Sqlite3Builder;

let sql = Sqlite3Builder::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

extern crate sqlite3builder;

use sqlite3builder::Sqlite3Builder;

let sql = Sqlite3Builder::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

extern crate sqlite3builder;

use sqlite3builder::Sqlite3Builder;

let sql = Sqlite3Builder::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

extern crate sqlite3builder;

use sqlite3builder::Sqlite3Builder;

let sql = Sqlite3Builder::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

extern crate sqlite3builder;

use sqlite3builder::Sqlite3Builder;

let sql = Sqlite3Builder::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 inner(&mut self) -> &mut Self

Use INNER JOIN

extern crate sqlite3builder;

use sqlite3builder::Sqlite3Builder;

let sql = Sqlite3Builder::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

extern crate sqlite3builder;

use sqlite3builder::Sqlite3Builder;

let sql = Sqlite3Builder::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.

extern crate sqlite3builder;

use sqlite3builder::Sqlite3Builder;

let sql = Sqlite3Builder::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
Source

pub fn on<S: ToString>(&mut self, constraint: S) -> &mut Self

Join constraint to the last JOIN part.

extern crate sqlite3builder;

use sqlite3builder::Sqlite3Builder;

let sql = Sqlite3Builder::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 distinct(&mut self) -> &mut Self

Set DISTINCT for fields.

extern crate sqlite3builder;

use sqlite3builder::Sqlite3Builder;

let sql = Sqlite3Builder::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.

extern crate sqlite3builder;

use sqlite3builder::Sqlite3Builder;

let sql = Sqlite3Builder::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.

extern crate sqlite3builder;

use sqlite3builder::Sqlite3Builder;

// Prepare query for total count

let mut db = Sqlite3Builder::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.

extern crate sqlite3builder;

use sqlite3builder::Sqlite3Builder;

let sql = Sqlite3Builder::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.

extern crate sqlite3builder;

use sqlite3builder::Sqlite3Builder;

// Prepare query for total count

let mut db = Sqlite3Builder::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 set<S, T>(&mut self, field: S, value: T) -> &mut Self
where S: ToString, T: ToString,

Add SET part (for UPDATE).

extern crate sqlite3builder;

use sqlite3builder::Sqlite3Builder;

let sql = Sqlite3Builder::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).

extern crate sqlite3builder;

use sqlite3builder::Sqlite3Builder;

let sql = Sqlite3Builder::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).

extern crate sqlite3builder;

use sqlite3builder::{Sqlite3Builder, quote};

let sql = Sqlite3Builder::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).

extern crate sqlite3builder;

use sqlite3builder::Sqlite3Builder;

let query = Sqlite3Builder::select_from("warehouse")
    .field("title")
    .field("preliminary_price * 2")
    .query()?;

assert_eq!("SELECT title, preliminary_price * 2 FROM warehouse", &query);

let sql = Sqlite3Builder::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 group_by<S: ToString>(&mut self, field: S) -> &mut Self

Add GROUP BY part.

extern crate sqlite3builder;

use sqlite3builder::Sqlite3Builder;

let sql = Sqlite3Builder::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.

extern crate sqlite3builder;

use sqlite3builder::{Sqlite3Builder, quote};

let sql = Sqlite3Builder::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.

extern crate sqlite3builder;

use sqlite3builder::{Sqlite3Builder, quote};

let sql = Sqlite3Builder::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.

extern crate sqlite3builder;

use sqlite3builder::{Sqlite3Builder, quote};

let sql = Sqlite3Builder::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.

extern crate sqlite3builder;

use sqlite3builder::{Sqlite3Builder, quote};

let sql = Sqlite3Builder::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.

extern crate sqlite3builder;

use sqlite3builder::Sqlite3Builder;

let sql = Sqlite3Builder::select_from("books")
    .field("title")
    .field("price")
    .and_where_gt("price", 300.to_string())
    .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.

extern crate sqlite3builder;

use sqlite3builder::Sqlite3Builder;

let sql = Sqlite3Builder::select_from("books")
    .field("title")
    .field("price")
    .and_where_ge("price", 300.to_string())
    .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.

extern crate sqlite3builder;

use sqlite3builder::Sqlite3Builder;

let sql = Sqlite3Builder::select_from("books")
    .field("title")
    .field("price")
    .and_where_lt("price", 300.to_string())
    .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.

extern crate sqlite3builder;

use sqlite3builder::Sqlite3Builder;

let sql = Sqlite3Builder::select_from("books")
    .field("title")
    .field("price")
    .and_where_le("price", 300.to_string())
    .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.

extern crate sqlite3builder;

use sqlite3builder::Sqlite3Builder;

let sql = Sqlite3Builder::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.

extern crate sqlite3builder;

use sqlite3builder::Sqlite3Builder;

let sql = Sqlite3Builder::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%.

extern crate sqlite3builder;

use sqlite3builder::Sqlite3Builder;

let sql = Sqlite3Builder::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%.

extern crate sqlite3builder;

use sqlite3builder::Sqlite3Builder;

let sql = Sqlite3Builder::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.

extern crate sqlite3builder;

use sqlite3builder::Sqlite3Builder;

let sql = Sqlite3Builder::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.

extern crate sqlite3builder;

use sqlite3builder::Sqlite3Builder;

let sql = Sqlite3Builder::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%.

extern crate sqlite3builder;

use sqlite3builder::Sqlite3Builder;

let sql = Sqlite3Builder::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%.

extern crate sqlite3builder;

use sqlite3builder::Sqlite3Builder;

let sql = Sqlite3Builder::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.

extern crate sqlite3builder;

use sqlite3builder::Sqlite3Builder;

let sql = Sqlite3Builder::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.

extern crate sqlite3builder;

use sqlite3builder::Sqlite3Builder;

let sql = Sqlite3Builder::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 or_where<S: ToString>(&mut self, cond: S) -> &mut Self

Add OR condition to the last WHERE condition.

extern crate sqlite3builder;

use sqlite3builder::Sqlite3Builder;

let sql = Sqlite3Builder::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.

extern crate sqlite3builder;

use sqlite3builder::{Sqlite3Builder, quote};

let sql = Sqlite3Builder::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.

extern crate sqlite3builder;

use sqlite3builder::{Sqlite3Builder, quote};

let sql = Sqlite3Builder::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.

extern crate sqlite3builder;

use sqlite3builder::Sqlite3Builder;

let sql = Sqlite3Builder::select_from("books")
    .field("title")
    .field("price")
    .and_where_lt("price", 100.to_string())
    .or_where_gt("price", 300.to_string())
    .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.

extern crate sqlite3builder;

use sqlite3builder::Sqlite3Builder;

let sql = Sqlite3Builder::select_from("books")
    .field("title")
    .field("price")
    .or_where_lt("price", 100.to_string())
    .or_where_ge("price", 300.to_string())
    .sql()?;

assert_eq!("SELECT title, price FROM books WHERE price < 100 OR price >= 300;", &sql);
// add                                           ^^^^^   ^^^    ^^^^^    ^^^
// here                                          field  value   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.

extern crate sqlite3builder;

use sqlite3builder::Sqlite3Builder;

let sql = Sqlite3Builder::select_from("books")
    .field("title")
    .field("price")
    .and_where_lt("price", 100.to_string())
    .or_where_lt("price", 300.to_string())
    .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.

extern crate sqlite3builder;

use sqlite3builder::Sqlite3Builder;

let sql = Sqlite3Builder::select_from("books")
    .field("title")
    .field("price")
    .or_where_le("price", 100.to_string())
    .or_where_le("price", 300.to_string())
    .sql()?;

assert_eq!("SELECT title, price FROM books WHERE price <= 100 OR price <= 300;", &sql);
// add                                           ^^^^^    ^^^    ^^^^^    ^^^
// here                                          field   value   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.

extern crate sqlite3builder;

use sqlite3builder::Sqlite3Builder;

let sql = Sqlite3Builder::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.

extern crate sqlite3builder;

use sqlite3builder::Sqlite3Builder;

let sql = Sqlite3Builder::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%.

extern crate sqlite3builder;

use sqlite3builder::Sqlite3Builder;

let sql = Sqlite3Builder::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%.

extern crate sqlite3builder;

use sqlite3builder::Sqlite3Builder;

let sql = Sqlite3Builder::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.

extern crate sqlite3builder;

use sqlite3builder::Sqlite3Builder;

let sql = Sqlite3Builder::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.

extern crate sqlite3builder;

use sqlite3builder::Sqlite3Builder;

let sql = Sqlite3Builder::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%.

extern crate sqlite3builder;

use sqlite3builder::Sqlite3Builder;

let sql = Sqlite3Builder::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%.

extern crate sqlite3builder;

use sqlite3builder::Sqlite3Builder;

let sql = Sqlite3Builder::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.

extern crate sqlite3builder;

use sqlite3builder::Sqlite3Builder;

let sql = Sqlite3Builder::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.

extern crate sqlite3builder;

use sqlite3builder::Sqlite3Builder;

let sql = Sqlite3Builder::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 union<S: ToString>(&mut self, query: S) -> &mut Self

Union query with subquery. ORDER BY must be in the last subquery.

extern crate sqlite3builder;

use sqlite3builder::Sqlite3Builder;

let append = Sqlite3Builder::select_from("books")
    .field("title")
    .field("price")
    .and_where("price < 100")
    .order_asc("title")
    .query()?;

let sql = Sqlite3Builder::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.

extern crate sqlite3builder;

use sqlite3builder::Sqlite3Builder;

let append = Sqlite3Builder::select_values(&["'The Great Gatsby'", "124"])
    .query_values()?;

let sql = Sqlite3Builder::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.

extern crate sqlite3builder;

use sqlite3builder::Sqlite3Builder;

let sql = Sqlite3Builder::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.

extern crate sqlite3builder;

use sqlite3builder::Sqlite3Builder;

let sql = Sqlite3Builder::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.

extern crate sqlite3builder;

use sqlite3builder::Sqlite3Builder;

let sql = Sqlite3Builder::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.

extern crate sqlite3builder;

use sqlite3builder::Sqlite3Builder;

let sql = Sqlite3Builder::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.

extern crate sqlite3builder;

use sqlite3builder::Sqlite3Builder;

let sql = Sqlite3Builder::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, Box<dyn Error>>

Build complete SQL command.

extern crate sqlite3builder;

use sqlite3builder::Sqlite3Builder;

let sql = Sqlite3Builder::select_from("books").sql()?;

assert_eq!("SELECT * FROM books;", &sql);
Source

pub fn subquery(&self) -> Result<String, Box<dyn Error>>

Build subquery SQL command.

extern crate sqlite3builder;

use sqlite3builder::Sqlite3Builder;

let cat = Sqlite3Builder::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 = Sqlite3Builder::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, Box<dyn Error>>

Build named subquery SQL command.

extern crate sqlite3builder;

use sqlite3builder::Sqlite3Builder;

let cat = Sqlite3Builder::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 = Sqlite3Builder::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, Box<dyn Error>>

SQL command generator for query or subquery.

extern crate sqlite3builder;

use sqlite3builder::Sqlite3Builder;

let query = Sqlite3Builder::select_from("warehouse")
    .field("title")
    .field("preliminary_price * 2")
    .query()?;

assert_eq!("SELECT title, preliminary_price * 2 FROM warehouse", &query);

let sql = Sqlite3Builder::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, Box<dyn Error>>

SQL command generator for query or subquery without a table.

extern crate sqlite3builder;

use sqlite3builder::{Sqlite3Builder, quote};

let values = Sqlite3Builder::select_values(&["10", &quote("100")])
    .query_values()?;

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

pub fn exec( &self, conn: &PooledConnection<SqliteConnectionManager>, ) -> Result<(), Box<dyn Error>>

Execute request

Source

pub fn get( &self, conn: &PooledConnection<SqliteConnectionManager>, ) -> Result<Vec<Vec<JValue>>, Box<dyn Error>>

Execute and return all data

Source

pub fn get_row( &self, conn: &PooledConnection<SqliteConnectionManager>, ) -> Result<Vec<JValue>, Box<dyn Error>>

Execute and return first row

Source

pub fn get_value( &self, conn: &PooledConnection<SqliteConnectionManager>, ) -> Result<JValue, Box<dyn Error>>

Execute and return first value

Source

pub fn get_int( &self, conn: &PooledConnection<SqliteConnectionManager>, ) -> Result<i64, Box<dyn Error>>

Execute and return first integer value

Source

pub fn get_str( &self, conn: &PooledConnection<SqliteConnectionManager>, ) -> Result<String, Box<dyn Error>>

Execute and return first string value

Source

pub fn get_cursor<'a>( &'a self, conn: &'a PooledConnection<SqliteConnectionManager>, ) -> Result<Cursor<'a>, Box<dyn Error>>

Get cursor for request

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> 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, 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.