Struct QueryBuilder

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

The QueryBuilder is how you can build dynamically queries using the builder pattern.

§Features

There is a number a features that are supported by the QueryBuilder:

  • All Major Query Types
    • Select: Build a select query
    • Insert: Build an insert query
    • Update: Build an update query
    • Delete: Build a delete query
  • Conditions: Build a query with conditions
    • Where: Build a query with where conditions
    • Order By: Build a query with order by conditions
    • Limit: Build a query with a limit
  • Joins: Build a query with joins 2 tables
    • Only Inner Joins are supported currently

§Example

use geekorm::prelude::*;

#[derive(Table, Debug, Default, Clone, serde::Serialize, serde::Deserialize)]
pub struct Users {
    pub id: PrimaryKeyInteger,
    pub username: String,
    pub age: i32,
    pub postcode: Option<String>,
}

// Build a query to create a new table
let create_query = Users::query_create().build()
    .expect("Failed to build create query");
println!("Create Query :: {}", create_query);

// Build a query to select rows from the table
let select_query = Users::query_select()
    .where_eq("username", "geekmasher")
    .order_by("age", QueryOrder::Asc)
    .build()
    .expect("Failed to build select query");
println!("Select Query :: {}", select_query);
// Output:
// SELECT (...) FROM User WHERE username = ? ORDER BY age ASC;

Implementations§

Source§

impl QueryBuilder

Source

pub fn new() -> Self

Create a new QueryBuilder

Source

pub fn select() -> QueryBuilder

Build a select query

Source

pub fn create() -> QueryBuilder

Build a create query

Source

pub fn all() -> QueryBuilder

Build a “get all rows” query

Source

pub fn insert() -> QueryBuilder

Build an insert query

Source

pub fn update() -> QueryBuilder

Build an update query

Source

pub fn delete() -> QueryBuilder

Build a delete query

Source

pub fn table(self, table: Table) -> Self

Set the table for the query builder

Source

pub fn columns(self, columns: Vec<&str>) -> Self

Set the columns for the query builder

Source

pub fn add_value(self, column: &str, value: impl Into<Value>) -> Self

Add a value to the list of values for parameterized queries

Source

pub fn and(self) -> Self

Add an AND condition to the where clause

Source

pub fn or(self) -> Self

Add an OR condition to the where clause

Source

pub fn where_eq(self, column: &str, value: impl Into<Value>) -> Self

Where clause for equals

Source

pub fn where_ne(self, column: &str, value: impl Into<Value>) -> Self

Where clause for not equals

Source

pub fn where_like(self, column: &str, value: impl Into<Value>) -> Self

Where clause for like

Source

pub fn where_gt(self, column: &str, value: impl Into<Value>) -> Self

Where clause for greater than

Source

pub fn where_lt(self, column: &str, value: impl Into<Value>) -> Self

Where clause for less than

Source

pub fn where_gte(self, column: &str, value: impl Into<Value>) -> Self

Where clause for greater than or equal to

Source

pub fn where_lte(self, column: &str, value: impl Into<Value>) -> Self

Where clause for less than or equal to

Source

pub fn filter(self, fields: Vec<(&str, impl Into<Value>)>) -> Self

Filter the query by multiple fields

Source

pub fn order_by(self, column: &str, order: QueryOrder) -> Self

Order the query by a particular column

Source

pub fn join(self, table: Table) -> Self

Adds a table to join with the current table

Note: GeekOrm only joins tables with the INNER JOIN clause and primary keys

Source

pub fn count(self) -> Self

Count the number of rows in the query

Source

pub fn limit(self, limit: usize) -> Self

Add a limit to the query

Source

pub fn offset(self, offset: usize) -> Self

Add an offset to the query

Source

pub fn page(self, page: &Page) -> Self

Add a page to the query

Source

pub fn build(&mut self) -> Result<Query, Error>

Build a Query from the QueryBuilder and perform some checks

Trait Implementations§

Source§

impl Clone for QueryBuilder

Source§

fn clone(&self) -> QueryBuilder

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

impl Debug for QueryBuilder

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for QueryBuilder

Source§

fn default() -> QueryBuilder

Returns the “default value” for a type. 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> FromRef<T> for T
where T: Clone,

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoRequest<T> for T

Source§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> MaybeSendSync for T