|
| 1 | +use crate::cruds::Cruds; |
1 | 2 | use crate::Transaction; |
2 | | -use platform_data::LinksConstants; |
3 | | -use postgres::{Error, Row}; |
| 3 | +use async_trait::async_trait; |
| 4 | +use postgres::Error; |
4 | 5 | use tokio_postgres as postgres; |
5 | 6 |
|
6 | | -pub struct Client { |
7 | | - index: u64, |
8 | | - client: postgres::Client, |
9 | | -} |
| 7 | +pub struct Client(u64, postgres::Client); |
10 | 8 |
|
11 | 9 | impl Client { |
12 | 10 | pub async fn new(client: postgres::Client) -> Result<Client, Error> { |
13 | 11 | let index = client.query("SELECT * FROM Links;", &[]).await?.len() as u64; |
14 | | - Ok(Self { index, client }) |
| 12 | + Ok(Self(index, client)) |
15 | 13 | } |
16 | 14 |
|
17 | 15 | 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)) |
118 | 18 | } |
| 19 | +} |
119 | 20 |
|
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 |
139 | 26 | } |
140 | 27 |
|
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 |
189 | 31 | } |
190 | 32 | } |
0 commit comments