Skip to main content

QueryBuilder

Struct QueryBuilder 

Source
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

Source

pub fn select(columns: &[&str]) -> Self

Start a SELECT query with the given columns.

Source

pub fn select_all() -> Self

Start a SELECT * query.

Source

pub fn distinct(self) -> Self

Add DISTINCT to the SELECT clause.

Source

pub fn from(self, table: &str) -> Self

Set the FROM table.

Source

pub fn join(self, table: &str, on: &str) -> Self

Add an INNER JOIN.

Source

pub fn left_join(self, table: &str, on: &str) -> Self

Add a LEFT JOIN.

Source

pub fn right_join(self, table: &str, on: &str) -> Self

Add a RIGHT JOIN.

Source

pub fn where_clause(self, condition: &str) -> Self

Add a WHERE condition. Multiple calls are combined with AND.

Source

pub fn group_by(self, expr: &str) -> Self

Add a GROUP BY expression.

Source

pub fn having(self, condition: &str) -> Self

Set the HAVING clause.

Source

pub fn order_by(self, expr: &str, ascending: bool) -> Self

Add an ORDER BY expression. ascending = true → ASC, false → DESC.

Source

pub fn limit(self, n: u64) -> Self

Set LIMIT.

Source

pub fn offset(self, n: u64) -> Self

Set OFFSET.

Source

pub fn build(self) -> Result<String, String>

Build the final SQL string.

Returns Err if no FROM table has been set.

Source

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.

Source

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.

Source

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

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

pub fn delete(table: &str, where_clause: Option<&str>) -> String

Build a DELETE FROM … [WHERE …] statement.

§Example
use oxisql_parse::QueryBuilder;

let sql = QueryBuilder::delete("users", Some("id = 99"));
assert_eq!(sql, "DELETE FROM users WHERE id = 99");

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 = !

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

fn try_from(value: U) -> Result<T, !>

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.