Skip to content

Commit c81e6c1

Browse files
committed
dep update
1 parent 6ee7bb0 commit c81e6c1

File tree

3 files changed

+58
-67
lines changed

3 files changed

+58
-67
lines changed

frameworks/Rust/xitca-web/Cargo.lock

Lines changed: 29 additions & 38 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

frameworks/Rust/xitca-web/Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,7 @@ codegen-units = 1
9595
panic = "abort"
9696

9797
[patch.crates-io]
98-
xitca-postgres = { git = "https://github.com/HFQR/xitca-web.git", rev = "7e6534f" }
99-
xitca-postgres-diesel = { git = "https://github.com/fakeshadow/xitca-postgres-diesel", rev = "8addeca" }
98+
xitca-postgres-diesel = { git = "https://github.com/fakeshadow/xitca-postgres-diesel", rev = "5fb1e6d5f0f20cfb5ef0833a44dc209b1f46f1df" }
10099

101100
diesel-async = { git = "https://github.com/weiznich/diesel_async", rev = "5b8262b" }
102101
mio = { git = "https://github.com/fakeshadow/mio", rev = "9bae6012b7ecfc6083350785f71a5e8265358178" }

frameworks/Rust/xitca-web/src/db.rs

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1-
// clippy is dumb and have no idea what should be lazy or not
2-
#![allow(clippy::unnecessary_lazy_evaluations)]
1+
use std::cell::RefCell;
32

43
use xitca_io::bytes::BytesMut;
5-
use xitca_postgres::{pipeline::Pipeline, pool::Pool, types::Type, AsyncLendingIterator};
4+
use xitca_postgres::{
5+
iter::AsyncLendingIterator,
6+
pipeline::Pipeline,
7+
pool::Pool,
8+
statement::{Statement, StatementNamed},
9+
types::Type,
10+
Execute, ExecuteMut,
11+
};
612

