Struct sea_query::query::InsertStatement[][src]

pub struct InsertStatement { /* fields omitted */ }
Expand description

Insert any new rows into an existing table

Examples

use sea_query::{*, tests_cfg::*};

let query = Query::insert()
    .into_table(Glyph::Table)
    .columns(vec![
        Glyph::Aspect,
        Glyph::Image,
    ])
    .values_panic(vec![
        5.15.into(),
        "12A".into(),
    ])
    .values_panic(vec![
        4.21.into(),
        "123".into(),
    ])
    .to_owned();

assert_eq!(
    query.to_string(MysqlQueryBuilder),
    r#"INSERT INTO `glyph` (`aspect`, `image`) VALUES (5.15, '12A'), (4.21, '123')"#
);
assert_eq!(
    query.to_string(PostgresQueryBuilder),
    r#"INSERT INTO "glyph" ("aspect", "image") VALUES (5.15, '12A'), (4.21, '123')"#
);
assert_eq!(
    query.to_string(SqliteQueryBuilder),
    r#"INSERT INTO `glyph` (`aspect`, `image`) VALUES (5.15, '12A'), (4.21, '123')"#
);

Implementations

impl InsertStatement[src]

pub fn new() -> Self[src]

Construct a new InsertStatement

pub fn into_table<T>(&mut self, tbl_ref: T) -> &mut Self where
    T: IntoTableRef
[src]

Specify which table to insert into.

Examples

See InsertStatement::values

pub fn columns<C, I>(&mut self, columns: I) -> &mut Self where
    C: IntoIden,
    I: IntoIterator<Item = C>, 
[src]

Specify what columns to insert.

Examples

See InsertStatement::values

pub fn values<I>(&mut self, values: I) -> Result<&mut Self> where
    I: IntoIterator<Item = Value>, 
[src]

Specify a row of values to be inserted.

Examples

use sea_query::{*, tests_cfg::*};

let query = Query::insert()
    .into_table(Glyph::Table)
    .columns(vec![
        Glyph::Aspect,
        Glyph::Image,
    ])
    .values(vec![
        2.1345.into(),
        "24B".into(),
    ])
    .unwrap()
    .values_panic(vec![
        5.15.into(),
        "12A".into(),
    ])
    .to_owned();

assert_eq!(
    query.to_string(MysqlQueryBuilder),
    r#"INSERT INTO `glyph` (`aspect`, `image`) VALUES (2.1345, '24B'), (5.15, '12A')"#
);
assert_eq!(
    query.to_string(PostgresQueryBuilder),
    r#"INSERT INTO "glyph" ("aspect", "image") VALUES (2.1345, '24B'), (5.15, '12A')"#
);
assert_eq!(
    query.to_string(SqliteQueryBuilder),
    r#"INSERT INTO `glyph` (`aspect`, `image`) VALUES (2.1345, '24B'), (5.15, '12A')"#
);

pub fn values_panic<I>(&mut self, values: I) -> &mut Self where
    I: IntoIterator<Item = Value>, 
[src]

Specify a row of values to be inserted, variation of InsertStatement::values.

pub fn returning(&mut self, select: SelectStatement) -> &mut Self[src]

RETURNING expressions. Postgres only.

use sea_query::{*, tests_cfg::*};

let query = Query::insert()
    .into_table(Glyph::Table)
    .columns(vec![
        Glyph::Image,
    ])
    .values_panic(vec![
        "12A".into(),
    ])
    .returning(Query::select().column(Glyph::Id).take())
    .to_owned();

assert_eq!(
    query.to_string(MysqlQueryBuilder),
    "INSERT INTO `glyph` (`image`) VALUES ('12A')"
);
assert_eq!(
    query.to_string(PostgresQueryBuilder),
    r#"INSERT INTO "glyph" ("image") VALUES ('12A') RETURNING "id""#
);
assert_eq!(
    query.to_string(SqliteQueryBuilder),
    "INSERT INTO `glyph` (`image`) VALUES ('12A')"
);

pub fn returning_col<C>(&mut self, col: C) -> &mut Self where
    C: IntoIden
[src]

RETURNING a column after insertion. Postgres only. This is equivalent to MySQL’s LAST_INSERT_ID. Wrapper over InsertStatement::returning().

use sea_query::{*, tests_cfg::*};

let query = Query::insert()
    .into_table(Glyph::Table)
    .columns(vec![
        Glyph::Image,
    ])
    .values_panic(vec![
        "12A".into(),
    ])
    .returning_col(Glyph::Id)
    .to_owned();

assert_eq!(
    query.to_string(MysqlQueryBuilder),
    "INSERT INTO `glyph` (`image`) VALUES ('12A')"
);
assert_eq!(
    query.to_string(PostgresQueryBuilder),
    r#"INSERT INTO "glyph" ("image") VALUES ('12A') RETURNING "id""#
);
assert_eq!(
    query.to_string(SqliteQueryBuilder),
    "INSERT INTO `glyph` (`image`) VALUES ('12A')"
);

