Struct Select

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

A builder for a SELECT statement.

Implementations§

Source§

impl<'a> Select<'a>

Source

pub fn from_table<T>(table: T) -> Self
where T: Into<Table<'a>>,

Creates a new SELECT statement for the given table.

let query = Select::from_table("users");
let (sql, _) = renderer::Postgres::build(query);

assert_eq!(r#"SELECT "users".* FROM "users""#, sql);

The table can be in multiple parts, defining the schema.

let query = Select::from_table(("crm", "users"));
let (sql, _) = renderer::Postgres::build(query);

assert_eq!(r#"SELECT "crm"."users".* FROM "crm"."users""#, sql);

Selecting from a nested SELECT.

let mut inner_select = Select::default();
inner_select.value(1);

let select = Table::from(inner_select).alias("num");
let query = Select::from_table(select.alias("num"));
let (sql, params) = renderer::Postgres::build(query);

assert_eq!(r#"SELECT "num".* FROM (SELECT $1) AS "num""#, sql);
assert_eq!(vec![Value::from(1)], params);
Source

pub fn and_from<T>(&mut self, table: T)
where T: Into<Table<'a>>,

Adds a table to be selected.

let mut query = Select::from_table("users");

let mut inner_select = Select::default();
inner_select.value(1);

query.and_from(Table::from(inner_select).alias("num"));
query.column(("users", "name"));
query.value(Table::from("num").asterisk());

let (sql, _) = renderer::Postgres::build(query);

assert_eq!(r#"SELECT "users"."name", "num".* FROM "users", (SELECT $1) AS "num""#, sql);
Source

pub fn value<T>(&mut self, value: T)
where T: Into<Expression<'a>>,

Selects a static value as the column.

let mut query = Select::default();
query.value(1);

let (sql, params) = renderer::Postgres::build(query);

assert_eq!("SELECT $1", sql);
assert_eq!(vec![Value::from(1)], params);
Source

pub fn column<T>(&mut self, column: T)
where T: Into<Column<'a>>,

Adds a column to be selected.

let mut query = Select::from_table("users");

query.column("name");
query.column(("users", "id"));
query.column((("crm", "users"), "foo"));

let (sql, _) = renderer::Postgres::build(query);

assert_eq!(r#"SELECT "name", "users"."id", "crm"."users"."foo" FROM "users""#, sql);
Source

pub fn columns<T, C>(&mut self, columns: T)
where T: IntoIterator<Item = C>, C: Into<Column<'a>>,

A bulk method to select multiple values.

let mut query = Select::from_table("users");
query.columns(["foo", "bar"]);

let (sql, _) = renderer::Postgres::build(query);

assert_eq!(r#"SELECT "foo", "bar" FROM "users""#, sql);
Source

pub fn distinct(&mut self)

Adds DISTINCT to the select query.

let mut query = Select::from_table("users");

query.column("foo");
query.column("bar");
query.distinct();

let (sql, _) = renderer::Postgres::build(query);

assert_eq!(r#"SELECT DISTINCT "foo", "bar" FROM "users""#, sql);
Source

pub fn so_that<T>(&mut self, conditions: T)
where T: Into<ConditionTree<'a>>,

Adds WHERE conditions to the query, replacing the previous conditions. See Comparable for more examples.

let mut query = Select::from_table("users");
query.so_that("foo".equals("bar"));

let (sql, params) = renderer::Postgres::build(query);

assert_eq!(r#"SELECT "users".* FROM "users" WHERE "foo" = $1"#, sql);

assert_eq!(vec![
   Value::from("bar"),
], params);
Source

pub fn and_where<T>(&mut self, conditions: T)
where T: Into<ConditionTree<'a>>,

Adds an additional WHERE condition to the query combining the possible previous condition with AND. See Comparable for more examples.

let mut query = Select::from_table("users");

query.so_that("foo".equals("bar"));
query.and_where("lol".equals("wtf"));

let (sql, params) = renderer::Postgres::build(query);

assert_eq!(r#"SELECT "users".* FROM "users" WHERE ("foo" = $1 AND "lol" = $2)"#, sql);

assert_eq!(vec![
   Value::from("bar"),
   Value::from("wtf"),
], params);
Source

pub fn or_where<T>(&mut self, conditions: T)
where T: Into<ConditionTree<'a>>,

Adds an additional WHERE condition to the query combining the possible previous condition with OR. See Comparable for more examples.

let mut query = Select::from_table("users");

query.so_that("foo".equals("bar"));
query.or_where("lol".equals("wtf"));

let (sql, params) = renderer::Postgres::build(query);

assert_eq!(r#"SELECT "users".* FROM "users" WHERE ("foo" = $1 OR "lol" = $2)"#, sql);

assert_eq!(vec![
   Value::from("bar"),
   Value::from("wtf"),
], params);
Source

pub fn inner_join<J>(&mut self, join: J)
where J: Into<JoinData<'a>>,

Adds INNER JOIN clause to the query.

let join = Table::from("posts")
    .alias("p")
    .on(("p", "user_id").equals(Column::from(("users", "id"))));

let mut query = Select::from_table("users");
query.inner_join(join);

let (sql, _) = renderer::Postgres::build(query);

assert_eq!(
    r#"SELECT "users".* FROM "users" INNER JOIN "posts" AS "p" ON "p"."user_id" = "users"."id""#,
    sql
);
Source

pub fn left_join<J>(&mut self, join: J)
where J: Into<JoinData<'a>>,

Adds LEFT JOIN clause to the query.

let join = Table::from("posts")
   .alias("p")
   .on(("p", "visible").equals(true));

let mut query = Select::from_table("users");
query.left_join(join);

let (sql, params) = renderer::Postgres::build(query);

assert_eq!(
    r#"SELECT "users".* FROM "users" LEFT JOIN "posts" AS "p" ON "p"."visible" = $1"#,
    sql
);

assert_eq!(
    vec![
        Value::from(true),
    ],
    params
);
Source

pub fn right_join<J>(&mut self, join: J)
where J: Into<JoinData<'a>>,

Adds RIGHT JOIN clause to the query.

let join = Table::from("posts")
   .alias("p")
   .on(("p", "visible").equals(true));


let mut query = Select::from_table("users");
query.right_join(join);

let (sql, params) = renderer::Postgres::build(query);

assert_eq!(
    r#"SELECT "users".* FROM "users" RIGHT JOIN "posts" AS "p" ON "p"."visible" = $1"#,
    sql
);

assert_eq!(
    vec![
        Value::from(true),
    ],
    params
);
Source

pub fn full_join<J>(&mut self, join: J)
where J: Into<JoinData<'a>>,

Adds FULL JOIN clause to the query.

let join = Table::from("posts")
   .alias("p")
   .on(("p", "visible").equals(true));

let mut query = Select::from_table("users");
query.full_join(join);

let (sql, params) = renderer::Postgres::build(query);

assert_eq!(
    r#"SELECT "users".* FROM "users" FULL JOIN "posts" AS "p" ON "p"."visible" = $1"#,
    sql
);

assert_eq!(
    vec![
        Value::from(true),
    ],
    params
);
Source

pub fn order_by<T>(&mut self, value: T)
where T: IntoOrderDefinition<'a>,

Adds an ordering to the ORDER BY section.

let mut query = Select::from_table("users");
query.order_by("foo");
query.order_by("baz".ascend());
query.order_by("bar".descend());

let (sql, _) = renderer::Postgres::build(query);

assert_eq!(r#"SELECT "users".* FROM "users" ORDER BY "foo", "baz" ASC, "bar" DESC"#, sql);
Source

pub fn group_by<T>(&mut self, value: T)
where T: IntoGroupByDefinition<'a>,

Adds a grouping to the GROUP BY section.

This does not check if the grouping is actually valid in respect to aggregated columns.

let mut query = Select::from_table("users");

query.column("foo");
query.column("bar");
query.group_by("foo");
query.group_by("bar");

let (sql, _) = renderer::Postgres::build(query);

assert_eq!(r#"SELECT "foo", "bar" FROM "users" GROUP BY "foo", "bar""#, sql);
Source

pub fn having<T>(&mut self, conditions: T)
where T: Into<ConditionTree<'a>>,

Adds group conditions to a query. Should be combined together with a group_by statement.

let mut query = Select::from_table("users");

query.column("foo");
query.column("bar");
query.group_by("foo");
query.having("foo".greater_than(100));

let (sql, params) = renderer::Postgres::build(query);

assert_eq!(r#"SELECT "foo", "bar" FROM "users" GROUP BY "foo" HAVING "foo" > $1"#, sql);
assert_eq!(vec![Value::from(100)], params);
Source

pub fn limit(&mut self, limit: u32)

Sets the LIMIT value.

let mut query = Select::from_table("users");
query.limit(10);

let (sql, params) = renderer::Postgres::build(query);

assert_eq!(r#"SELECT "users".* FROM "users" LIMIT $1"#, sql);
assert_eq!(vec![Value::from(10_i64)], params);
Source

pub fn offset(&mut self, offset: u32)

Sets the OFFSET value.

let mut query = Select::from_table("users");
query.offset(10);

let (sql, params) = renderer::Postgres::build(query);

assert_eq!(r#"SELECT "users".* FROM "users" OFFSET $1"#, sql);
assert_eq!(vec![Value::from(10_i64)], params);
Source

pub fn with(&mut self, cte: CommonTableExpression<'a>)

Adds a common table expression to the select.

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<'a> Debug for Select<'a>

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<'a> From<Select<'a>> for ConditionTree<'a>

Source§

fn from(sel: Select<'a>) -> Self

Converts to this type from the input type.
Source§

impl<'a> From<Select<'a>> for Expression<'a>

Source§

fn from(sel: Select<'a>) -> Expression<'a>

Converts to this type from the input type.
Source§

impl<'a> From<Select<'a>> for Query<'a>

Source§

fn from(sel: Select<'a>) -> Query<'a>

Converts to this type from the input type.
Source§

impl<'a> From<Select<'a>> for Table<'a>

Source§

fn from(select: Select<'a>) -> Self

Converts to this type from the input type.
Source§

impl<'a> PartialEq for Select<'a>

Source§

fn eq(&self, other: &Select<'a>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'a> StructuralPartialEq for Select<'a>

Auto Trait Implementations§

§

impl<'a> Freeze for Select<'a>

§

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 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<'a, T> Conjunctive<'a> for T
where T: Into<Expression<'a>>,

Source§

fn and<E>(self, other: E) -> ConditionTree<'a>
where E: Into<Expression<'a>>,

Builds an AND condition having self as the left leaf and other as the right.
Source§

fn or<E>(self, other: E) -> ConditionTree<'a>
where E: Into<Expression<'a>>,

Builds an OR condition having self as the left leaf and other as the right.
Source§

fn not(self) -> ConditionTree<'a>

Builds a NOT condition having self as the condition.
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<'a, U> Joinable<'a> for U
where U: Into<Table<'a>>,

Source§

fn on<T>(self, conditions: T) -> JoinData<'a>
where T: Into<ConditionTree<'a>>,

Add the JOIN conditions.
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.