pub struct QueryAs<T> { /* private fields */ }Expand description
类型化裸 SQL 查询对象(SQLx query_as! 的 sz-orm 等效物)。
与 Query 的区别:QueryAs<T> 将行映射为 T: FromQueryResult,
提供类型安全的查询结果;Query 返回 Vec<HashMap<String, Value>>。
§用法
ⓘ
use sz_orm_core::queryable::QueryAs;
#[derive(FromQueryResult)]
struct User { id: i64, name: String }
let q = QueryAs::<User>::new("SELECT id, name FROM users WHERE id = ?");
let users: Vec<User> = q.fetch_all(&mut conn).await?;
let one: User = QueryAs::<User>::new("SELECT id, name FROM users LIMIT 1")
.fetch_one(&mut conn)
.await?;Implementations§
Source§impl<T: FromQueryResult> QueryAs<T>
impl<T: FromQueryResult> QueryAs<T>
Sourcepub async fn fetch_all(
&self,
conn: &mut dyn Connection,
) -> Result<Vec<T>, DbError>
pub async fn fetch_all( &self, conn: &mut dyn Connection, ) -> Result<Vec<T>, DbError>
执行查询,返回所有行映射为 Vec<T>
运行时列名验证:比对 DB 返回的列名与 T::row_desc()(由
#[derive(FromQueryResult)] 自动生成)。若 SQL SELECT 列不在 struct 字段中,
返回 DbError::QueryError,避免静默忽略多余列或类型不匹配。
运行时列类型验证(P0-2):比对 DB 返回值的实际类型与
T::column_types() 期望类型。若不兼容(如 DB 返回 TEXT 但 struct 期望
i64),返回 DbError::QueryError,防止静默类型截断。
Sourcepub async fn fetch_one(&self, conn: &mut dyn Connection) -> Result<T, DbError>
pub async fn fetch_one(&self, conn: &mut dyn Connection) -> Result<T, DbError>
执行查询,期望恰好一行;0 行或多行均返回错误
Sourcepub async fn fetch_optional(
&self,
conn: &mut dyn Connection,
) -> Result<Option<T>, DbError>
pub async fn fetch_optional( &self, conn: &mut dyn Connection, ) -> Result<Option<T>, DbError>
执行查询,返回 0 或 1 行;0 行返回 Ok(None)
Trait Implementations§
Auto Trait Implementations§
impl<T> Freeze for QueryAs<T>
impl<T> RefUnwindSafe for QueryAs<T>where
T: RefUnwindSafe,
impl<T> Send for QueryAs<T>where
T: Send,
impl<T> Sync for QueryAs<T>where
T: Sync,
impl<T> Unpin for QueryAs<T>where
T: Unpin,
impl<T> UnsafeUnpin for QueryAs<T>
impl<T> UnwindSafe for QueryAs<T>where
T: UnwindSafe,
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> 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