Skip to content

Commit e38be0d

Browse files
authored
Use trait instead of two equal structs
1 parent 52d793d commit e38be0d

File tree

5 files changed

+220
-347
lines changed

5 files changed

+220
-347
lines changed

rust/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ tokio-postgres = "0.7.6"
99
criterion = "0.3.6"
1010
platform-data = "0.1.0-alpha.1"
1111
doublets = "0.1.0-pre+beta.15"
12+
async-trait = "0.1.57"
1213

1314
[[bench]]
1415
name = "bench"

rust/src/client.rs

Lines changed: 16 additions & 174 deletions
Original file line numberDiff line numberDiff line change
@@ -1,190 +1,32 @@
1+
use crate::cruds::Cruds;
12
use crate::Transaction;
2-
use platform_data::LinksConstants;
3-
use postgres::{Error, Row};
3+
use async_trait::async_trait;
4+
use postgres::Error;
45
use tokio_postgres as postgres;
56

6-
pub struct Client {
7-
index: u64,
8-
client: postgres::Client,
9-
}
7+
pub struct Client(u64, postgres::Client);
108

119
impl Client {
1210
pub async fn new(client: postgres::Client) -> Result<Client, Error> {
1311
let index = client.query("SELECT * FROM Links;", &[]).await?.len() as u64;
14-
Ok(Self { index, client })
12+
Ok(Self(index, client))
1513
}
1614

1715
pub async fn transaction(&mut self) -> Result<Transaction<'_>, Error> {
18-
let transaction = self.client.transaction().await.unwrap();
19-
Ok(Transaction::new(transaction, self.index))
20-
}
21-
22-
pub async fn create(&mut self, substitution: &[u64]) -> Result<Vec<Row>, Error> {
23-
self.index += 1;
24-
self.client
25-
.query(
26-
&format!(
27-
"INSERT INTO Links VALUES ({}, {}, {});",
28-
self.index, substitution[0], substitution[1]
29-
),
30-
&[],
31-
)
32-
.await
33-
}
34-
35-
pub async fn count(&self, restriction: &[u64]) -> Result<i64, Error> {
36-
let mut result = Vec::new();
37-
let any = LinksConstants::new().any;
38-
match restriction[..] {
39-
[any_id, any_source, any_target]
40-
if any_id == any && any_id == any_source && any_source == any_target =>
41-
{
42-
result = self
43-
.client
44-
.query("SELECT COUNT (*) FROM Links;", &[])
45-
.await?;
46-
}
47-
[any_id, source, any_target] if any_id == any && any_id == any_target => {
48-
result = self
49-
.client
50-
.query(
51-
&format!("SELECT COUNT (*) FROM Links WHERE from_id = {};", source),
52-
&[],
53-
)
54-
.await?;
55-
}
56-
[any_id, any_source, target] if any_id == any && any_id == any_source => {
57-
result = self
58-
.client
59-
.query(
60-
&format!("SELECT COUNT (*) FROM Links WHERE to_id = {};", target),
61-
&[],
62-
)
63-
.await?;
64-
}
65-
[id, any_source, any_target] if any_source == any && any_source == any_target => {
66-
result = self
67-
.client
68-
.query(
69-
&format!("SELECT COUNT (*) FROM Links WHERE id = {}", id),
70-
&[],
71-
)
72-
.await?;
73-
}
74-
[any_id, source, target] if any_id == any => {
75-
result = self
76-
.client
77-
.query(
78-
&format!(
79-
"SELECT COUNT (*) FROM Links WHERE from_id = {} AND to_id = {};",
80-
source, target
81-
),
82-
&[],
83-
)
84-
.await?;
85-
}
86-
[] | [_, ..] => panic!("Constraints violation, use 'any'"),
87-
}
88-
Ok(result[0].get(0))
89-
}
90-
91-
pub async fn update(
92-
&self,
93-
restriction: &[u64],
94-
substitution: &[u64],
95-
) -> Result<Vec<Row>, Error> {
96-
if restriction.len() == 1 || restriction.len() == 3 {
97-
self.client
98-
.query(
99-
&format!(
100-
"UPDATE Links SET from_id = {}, to_id = {} WHERE id = {};",
101-
substitution[0], substitution[1], restriction[0]
102-
),
103-
&[],
104-
)
105-
.await
106-
} else {
107-
self.client
108-
.query(
109-
&format!("UPDATE Links SET from_id = {}, to_id = {} WHERE from_id = {} AND to_id = {};",
110-
substitution[0],
111-
substitution[1],
112-
restriction[0],
113-
restriction[1]),
114-
&[],
115-
)
116-
.await
117-
}
16+
let transaction = self.1.transaction().await.unwrap();
17+
Ok(Transaction::new(transaction, self.0))
11818
}
19+
}
11920

