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

Builder to contruct a select command

Implementations

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

use sql_query_builder::SelectBuilder;

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

Gets the current state of the SelectBuilder returns it as string

Prints the current state of the SelectBuilder 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

use sql_query_builder::SelectBuilder;

let select = SelectBuilder::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

use sql_query_builder::SelectBuilder;

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

Output

SELECT *
FROM users

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

The from clause

The group by clause

The having clause

The cross join clause

The inner join clause

The left join clause

The right join clause

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

The limit clause. This method overrides the previous value

use sql_query_builder::SelectBuilder;

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

let select = SelectBuilder::new()
  .limit("1000")
  .limit("123");

Create SelectBuilder’s instance

The offset clause. This method overrides the previous value

use sql_query_builder::SelectBuilder;

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

let select = SelectBuilder::new()
  .offset("1000")
  .offset("1500");

The order by clause

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

Adds at the beginning a raw SQL query.

use sql_query_builder::SelectBuilder;

let raw_query = "select * from users u inner join address addr on u.login = addr.owner_login";
let select_query = SelectBuilder::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

Adds a raw SQL query after a specified clause.

use sql_query_builder::{SelectClause, SelectBuilder};

let raw_join = "inner join address addr on u.login = addr.owner_login";
let select_query = SelectBuilder::new()
  .select("*")
  .from("users u")
  .raw_after(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

Adds a raw SQL query before a specified clause.

use sql_query_builder::{SelectClause, SelectBuilder};

let raw_query = "from users u inner join address addr on u.login = addr.owner_login";
let select_query = SelectBuilder::new()
  .select("*")
  .raw_before(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

The select clause

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

The where clause

use sql_query_builder::SelectBuilder;

let select = SelectBuilder::new()
  .from("users")
  .where_clause("login = $1");

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

use sql_query_builder::{InsertBuilder, SelectBuilder};

let logins = SelectBuilder::new().select("login").from("users").where_clause("id in ($1)");
let select = SelectBuilder::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

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

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

Formats the value using the given formatter. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

Uses borrowed data to replace owned data, usually by cloning. Read more

Converts the given value to a String. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.