pub struct InsertQuery { /* private fields */ }Expand description
INSERT 查询构造器
Implementations§
Source§impl InsertQuery
impl InsertQuery
Sourcepub fn new() -> InsertQuery
pub fn new() -> InsertQuery
创建空的 INSERT 查询
Sourcepub fn into_table(self, table: &str) -> InsertQuery
pub fn into_table(self, table: &str) -> InsertQuery
设置目标表
Sourcepub fn value(self, column: &str, value: &str) -> InsertQuery
pub fn value(self, column: &str, value: &str) -> InsertQuery
添加列值对(值应为已转义的 SQL 字面量)
Sourcepub fn values(self, pairs: &[(&str, &str)]) -> InsertQuery
pub fn values(self, pairs: &[(&str, &str)]) -> InsertQuery
批量添加列值对
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
冲突时忽略插入。适用于 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)],
) -> 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, ...
冲突时更新指定列。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)],
) -> InsertQuery
pub fn on_duplicate_key_update( self, assignments: &[(&str, &str)], ) -> InsertQuery
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) -> InsertQuery
pub fn replace(self) -> InsertQuery
MySQL:使用 REPLACE INTO 代替 INSERT INTO
主键/唯一键冲突时先删除旧行再插入新行。 适用于 MySQL/MariaDB/TiDB 方言。
Sourcepub fn returning(self, columns: &[&str]) -> InsertQuery
pub fn returning(self, columns: &[&str]) -> InsertQuery
设置 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) -> InsertQuery
pub fn returning_all(self) -> InsertQuery
设置 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
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> ⓘ
Converts
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> ⓘ
Converts
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