pub fn json(&mut self, object: JsonValue) -> &mut Self[src]

This is supported on crate feature with-json only.

Specify a row of values to be inserted, taking input of json values. A convenience method if you have multiple rows to insert at once.

Examples

use sea_query::{*, tests_cfg::*};

let query = Query::insert()
    .into_table(Glyph::Table)
    .columns(vec![
        Glyph::Aspect,
        Glyph::Image,
    ])
    .json(json!({
        "aspect": 2.1345,
        "image": "24B",
    }))
    .json(json!({
        "aspect": 4.21,
        "image": "123",
    }))
    .to_owned();

assert_eq!(
    query.to_string(MysqlQueryBuilder),
    r#"INSERT INTO `glyph` (`aspect`, `image`) VALUES (2.1345, '24B'), (4.21, '123')"#
);
assert_eq!(
    query.to_string(PostgresQueryBuilder),
    r#"INSERT INTO "glyph" ("aspect", "image") VALUES (2.1345, '24B'), (4.21, '123')"#
);
assert_eq!(
    query.to_string(SqliteQueryBuilder),
    r#"INSERT INTO `glyph` (`aspect`, `image`) VALUES (2.1345, '24B'), (4.21, '123')"#
);

impl InsertStatement[src]

pub fn to_string<T: QueryBuilder>(&self, query_builder: T) -> String[src]

pub fn build<T: QueryBuilder>(&self, query_builder: T) -> (String, Values)[src]

pub fn build_any(&self, query_builder: &dyn QueryBuilder) -> (String, Values)[src]

Trait Implementations

impl Clone for InsertStatement[src]

fn clone(&self) -> InsertStatement[src]

Returns a copy of the value. Read more

fn clone_from(&mut self, source: &Self)1.0.0[src]

Performs copy-assignment from source. Read more

impl Debug for InsertStatement[src]

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

Formats the value using the given formatter. Read more

impl Default for InsertStatement[src]

fn default() -> InsertStatement[src]

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

impl QueryStatementBuilder for InsertStatement[src]

fn build_collect<T: QueryBuilder>(
    &self,
    query_builder: T,
    collector: &mut dyn FnMut(Value)
) -> String
[src]

Build corresponding SQL statement for certain database backend and collect query parameters

Examples

use sea_query::{*, tests_cfg::*};

let query = Query::insert()
    .into_table(Glyph::Table)
    .columns(vec![
        Glyph::Aspect,
        Glyph::Image,
    ])
    .values_panic(vec![
        3.1415.into(),
        "041080".into(),
    ])
    .to_owned();

assert_eq!(
    query.to_string(MysqlQueryBuilder),
    r#"INSERT INTO `glyph` (`aspect`, `image`) VALUES (3.1415, '041080')"#
);

let mut params = Vec::new();
let mut collector = |v| params.push(v);

assert_eq!(
    query.build_collect(MysqlQueryBuilder, &mut collector),
    r#"INSERT INTO `glyph` (`aspect`, `image`) VALUES (?, ?)"#
);
assert_eq!(
    params,
    vec![
        Value::Double(3.1415),
        Value::String(Box::new(String::from("041080"))),
    ]
);

fn build_collect_any(
    &self,
    query_builder: &dyn QueryBuilder,
    collector: &mut dyn FnMut(Value)
) -> String
[src]

Build corresponding SQL statement for certain database backend and collect query parameters

fn to_string<T: QueryBuilder>(&self, query_builder: T) -> String[src]

Build corresponding SQL statement for certain database backend and return SQL string Read more

fn build<T: QueryBuilder>(&self, query_builder: T) -> (String, Values)[src]

Build corresponding SQL statement for certain database backend and collect query parameters into a vector Read more

fn build_any(&self, query_builder: &dyn QueryBuilder) -> (String, Values)[src]

Build corresponding SQL statement for certain database backend and collect query parameters into a vector

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

pub fn type_id(&self) -> TypeId[src]

Gets the TypeId of self. Read more

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

pub fn borrow(&self) -> &T[src]

Immutably borrows from an owned value. Read more

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

pub fn borrow_mut(&mut self) -> &mut T[src]

Mutably borrows from an owned value. Read more

impl<T> From<T> for T[src]

pub fn from(t: T) -> T[src]

Performs the conversion.

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

pub fn into(self) -> U[src]

Performs the conversion.

impl<T> Same<T> for T

type Output = T

Should always be Self

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

pub fn to_owned(&self) -> T[src]

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

pub fn clone_into(&self, target: &mut T)[src]

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

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

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>[src]

Performs the conversion.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

pub fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>[src]

Performs the conversion.

impl<V, T> VZip<V> for T where
    V: MultiLane<T>, 

pub fn vzip(self) -> V