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

Construct a new InsertStatement

Specify which table to insert into.

Examples

See InsertStatement::values

Specify what columns to insert.

Examples

See InsertStatement::values

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')"#
);

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

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

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

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

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(Some(3.1415)),
        Value::String(Some(Box::new(String::from("041080")))),
    ]
);

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

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

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

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

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

Performs the conversion.

Performs the conversion.

Should always be Self

The resulting type after obtaining ownership.

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

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

recently added

Uses borrowed data to replace owned data, usually by cloning. 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.