Struct sea_query::query::InsertStatement
source ¡ [−]pub struct InsertStatement { /* private fields */ }
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
sourceimpl InsertStatement
impl InsertStatement
sourcepub fn new() -> Self
pub fn new() -> Self
Construct a new InsertStatement
sourcepub fn replace(&mut self) -> &mut Self
pub fn replace(&mut self) -> &mut Self
Use REPLACE instead of INSERT
Examples
use sea_query::{tests_cfg::*, *};
let query = Query::insert()
.replace()
.into_table(Glyph::Table)
.columns(vec![Glyph::Aspect, Glyph::Image])
.values_panic(vec![5.15.into(), "12A".into()])
.to_owned();
assert_eq!(
query.to_string(MysqlQueryBuilder),
r#"REPLACE INTO `glyph` (`aspect`, `image`) VALUES (5.15, '12A')"#
);
assert_eq!(
query.to_string(SqliteQueryBuilder),
r#"REPLACE INTO "glyph" ("aspect", "image") VALUES (5.15, '12A')"#
);
sourcepub fn into_table<T>(&mut self, tbl_ref: T) -> &mut Self where
T: IntoTableRef,
pub fn into_table<T>(&mut self, tbl_ref: T) -> &mut Self where
T: IntoTableRef,
sourcepub fn columns<C, I>(&mut self, columns: I) -> &mut Self where
C: IntoIden,
I: IntoIterator<Item = C>,
pub fn columns<C, I>(&mut self, columns: I) -> &mut Self where
C: IntoIden,
I: IntoIterator<Item = C>,
sourcepub fn values<I>(&mut self, values: I) -> Result<&mut Self> where
I: IntoIterator<Item = Value>,
pub fn values<I>(&mut self, values: I) -> Result<&mut Self> where
I: IntoIterator<Item = Value>,
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')"#
);
sourcepub fn select_from<S>(&mut self, select: S) -> Result<&mut Self> where
S: Into<SelectStatement>,
pub fn select_from<S>(&mut self, select: S) -> Result<&mut Self> where
S: Into<SelectStatement>,
Specify a select query whose values to be inserted.
Examples
use sea_query::{tests_cfg::*, *};
let query = Query::insert()
.into_table(Glyph::Table)
.columns(vec![Glyph::Aspect, Glyph::Image])
.select_from(Query::select()
.column(Glyph::Aspect)
.column(Glyph::Image)
.from(Glyph::Table)
.and_where(Expr::col(Glyph::Image).like("0%"))
.to_owned()
)
.unwrap()
.to_owned();
assert_eq!(
query.to_string(MysqlQueryBuilder),
r#"INSERT INTO `glyph` (`aspect`, `image`) SELECT `aspect`, `image` FROM `glyph` WHERE `image` LIKE '0%'"#
);
assert_eq!(
query.to_string(PostgresQueryBuilder),
r#"INSERT INTO "glyph" ("aspect", "image") SELECT "aspect", "image" FROM "glyph" WHERE "image" LIKE '0%'"#
);
assert_eq!(
query.to_string(SqliteQueryBuilder),
r#"INSERT INTO "glyph" ("aspect", "image") SELECT "aspect", "image" FROM "glyph" WHERE "image" LIKE '0%'"#
);
sourcepub fn exprs<I>(&mut self, values: I) -> Result<&mut Self> where
I: IntoIterator<Item = SimpleExpr>,
pub fn exprs<I>(&mut self, values: I) -> Result<&mut Self> where
I: IntoIterator<Item = SimpleExpr>,
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])
.exprs(vec![
Expr::val(2).into(),
Func::cast_as("2020-02-02 00:00:00", Alias::new("DATE")),
])
.unwrap()
.to_owned();
assert_eq!(
query.to_string(MysqlQueryBuilder),
r#"INSERT INTO `glyph` (`aspect`, `image`) VALUES (2, CAST('2020-02-02 00:00:00' AS DATE))"#
);
assert_eq!(
query.to_string(PostgresQueryBuilder),
r#"INSERT INTO "glyph" ("aspect", "image") VALUES (2, CAST('2020-02-02 00:00:00' AS DATE))"#
);
assert_eq!(
query.to_string(SqliteQueryBuilder),
r#"INSERT INTO "glyph" ("aspect", "image") VALUES (2, CAST('2020-02-02 00:00:00' AS DATE))"#
);
sourcepub fn values_panic<I>(&mut self, values: I) -> &mut Self where
I: IntoIterator<Item = Value>,
pub fn values_panic<I>(&mut self, values: I) -> &mut Self where
I: IntoIterator<Item = Value>,
Specify a row of values to be inserted, variation of InsertStatement::values
.
sourcepub fn exprs_panic<I>(&mut self, values: I) -> &mut Self where
I: IntoIterator<Item = SimpleExpr>,
pub fn exprs_panic<I>(&mut self, values: I) -> &mut Self where
I: IntoIterator<Item = SimpleExpr>,
Specify a row of values to be inserted, variation of InsertStatement::exprs
.
sourcepub fn on_conflict(&mut self, on_conflict: OnConflict) -> &mut Self
pub fn on_conflict(&mut self, on_conflict: OnConflict) -> &mut Self
ON CONFLICT expression
Examples
OnConflict::update_columns
: Update column value of existing row with inserting valueOnConflict::update_values
: Update column value of existing row with valueOnConflict::update_exprs
: Update column value of existing row with expression
sourcepub fn returning(&mut self, select: SelectStatement) -> &mut Self
pub fn returning(&mut self, select: SelectStatement) -> &mut Self
RETURNING expressions.
Note:
Works on
- PostgreSQL
- SQLite
- SQLite version >= 3.35.0
- Note that sea-query wonât try to enforce either of these constraints
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),
r#"INSERT INTO "glyph" ("image") VALUES ('12A') RETURNING "id""#
);
sourcepub fn returning_col<C>(&mut self, col: C) -> &mut Self where
C: IntoIden,
pub fn returning_col<C>(&mut self, col: C) -> &mut Self where
C: IntoIden,
RETURNING a column after insertion. This is equivalent to MySQLâs LAST_INSERT_ID.
Wrapper over InsertStatement::returning()
.
Note:
Works on
- PostgreSQL
- SQLite
- SQLite version >= 3.35.0
- Note that sea-query wonât try to enforce either of these constraints
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),
r#"INSERT INTO "glyph" ("image") VALUES ('12A') RETURNING "id""#
);
sourcepub fn with(self, clause: WithClause) -> WithQuery
pub fn with(self, clause: WithClause) -> WithQuery
Create a WithQuery by specifying a WithClause to execute this query with.
Examples
use sea_query::{*, IntoCondition, IntoIden, tests_cfg::*};
let select = SelectStatement::new()
.columns([Glyph::Id, Glyph::Image, Glyph::Aspect])
.from(Glyph::Table)
.to_owned();
let cte = CommonTableExpression::new()
.query(select)
.column(Glyph::Id)
.column(Glyph::Image)
.column(Glyph::Aspect)
.table_name(Alias::new("cte"))
.to_owned();
let with_clause = WithClause::new().cte(cte).to_owned();
let select = SelectStatement::new()
.columns([Glyph::Id, Glyph::Image, Glyph::Aspect])
.from(Alias::new("cte"))
.to_owned();
let mut insert = Query::insert();
insert
.into_table(Glyph::Table)
.columns([Glyph::Id, Glyph::Image, Glyph::Aspect])
.select_from(select)
.unwrap();
let query = insert.with(with_clause);
assert_eq!(
query.to_string(MysqlQueryBuilder),
r#"WITH `cte` (`id`, `image`, `aspect`) AS (SELECT `id`, `image`, `aspect` FROM `glyph`) INSERT INTO `glyph` (`id`, `image`, `aspect`) SELECT `id`, `image`, `aspect` FROM `cte`"#
);
assert_eq!(
query.to_string(PostgresQueryBuilder),
r#"WITH "cte" ("id", "image", "aspect") AS (SELECT "id", "image", "aspect" FROM "glyph") INSERT INTO "glyph" ("id", "image", "aspect") SELECT "id", "image", "aspect" FROM "cte""#
);
assert_eq!(
query.to_string(SqliteQueryBuilder),
r#"WITH "cte" ("id", "image", "aspect") AS (SELECT "id", "image", "aspect" FROM "glyph") INSERT INTO "glyph" ("id", "image", "aspect") SELECT "id", "image", "aspect" FROM "cte""#
);
sourcepub fn or_default_values(&mut self) -> &mut Self
pub fn or_default_values(&mut self) -> &mut Self
Insert with default values if columns and values are not supplied.
Examples
use sea_query::{tests_cfg::*, *};
// Insert default
let query = Query::insert()
.into_table(Glyph::Table)
.or_default_values()
.to_owned();
assert_eq!(
query.to_string(MysqlQueryBuilder),
r#"INSERT INTO `glyph` VALUES ()"#
);
assert_eq!(
query.to_string(PostgresQueryBuilder),
r#"INSERT INTO "glyph" VALUES (DEFAULT)"#
);
assert_eq!(
query.to_string(SqliteQueryBuilder),
r#"INSERT INTO "glyph" DEFAULT VALUES"#
);
// Ordinary insert as columns and values are supplied
let query = Query::insert()
.into_table(Glyph::Table)
.or_default_values()
.columns(vec![Glyph::Image])
.values_panic(vec!["ABC".into()])
.to_owned();
assert_eq!(
query.to_string(MysqlQueryBuilder),
r#"INSERT INTO `glyph` (`image`) VALUES ('ABC')"#
);
assert_eq!(
query.to_string(PostgresQueryBuilder),
r#"INSERT INTO "glyph" ("image") VALUES ('ABC')"#
);
assert_eq!(
query.to_string(SqliteQueryBuilder),
r#"INSERT INTO "glyph" ("image") VALUES ('ABC')"#
);
sourcepub fn or_default_values_many(&mut self, num_rows: u32) -> &mut Self
pub fn or_default_values_many(&mut self, num_rows: u32) -> &mut Self
Insert multiple rows with default values if columns and values are not supplied.
Examples
use sea_query::{tests_cfg::*, *};
// Insert default
let query = Query::insert()
.into_table(Glyph::Table)
.or_default_values_many(3)
.to_owned();
assert_eq!(
query.to_string(MysqlQueryBuilder),
r#"INSERT INTO `glyph` VALUES (), (), ()"#
);
assert_eq!(
query.to_string(PostgresQueryBuilder),
r#"INSERT INTO "glyph" VALUES (DEFAULT), (DEFAULT), (DEFAULT)"#
);
assert_eq!(
query.to_string(SqliteQueryBuilder),
r#"INSERT INTO "glyph" DEFAULT VALUES"#
);
// Ordinary insert as columns and values are supplied
let query = Query::insert()
.into_table(Glyph::Table)
.or_default_values_many(3)
.columns(vec![Glyph::Image])
.values_panic(vec!["ABC".into()])
.to_owned();
assert_eq!(
query.to_string(MysqlQueryBuilder),
r#"INSERT INTO `glyph` (`image`) VALUES ('ABC')"#
);
assert_eq!(
query.to_string(PostgresQueryBuilder),
r#"INSERT INTO "glyph" ("image") VALUES ('ABC')"#
);
assert_eq!(
query.to_string(SqliteQueryBuilder),
r#"INSERT INTO "glyph" ("image") VALUES ('ABC')"#
);
Trait Implementations
sourceimpl Clone for InsertStatement
impl Clone for InsertStatement
sourcefn clone(&self) -> InsertStatement
fn clone(&self) -> InsertStatement
Returns a copy of the value. Read more
1.0.0 ¡ sourcefn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
. Read more
sourceimpl Debug for InsertStatement
impl Debug for InsertStatement
sourceimpl Default for InsertStatement
impl Default for InsertStatement
sourcefn default() -> InsertStatement
fn default() -> InsertStatement
Returns the âdefault valueâ for a type. Read more
sourceimpl QueryStatementBuilder for InsertStatement
impl QueryStatementBuilder for InsertStatement
sourcefn build_collect_any_into(
&self,
query_builder: &dyn QueryBuilder,
sql: &mut SqlWriter,
collector: &mut dyn FnMut(Value)
)
fn build_collect_any_into(
&self,
query_builder: &dyn QueryBuilder,
sql: &mut SqlWriter,
collector: &mut dyn FnMut(Value)
)
Build corresponding SQL statement into the SqlWriter for certain database backend and collect query parameters
fn into_sub_query_statement(self) -> SubQueryStatement
sourcefn build_any(&self, query_builder: &dyn QueryBuilder) -> (String, Values)
fn build_any(&self, query_builder: &dyn QueryBuilder) -> (String, Values)
Build corresponding SQL statement for certain database backend and collect query parameters into a vector
sourcefn build_collect_any(
&self,
query_builder: &dyn QueryBuilder,
collector: &mut dyn FnMut(Value)
) -> String
fn build_collect_any(
&self,
query_builder: &dyn QueryBuilder,
collector: &mut dyn FnMut(Value)
) -> String
Build corresponding SQL statement for certain database backend and collect query parameters
sourceimpl QueryStatementWriter for InsertStatement
impl QueryStatementWriter for InsertStatement
sourcefn build_collect<T: QueryBuilder>(
&self,
query_builder: T,
collector: &mut dyn FnMut(Value)
) -> String
fn build_collect<T: QueryBuilder>(
&self,
query_builder: T,
collector: &mut dyn FnMut(Value)
) -> String
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")))),
]
);
sourcefn to_string<T: QueryBuilder>(&self, query_builder: T) -> String
fn to_string<T: QueryBuilder>(&self, query_builder: T) -> String
Build corresponding SQL statement for certain database backend and return SQL string Read more
Auto Trait Implementations
impl !RefUnwindSafe for InsertStatement
impl Send for InsertStatement
impl Sync for InsertStatement
impl Unpin for InsertStatement
impl !UnwindSafe for InsertStatement
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable ¡ sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
fn vzip(self) -> V
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
sourcefn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more