Skip to content

Commit 7182e3a

Browse files
committed
feat: redone the FromSql and FromSqlOwned trait bounds
1 parent dee296b commit 7182e3a

10 files changed

Lines changed: 115 additions & 125 deletions

File tree

canyon_core/src/connection/clients/mssql.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ impl DbConnection for SqlServerConnector {
8787
}
8888
}
8989

90-
async fn query_one_for<T: FromSqlOwnedValue<T>>(
90+
async fn query_one_for<T: FromSqlOwnedValue>(
9191
&self,
9292
stmt: &str,
9393
params: &[&'_ dyn QueryParameter],

canyon_core/src/connection/clients/mysql.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ impl DbConnection for MySQLConnector {
6262
}
6363
}
6464

65-
async fn query_one_for<T: FromSqlOwnedValue<T>>(
65+
async fn query_one_for<T: FromSqlOwnedValue>(
6666
&self,
6767
stmt: &str,
6868
params: &[&'_ dyn QueryParameter],

canyon_core/src/connection/clients/postgresql.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ impl DbConnection for PostgresConnector {
8585
}
8686
}
8787

88-
async fn query_one_for<T: FromSqlOwnedValue<T>>(
88+
async fn query_one_for<T: FromSqlOwnedValue>(
8989
&self,
9090
stmt: &str,
9191
params: &[&'_ dyn QueryParameter],

canyon_core/src/connection/contracts/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ pub trait DbConnection {
9090
/// A [Future] that resolves to a [Result] containing the value of type `T` on success or an error on failure.
9191
///
9292
/// The `T` type must implement the [`FromSqlOwnedValue`] trait.
93-
fn query_one_for<T: FromSqlOwnedValue<T>>(
93+
fn query_one_for<T: FromSqlOwnedValue>(
9494
&self,
9595
stmt: &str,
9696
params: &[&dyn QueryParameter],

canyon_core/src/connection/impl_db_connection_macro.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ macro_rules! impl_db_connection_for_db_connector {
6868
}
6969
}
7070

71-
async fn query_one_for<T: FromSqlOwnedValue<T>>(
71+
async fn query_one_for<T: FromSqlOwnedValue>(
7272
&self,
7373
stmt: &str,
7474
params: &[&'_ dyn QueryParameter],
@@ -150,7 +150,7 @@ macro_rules! impl_db_connection_for_str {
150150
conn.query_one::<R>(stmt, params).await
151151
}
152152

153-
async fn query_one_for<T: $crate::rows::FromSqlOwnedValue<T>>(
153+
async fn query_one_for<T: $crate::rows::FromSqlOwnedValue>(
154154
&self,
155155
stmt: &str,
156156
params: &[&'_ dyn $crate::query::parameters::QueryParameter],

canyon_core/src/connection/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ where
100100
self.lock().await.query_one::<R>(stmt, params).await
101101
}
102102

103-
async fn query_one_for<F: FromSqlOwnedValue<F>>(
103+
async fn query_one_for<F: FromSqlOwnedValue>(
104104
&self,
105105
stmt: &str,
106106
params: &[&'_ dyn QueryParameter],

canyon_core/src/query/bounds.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::rows::FromSqlOwnedValue;
1111
/// Field collections exclude the primary key because they currently represent
1212
/// the values and columns used by entity insertion.
1313
pub trait EntityRuntimeInfo {
14-
type PrimaryKey: FromSqlOwnedValue<Self::PrimaryKey>;
14+
type PrimaryKey: FromSqlOwnedValue;
1515

1616
/// Returns the insertable field values in declaration order.
1717
///

canyon_core/src/query/query.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,14 @@ impl<'a> Query<'a> {
5454
<T as Transaction>::query(&self.sql, &self.params, default_conn).await
5555
}
5656

57-
pub async fn launch_one_for_default<T: Transaction, F: FromSqlOwnedValue<F>>(
57+
pub async fn launch_one_for_default<T: Transaction, F: FromSqlOwnedValue>(
5858
self,
5959
) -> Result<F, Box<dyn Error + Send + Sync>> {
6060
let default_conn = Canyon::instance()?.get_default_connection()?;
6161
<T as Transaction>::query_one_for(&self.sql, &self.params, default_conn).await
6262
}
6363

64-
pub async fn launch_one_for_with<T: Transaction, F: FromSqlOwnedValue<F>, I: DbConnection>(
64+
pub async fn launch_one_for_with<T: Transaction, F: FromSqlOwnedValue, I: DbConnection>(
6565
self,
6666
input: I,
6767
) -> Result<F, Box<dyn Error + Send + Sync>> {

canyon_core/src/rows.rs

Lines changed: 104 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ use tokio_postgres::{self};
1515
use crate::mapper::RowMapper;
1616
use crate::row::Row;
1717

18-
use cfg_if::cfg_if;
19-
2018
/// Lightweight wrapper over the collection of results of the different crates
2119
/// supported by Canyon-SQL.
2220
///
@@ -33,16 +31,6 @@ pub enum CanyonRows {
3331
MySQL(Vec<mysql_async::Row>),
3432
}
3533

36-
// impl IntoResults for Result<CanyonRows, CanyonError> {
37-
// fn into_results<R>(self) -> Result<Vec<R>, CanyonError>
38-
// where
39-
// R: RowMapper,
40-
// Vec<R>: FromIterator<<R as RowMapper>::Output>,
41-
// {
42-
// self.map(move |rows| rows.into_results::<R>())
43-
// }
44-
// }
45-
4634
impl CanyonRows {
4735
#[cfg(feature = "postgres")]
4836
pub fn get_postgres_rows(&self) -> &Vec<tokio_postgres::Row> {
@@ -68,22 +56,6 @@ impl CanyonRows {
6856
}
6957
}
7058

71-
// /// Consumes `self` and returns the wrapped [`std::vec::Vec`] with the instances of R
72-
// pub fn into_results<R>(self) -> Vec<R>
73-
// where
74-
// R: RowMapper,
75-
// Vec<R>: FromIterator<<R as RowMapper>::Output>,
76-
// {
77-
// match self {
78-
// #[cfg(feature = "postgres")]
79-
// Self::Postgres(v) => v.iter().map(|row| R::deserialize_postgresql(row)?).collect(),
80-
// #[cfg(feature = "mssql")]
81-
// Self::Tiberius(v) => v.iter().map(|row| R::deserialize_sqlserver(row)?).collect(),
82-
// #[cfg(feature = "mysql")]
83-
// Self::MySQL(v) => v.iter().map(|row| R::deserialize_mysql(row)?).collect(),
84-
// }
85-
// }
86-
8759
/// Returns the entity at the given index for the returned rows
8860
///
8961
/// This is just a wrapper get operation over the [Vec] get operation
@@ -136,90 +108,108 @@ impl CanyonRows {
136108
}
137109
}
138110

139-
cfg_if! {
140-
if #[cfg(all(feature = "postgres", feature = "mysql", feature = "mssql"))] {
141-
pub trait FromSql<'a, T>: tokio_postgres::types::FromSql<'a>
142-
+ tiberius::FromSql<'a>
143-
+ mysql_async::prelude::FromValue {}
144-
impl<'a, T> FromSql<'a, T> for T where T:
145-
tokio_postgres::types::FromSql<'a>
146-
+ tiberius::FromSql<'a>
147-
+ mysql_async::prelude::FromValue
148-
{}
149-
150-
pub trait FromSqlOwnedValue<T>: tokio_postgres::types::FromSqlOwned
151-
+ tiberius::FromSqlOwned
152-
+ mysql_async::prelude::FromValue {}
153-
impl<T> FromSqlOwnedValue<T> for T where T:
154-
tokio_postgres::types::FromSqlOwned
155-
+ tiberius::FromSqlOwned
156-
+ mysql_async::prelude::FromValue
157-
{}
158-
} else if #[cfg(all(feature = "postgres", feature = "mysql"))] {
159-
pub trait FromSql<'a, T>: tokio_postgres::types::FromSql<'a>
160-
+ mysql_async::prelude::FromValue {}
161-
impl<'a, T> FromSql<'a, T> for T where T:
162-
tokio_postgres::types::FromSql<'a>
163-
+ mysql_async::prelude::FromValue
164-
{}
165-
166-
pub trait FromSqlOwnedValue<T>: tokio_postgres::types::FromSqlOwned
167-
+ mysql_async::prelude::FromValue {}
168-
impl<T> FromSqlOwnedValue<T> for T where T:
169-
tokio_postgres::types::FromSqlOwned
170-
+ mysql_async::prelude::FromValue
171-
{}
172-
} else if #[cfg(all(feature = "postgres", feature = "mssql"))] {
173-
pub trait FromSql<'a, T>: tokio_postgres::types::FromSql<'a>
174-
+ tiberius::FromSql<'a> {}
175-
impl<'a, T> FromSql<'a, T> for T where T:
176-
tokio_postgres::types::FromSql<'a>
177-
+ tiberius::FromSql<'a>
178-
{}
179-
180-
pub trait FromSqlOwnedValue<T>: tokio_postgres::types::FromSqlOwned
181-
+ tiberius::FromSqlOwned {}
182-
impl<T> FromSqlOwnedValue<T> for T where T:
183-
tokio_postgres::types::FromSqlOwned
184-
+ tiberius::FromSqlOwned
185-
{}
186-
} else if #[cfg(all(feature = "mysql", feature = "mssql"))] {
187-
pub trait FromSql<'a, T>: mysql_async::prelude::FromValue
188-
+ tiberius::FromSql<'a> {}
189-
impl<'a, T> FromSql<'a, T> for T where T:
190-
mysql_async::prelude::FromValue
191-
+ tiberius::FromSql<'a>
192-
{}
193-
194-
pub trait FromSqlOwnedValue<T>: mysql_async::prelude::FromValue
195-
+ tiberius::FromSqlOwned {}
196-
impl<T> FromSqlOwnedValue<T> for T where T:
197-
mysql_async::prelude::FromValue
198-
+ tiberius::FromSqlOwned
199-
{}
200-
} else if #[cfg(feature = "postgres")] {
201-
pub trait FromSql<'a, T>: tokio_postgres::types::FromSql<'a> {}
202-
impl<'a, T> FromSql<'a, T> for T where T:
203-
tokio_postgres::types::FromSql<'a> {}
204-
205-
pub trait FromSqlOwnedValue<T>: tokio_postgres::types::FromSqlOwned {}
206-
impl<T> FromSqlOwnedValue<T> for T where T:
207-
tokio_postgres::types::FromSqlOwned {}
208-
} else if #[cfg(feature = "mysql")] {
209-
pub trait FromSql<'a, T>: mysql_async::prelude::FromValue {}
210-
impl<'a, T> FromSql<'a, T> for T where T:
211-
mysql_async::prelude::FromValue {}
212-
213-
pub trait FromSqlOwnedValue<T>: mysql_async::prelude::FromValue {}
214-
impl<T> FromSqlOwnedValue<T> for T where T:
215-
mysql_async::prelude::FromValue {}
216-
} else if #[cfg(feature = "mssql")] {
217-
pub trait FromSql<'a, T>: tiberius::FromSql<'a> {}
218-
impl<'a, T> FromSql<'a, T> for T where T:
219-
tiberius::FromSql<'a> {}
220-
221-
pub trait FromSqlOwnedValue<T>: tiberius::FromSqlOwned {}
222-
impl<T> FromSqlOwnedValue<T> for T where T:
223-
tiberius::FromSqlOwned {}
224-
}
111+
pub trait FromSql<'a>:
112+
__backend_from_sql::PostgresFromSql<'a>
113+
+ __backend_from_sql::MySqlFromSql
114+
+ __backend_from_sql::MsSqlFromSql<'a>
115+
{
116+
}
117+
118+
impl<'a, T> FromSql<'a> for T where
119+
T: __backend_from_sql::PostgresFromSql<'a>
120+
+ __backend_from_sql::MySqlFromSql
121+
+ __backend_from_sql::MsSqlFromSql<'a>
122+
{
123+
}
124+
125+
pub trait FromSqlOwnedValue:
126+
__backend_from_sql_owned::PostgresFromSqlOwned
127+
+ __backend_from_sql_owned::MySqlFromSqlOwned
128+
+ __backend_from_sql_owned::MsSqlFromSqlOwned
129+
{
130+
}
131+
132+
impl<T> FromSqlOwnedValue for T where
133+
T: __backend_from_sql_owned::PostgresFromSqlOwned
134+
+ __backend_from_sql_owned::MySqlFromSqlOwned
135+
+ __backend_from_sql_owned::MsSqlFromSqlOwned
136+
{
137+
}
138+
139+
#[doc(hidden)]
140+
pub mod __backend_from_sql {
141+
#[cfg(feature = "postgres")]
142+
pub trait PostgresFromSql<'a>: tokio_postgres::types::FromSql<'a> {}
143+
144+
#[cfg(feature = "postgres")]
145+
impl<'a, T> PostgresFromSql<'a> for T where T: tokio_postgres::types::FromSql<'a> {}
146+
147+
#[cfg(not(feature = "postgres"))]
148+
pub trait PostgresFromSql<'a> {}
149+
150+
#[cfg(not(feature = "postgres"))]
151+
impl<'a, T> PostgresFromSql<'a> for T {}
152+
153+
#[cfg(feature = "mysql")]
154+
pub trait MySqlFromSql: mysql_async::prelude::FromValue {}
155+
156+
#[cfg(feature = "mysql")]
157+
impl<T> MySqlFromSql for T where T: mysql_async::prelude::FromValue {}
158+
159+
#[cfg(not(feature = "mysql"))]
160+
pub trait MySqlFromSql {}
161+
162+
#[cfg(not(feature = "mysql"))]
163+
impl<T> MySqlFromSql for T {}
164+
165+
#[cfg(feature = "mssql")]
166+
pub trait MsSqlFromSql<'a>: tiberius::FromSql<'a> {}
167+
168+
#[cfg(feature = "mssql")]
169+
impl<'a, T> MsSqlFromSql<'a> for T where T: tiberius::FromSql<'a> {}
170+
171+
#[cfg(not(feature = "mssql"))]
172+
pub trait MsSqlFromSql<'a> {}
173+
174+
#[cfg(not(feature = "mssql"))]
175+
impl<'a, T> MsSqlFromSql<'a> for T {}
176+
}
177+
178+
#[doc(hidden)]
179+
pub mod __backend_from_sql_owned {
180+
#[cfg(feature = "postgres")]
181+
pub trait PostgresFromSqlOwned: tokio_postgres::types::FromSqlOwned {}
182+
183+
#[cfg(feature = "postgres")]
184+
impl<T> PostgresFromSqlOwned for T where T: tokio_postgres::types::FromSqlOwned {}
185+
186+
#[cfg(not(feature = "postgres"))]
187+
pub trait PostgresFromSqlOwned {}
188+
189+
#[cfg(not(feature = "postgres"))]
190+
impl<T> PostgresFromSqlOwned for T {}
191+
192+
#[cfg(feature = "mysql")]
193+
pub trait MySqlFromSqlOwned: mysql_async::prelude::FromValue {}
194+
195+
#[cfg(feature = "mysql")]
196+
impl<T> MySqlFromSqlOwned for T where T: mysql_async::prelude::FromValue {}
197+
198+
#[cfg(not(feature = "mysql"))]
199+
pub trait MySqlFromSqlOwned {}
200+
201+
#[cfg(not(feature = "mysql"))]
202+
impl<T> MySqlFromSqlOwned for T {}
203+
204+
#[cfg(feature = "mssql")]
205+
pub trait MsSqlFromSqlOwned: tiberius::FromSqlOwned {}
206+
207+
#[cfg(feature = "mssql")]
208+
impl<T> MsSqlFromSqlOwned for T where T: tiberius::FromSqlOwned {}
209+
210+
#[cfg(not(feature = "mssql"))]
211+
pub trait MsSqlFromSqlOwned {}
212+
213+
#[cfg(not(feature = "mssql"))]
214+
impl<T> MsSqlFromSqlOwned for T {}
225215
}

canyon_core/src/transaction.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ pub trait Transaction {
6767
async move { input.query_one::<R>(stmt.as_ref(), params.as_ref()).await }
6868
}
6969

70-
fn query_one_for<'a, S, Z, F: FromSqlOwnedValue<F>>(
70+
fn query_one_for<'a, S, Z, F: FromSqlOwnedValue>(
7171
stmt: S,
7272
params: Z,
7373
input: impl DbConnection + Send + 'a,

0 commit comments

Comments
 (0)