Change one() to return Option<Model>
This commit is contained in:
parent
1a52e64572
commit
744d62a310
@ -30,7 +30,7 @@ pub async fn insert_and_update(db: &Database) -> Result<(), ExecErr> {
|
|||||||
println!();
|
println!();
|
||||||
println!("Pear: {:?}\n", pear);
|
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());
|
pear.name = Val::set("Sweet pear".to_owned());
|
||||||
|
|
||||||
let res = fruit::Entity::update(pear).exec(db).await?;
|
let res = fruit::Entity::update(pear).exec(db).await?;
|
||||||
|
@ -70,7 +70,8 @@ async fn find_together(db: &Database) -> Result<(), QueryErr> {
|
|||||||
async fn find_one(db: &Database) -> Result<(), QueryErr> {
|
async fn find_one(db: &Database) -> Result<(), QueryErr> {
|
||||||
print!("find one by primary key: ");
|
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!();
|
||||||
println!("{:?}", cheese);
|
println!("{:?}", cheese);
|
||||||
@ -142,22 +143,26 @@ async fn find_many_to_many(db: &Database) -> Result<(), QueryErr> {
|
|||||||
|
|
||||||
let cheese = cake::Entity::find_by(1).one(db).await?;
|
let cheese = cake::Entity::find_by(1).one(db).await?;
|
||||||
|
|
||||||
let fillings: Vec<filling::Model> = cheese.find_filling().all(db).await?;
|
if let Some(cheese) = cheese {
|
||||||
|
let fillings: Vec<filling::Model> = cheese.find_filling().all(db).await?;
|
||||||
|
|
||||||
println!();
|
println!();
|
||||||
for ff in fillings.iter() {
|
for ff in fillings.iter() {
|
||||||
println!("{:?}\n", ff);
|
println!("{:?}\n", ff);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
print!("find cakes for lemon: ");
|
print!("find cakes for lemon: ");
|
||||||
|
|
||||||
let lemon = filling::Entity::find_by(2).one(db).await?;
|
let lemon = filling::Entity::find_by(2).one(db).await?;
|
||||||
|
|
||||||
let cakes: Vec<cake::Model> = lemon.find_cake().all(db).await?;
|
if let Some(lemon) = lemon {
|
||||||
|
let cakes: Vec<cake::Model> = lemon.find_cake().all(db).await?;
|
||||||
|
|
||||||
println!();
|
println!();
|
||||||
for cc in cakes.iter() {
|
for cc in cakes.iter() {
|
||||||
println!("{:?}\n", cc);
|
println!("{:?}\n", cc);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -23,7 +23,7 @@ pub trait Connector {
|
|||||||
pub trait Connection {
|
pub trait Connection {
|
||||||
async fn execute(&self, stmt: Statement) -> Result<ExecResult, ExecErr>;
|
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>;
|
async fn query_all(&self, stmt: Statement) -> Result<Vec<QueryResult>, QueryErr>;
|
||||||
}
|
}
|
||||||
|
@ -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
|
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
|
self.into_model::<E::Model, F::Model>().one(db).await
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -137,11 +137,14 @@ where
|
|||||||
self.query.build(builder).into()
|
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();
|
let builder = db.get_query_builder_backend();
|
||||||
self.query.limit(1);
|
self.query.limit(1);
|
||||||
let row = db.get_connection().query_one(self.build(builder)).await?;
|
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> {
|
pub async fn all(self, db: &Database) -> Result<Vec<S::Item>, QueryErr> {
|
||||||
|
@ -46,16 +46,19 @@ impl Connection for &SqlxMySqlPoolConnection {
|
|||||||
Err(ExecErr)
|
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);
|
debug_print!("{}", stmt);
|
||||||
|
|
||||||
let query = sqlx_query(&stmt);
|
let query = sqlx_query(&stmt);
|
||||||
if let Ok(conn) = &mut self.pool.acquire().await {
|
if let Ok(conn) = &mut self.pool.acquire().await {
|
||||||
if let Ok(row) = query.fetch_one(conn).await {
|
if let Ok(row) = query.fetch_one(conn).await {
|
||||||
return Ok(row.into());
|
Ok(Some(row.into()))
|
||||||
|
} else {
|
||||||
|
Ok(None)
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
Err(QueryErr)
|
||||||
}
|
}
|
||||||
Err(QueryErr)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn query_all(&self, stmt: Statement) -> Result<Vec<QueryResult>, QueryErr> {
|
async fn query_all(&self, stmt: Statement) -> Result<Vec<QueryResult>, QueryErr> {
|
||||||
|
@ -222,8 +222,11 @@ where
|
|||||||
if res.last_insert_id != 0 {
|
if res.last_insert_id != 0 {
|
||||||
let find = E::find_by(res.last_insert_id).one(db);
|
let find = E::find_by(res.last_insert_id).one(db);
|
||||||
let res = find.await;
|
let res = find.await;
|
||||||
let model: E::Model = res.map_err(|_| ExecErr)?;
|
let model: Option<E::Model> = res.map_err(|_| ExecErr)?;
|
||||||
Ok(model.into())
|
match model {
|
||||||
|
Some(model) => Ok(model.into()),
|
||||||
|
None => Err(ExecErr),
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
Ok(A::default())
|
Ok(A::default())
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user