pub struct Db { /* private fields */ }Expand description
Implementations§
Source§impl Db
impl Db
Sourcepub fn sqlite(pool: SqlitePool) -> Self
pub fn sqlite(pool: SqlitePool) -> Self
从 SQLite 连接池创建 Db 实例
Sourcepub fn mysql_pool(&self) -> &MySqlPool
pub fn mysql_pool(&self) -> &MySqlPool
获取 MySQL 连接池引用(非 MySQL 则 panic)
Sourcepub fn sqlite_pool(&self) -> &SqlitePool
pub fn sqlite_pool(&self) -> &SqlitePool
获取 SQLite 连接池引用(非 SQLite 则 panic)
Sourcepub async fn query_one(
&self,
sql: &str,
params: &[&str],
) -> DbResult<Option<Row>>
pub async fn query_one( &self, sql: &str, params: &[&str], ) -> DbResult<Option<Row>>
执行原始 SQL 查询(单条),返回 Option<Row>
参数使用 $1、$2 占位符,按顺序绑定。
Sourcepub async fn query(&self, sql: &str, params: &[&str]) -> DbResult<Vec<Row>>
pub async fn query(&self, sql: &str, params: &[&str]) -> DbResult<Vec<Row>>
执行原始 SQL 查询(多条),返回 Vec<Row>
Sourcepub async fn query_page(
&self,
sql: &str,
params: &[&str],
page: &PageQuery,
) -> DbResult<(Vec<Row>, u64)>
pub async fn query_page( &self, sql: &str, params: &[&str], page: &PageQuery, ) -> DbResult<(Vec<Row>, u64)>
分页查询:自动包裹 COUNT 和 LIMIT/OFFSET
返回 (数据列表, 总条数)。传入的 SQL 应为无 LIMIT/OFFSET 的完整查询。
Sourcepub async fn count(&self, sql: &str, params: &[&str]) -> DbResult<u64>
pub async fn count(&self, sql: &str, params: &[&str]) -> DbResult<u64>
执行 COUNT 查询,返回行数(自动转换为 u64)
Sourcepub async fn insert(&self, row: &Row) -> DbResult<Row>
pub async fn insert(&self, row: &Row) -> DbResult<Row>
插入单条记录(Row 需设置 table 和字段值)
PostgreSQL 使用 RETURNING * 直接返回插入后的完整行;
MySQL/SQLite 插入后通过主键回查。
§错误
- Row 缺少表名 →
Argument错误 - 没有变更字段 →
Argument错误
Sourcepub async fn batch_insert(&self, rows: &[Row]) -> DbResult<u64>
pub async fn batch_insert(&self, rows: &[Row]) -> DbResult<u64>
批量插入记录,返回受影响行数
所有 Row 必须来自同一张表且变更字段一致。
Sourcepub async fn update(&self, row: &Row) -> DbResult<Option<Row>>
pub async fn update(&self, row: &Row) -> DbResult<Option<Row>>
更新单条记录(根据 Row 的 changes 和主键)
仅更新 changes 中标记的字段,主键必须存在于 data 中。
PostgreSQL 使用 RETURNING * 返回更新后的行。
Sourcepub async fn batch_update(
&self,
table: &str,
sets: &Row,
where_sql: &str,
where_params: &[&str],
) -> DbResult<u64>
pub async fn batch_update( &self, table: &str, sets: &Row, where_sql: &str, where_params: &[&str], ) -> DbResult<u64>
批量更新(按 WHERE 条件),返回受影响行数
sets: 要更新的字段值(Row 含 changes)where_sql: WHERE 子句(不含WHERE关键字),参数使用$1占位符where_params: WHERE 子句的参数值
Sourcepub async fn delete_by_id(
&self,
table: &str,
id: impl Into<Value>,
) -> DbResult<bool>
pub async fn delete_by_id( &self, table: &str, id: impl Into<Value>, ) -> DbResult<bool>
按主键删除记录,返回是否成功删除
Sourcepub async fn batch_delete_by_ids(
&self,
table: &str,
ids: &[impl AsRef<str>],
) -> DbResult<u64>
pub async fn batch_delete_by_ids( &self, table: &str, ids: &[impl AsRef<str>], ) -> DbResult<u64>
批量按主键删除,返回受影响行数
Sourcepub async fn execute(&self, sql: &str, params: &[&str]) -> DbResult<u64>
pub async fn execute(&self, sql: &str, params: &[&str]) -> DbResult<u64>
执行 INSERT/UPDATE/DELETE 等写操作,返回受影响行数
Sourcepub async fn transaction<F, Fut, T>(&self, f: F) -> DbResult<T>
pub async fn transaction<F, Fut, T>(&self, f: F) -> DbResult<T>
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Db
impl !RefUnwindSafe for Db
impl Send for Db
impl Sync for Db
impl Unpin for Db
impl UnsafeUnpin for Db
impl !UnwindSafe for Db
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
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