120-
pub async fn delete(&self, restriction: &[u64]) -> Result<Vec<Row>, Error> {
121-
if restriction.len() == 1 || restriction.len() == 3 {
122-
self.client
123-
.query(
124-
&format!("DELETE FROM Links WHERE id = {};", restriction[0]),
125-
&[],
126-
)
127-
.await
128-
} else {
129-
self.client
130-
.query(
131-
&format!(
132-
"DELETE FROM Links WHERE from_id = {} AND to_id = {};",
133-
restriction[0], restriction[1]
134-
),
135-
&[],
136-
)
137-
.await
138-
}
21+
#[async_trait]
22+
impl Cruds<'_, postgres::Client> for Client {
23+
#[inline]
24+
fn zero(&mut self) -> &mut u64 {
25+
&mut self.0
13926
}
14027

141-
pub async fn each(&self, restriction: &[u64]) -> Result<Vec<Row>, Error> {
142-
let mut result = Vec::new();
143-
let any = LinksConstants::new().any;
144-
match restriction[..] {
145-
[any_id, any_source, any_target]
146-
if any_id == any && any_id == any_source && any_source == any_target =>
147-
{
148-
result = self.client.query("SELECT * FROM Links;", &[]).await?;
149-
}
150-
[any_id, source, any_target] if any_id == any && any_id == any_target => {
151-
result = self
152-
.client
153-
.query(
154-
&format!("SELECT * FROM Links WHERE from_id = {};", source),
155-
&[],
156-
)
157-
.await?;
158-
}
159-
[any_id, any_source, target] if any_id == any && any_id == any_source => {
160-
result = self
161-
.client
162-
.query(
163-
&format!("SELECT * FROM Links WHERE to_id = {};", target),
164-
&[],
165-
)
166-
.await?;
167-
}
168-
[id, any_source, any_target] if any_source == any && any_source == any_target => {
169-
result = self
170-
.client
171-
.query(&format!("SELECT * FROM Links WHERE id = {};", id), &[])
172-
.await?;
173-
}
174-
[any_id, source, target] if any_id == any => {
175-
result = self
176-
.client
177-
.query(
178-
&format!(
179-
"SELECT * FROM Links WHERE from_id = {} AND to_id = {};",
180-
source, target
181-
),
182-
&[],
183-
)
184-
.await?;
185-
}
186-
[] | [_, ..] => panic!("Constraints violation, use 'any'"),
187-
}
188-
Ok(result)
28+
#[inline]
29+
fn one(&mut self) -> &postgres::Client {
30+
&self.1
18931
}
19032
}

rust/src/cruds.rs

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
use async_trait::async_trait;
2+
use platform_data::LinksConstants;
3+
use std::cell::RefCell;
4+
use tokio_postgres::{Error, GenericClient, Row};
5+
6+
type Result<T, E = Error> = std::result::Result<T, E>;
7+
8+
#[async_trait]
9+
pub trait Cruds<'a, Executor>
10+
where
11+
Executor: Send + GenericClient + Sync,
12+
{
13+
fn zero(&mut self) -> &mut u64;
14+
15+
fn one(&mut self) -> &Executor;
16+
17+
async fn create(&mut self, substitution: &[u64]) -> Result<Vec<Row>> {
18+
let index = RefCell::new(self.zero());
19+
**index.borrow_mut() += 1;
20+
let index = **index.borrow();
21+
self.one()
22+
.query(
23+
&format!(
24+
"INSERT INTO Links VALUES ({}, {}, {});",
25+
index, substitution[0], substitution[1]
26+
),
27+
&[],
28+
)
29+
.await
30+
}
31+
32+
async fn each(&mut self, restriction: &[u64]) -> Result<Vec<Row>> {
33+
let mut result = Vec::new();
34+
let any = LinksConstants::new().any;
35+
match restriction[..] {
36+
[any_id, any_source, any_target]
37+
if any_id == any && any_id == any_source && any_source == any_target =>
38+
{
39+
result = self.one().query("SELECT * FROM Links;", &[]).await?;
40+
}
41+
[any_id, source, any_target] if any_id == any && any_id == any_target => {
42+
result = self
43+
.one()
44+
.query(
45+
&format!("SELECT * FROM Links WHERE from_id = {};", source),
46+
&[],
47+
)
48+
.await?;
49+
}
50+
[any_id, any_source, target] if any_id == any && any_id == any_source => {
51+
result = self
52+
.one()
53+
.query(
54+
&format!("SELECT * FROM Links WHERE to_id = {};", target),
55+
&[],
56+
)
57+
.await?;
58+
}
59+
[id, any_source, any_target] if any_source == any && any_source == any_target => {
60+
result = self
61+
.one()
62+
.query(&format!("SELECT * FROM Links WHERE id = {};", id), &[])
63+
.await?;
64+
}
65+
[any_id, source, target] if any_id == any => {
66+
result = self
67+
.one()
68+
.query(
69+
&format!(
70+
"SELECT * FROM Links WHERE from_id = {} AND to_id = {};",
71+
source, target
72+
),
73+
&[],
74+
)
75+
.await?;
76+
}
77+
[] | [_, ..] => panic!("Constraints violation, use 'any'"),
78+
}
79+
Ok(result)
80+
}
81+
82+
async fn count(&mut self, restriction: &[u64]) -> Result<i64> {
83+
let mut result = Vec::new();
84+
let any = LinksConstants::new().any;
85+
match restriction[..] {
86+
[any_id, any_source, any_target]
87+
if any_id == any && any_id == any_source && any_source == any_target =>
88+
{
89+
result = self
90+
.one()
91+
.query("SELECT COUNT (*) FROM Links;", &[])
92+
.await?;
93+
}
94+
[any_id, source, any_target] if any_id == any && any_id == any_target => {
95+
result = self
96+
.one()
97+
.query(
98+
&format!("SELECT COUNT (*) FROM Links WHERE from_id = {};", source),
99+
&[],
100+
)
101+
.await?;
102+
}
103+
[any_id, any_source, target] if any_id == any && any_id == any_source => {
104+
result = self
105+
.one()
106+
.query(
107+
&format!("SELECT COUNT (*) FROM Links WHERE to_id = {};", target),
108+
&[],
109+
)
110+
.await?;
111+
}
112+
[id, any_source, any_target] if any_source == any && any_source == any_target => {
113+
result = self
114+
.one()
115+
.query(
116+
&format!("SELECT COUNT (*) FROM Links WHERE id = {}", id),
117+
&[],
118+
)
119+
.await?;
120+
}
121+
[any_id, source, target] if any_id == any => {
122+
result = self
123+
.one()
124+
.query(
125+
&format!(
126+
"SELECT COUNT (*) FROM Links WHERE from_id = {} AND to_id = {};",
127+
source, target
128+
),
129+
&[],
130+
)
131+
.await?;
132+
}
133+
[] | [_, ..] => panic!("Constraints violation, use 'any'"),
134+
}
135+
Ok(result[0].get(0))
136+
}
137+
138+
async fn update(&mut self, restriction: &[u64], substitution: &[u64]) -> Result<Vec<Row>> {
139+
if restriction.len() == 1 || restriction.len() == 3 {
140+
self.one()
141+
.query(
142+
&format!(
143+
"UPDATE Links SET from_id = {}, to_id = {} WHERE id = {};",
144+
substitution[0], substitution[1], restriction[0]
145+
),
146+
&[],
147+
)
148+
.await
149+
} else {
150+
self.one()
151+
.query(
152+
&format!("UPDATE Links SET from_id = {}, to_id = {} WHERE from_id = {} AND to_id = {};",
153+
substitution[0],
154+
substitution[1],
155+
restriction[0],
156+
restriction[1]),
157+
&[],
158+
)
159+
.await
160+
}
161+
}
162+
163+
async fn delete(&mut self, restriction: &[u64]) -> Result<Vec<Row>> {
164+
if restriction.len() == 1 || restriction.len() == 3 {
165+
self.one()
166+
.query(
167+
&format!("DELETE FROM Links WHERE id = {};", restriction[0]),
168+
&[],
169+
)
170+
.await
171+
} else {
172+
self.one()
173+
.query(
174+
&format!(
175+
"DELETE FROM Links WHERE from_id = {} AND to_id = {};",
176+
restriction[0], restriction[1]
177+
),
178+
&[],
179+
)
180+
.await
181+
}
182+
}
183+
}

0 commit comments

Comments
 (0)