Insert

Struct Insert 

Source
pub struct Insert { /* private fields */ }
Expand description

Builder of Insert command.

Basic API

use sql_query_builder as sql;

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')

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 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 insert_into(self, table_name: &str) -> Self

The insert into clause. This method overrides the previous value

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

let insert = sql::Insert::new()
  .insert_into("addresses (state, country)")
  .insert_into("users (login, name)");
§Mutually exclusive methods

Avaliable on sqlite only

Methods Insert::insert_into, Insert::insert_or and Insert::replace_into are mutually exclusive, the last called will overrides the previous ones.

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

Output

REPLACE INTO users (login, name)

Avaliable on mysql only

Methods Insert::insert_into and the splitted version (Insert::insert, Insert::into, Insert::column) are mutually exclusive, the last called will overrides the previous ones.

let insert = sql::Insert::new()
  .insert_into("users (login, name)")
  .insert("low_priority")
  .into("users")
  .column("login");

Output

INSERT low_priority INTO users (login)
Source

pub fn new() -> Self

Creates instance of the Insert command

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 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 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 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 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, expression: &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')
§Composable methods

The methods Insert::values, Insert::row are composable, when both was used each line will receive the contructor clause row.

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

Output

VALUES ROW('foo', 'Foo'), ROW('bar', 'Bar')
§Mutually exclusive methods

The methods Insert::values/Insert::row, Insert::select and Insert::set are mutually exclusive, the last called will overrides the previous ones. Insert::row and Insert::set are available on crate feature mysql only.

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

Output

SET login = 'foo', name = 'Foo'
Source§

impl Insert

Source

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

Available on crate features postgresql and sqlite only.

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 returning(self, output_name: &str) -> Self

Available on crate features postgresql and sqlite only.

The returning clause

§Example
let 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 + Send + Sync, ) -> 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 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, table_name: &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)
Source§

impl Insert

Source

pub fn column(self, column_name: &str) -> Self

Available on crate feature mysql only.

Defines the columns of the table used to insert values.

§Example
 let query = sql::Insert::new()
   .into("users")
   .column("login")
   .column("name")
   .as_string();

Outputs

INTO users (login, name)
Source

pub fn insert(self, modifier: &str) -> Self

Available on crate feature mysql only.

The insert clause, used to defined modifiers to change de insert execution

§Example
 let query = sql::Insert::new()
   .insert("LOW_PRIORITY")
   .into("users")
   .column("login")
   .as_string();

Outputs

INSERT LOW_PRIORITY INTO users (login)
Source

pub fn into(self, table: &str) -> Self

Available on crate feature mysql only.

The into clause, defines the name of the table to be used

§Example
 let query = sql::Insert::new()
   .into("users")
   .column("login")
   .as_string();

Outputs

INTO users (login)
Source

pub fn partition(self, name: &str) -> Self

Available on crate feature mysql only.

The partition clause

§Example
let query = sql::Insert::new()
  .into("employees")
  .partition("p1")
  .to_string();

Output

INTO employees PARTITION (p1)
Source

pub fn on_duplicate_key_update(self, assignment: &str) -> Self

Available on crate feature mysql only.

The ON DUPLICATE KEY UPDATE clause

§Example
let query = sql::Insert::new()
  .insert_into("t1 (a, b, c)")
  .values("(1, 2, 3)")
  .on_duplicate_key_update("c = c+1")
  .as_string();

Output

INSERT INTO t1 (a, b, c)
VALUES (1, 2, 3)
ON DUPLICATE KEY UPDATE c = c+1
Source

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

Available on crate feature mysql only.

The values clause with the row constructor clause

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

Output

INSERT INTO users (login, name) VALUES ROW('foo', 'Foo'), ROW('bar', 'Bar')
Source

pub fn set(self, assignment: &str) -> Self

Available on crate feature mysql only.

The set clause

§Example
let update_query = sql::Insert::new()
  .set("name = 'Bar'")
  .as_string();

Output

SET name = 'Bar'

Trait Implementations§

Source§

impl Clone for Insert

Source§

fn clone(&self) -> Insert

Returns a duplicate 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> 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<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,

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> ToString for T
where T: Display + ?Sized,

Source§

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>,

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.