pub struct Sqlite3Builder { /* private fields */ }Expand description
Main Sqlite3 builder
Implementations§
Source§impl Sqlite3Builder
impl Sqlite3Builder
Sourcepub fn select_from<S: ToString>(table: S) -> Self
pub fn select_from<S: ToString>(table: S) -> Self
Create SELECT query. You may specify comma separted list of tables.
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 tableSourcepub fn select_values<S: ToString>(values: &[S]) -> Self
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", "e("100")])
.sql()?;
assert_eq!("SELECT 10, '100';", &sql);
// add ^^^^^^^^^
// here valuesSourcepub fn insert_into<S: ToString>(table: S) -> Self
pub fn insert_into<S: ToString>(table: S) -> Self
Create INSERT query.
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 tableSourcepub fn update_table<S: ToString>(table: S) -> Self
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 tableSourcepub fn delete_from<S: ToString>(table: S) -> Self
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 tableSourcepub fn natural(&mut self) -> &mut Self
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 ^^^^^^^Sourcepub fn left(&mut self) -> &mut Self
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 ^^^^Sourcepub fn left_outer(&mut self) -> &mut Self
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 ^^^^^^^^^^Sourcepub fn right(&mut self) -> &mut Self
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 ^^^^^Sourcepub fn inner(&mut self) -> &mut Self
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 ^^^^^Sourcepub fn cross(&mut self) -> &mut Self
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 ^^^^^Sourcepub fn join<S: ToString>(&mut self, table: S) -> &mut Self
pub fn join<S: ToString>(&mut self, table: S) -> &mut Self
Join with table.
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 tableSourcepub fn on<S: ToString>(&mut self, constraint: S) -> &mut Self
pub fn on<S: ToString>(&mut self, constraint: S) -> &mut Self
Join constraint to the last JOIN part.
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 constraintSourcepub fn distinct(&mut self) -> &mut Self
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 ^^^^^^^^Sourcepub fn fields<S: ToString>(&mut self, fields: &[S]) -> &mut Self
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 fieldsSourcepub fn set_fields<S: ToString>(&mut self, fields: &[S]) -> &mut Self
pub fn set_fields<S: ToString>(&mut self, fields: &[S]) -> &mut Self
Replace fields.
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);Sourcepub fn field<S: ToString>(&mut self, field: S) -> &mut Self
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 fieldSourcepub fn set_field<S: ToString>(&mut self, field: S) -> &mut Self
pub fn set_field<S: ToString>(&mut self, field: S) -> &mut Self
Replace fields with choosed one.
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);Sourcepub fn set<S, T>(&mut self, field: S, value: T) -> &mut Self
pub fn set<S, T>(&mut self, field: S, value: T) -> &mut Self
Add SET part (for UPDATE).
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 valueSourcepub fn set_str<S, T>(&mut self, field: S, value: T) -> &mut Self
pub fn set_str<S, T>(&mut self, field: S, value: T) -> &mut Self
Add SET part with escaped string value (for UPDATE).
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 valueSourcepub fn values<S: ToString>(&mut self, values: &[S]) -> &mut Self
pub fn values<S: ToString>(&mut self, values: &[S]) -> &mut Self
Add VALUES part (for INSERT).
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 valuesSourcepub fn select<S: ToString>(&mut self, query: S) -> &mut Self
pub fn select<S: ToString>(&mut self, query: S) -> &mut Self
Add SELECT part (for INSERT).
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 querySourcepub fn group_by<S: ToString>(&mut self, field: S) -> &mut Self
pub fn group_by<S: ToString>(&mut self, field: S) -> &mut Self
Add GROUP BY part.
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 fieldSourcepub fn having<S: ToString>(&mut self, cond: S) -> &mut Self
pub fn having<S: ToString>(&mut self, cond: S) -> &mut Self
Add HAVING condition.
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 condSourcepub fn and_where<S: ToString>(&mut self, cond: S) -> &mut Self
pub fn and_where<S: ToString>(&mut self, cond: S) -> &mut Self
Add WHERE condition.
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 condSourcepub fn and_where_eq<S, T>(&mut self, field: S, value: T) -> &mut Self
pub fn and_where_eq<S, T>(&mut self, field: S, value: T) -> &mut Self
Add WHERE condition for equal parts.
extern crate sqlite3builder;
use sqlite3builder::{Sqlite3Builder, quote};
let sql = Sqlite3Builder::select_from("books")
.field("price")
.and_where_eq("title", "e("Harry Potter and the Philosopher's Stone"))
.sql()?;
assert_eq!("SELECT price FROM books WHERE title = 'Harry Potter and the Philosopher''s Stone';", &sql);
// add ^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// here field valueSourcepub fn and_where_ne<S, T>(&mut self, field: S, value: T) -> &mut Self
pub fn and_where_ne<S, T>(&mut self, field: S, value: T) -> &mut Self
Add WHERE condition for non-equal parts.
extern crate sqlite3builder;
use sqlite3builder::{Sqlite3Builder, quote};
let sql = Sqlite3Builder::select_from("books")
.field("price")
.and_where_ne("title", "e("Harry Potter and the Philosopher's Stone"))
.sql()?;
assert_eq!("SELECT price FROM books WHERE title <> 'Harry Potter and the Philosopher''s Stone';", &sql);
// add ^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// here field valueSourcepub fn and_where_gt<S, T>(&mut self, field: S, value: T) -> &mut Self
pub fn and_where_gt<S, T>(&mut self, field: S, value: T) -> &mut Self
Add WHERE condition for field greater than value.
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 valueSourcepub fn and_where_ge<S, T>(&mut self, field: S, value: T) -> &mut Self
pub fn and_where_ge<S, T>(&mut self, field: S, value: T) -> &mut Self
Add WHERE condition for field not less than value.
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 valueSourcepub fn and_where_lt<S, T>(&mut self, field: S, value: T) -> &mut Self
pub fn and_where_lt<S, T>(&mut self, field: S, value: T) -> &mut Self
Add WHERE condition for field less than value.
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 valueSourcepub fn and_where_le<S, T>(&mut self, field: S, value: T) -> &mut Self
pub fn and_where_le<S, T>(&mut self, field: S, value: T) -> &mut Self
Add WHERE condition for field not greater than value.
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 valueSourcepub fn and_where_like<S, T>(&mut self, field: S, mask: T) -> &mut Self
pub fn and_where_like<S, T>(&mut self, field: S, mask: T) -> &mut Self
Add WHERE LIKE condition.
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 maskSourcepub fn and_where_like_right<S, T>(&mut self, field: S, mask: T) -> &mut Self
pub fn and_where_like_right<S, T>(&mut self, field: S, mask: T) -> &mut Self
Add WHERE LIKE %condition.
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 maskSourcepub fn and_where_like_left<S, T>(&mut self, field: S, mask: T) -> &mut Self
pub fn and_where_like_left<S, T>(&mut self, field: S, mask: T) -> &mut Self
Add WHERE LIKE condition%.
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 maskSourcepub fn and_where_like_any<S, T>(&mut self, field: S, mask: T) -> &mut Self
pub fn and_where_like_any<S, T>(&mut self, field: S, mask: T) -> &mut Self
Add WHERE LIKE %condition%.
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 maskSourcepub fn and_where_not_like<S, T>(&mut self, field: S, mask: T) -> &mut Self
pub fn and_where_not_like<S, T>(&mut self, field: S, mask: T) -> &mut Self
Add WHERE NOT LIKE condition.
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 maskSourcepub fn and_where_not_like_right<S, T>(&mut self, field: S, mask: T) -> &mut Self
pub fn and_where_not_like_right<S, T>(&mut self, field: S, mask: T) -> &mut Self
Add WHERE NOT LIKE %condition.
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 maskSourcepub fn and_where_not_like_left<S, T>(&mut self, field: S, mask: T) -> &mut Self
pub fn and_where_not_like_left<S, T>(&mut self, field: S, mask: T) -> &mut Self
Add WHERE NOT LIKE condition%.
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 maskSourcepub fn and_where_not_like_any<S, T>(&mut self, field: S, mask: T) -> &mut Self
pub fn and_where_not_like_any<S, T>(&mut self, field: S, mask: T) -> &mut Self
Add WHERE NOT LIKE %condition%.
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 maskSourcepub fn and_where_is_null<S: ToString>(&mut self, field: S) -> &mut Self
pub fn and_where_is_null<S: ToString>(&mut self, field: S) -> &mut Self
Add WHERE IS NULL condition.
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 fieldSourcepub fn and_where_is_not_null<S: ToString>(&mut self, field: S) -> &mut Self
pub fn and_where_is_not_null<S: ToString>(&mut self, field: S) -> &mut Self
Add WHERE IS NOT NULL condition.
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 fieldSourcepub fn or_where<S: ToString>(&mut self, cond: S) -> &mut Self
pub fn or_where<S: ToString>(&mut self, cond: S) -> &mut Self
Add OR condition to the last WHERE condition.
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 condSourcepub fn or_where_eq<S, T>(&mut self, field: S, value: T) -> &mut Self
pub fn or_where_eq<S, T>(&mut self, field: S, value: T) -> &mut Self
Add OR condition of equal parts to the last WHERE condition.
extern crate sqlite3builder;
use sqlite3builder::{Sqlite3Builder, quote};
let sql = Sqlite3Builder::select_from("books")
.field("price")
.and_where_eq("title", "e("Harry Potter and the Philosopher's Stone"))
.or_where_eq("title", "e("Harry Potter and the Chamber of Secrets"))
.sql()?;
assert_eq!("SELECT price FROM books WHERE title = 'Harry Potter and the Philosopher''s Stone' OR title = 'Harry Potter and the Chamber of Secrets';", &sql);
// add ^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// here field valueSourcepub fn or_where_ne<S, T>(&mut self, field: S, value: T) -> &mut Self
pub fn or_where_ne<S, T>(&mut self, field: S, value: T) -> &mut Self
Add OR condition of non-equal parts to the last WHERE condition.
extern crate sqlite3builder;
use sqlite3builder::{Sqlite3Builder, quote};
let sql = Sqlite3Builder::select_from("books")
.field("price")
.or_where_ne("title", "e("Harry Potter and the Philosopher's Stone"))
.or_where_ne("title", "e("Harry Potter and the Chamber of Secrets"))
.sql()?;
assert_eq!("SELECT price FROM books WHERE title <> 'Harry Potter and the Philosopher''s Stone' OR title <> 'Harry Potter and the Chamber of Secrets';", &sql);
// add ^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// here field value field valueSourcepub fn or_where_gt<S, T>(&mut self, field: S, value: T) -> &mut Self
pub fn or_where_gt<S, T>(&mut self, field: S, value: T) -> &mut Self
Add OR condition for field greater than value to the last WHERE condition.
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 valueSourcepub fn or_where_ge<S, T>(&mut self, field: S, value: T) -> &mut Self
pub fn or_where_ge<S, T>(&mut self, field: S, value: T) -> &mut Self
Add OR condition for field not less than value to the last WHERE condition.
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 valueSourcepub fn or_where_lt<S, T>(&mut self, field: S, value: T) -> &mut Self
pub fn or_where_lt<S, T>(&mut self, field: S, value: T) -> &mut Self
Add OR condition for field less than value to the last WHERE condition.
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 valueSourcepub fn or_where_le<S, T>(&mut self, field: S, value: T) -> &mut Self
pub fn or_where_le<S, T>(&mut self, field: S, value: T) -> &mut Self
Add OR condition for field not greater than value to the last WHERE condition.
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 valueSourcepub fn or_where_like<S, T>(&mut self, field: S, mask: T) -> &mut Self
pub fn or_where_like<S, T>(&mut self, field: S, mask: T) -> &mut Self
Add OR LIKE condition to the last WHERE condition.
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 maskSourcepub fn or_where_like_right<S, T>(&mut self, field: S, mask: T) -> &mut Self
pub fn or_where_like_right<S, T>(&mut self, field: S, mask: T) -> &mut Self
Add OR LIKE condition to the last WHERE %condition.
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 maskSourcepub fn or_where_like_left<S, T>(&mut self, field: S, mask: T) -> &mut Self
pub fn or_where_like_left<S, T>(&mut self, field: S, mask: T) -> &mut Self
Add OR LIKE condition to the last WHERE condition%.
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 maskSourcepub fn or_where_like_any<S, T>(&mut self, field: S, mask: T) -> &mut Self
pub fn or_where_like_any<S, T>(&mut self, field: S, mask: T) -> &mut Self
Add OR LIKE condition to the last WHERE %condition%.
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 maskSourcepub fn or_where_not_like<S, T>(&mut self, field: S, mask: T) -> &mut Self
pub fn or_where_not_like<S, T>(&mut self, field: S, mask: T) -> &mut Self
Add OR NOT LIKE condition to the last WHERE condition.
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 maskSourcepub fn or_where_not_like_right<S, T>(&mut self, field: S, mask: T) -> &mut Self
pub fn or_where_not_like_right<S, T>(&mut self, field: S, mask: T) -> &mut Self
Add OR NOT LIKE condition to the last WHERE %condition.
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 maskSourcepub fn or_where_not_like_left<S, T>(&mut self, field: S, mask: T) -> &mut Self
pub fn or_where_not_like_left<S, T>(&mut self, field: S, mask: T) -> &mut Self
Add OR NOT LIKE condition to the last WHERE condition%.
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 maskSourcepub fn or_where_not_like_any<S, T>(&mut self, field: S, mask: T) -> &mut Self
pub fn or_where_not_like_any<S, T>(&mut self, field: S, mask: T) -> &mut Self
Add OR NOT LIKE condition to the last WHERE %condition%.
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 maskSourcepub fn or_where_is_null<S: ToString>(&mut self, field: S) -> &mut Self
pub fn or_where_is_null<S: ToString>(&mut self, field: S) -> &mut Self
Add OR IS NULL condition to the last WHERE condition.
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 fieldSourcepub fn or_where_is_not_null<S: ToString>(&mut self, field: S) -> &mut Self
pub fn or_where_is_not_null<S: ToString>(&mut self, field: S) -> &mut Self
Add OR IS NOT NULL condition to the last WHERE condition.
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 fieldSourcepub fn union<S: ToString>(&mut self, query: S) -> &mut Self
pub fn union<S: ToString>(&mut self, query: S) -> &mut Self
Union query with subquery. ORDER BY must be in the last subquery.
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 querySourcepub fn union_all<S: ToString>(&mut self, query: S) -> &mut Self
pub fn union_all<S: ToString>(&mut self, query: S) -> &mut Self
Union query with all subquery. ORDER BY must be in the last subquery.
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 querySourcepub fn order_by<S: ToString>(&mut self, field: S, desc: bool) -> &mut Self
pub fn order_by<S: ToString>(&mut self, field: S, desc: bool) -> &mut Self
Add ORDER BY.
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 fieldSourcepub fn order_asc<S: ToString>(&mut self, field: S) -> &mut Self
pub fn order_asc<S: ToString>(&mut self, field: S) -> &mut Self
Add ORDER BY ASC.
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 fieldSourcepub fn order_desc<S: ToString>(&mut self, field: S) -> &mut Self
pub fn order_desc<S: ToString>(&mut self, field: S) -> &mut Self
Add ORDER BY DESC.
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 fieldSourcepub fn limit<S: ToString>(&mut self, limit: S) -> &mut Self
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 limitSourcepub fn offset<S: ToString>(&mut self, offset: S) -> &mut Self
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 offsetSourcepub fn sql(&self) -> Result<String, Box<dyn Error>>
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);Sourcepub fn subquery(&self) -> Result<String, Box<dyn Error>>
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);Sourcepub fn subquery_as<S: ToString>(
&self,
name: S,
) -> Result<String, Box<dyn Error>>
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);Sourcepub fn query(&self) -> Result<String, Box<dyn Error>>
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);Sourcepub fn query_values(&self) -> Result<String, Box<dyn Error>>
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", "e("100")])
.query_values()?;
assert_eq!("SELECT 10, '100'", &values);Sourcepub fn exec(
&self,
conn: &PooledConnection<SqliteConnectionManager>,
) -> Result<(), Box<dyn Error>>
pub fn exec( &self, conn: &PooledConnection<SqliteConnectionManager>, ) -> Result<(), Box<dyn Error>>
Execute request
Sourcepub fn get(
&self,
conn: &PooledConnection<SqliteConnectionManager>,
) -> Result<Vec<Vec<JValue>>, Box<dyn Error>>
pub fn get( &self, conn: &PooledConnection<SqliteConnectionManager>, ) -> Result<Vec<Vec<JValue>>, Box<dyn Error>>
Execute and return all data
Sourcepub fn get_row(
&self,
conn: &PooledConnection<SqliteConnectionManager>,
) -> Result<Vec<JValue>, Box<dyn Error>>
pub fn get_row( &self, conn: &PooledConnection<SqliteConnectionManager>, ) -> Result<Vec<JValue>, Box<dyn Error>>
Execute and return first row
Sourcepub fn get_value(
&self,
conn: &PooledConnection<SqliteConnectionManager>,
) -> Result<JValue, Box<dyn Error>>
pub fn get_value( &self, conn: &PooledConnection<SqliteConnectionManager>, ) -> Result<JValue, Box<dyn Error>>
Execute and return first value
Sourcepub fn get_int(
&self,
conn: &PooledConnection<SqliteConnectionManager>,
) -> Result<i64, Box<dyn Error>>
pub fn get_int( &self, conn: &PooledConnection<SqliteConnectionManager>, ) -> Result<i64, Box<dyn Error>>
Execute and return first integer value
Sourcepub fn get_str(
&self,
conn: &PooledConnection<SqliteConnectionManager>,
) -> Result<String, Box<dyn Error>>
pub fn get_str( &self, conn: &PooledConnection<SqliteConnectionManager>, ) -> Result<String, Box<dyn Error>>
Execute and return first string value
Sourcepub fn get_cursor<'a>(
&'a self,
conn: &'a PooledConnection<SqliteConnectionManager>,
) -> Result<Cursor<'a>, Box<dyn Error>>
pub fn get_cursor<'a>( &'a self, conn: &'a PooledConnection<SqliteConnectionManager>, ) -> Result<Cursor<'a>, Box<dyn Error>>
Get cursor for request