pub struct QueryBuilder { /* private fields */ }Expand description
Fluent SQL query builder.
§Example
use oxisql_parse::QueryBuilder;
let sql = QueryBuilder::select(&["id", "name", "email"])
.from("users")
.join("orders", "users.id = orders.user_id")
.where_clause("users.active = TRUE")
.where_clause("orders.status = 'pending'")
.order_by("users.name", true)
.limit(10)
.offset(20)
.build()
.expect("valid query");
assert!(sql.contains("SELECT id, name, email"));Implementations§
Source§impl QueryBuilder
impl QueryBuilder
Sourcepub fn select_all() -> Self
pub fn select_all() -> Self
Start a SELECT * query.
Sourcepub fn right_join(self, table: &str, on: &str) -> Self
pub fn right_join(self, table: &str, on: &str) -> Self
Add a RIGHT JOIN.
Sourcepub fn where_clause(self, condition: &str) -> Self
pub fn where_clause(self, condition: &str) -> Self
Add a WHERE condition. Multiple calls are combined with AND.
Sourcepub fn order_by(self, expr: &str, ascending: bool) -> Self
pub fn order_by(self, expr: &str, ascending: bool) -> Self
Add an ORDER BY expression. ascending = true → ASC, false → DESC.
Sourcepub fn build(self) -> Result<String, String>
pub fn build(self) -> Result<String, String>
Build the final SQL string.
Returns Err if no FROM table has been set.
Sourcepub fn build_and_parse(self) -> Result<Vec<Statement>, String>
pub fn build_and_parse(self) -> Result<Vec<Statement>, String>
Build and immediately parse the SQL to verify it is syntactically valid.
Returns Err if building fails or if the resulting SQL cannot be parsed.
Sourcepub fn build_ref(&self) -> Result<String, String>
pub fn build_ref(&self) -> Result<String, String>
Build the SQL string without consuming self.
Internally clones the builder fields and delegates to Self::build.
Prefer build when the builder is no longer needed.
Sourcepub fn insert(table: &str, columns: &[&str], values: &[&str]) -> String
pub fn insert(table: &str, columns: &[&str], values: &[&str]) -> String
Build an INSERT INTO … VALUES (…) statement.
§Example
use oxisql_parse::QueryBuilder;
let sql = QueryBuilder::insert("users", &["id", "name"], &["1", "'Alice'"]);
assert_eq!(sql, "INSERT INTO users (id, name) VALUES (1, 'Alice')");Sourcepub fn update(
table: &str,
sets: &[(&str, &str)],
where_clause: Option<&str>,
) -> String
pub fn update( table: &str, sets: &[(&str, &str)], where_clause: Option<&str>, ) -> String
Build an UPDATE … SET … [WHERE …] statement.
§Example
use oxisql_parse::QueryBuilder;
let sql = QueryBuilder::update(
"users",
&[("name", "'Bob'"), ("active", "FALSE")],
Some("id = 42"),
);
assert!(sql.starts_with("UPDATE users SET"));
assert!(sql.contains("WHERE id = 42"));Auto Trait Implementations§
impl Freeze for QueryBuilder
impl RefUnwindSafe for QueryBuilder
impl Send for QueryBuilder
impl Sync for QueryBuilder
impl Unpin for QueryBuilder
impl UnsafeUnpin for QueryBuilder
impl UnwindSafe for QueryBuilder
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more