Change one() to return Option<Model>

This commit is contained in:
Chris Tsang 2021-06-05 23:44:11 +08:00
parent 1a52e64572
commit 744d62a310
6 changed files with 34 additions and 20 deletions

View File

@ -30,7 +30,7 @@ pub async fn insert_and_update(db: &Database) -> Result<(), ExecErr> {
println!();
println!("Pear: {:?}\n", pear);
let mut pear: fruit::ActiveModel = pear.into();
let mut pear: fruit::ActiveModel = pear.unwrap().into();
pear.name = Val::set("Sweet pear".to_owned());
let res = fruit::Entity::update(pear).exec(db).await?;

View File

@ -70,7 +70,8 @@ async fn find_together(db: &Database) -> Result<(), QueryErr> {
async fn find_one(db: &Database) -> Result<(), QueryErr> {
print!("find one by primary key: ");
let cheese = cake::Entity::find_by(1).one(db).await?;
let cheese: Option<cake::Model> = cake::Entity::find_by(1).one(db).await?;
let cheese = cheese.unwrap();
println!();
println!("{:?}", cheese);
@ -142,23 +143,27 @@ async fn find_many_to_many(db: &Database) -> Result<(), QueryErr> {
let cheese = cake::Entity::find_by(1).one(db).await?;
if let Some(cheese) = cheese {
let fillings: Vec<filling::Model> = cheese.find_filling().all(db).await?;
println!();
for ff in fillings.iter() {
println!("{:?}\n", ff);
}
}
print!("find cakes for lemon: ");
let lemon = filling::Entity::find_by(2).one(db).await?;
if let Some(lemon) = lemon {
let cakes: Vec<cake::Model> = lemon.find_cake().all(db).await?;
println!();
for cc in cakes.iter() {
println!("{:?}\n", cc);
}
}
Ok(())
}

View File

@ -23,7 +23,7 @@ pub trait Connector {
pub trait Connection {
async fn execute(&self, stmt: Statement) -> Result<ExecResult, ExecErr>;
async fn query_one(&self, stmt: Statement) -> Result<QueryResult, QueryErr>;
async fn query_one(&self, stmt: Statement) -> Result<Option<QueryResult>, QueryErr>;
async fn query_all(&self, stmt: Statement) -> Result<Vec<QueryResult>, QueryErr>;
}

View File

@ -84,7 +84,7 @@ where
}
}
pub async fn one(self, db: &Database) -> Result<E::Model, QueryErr> {
pub async fn one(self, db: &Database) -> Result<Option<E::Model>, QueryErr> {
self.into_model::<E::Model>().one(db).await
}
@ -117,7 +117,7 @@ where
}
}
pub async fn one(self, db: &Database) -> Result<(E::Model, F::Model), QueryErr> {
pub async fn one(self, db: &Database) -> Result<Option<(E::Model, F::Model)>, QueryErr> {
self.into_model::<E::Model, F::Model>().one(db).await
}
@ -137,11 +137,14 @@ where
self.query.build(builder).into()
}
pub async fn one(mut self, db: &Database) -> Result<S::Item, QueryErr> {
pub async fn one(mut self, db: &Database) -> Result<Option<S::Item>, QueryErr> {
let builder = db.get_query_builder_backend();
self.query.limit(1);
let row = db.get_connection().query_one(self.build(builder)).await?;
Ok(S::from_raw_query_result(row)?)
match row {
Some(row) => Ok(Some(S::from_raw_query_result(row)?)),
None => Ok(None),
}
}
pub async fn all(self, db: &Database) -> Result<Vec<S::Item>, QueryErr> {

View File

@ -46,17 +46,20 @@ impl Connection for &SqlxMySqlPoolConnection {
Err(ExecErr)
}
async fn query_one(&self, stmt: Statement) -> Result<QueryResult, QueryErr> {
async fn query_one(&self, stmt: Statement) -> Result<Option<QueryResult>, QueryErr> {
debug_print!("{}", stmt);
let query = sqlx_query(&stmt);
if let Ok(conn) = &mut self.pool.acquire().await {
if let Ok(row) = query.fetch_one(conn).await {
return Ok(row.into());
}
Ok(Some(row.into()))
} else {
Ok(None)
}
} else {
Err(QueryErr)
}
}
async fn query_all(&self, stmt: Statement) -> Result<Vec<QueryResult>, QueryErr> {
debug_print!("{}", stmt);

View File

@ -222,8 +222,11 @@ where
if res.last_insert_id != 0 {
let find = E::find_by(res.last_insert_id).one(db);
let res = find.await;
let model: E::Model = res.map_err(|_| ExecErr)?;
Ok(model.into())
let model: Option<E::Model> = res.map_err(|_| ExecErr)?;
match model {
Some(model) => Ok(model.into()),
None => Err(ExecErr),
}
} else {
Ok(A::default())
}