Struct sql_query_builder::Insert

source ·
pub struct Insert { /* private fields */ }
Expand description

Builder to contruct a Insert command

Implementations§

source§

impl Insert

source

pub fn as_string(&self) -> String

Gets the current state of the Insert and returns it as string

§Example
let query = sql::Insert::new()
  .insert_into("users (login)")
  .values("('foo')")
  .as_string();

Output

INSERT INTO users (login) VALUES ('foo')
source

pub fn debug(self) -> Self

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

§Example
let insert_query = sql::Insert::new()
  .insert_into("users (login, name)")
  .values("('foo', 'Foo')")
  .debug()
  .values("('bar', 'Bar')")
  .as_string();

Prints to the standard output

-- ------------------------------------------------------------------------------
INSERT INTO users (login, name)
VALUES ('foo', 'Foo')
-- ------------------------------------------------------------------------------
source

pub fn default_values(self) -> Self

The default values clause

§Example
let insert_query = sql::Insert::new()
  .insert_into("users")
  .default_values()
  .to_string();

Output

INSERT INTO users DEFAULT VALUES
source

pub fn new() -> Self

Creates instance of the Insert command

source

pub fn on_conflict(self, conflict: &str) -> Self

The on conflict clause. This method overrides the previous value

§Example
let query = sql::Insert::new()
  .insert_into("users (login)")
  .on_conflict("do nothing")
  .as_string();

Output

INSERT INTO users (login) ON CONFLICT do nothing
source

pub fn print(self) -> Self

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

source

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

The select clause. This method overrides the previous value

§Example
let insert_query = sql::Insert::new()
  .insert_into("users (login, name)")
  .select(
    sql::Select::new()
      .select("login, name")
      .from("users_bk")
      .where_clause("active = true"),
  )
  .as_string();

Output

INSERT INTO users (login, name)
SELECT login, name
FROM users_bk
WHERE active = true
source

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

Adds at the beginning a raw SQL query.

§Example
let raw_query = "insert into users (login, name)";

let insert_query = sql::Insert::new()
  .raw(raw_query)
  .values("('foo', 'Foo')")
  .as_string();

Output

insert into users (login, name) VALUES ('foo', 'Foo')
source

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

Adds a raw SQL query after a specified clause.

§Example
let raw = "values ('foo', 'Foo')";

let insert_query = sql::Insert::new()
  .insert_into("users (login, name)")
  .raw_after(sql::InsertClause::InsertInto, raw)
  .as_string();

Output

INSERT INTO users (login, name) values ('foo', 'Foo')
source

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

Adds a raw SQL query before a specified clause.

§Example
let raw = "insert into users (login, name)";

let insert_query = sql::Insert::new()
  .raw_before(sql::InsertClause::Values, raw)
  .values("('bar', 'Bar')")
  .as_string();

Output

insert into users (login, name) VALUES ('bar', 'Bar')
source

pub fn values(self, value: &str) -> Self

The values clause

§Example
let query = sql::Insert::new()
  .insert_into("users (login, name)")
  .values("('foo', 'Foo')")
  .values("('bar', 'Bar')")
  .as_string();

Output

INSERT INTO users (login, name) VALUES ('foo', 'Foo'), ('bar', 'Bar')
source§

impl Insert

source

pub fn returning(self, output_name: &str) -> Self

Available on crate features postgresql and sqlite only.

The returning clause

§Example
let insert_query = sql::Insert::new()
  .insert_into("users")
  .returning("id")
  .returning("login")
  .to_string();

Output

INSERT INTO users RETURNING id, login
source

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

Available on crate features postgresql and sqlite only.

The with clause

§Example
let active_users = sql::Select::new()
  .select("*")
  .from("users_bk")
  .where_clause("ative = true");

let insert_query = sql::Insert::new()
  .with("active_users", active_users)
  .insert_into("users")
  .select(sql::Select::new().select("*").from("active_users"))
  .to_string();

Output

WITH active_users AS (
  SELECT *
  FROM users_bk
  WHERE ative = true
)
INSERT INTO users
SELECT *
FROM active_users
source§

impl Insert

source

pub fn insert_or(self, expression: &str) -> Self

Available on crate feature sqlite only.

The insert or <keyword> into clause

§Example
let insert = sql::Insert::new()
  .insert_or("abort into users (login, name)");

let insert = sql::Insert::new()
  .insert_or("fail into addresses (state, country)")
  .insert_or("abort into users (login, name)");

Output

INSERT OR abort into users (login, name)
source

pub fn replace_into(self, expression: &str) -> Self

Available on crate feature sqlite only.

The replace into clause, this method overrides the previous value

§Example
let insert = sql::Insert::new()
  .replace_into("users (login, name)");

Output

REPLACE INTO users (login, name)

Trait Implementations§

source§

impl Clone for Insert

source§

fn clone(&self) -> Insert

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 Insert

source§

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

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

impl Default for Insert

source§

fn default() -> Insert

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

impl Display for Insert

source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Insert

§

impl !RefUnwindSafe for Insert

§

impl !Send for Insert

§

impl !Sync for Insert

§

impl Unpin for Insert

§

impl !UnwindSafe for Insert

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> 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<T> ToOwned for T
where 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 T
where 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 T
where 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 T
where 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.