cargo fmt
This commit is contained in:
parent
937af05003
commit
b818138724
@ -1,4 +1,4 @@
|
|||||||
pub use super::cake::Entity as Cake;
|
pub use super::cake::Entity as Cake;
|
||||||
pub use super::cake_filling::Entity as CakeFilling;
|
pub use super::cake_filling::Entity as CakeFilling;
|
||||||
pub use super::filling::Entity as Filling;
|
pub use super::filling::Entity as Filling;
|
||||||
pub use super::fruit::Entity as Fruit;
|
pub use super::fruit::Entity as Fruit;
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
use sea_orm::{entity::*, query::*, Database};
|
|
||||||
use super::*;
|
use super::*;
|
||||||
|
use sea_orm::{entity::*, query::*, Database};
|
||||||
|
|
||||||
pub async fn all_about_operation(db: &Database) -> Result<(), ExecErr> {
|
pub async fn all_about_operation(db: &Database) -> Result<(), ExecErr> {
|
||||||
insert_and_update(db).await?;
|
insert_and_update(db).await?;
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
use sea_orm::{entity::*, query::*, Database, FromQueryResult};
|
|
||||||
use super::*;
|
use super::*;
|
||||||
|
use sea_orm::{entity::*, query::*, Database, FromQueryResult};
|
||||||
|
|
||||||
pub async fn all_about_select(db: &Database) -> Result<(), QueryErr> {
|
pub async fn all_about_select(db: &Database) -> Result<(), QueryErr> {
|
||||||
find_all(db).await?;
|
find_all(db).await?;
|
||||||
@ -66,10 +66,7 @@ async fn find_all(db: &Database) -> Result<(), QueryErr> {
|
|||||||
async fn find_together(db: &Database) -> Result<(), QueryErr> {
|
async fn find_together(db: &Database) -> Result<(), QueryErr> {
|
||||||
print!("find cakes and fruits: ");
|
print!("find cakes and fruits: ");
|
||||||
|
|
||||||
let both = Cake::find()
|
let both = Cake::find().left_join_and_select(Fruit).all(db).await?;
|
||||||
.left_join_and_select(Fruit)
|
|
||||||
.all(db)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
println!();
|
println!();
|
||||||
for bb in both.iter() {
|
for bb in both.iter() {
|
||||||
@ -141,10 +138,7 @@ async fn count_fruits_by_cake(db: &Database) -> Result<(), QueryErr> {
|
|||||||
async fn find_many_to_many(db: &Database) -> Result<(), QueryErr> {
|
async fn find_many_to_many(db: &Database) -> Result<(), QueryErr> {
|
||||||
print!("find cakes and fillings: ");
|
print!("find cakes and fillings: ");
|
||||||
|
|
||||||
let both = Cake::find()
|
let both = Cake::find().left_join_and_select(Filling).all(db).await?;
|
||||||
.left_join_and_select(Filling)
|
|
||||||
.all(db)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
println!();
|
println!();
|
||||||
for bb in both.iter() {
|
for bb in both.iter() {
|
||||||
@ -246,9 +240,9 @@ async fn count_fruits_by_cake_json(db: &Database) -> Result<(), QueryErr> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async fn find_all_stream(db: &Database) -> Result<(), QueryErr> {
|
async fn find_all_stream(db: &Database) -> Result<(), QueryErr> {
|
||||||
|
use async_std::task::sleep;
|
||||||
use futures::TryStreamExt;
|
use futures::TryStreamExt;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use async_std::task::sleep;
|
|
||||||
|
|
||||||
println!("find all cakes: ");
|
println!("find all cakes: ");
|
||||||
let mut cake_paginator = cake::Entity::find().paginate(db, 2);
|
let mut cake_paginator = cake::Entity::find().paginate(db, 2);
|
||||||
@ -279,7 +273,10 @@ async fn find_all_stream(db: &Database) -> Result<(), QueryErr> {
|
|||||||
|
|
||||||
println!();
|
println!();
|
||||||
println!("find all fruits in json with stream: ");
|
println!("find all fruits in json with stream: ");
|
||||||
let mut json_stream = fruit::Entity::find().into_json().paginate(db, 2).into_stream();
|
let mut json_stream = fruit::Entity::find()
|
||||||
|
.into_json()
|
||||||
|
.paginate(db, 2)
|
||||||
|
.into_stream();
|
||||||
while let Some(jsons) = json_stream.try_next().await? {
|
while let Some(jsons) = json_stream.try_next().await? {
|
||||||
for json in jsons {
|
for json in jsons {
|
||||||
println!("{:?}", json);
|
println!("{:?}", json);
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
use crate::{Connection, Database, QueryErr, SelectorTrait};
|
use crate::{Connection, Database, QueryErr, SelectorTrait};
|
||||||
use futures::Stream;
|
|
||||||
use async_stream::stream;
|
use async_stream::stream;
|
||||||
use std::{marker::PhantomData, pin::Pin};
|
use futures::Stream;
|
||||||
use sea_query::{Alias, Expr, SelectStatement};
|
use sea_query::{Alias, Expr, SelectStatement};
|
||||||
|
use std::{marker::PhantomData, pin::Pin};
|
||||||
|
|
||||||
pub type PinBoxStream<'db, Item> = Pin<Box<dyn Stream<Item = Item> + 'db>>;
|
pub type PinBoxStream<'db, Item> = Pin<Box<dyn Stream<Item = Item> + 'db>>;
|
||||||
|
|
||||||
@ -23,7 +23,9 @@ where
|
|||||||
S: SelectorTrait + 'db,
|
S: SelectorTrait + 'db,
|
||||||
{
|
{
|
||||||
pub async fn fetch_page(&mut self, page: usize) -> Result<Vec<S::Item>, QueryErr> {
|
pub async fn fetch_page(&mut self, page: usize) -> Result<Vec<S::Item>, QueryErr> {
|
||||||
self.query.limit(self.page_size as u64).offset((self.page_size * page) as u64);
|
self.query
|
||||||
|
.limit(self.page_size as u64)
|
||||||
|
.offset((self.page_size * page) as u64);
|
||||||
let builder = self.db.get_query_builder_backend();
|
let builder = self.db.get_query_builder_backend();
|
||||||
let stmt = self.query.build(builder).into();
|
let stmt = self.query.build(builder).into();
|
||||||
let rows = self.db.get_connection().query_all(stmt).await?;
|
let rows = self.db.get_connection().query_all(stmt).await?;
|
||||||
@ -45,7 +47,7 @@ where
|
|||||||
.expr(Expr::cust("COUNT(*) AS num_rows"))
|
.expr(Expr::cust("COUNT(*) AS num_rows"))
|
||||||
.from_subquery(
|
.from_subquery(
|
||||||
self.query.clone().reset_limit().reset_offset().to_owned(),
|
self.query.clone().reset_limit().reset_offset().to_owned(),
|
||||||
Alias::new("sub_query")
|
Alias::new("sub_query"),
|
||||||
)
|
)
|
||||||
.build(builder)
|
.build(builder)
|
||||||
.into();
|
.into();
|
||||||
@ -53,7 +55,9 @@ where
|
|||||||
Some(res) => res,
|
Some(res) => res,
|
||||||
None => return Ok(0),
|
None => return Ok(0),
|
||||||
};
|
};
|
||||||
let num_rows = result.try_get::<i32>("", "num_rows").map_err(|_e| QueryErr)? as usize;
|
let num_rows = result
|
||||||
|
.try_get::<i32>("", "num_rows")
|
||||||
|
.map_err(|_e| QueryErr)? as usize;
|
||||||
let num_pages = (num_rows / self.page_size) + (num_rows % self.page_size > 0) as usize;
|
let num_pages = (num_rows / self.page_size) + (num_rows % self.page_size > 0) as usize;
|
||||||
Ok(num_pages)
|
Ok(num_pages)
|
||||||
}
|
}
|
||||||
@ -65,11 +69,7 @@ where
|
|||||||
pub async fn fetch_and_next(&mut self) -> Result<Option<Vec<S::Item>>, QueryErr> {
|
pub async fn fetch_and_next(&mut self) -> Result<Option<Vec<S::Item>>, QueryErr> {
|
||||||
let vec = self.fetch().await?;
|
let vec = self.fetch().await?;
|
||||||
self.next();
|
self.next();
|
||||||
let opt = if !vec.is_empty() {
|
let opt = if !vec.is_empty() { Some(vec) } else { None };
|
||||||
Some(vec)
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
};
|
|
||||||
Ok(opt)
|
Ok(opt)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,4 +1,7 @@
|
|||||||
use crate::{Connection, Database, EntityTrait, FromQueryResult, JsonValue, Paginator, QueryErr, QueryResult, Select, SelectTwo, Statement, TypeErr, query::combine};
|
use crate::{
|
||||||
|
query::combine, Connection, Database, EntityTrait, FromQueryResult, JsonValue, Paginator,
|
||||||
|
QueryErr, QueryResult, Select, SelectTwo, Statement, TypeErr,
|
||||||
|
};
|
||||||
use sea_query::{QueryBuilder, SelectStatement};
|
use sea_query::{QueryBuilder, SelectStatement};
|
||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
|
|
||||||
@ -89,7 +92,11 @@ where
|
|||||||
self.into_model::<E::Model>().all(db).await
|
self.into_model::<E::Model>().all(db).await
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn paginate<'db>(self, db: &'db Database, page_size: usize) -> Paginator<'db, SelectModel<E::Model>> {
|
pub fn paginate<'db>(
|
||||||
|
self,
|
||||||
|
db: &'db Database,
|
||||||
|
page_size: usize,
|
||||||
|
) -> Paginator<'db, SelectModel<E::Model>> {
|
||||||
self.into_model::<E::Model>().paginate(db, page_size)
|
self.into_model::<E::Model>().paginate(db, page_size)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -18,7 +18,10 @@ impl FromQueryResult for JsonValue {
|
|||||||
macro_rules! match_mysql_type {
|
macro_rules! match_mysql_type {
|
||||||
( $type: ty ) => {
|
( $type: ty ) => {
|
||||||
if <$type as Type<MySql>>::type_info().eq(col_type) {
|
if <$type as Type<MySql>>::type_info().eq(col_type) {
|
||||||
map.insert(col.to_owned(), json!(res.try_get::<Option<$type>>(pre, &col)?));
|
map.insert(
|
||||||
|
col.to_owned(),
|
||||||
|
json!(res.try_get::<Option<$type>>(pre, &col)?),
|
||||||
|
);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user