713
use super::{
814
ser::{Fortune, Fortunes, World},
@@ -11,17 +17,14 @@ use super::{
1117

1218
pub struct Client {
1319
pool: Pool,
14-
shared: std::cell::RefCell<Shared>,
20+
shared: RefCell<Shared>,
1521
updates: Box<[Box<str>]>,
1622
}
1723

1824
type Shared = (Rand, BytesMut);
1925

20-
const FORTUNE_SQL: &str = "SELECT * FROM fortune";
21-
const FORTUNE_SQL_TYPES: &[Type] = &[];
22-
23-
const WORLD_SQL: &str = "SELECT * FROM world WHERE id=$1";
24-
const WORLD_SQL_TYPES: &[Type] = &[Type::INT4];
26+
const FORTUNE_STMT: StatementNamed = Statement::named("SELECT * FROM fortune", &[]);
27+
const WORLD_STMT: StatementNamed = Statement::named("SELECT * FROM world WHERE id=$1", &[Type::INT4]);
2528

2629
fn update_query(num: usize) -> Box<str> {
2730
bulk_update_gen(|query| {
@@ -35,10 +38,9 @@ fn update_query(num: usize) -> Box<str> {
3538
}
3639

3740
pub async fn create() -> HandleResult<Client> {
38-
let pool = Pool::builder(DB_URL).capacity(1).build()?;
3941
Ok(Client {
40-
pool,
41-
shared: std::cell::RefCell::new((Rand::default(), BytesMut::new())),
42+
pool: Pool::builder(DB_URL).capacity(1).build()?,
43+
shared: RefCell::new((Rand::default(), BytesMut::new())),
4244
updates: core::iter::once(Box::from(""))
4345
.chain((1..=500).map(update_query))
4446
.collect(),
@@ -48,24 +50,24 @@ pub async fn create() -> HandleResult<Client> {
4850
impl Client {
4951
pub async fn get_world(&self) -> HandleResult<World> {
5052
let mut conn = self.pool.get().await?;
51-
let stmt = conn.prepare_cache(WORLD_SQL, WORLD_SQL_TYPES).await?;
53+
let stmt = WORLD_STMT.execute_mut(&mut conn).await?;
5254
let id = self.shared.borrow_mut().0.gen_id();
53-
let mut res = conn.consume().query(stmt.bind([id]))?;
54-
let row = res.try_next().await?.ok_or_else(|| "World does not exist")?;
55+
let mut res = stmt.bind([id]).query(&conn.consume()).await?;
56+
let row = res.try_next().await?.ok_or("request World does not exist")?;
5557
Ok(World::new(row.get(0), row.get(1)))
5658
}
5759

5860
pub async fn get_worlds(&self, num: u16) -> HandleResult<Vec<World>> {
5961
let len = num as usize;
6062

6163
let mut conn = self.pool.get().await?;
62-
let stmt = conn.prepare_cache(WORLD_SQL, WORLD_SQL_TYPES).await?;
64+
let stmt = WORLD_STMT.execute_mut(&mut conn).await?;
6365

6466
let mut res = {
6567
let (ref mut rng, ref mut buf) = *self.shared.borrow_mut();
6668
let mut pipe = Pipeline::with_capacity_from_buf(len, buf);
67-
(0..num).try_for_each(|_| pipe.query(stmt.bind([rng.gen_id()])))?;
68-
conn.consume().pipeline(pipe)?
69+
(0..num).try_for_each(|_| stmt.bind([rng.gen_id()]).query_mut(&mut pipe))?;
70+
pipe.query(&conn.consume())?
6971
};
7072

7173
let mut worlds = Vec::with_capacity(len);
@@ -82,11 +84,10 @@ impl Client {
8284
pub async fn update(&self, num: u16) -> HandleResult<Vec<World>> {
8385
let len = num as usize;
8486

85-
let update = self.updates.get(len).ok_or_else(|| "num out of bound")?;
86-
87+
let update = self.updates.get(len).ok_or("request num is out of range")?;
8788
let mut conn = self.pool.get().await?;
88-
let world_stmt = conn.prepare_cache(WORLD_SQL, WORLD_SQL_TYPES).await?;
89-
let update_stmt = conn.prepare_cache(update, &[]).await?;
89+
let world_stmt = WORLD_STMT.execute_mut(&mut conn).await?;
90+
let update_stmt = Statement::named(update, &[]).execute_mut(&mut conn).await?;
9091

9192
let mut params = Vec::with_capacity(len);
9293

@@ -97,10 +98,10 @@ impl Client {
9798
let w_id = rng.gen_id();
9899
let r_id = rng.gen_id();
99100
params.push([w_id, r_id]);
100-
pipe.query(world_stmt.bind([w_id]))
101+
world_stmt.bind([w_id]).query_mut(&mut pipe)
101102
})?;
102-
pipe.query(update_stmt.bind(sort_update_params(&params)))?;
103-
conn.consume().pipeline(pipe)?
103+
update_stmt.bind(sort_update_params(&params)).query_mut(&mut pipe)?;
104+
pipe.query(&conn.consume())?
104105
};
105106

106107
let mut worlds = Vec::with_capacity(len);
@@ -122,8 +123,8 @@ impl Client {
122123
items.push(Fortune::new(0, "Additional fortune added at request time."));
123124

124125
let mut conn = self.pool.get().await?;
125-
let stmt = conn.prepare_cache(FORTUNE_SQL, FORTUNE_SQL_TYPES).await?;
126-
let mut res = conn.consume().query(&stmt)?;
126+
let stmt = FORTUNE_STMT.execute_mut(&mut conn).await?;
127+
let mut res = stmt.query(&conn.consume()).await?;
127128

128129
while let Some(row) = res.try_next().await? {
129130
items.push(Fortune::new(row.get(0), row.get::<String>(1)));

0 commit comments

Comments
 (0)