pub enum UpsertStrategy {
None,
OnConflictDoNothing(Vec<String>),
OnConflictDoUpdate(Vec<String>, Vec<(String, String)>),
OnDuplicateKeyUpdate(Vec<(String, String)>),
Replace,
}Expand description
Upsert(插入或更新)策略
支持三种主流数据库的冲突处理语法:
- PostgreSQL/SQLite:
ON CONFLICT ... DO NOTHING/DO UPDATE - MySQL/MariaDB/TiDB:
ON DUPLICATE KEY UPDATE/REPLACE INTO
§方言兼容性
| 策略 | MySQL | PostgreSQL | SQLite |
|---|---|---|---|
OnConflictDoNothing | ✗ | ✓ | ✓ |
OnConflictDoUpdate | ✗ | ✓ | ✓ |
OnDuplicateKeyUpdate | ✓ | ✗ | ✗ |
Replace | ✓ | ✗ | ✓(INSERT OR REPLACE) |
不兼容的组合会在 build_with_dialect 中跳过 upsert 子句(不报错,由调用方确保方言匹配)。
Variants§
None
不使用 upsert(默认)
OnConflictDoNothing(Vec<String>)
PostgreSQL/SQLite:ON CONFLICT (cols) DO NOTHING
OnConflictDoUpdate(Vec<String>, Vec<(String, String)>)
PostgreSQL/SQLite:ON CONFLICT (cols) DO UPDATE SET col = expr, ...
(conflict_cols, update_assignments)
OnDuplicateKeyUpdate(Vec<(String, String)>)
MySQL:ON DUPLICATE KEY UPDATE col = expr, ...
Replace
MySQL:REPLACE INTO(先删除冲突行再插入)
Trait Implementations§
Source§impl Clone for UpsertStrategy
impl Clone for UpsertStrategy
Source§fn clone(&self) -> UpsertStrategy
fn clone(&self) -> UpsertStrategy
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 UpsertStrategy
impl Debug for UpsertStrategy
Source§impl Default for UpsertStrategy
impl Default for UpsertStrategy
Source§fn default() -> UpsertStrategy
fn default() -> UpsertStrategy
Returns the “default value” for a type. Read more
Auto Trait Implementations§
impl Freeze for UpsertStrategy
impl RefUnwindSafe for UpsertStrategy
impl Send for UpsertStrategy
impl Sync for UpsertStrategy
impl Unpin for UpsertStrategy
impl UnsafeUnpin for UpsertStrategy
impl UnwindSafe for UpsertStrategy
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