pub struct Select { /* private fields */ }
Expand description

SELECT statement, possibly with additional clauses.

Finalize and turn into String by calling to_string.

See select docs for more details and examples.

Implementations§

source§

impl Select

source

pub fn and_select( self, expressions: impl IntoIteratorOfSameType<Expression> ) -> Self

Add more expressions to be selected

use scooby::postgres::select;

let sql = select(("id", "name"))
    .from("Person")
    .and_select("age")
    .and_select(("occupation_id", "city_id"))
    .to_string();

assert_eq!(sql, "SELECT id, name, age, occupation_id, city_id FROM Person");
source

pub fn all(self) -> Self

Explicitly specify SELECT ALL, i.e. non-distinct query

use scooby::postgres::select;
let sql = select("*").all().from("City").to_string();
assert_eq!(sql, "SELECT ALL * FROM City");
source

pub fn distinct(self) -> Self

Set a simple DISTINCT clause

use scooby::postgres::select;
let sql = select("*").distinct().from("City").to_string();
assert_eq!(sql, "SELECT DISTINCT * FROM City");
source

pub fn distinct_on( self, expressions: impl IntoIteratorOfSameType<Expression> ) -> Self

Set a DISTINCT ON (...) clause

use scooby::postgres::select;
let sql = select("*").distinct_on("id").from("City").to_string();
assert_eq!(sql, "SELECT DISTINCT ON (id) * FROM City");
source

pub fn from(self, from: impl IntoIteratorOfSameType<FromItem>) -> Self

Set or add more items in a FROM clause

Import Joinable for convenient joins.

You can pass subselects as well, so long as you make sure they’re aliased.

use scooby::postgres::select;
let sql = select("*").from("City").from(("Other", "Third")).to_string();
assert_eq!(sql, "SELECT * FROM City, Other, Third");
use scooby::postgres::{select, Joinable};

let sql = select("col1")
    .from(
        "Person p"
            .inner_join("City c")
            .on("c.id = p.city_id")
            .left_join("Belonging b")
            .on("p.id = b.person_id"),
    )
    .to_string();

assert_eq!(sql, "SELECT col1 FROM Person p INNER JOIN City c ON c.id = p.city_id LEFT JOIN Belonging b ON p.id = b.person_id");
use scooby::postgres::{select, Aliasable};

let sql = select("*")
    .from(select("id").from("City").as_("x"))
    .to_string();

assert_eq!(sql, "SELECT * FROM (SELECT id FROM City) AS x");
source

pub fn where_(self, conditions: impl IntoIteratorOfSameType<Condition>) -> Self

Add one or more WHERE conditions, AND’ed together with themselves and existing conditions.

use scooby::postgres::select;

let sql = select("col1")
    .from("Dummy")
    .where_(("x > 1", "y > 1"))
    .where_("z > 1")
    .to_string();

assert_eq!(sql, "SELECT col1 FROM Dummy WHERE x > 1 AND y > 1 AND z > 1");
source

pub fn group_by( self, groupings: impl IntoIteratorOfSameType<Expression> ) -> Self

Add one or more expressions in a GROUP BY clause

use scooby::postgres::select;

let sql = select(("country_id", "COUNT(*)"))
    .from("City")
    .group_by("country_id")
    .to_string();

assert_eq!(sql, "SELECT country_id, COUNT(*) FROM City GROUP BY country_id");
source

pub fn having(self, conditions: impl IntoIteratorOfSameType<Condition>) -> Self

Add one or more expressions in a HAVING clause

use scooby::postgres::select;

let sql = select(("country_id", "COUNT(*)"))
    .from("City")
    .group_by("country_id")
    .having("COUNT(*) > 10000")
    .to_string();

assert_eq!(sql, "SELECT country_id, COUNT(*) FROM City GROUP BY country_id HAVING COUNT(*) > 10000");
source

pub fn order_by(self, order_bys: impl IntoIteratorOfSameType<OrderBy>) -> Self

Set or add more sort expressions in an ORDER BY clause.

Import Orderable for convenient order specifications.

use scooby::postgres::select;
let sql = select("*").from("City").order_by("id").to_string();
assert_eq!(sql, "SELECT * FROM City ORDER BY id");
use scooby::postgres::{select, Orderable};

let sql = select("*")
    .from("City")
    .order_by(("last_modified".desc(), "id".desc()))
    .to_string();

assert_eq!(sql, "SELECT * FROM City ORDER BY last_modified DESC, id DESC");
source

pub fn limit(self, limit: impl Into<Limit>) -> Self

Set a LIMIT clause

use scooby::postgres::select;
let sql = select("*").from("City").limit(10).to_string();
assert_eq!(sql, "SELECT * FROM City LIMIT 10");
source

pub fn offset(self, offset: impl Into<Offset>) -> Self

Set an OFFSET clause

use scooby::postgres::select;
let sql = select("*").from("City").offset(10).to_string();
assert_eq!(sql, "SELECT * FROM City OFFSET 10");

Trait Implementations§

source§

impl Aliasable for Select

source§

fn as_(self, alias: &str) -> Alias

source§

impl Clone for Select

source§

fn clone(&self) -> Select

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 Default for Select

source§

fn default() -> Select

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§

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.