Struct sql_query_builder::Select
source · [−]pub struct Select<'a> { /* private fields */ }Expand description
Builder to contruct a Select command
Implementations
sourceimpl<'a> Select<'a>
impl<'a> Select<'a>
sourcepub fn and(self, condition: &'a str) -> Self
pub fn and(self, condition: &'a 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");sourcepub fn as_string(&self) -> String
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'sourcepub fn debug(self) -> Self
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 = trueYou 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 userssourcepub fn except(self, select: Self) -> Self
pub fn except(self, select: Self) -> Self
The except clause, this method can be used enabling the feature flag postgresql
sourcepub fn cross_join(self, table: &'a str) -> Self
pub fn cross_join(self, table: &'a str) -> Self
The cross join clause
sourcepub fn inner_join(self, table: &'a str) -> Self
pub fn inner_join(self, table: &'a str) -> Self
The inner join clause
sourcepub fn right_join(self, table: &'a str) -> Self
pub fn right_join(self, table: &'a str) -> Self
The right join clause
sourcepub fn intersect(self, select: Self) -> Self
pub fn intersect(self, select: Self) -> Self
The intersect clause, this method can be used enabling the feature flag postgresql
sourcepub fn limit(self, num: &'a str) -> Self
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");sourcepub fn offset(self, num: &'a str) -> Self
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");sourcepub fn print(self) -> Self
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.
sourcepub fn raw(self, raw_sql: &'a str) -> Self
pub fn raw(self, raw_sql: &'a 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 = foosourcepub fn raw_after(self, clause: SelectClause, raw_sql: &'a str) -> Self
pub fn raw_after(self, clause: SelectClause, raw_sql: &'a 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 = foosourcepub fn raw_before(self, clause: SelectClause, raw_sql: &'a str) -> Self
pub fn raw_before(self, clause: SelectClause, raw_sql: &'a 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 = foosourcepub fn union(self, select: Self) -> Self
pub fn union(self, select: Self) -> Self
The union clause, this method can be used enabling the feature flag postgresql
sourcepub fn where_clause(self, condition: &'a str) -> Self
pub fn where_clause(self, condition: &'a str) -> Self
The where clause
Examples
use sql_query_builder as sql;
let select = sql::Select::new()
.from("users")
.where_clause("login = $1");sourcepub fn with(self, name: &'a str, query: impl WithQuery + 'static) -> Self
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
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
sourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more