pub struct Select<'a> { /* private fields */ }
Expand description

Builder to contruct a Select command

Implementations§

source§

impl<'a> Select<'a>

source

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

The same as where_clause method, useful to write more idiomatic SQL query

Examples
use sql_query_builder as sql;

let select = sql::Select::new()
  .where_clause("login = foo")
  .and("active = true");
source

pub fn as_string(&self) -> String

Gets the current state of the Select returns it as string

Examples
use sql_query_builder as sql;

let query = sql::Select::new()
  .select("id")
  .from("users")
  .where_clause("login = 'foo'")
  .as_string();

Output

SELECT id FROM users WHERE login = 'foo'
source

pub fn debug(self) -> Self

Prints the current state of the Select into console output in a more ease to read version. This method is useful to debug complex queries or just to print the generated SQL while you type

Examples
use sql_query_builder as sql;

let select = sql::Select::new()
  .select("*")
  .from("users")
  .where_clause("login = foo")
  .and("active = true")
  .debug();

Output

SELECT *
FROM users
WHERE login = foo AND active = true

You can debug different parts of the select putting it in another position

Examples
use sql_query_builder as sql;

let select_query = sql::Select::new()
  .select("*")
  .from("users")
  .debug()
  .where_clause("login = foo")
  .and("active = true")
  .as_string();

Output

SELECT *
FROM users
source

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

The from clause

source

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

The group by clause

source

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

The having clause

source

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

The cross join clause

source

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

The inner join clause

source

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

The left join clause

source

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

The right join clause

source

pub fn limit(self, num: &'a str) -> Self

The limit clause. This method overrides the previous value

Examples
use sql_query_builder as sql;

let select = sql::Select::new()
  .limit("123");

let select = sql::Select::new()
  .limit("1000")
  .limit("123");
source

pub fn new() -> Self

Create Select’s instance

source

pub fn offset(self, num: &'a str) -> Self

The offset clause. This method overrides the previous value

Examples
use sql_query_builder as sql;

let select = sql::Select::new()
  .offset("1500");

let select = sql::Select::new()
  .offset("1000")
  .offset("1500");
source

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

The order by clause

source

pub fn print(self) -> Self

Prints the current state of the Select into console output similar to debug method, the difference is that this method prints in one line.

source

pub fn raw(self, raw_sql: &str) -> Self

Adds at the beginning a raw SQL query.

Examples
use sql_query_builder as sql;

let raw_query = "select * from users u inner join address addr on u.login = addr.owner_login";
let select_query = sql::Select::new()
  .raw(raw_query)
  .where_clause("u.login = foo")
  .as_string();

Output

select * from users u inner join address addr on u.login = addr.owner_login
WHERE u.login = foo
source

pub fn raw_after(self, clause: SelectClause, raw_sql: &str) -> Self

Adds a raw SQL query after a specified clause.

Examples
use sql_query_builder as sql;

let raw_join = "inner join address addr on u.login = addr.owner_login";
let select_query = sql::Select::new()
  .select("*")
  .from("users u")
  .raw_after(sql::SelectClause::From, raw_join)
  .where_clause("u.login = foo")
  .as_string();

Output

SELECT *
FROM users u
inner join address addr on u.login = addr.owner_login
WHERE u.login = foo
source

pub fn raw_before(self, clause: SelectClause, raw_sql: &str) -> Self

Adds a raw SQL query before a specified clause.

Examples
use sql_query_builder as sql;

let raw_query = "from users u inner join address addr on u.login = addr.owner_login";
let select_query = sql::Select::new()
  .select("*")
  .raw_before(sql::SelectClause::Where, raw_query)
  .where_clause("u.login = foo")
  .as_string();

Output

SELECT *
from users u inner join address addr on u.login = addr.owner_login
WHERE u.login = foo
source

pub fn select(self, column: &str) -> Self

The select clause

source

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

The where clause

Examples
use sql_query_builder as sql;

let select = sql::Select::new()
  .from("users")
  .where_clause("login = $1");
source§

impl<'a> Select<'a>

source

pub fn except(self, select: Self) -> Self

The except clause, this method can be used enabling the feature flag postgresql

source

pub fn intersect(self, select: Self) -> Self

The intersect clause, this method can be used enabling the feature flag postgresql

source

pub fn union(self, select: Self) -> Self

The union clause, this method can be used enabling the feature flag postgresql

source

pub fn with(self, name: &'a str, query: impl WithQuery + 'static) -> Self

The with clause, this method can be used enabling the feature flag postgresql

Examples
use sql_query_builder as sql;

let logins = sql::Select::new().select("login").from("users").where_clause("id in ($1)");
let select = sql::Select::new()
  .with("logins", logins)
  .select("name, price")
  .from("orders")
  .where_clause("owner_login in (select * from logins)")
  .debug();

Output

WITH logins AS (
  SELECT login
  FROM users
  WHERE id in ($1)
)
SELECT name, price
FROM orders
WHERE owner_login in (select * from active_users)

Trait Implementations§

source§

impl<'a> Clone for Select<'a>

source§

fn clone(&self) -> Select<'a>

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 Select<'_>

source§

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

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

impl<'a> Default for Select<'a>

source§

fn default() -> Select<'a>

Returns the “default value” for a type. Read more
source§

impl Display for Select<'_>

source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> !RefUnwindSafe for Select<'a>

§

impl<'a> !Send for Select<'a>

§

impl<'a> !Sync for Select<'a>

§

impl<'a> Unpin for Select<'a>

§

impl<'a> !UnwindSafe for Select<'a>

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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 Twhere 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> ToString for Twhere T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for Twhere 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 Twhere 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.