Struct geekorm_core::queries::builder::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::{QueryOrder, PrimaryKeyInteger};
use geekorm::prelude::*;

#[derive(Debug, Default, Clone, GeekTable)]
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::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::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() -> Query

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 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 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 build(&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> 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> ToOwned for T
where T: Clone,

§

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

§

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

§

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.