pub struct InsertQuery { /* private fields */ }Expand description
INSERT 查询构造器
Implementations§
Source§impl InsertQuery
impl InsertQuery
Sourcepub fn into_table(self, table: &str) -> Self
pub fn into_table(self, table: &str) -> Self
设置目标表
Sourcepub fn on_conflict_do_nothing(self, conflict_cols: &[&str]) -> Self
pub fn on_conflict_do_nothing(self, conflict_cols: &[&str]) -> Self
PostgreSQL/SQLite:ON CONFLICT (cols) DO NOTHING
冲突时忽略插入。适用于 PostgreSQL、SQLite 方言。 MySQL 方言下此设置被忽略(MySQL 不支持 ON CONFLICT 语法)。
§示例
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)],
) -> Self
pub fn on_conflict_do_update( self, conflict_cols: &[&str], assignments: &[(&str, &str)], ) -> Self
PostgreSQL/SQLite:ON CONFLICT (cols) DO UPDATE SET col = expr, ...
冲突时更新指定列。assignments 为 (列名, 表达式) 对。
表达式中可使用 EXCLUDED.col 引用待插入的值(PG/SQLite 标准)。
§示例
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)]) -> Self
pub fn on_duplicate_key_update(self, assignments: &[(&str, &str)]) -> Self
MySQL:ON DUPLICATE KEY UPDATE col = expr, ...
主键/唯一键冲突时更新指定列。assignments 为 (列名, 表达式) 对。
表达式中可使用 VALUES(col) 引用待插入的值(MySQL 语法)。
§示例
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) -> Self
pub fn replace(self) -> Self
MySQL:使用 REPLACE INTO 代替 INSERT INTO
主键/唯一键冲突时先删除旧行再插入新行。 适用于 MySQL/MariaDB/TiDB 方言。
Sourcepub fn returning(self, columns: &[&str]) -> Self
pub fn returning(self, columns: &[&str]) -> Self
设置 RETURNING 子句(PostgreSQL/SQLite 3.35+ 支持)
在 INSERT 后返回指定列的值,常用于获取自增主键或默认值。
MySQL 不支持 RETURNING,在 build()(MySQL 风格)中会被忽略;
在 build_with_dialect() 中仅 PostgreSQL/SQLite 方言渲染。
§示例
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) -> Self
pub fn returning_all(self) -> Self
设置 RETURNING *(返回所有列)
Sourcepub fn build_with_dialect(self, db_type: DbType) -> String
pub fn build_with_dialect(self, db_type: DbType) -> String
按指定方言生成 SQL
§Upsert 方言兼容性
- MySQL 家族:支持
OnDuplicateKeyUpdate、Replace - PostgreSQL/SQLite:支持
OnConflictDoNothing、OnConflictDoUpdate - 不兼容的组合会跳过 upsert 子句(不报错)
Trait Implementations§
Source§impl Clone for InsertQuery
impl Clone for InsertQuery
Source§fn clone(&self) -> InsertQuery
fn clone(&self) -> InsertQuery
Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
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
Returns the “default value” for a type. Read more
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
Mutably borrows from an owned value. Read more