< 返回版块

LH-ming 发表于 2022-09-28 10:17

Tags:sqlx,

sqlx FromRow trait

pub trait FromRow<'r, R: Row>: Sized {

fn from_row(row: &'r R) -> Result<Self, Error>;

}

async fn query(pool:Pool,sql:&str)->Result<Vec<大t>>

where T: //这里不会写FromRow约束

{

let data:Vec<T>=sqlx::query_as(sql)
    .fetch_all(&pool)
    .await?;
Ok(data) 

}

请前辈指点! 栓q!

评论区

写评论
作者 LH-ming 2022-09-30 10:02

谢谢解惑,已经解决,崇拜你。昨天就在crates里,看到了你的sqlx-model很牛逼。再次感谢。

--
👇
shanliu: ``` pub async fn fetch_one_by_scalar_pk<'c,M,PT,E>( &self, pk_scalar: PT, executor: E, ) ->Result<M,Error> where for<'q> PT:'q+ Send + sqlx::Encode<'q, DB> + sqlx::Type, for<'r> M: FromRow<'r, DB::Row>+Send+Unpin+ModelTableField, for<'n> <DB as HasArguments<'n>>::Arguments: Arguments<'n>+IntoArguments<'n,DB>, E: Executor<'c, Database = DB> { let where_sql=scalar_pk_where!(DB,self.table_pk); let sql=format!( "SELECT {} FROM {} WHERE {}", self.table_field.to_vec().join(","), self.table_name.full_name(), where_sql ); let mut res=sqlx::query_as::<DB, M,>(sql.as_str()); res=res.bind(pk_scalar); res.fetch_one(executor).await }

https://github.com/shanliu/sqlx-model/blob/260519613335a1176690941be1d9c880b4a72871/src/curd/select.rs#L333
作者 LH-ming 2022-09-30 09:55

大佬谢谢你的讲解,已经解决。又收获了知识点。么么。再次感谢

--
👇
苦瓜小仔: 遵循编译器、RA 和 clippy 的提示编程。

RustExplore

use sqlx::*;

pub trait FromRow<'r, R: Row>: Sized {
    fn from_row(row: &'r R) -> Result<Self>;
}

async fn _query<T, DB: Database>(pool: Pool<DB>, sql: &str) -> Result<Vec<T>>
    where T: for<'r> sqlx::FromRow<'r, <DB as Database>::Row>
              + std::marker::Unpin
              + Send
              + Sync,
          for<'c> &'c mut DB::Connection: Executor<'c, Database = DB>,
          for<'a> <DB as database::HasArguments<'a>>::Arguments: IntoArguments<'a, DB>
{
    let data: Vec<T> = query_as(sql).fetch_all(&pool).await?;
    Ok(data)
}
github.com/shanliu/lsys 2022-09-29 16:23
        &self,
        pk_scalar: PT,
        executor: E,
    )
        ->Result<M,Error>
        where
        for<'q> PT:'q+ Send + sqlx::Encode<'q, DB> + sqlx::Type<DB>,
        for<'r> M:  FromRow<'r, DB::Row>+Send+Unpin+ModelTableField<DB>,
        for<'n> <DB as HasArguments<'n>>::Arguments:
            Arguments<'n>+IntoArguments<'n,DB>,
        E: Executor<'c, Database = DB>
    {
        let where_sql=scalar_pk_where!(DB,self.table_pk);
        let sql=format!(
            "SELECT {} FROM {} WHERE {}",
            self.table_field.to_vec().join(","),
            self.table_name.full_name(),
            where_sql
        );
        let mut res=sqlx::query_as::<DB, M,>(sql.as_str());
        res=res.bind(pk_scalar);
        res.fetch_one(executor).await
    }

https://github.com/shanliu/sqlx-model/blob/260519613335a1176690941be1d9c880b4a72871/src/curd/select.rs#L333

苦瓜小仔 2022-09-28 23:54

遵循编译器、RA 和 clippy 的提示编程。

RustExplore

use sqlx::*;

pub trait FromRow<'r, R: Row>: Sized {
    fn from_row(row: &'r R) -> Result<Self>;
}

async fn _query<T, DB: Database>(pool: Pool<DB>, sql: &str) -> Result<Vec<T>>
    where T: for<'r> sqlx::FromRow<'r, <DB as Database>::Row>
              + std::marker::Unpin
              + Send
              + Sync,
          for<'c> &'c mut DB::Connection: Executor<'c, Database = DB>,
          for<'a> <DB as database::HasArguments<'a>>::Arguments: IntoArguments<'a, DB>
{
    let data: Vec<T> = query_as(sql).fetch_all(&pool).await?;
    Ok(data)
}
1 共 4 条评论, 1 页