pub struct InsertQuery { /* private fields */ }Expand description
INSERT query builder
Implementations§
Source§impl InsertQuery
impl InsertQuery
Sourcepub fn new() -> InsertQuery
pub fn new() -> InsertQuery
Create an empty INSERT query
Sourcepub fn into_table(self, table: &str) -> InsertQuery
pub fn into_table(self, table: &str) -> InsertQuery
Set the target table
Sourcepub fn value(self, column: &str, value: &str) -> InsertQuery
pub fn value(self, column: &str, value: &str) -> InsertQuery
Add a column-value pair (value should be an already-escaped SQL literal)
Sourcepub fn values(self, pairs: &[(&str, &str)]) -> InsertQuery
pub fn values(self, pairs: &[(&str, &str)]) -> InsertQuery
Batch add column-value pairs
Sourcepub fn on_conflict_do_nothing(self, conflict_cols: &[&str]) -> InsertQuery
pub fn on_conflict_do_nothing(self, conflict_cols: &[&str]) -> InsertQuery
PostgreSQL/SQLite: ON CONFLICT (cols) DO NOTHING
Ignore insertion on conflict. Applicable to PostgreSQL and SQLite dialects. Under MySQL dialect this setting is ignored (MySQL does not support ON CONFLICT syntax).
§Example
use sz_orm_core::DbType;
use sz_orm_query_builder::Query;
let sql = Query::insert()
.into_table("users")
.value("id", "1")
.value("name", "'Alice'")
.on_conflict_do_nothing(&["id"])
.build_with_dialect(DbType::PostgreSQL);
// INSERT INTO "users" ("id", "name") VALUES (1, 'Alice') ON CONFLICT ("id") DO NOTHINGSourcepub fn on_conflict_do_update(
self,
conflict_cols: &[&str],
assignments: &[(&str, &str)],
) -> InsertQuery
pub fn on_conflict_do_update( self, conflict_cols: &[&str], assignments: &[(&str, &str)], ) -> InsertQuery
PostgreSQL/SQLite: ON CONFLICT (cols) DO UPDATE SET col = expr, ...
Update specified columns on conflict. assignments is a list of (column, expression) pairs.
Expressions may use EXCLUDED.col to reference the to-be-inserted value (PG/SQLite standard).
§Example
use sz_orm_core::DbType;
use sz_orm_query_builder::Query;
let sql = Query::insert()
.into_table("users")
.value("id", "1")
.value("name", "'Alice'")
.value("count", "1")
.on_conflict_do_update(
&["id"],
&[("name", "EXCLUDED.name"), ("count", "users.count + 1")],
)
.build_with_dialect(DbType::PostgreSQL);
// INSERT INTO "users" (...) VALUES (...) ON CONFLICT ("id") DO UPDATE SET
// "name" = EXCLUDED.name, "count" = users.count + 1Sourcepub fn on_duplicate_key_update(
self,
assignments: &[(&str, &str)],
) -> InsertQuery
pub fn on_duplicate_key_update( self, assignments: &[(&str, &str)], ) -> InsertQuery
MySQL: ON DUPLICATE KEY UPDATE col = expr, ...
Update specified columns on primary/unique key conflict. assignments is a list of
(column, expression) pairs. Expressions may use VALUES(col) to reference the
to-be-inserted value (MySQL syntax).
§Example
use sz_orm_core::DbType;
use sz_orm_query_builder::Query;
let sql = Query::insert()
.into_table("users")
.value("id", "1")
.value("name", "'Alice'")
.value("count", "1")
.on_duplicate_key_update(&[("name", "VALUES(name)"), ("count", "count + 1")])
.build_with_dialect(DbType::MySQL);
// INSERT INTO `users` (`id`, `name`, `count`) VALUES (1, 'Alice', 1)
// ON DUPLICATE KEY UPDATE `name` = VALUES(name), `count` = count + 1Sourcepub fn replace(self) -> InsertQuery
pub fn replace(self) -> InsertQuery
MySQL: use REPLACE INTO instead of INSERT INTO
On primary/unique key conflict, delete the old row first then insert the new row. Applicable to MySQL/MariaDB/TiDB dialects.
Sourcepub fn returning(self, columns: &[&str]) -> InsertQuery
pub fn returning(self, columns: &[&str]) -> InsertQuery
Set the RETURNING clause (supported by PostgreSQL/SQLite 3.35+)
Returns the values of specified columns after INSERT, commonly used to obtain
auto-increment primary keys or default values. MySQL does not support RETURNING;
it is ignored in build() (MySQL style) and only rendered for PostgreSQL/SQLite
dialects in build_with_dialect().
§Example
use sz_orm_core::DbType;
use sz_orm_query_builder::Query;
let sql = Query::insert()
.into_table("users")
.value("name", "'Alice'")
.returning(&["id", "created_at"])
.build_with_dialect(DbType::PostgreSQL);
// INSERT INTO "users" ("name") VALUES ('Alice') RETURNING "id", "created_at"Sourcepub fn returning_all(self) -> InsertQuery
pub fn returning_all(self) -> InsertQuery
Set RETURNING * (return all columns)
Sourcepub fn build(self) -> String
pub fn build(self) -> String
Build INSERT SQL (no dialect, hard-coded backticks, MySQL style)
§Security (gate 9 fix)
Identifiers are escaped via quote_ident() and wrapped in backticks, preventing
malicious identifiers containing ` from breaking out.
§Upsert behavior
Replace: generatesREPLACE INTOinstead ofINSERT INTOOnDuplicateKeyUpdate: appendsON DUPLICATE KEY UPDATEclauseOnConflictDoNothing/OnConflictDoUpdate: skipped (MySQL does not support)
Sourcepub fn build_with_dialect(self, db_type: DbType) -> String
pub fn build_with_dialect(self, db_type: DbType) -> String
Generate SQL for the specified dialect
§Upsert dialect compatibility
- MySQL family: supports
OnDuplicateKeyUpdate,Replace - PostgreSQL/SQLite: supports
OnConflictDoNothing,OnConflictDoUpdate - Incompatible combinations skip the upsert clause (no error)
Trait Implementations§
Source§impl Clone for InsertQuery
impl Clone for InsertQuery
Source§fn clone(&self) -> InsertQuery
fn clone(&self) -> InsertQuery
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for InsertQuery
impl Debug for InsertQuery
Source§impl Default for InsertQuery
impl Default for InsertQuery
Source§fn default() -> InsertQuery
fn default() -> InsertQuery
Auto Trait Implementations§
impl Freeze for InsertQuery
impl RefUnwindSafe for InsertQuery
impl Send for InsertQuery
impl Sync for InsertQuery
impl Unpin for InsertQuery
impl UnsafeUnpin for InsertQuery
impl UnwindSafe for InsertQuery
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more