Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1b3cedb

Browse files
committed
Relaxed the mandatory until now Debug + Clone bounds on the Crud Operations proc macro. Now they're free of that restriction
1 parent ae67c21 commit 1b3cedb

File tree

9 files changed

+37
-43
lines changed

9 files changed

+37
-43
lines changed

canyon_crud/src/bounds.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use canyon_connection::{
99
tokio_postgres::{types::ToSql, self}
1010
};
1111
use chrono::{DateTime, FixedOffset, NaiveDate, NaiveDateTime, NaiveTime, Utc};
12-
use std::{fmt::Debug, any::Any};
12+
use std::any::Any;
1313

1414
/// Created for retrieve the field's name of a field of a struct, giving
1515
/// the Canoyn's autogenerated enum with the variants that maps this
@@ -35,7 +35,7 @@ use std::{fmt::Debug, any::Any};
3535
/// `let struct_field_name_from_variant = StructField::some_field.field_name_as_str();`
3636
pub trait FieldIdentifier<T>
3737
where
38-
T: Transaction<T> + CrudOperations<T> + RowMapper<T> + Debug,
38+
T: Transaction<T> + CrudOperations<T> + RowMapper<T>
3939
{
4040
fn as_str<'a>(&'a self) -> &'static str;
4141
}
@@ -62,7 +62,7 @@ where
6262
/// ```
6363
pub trait FieldValueIdentifier<'a, T>
6464
where
65-
T: Transaction<T> + CrudOperations<T> + RowMapper<T> + Debug,
65+
T: Transaction<T> + CrudOperations<T> + RowMapper<T>
6666
{
6767
fn value(self) -> (&'static str, &'a dyn QueryParameters<'a>);
6868
}

canyon_crud/src/crud.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::fmt::{Debug, Display};
1+
use std::fmt::Display;
22

33
use async_trait::async_trait;
44
use canyon_connection::CACHED_DATABASE_CONN;
@@ -18,7 +18,7 @@ use crate::bounds::QueryParameters;
1818
/// automatically map it to an struct.
1919
#[async_trait]
2020
#[allow(clippy::question_mark)]
21-
pub trait Transaction<T: Debug> {
21+
pub trait Transaction<T> {
2222
/// Performs a query against the targeted database by the selected datasource.
2323
///
2424
/// No datasource means take the entry zero
@@ -77,7 +77,7 @@ pub trait Transaction<T: Debug> {
7777
#[async_trait]
7878
pub trait CrudOperations<T>: Transaction<T>
7979
where
80-
T: Debug + CrudOperations<T> + RowMapper<T>,
80+
T: CrudOperations<T> + RowMapper<T>
8181
{
8282
async fn find_all<'a>() -> Result<Vec<T>, Box<(dyn std::error::Error + Send + Sync + 'static)>>;
8383

@@ -153,9 +153,8 @@ mod postgres_query_launcher {
153153
use crate::bounds::QueryParameters;
154154
use crate::result::DatabaseResult;
155155
use canyon_connection::canyon_database_connector::DatabaseConnection;
156-
use std::fmt::Debug;
157156

158-
pub async fn launch<'a, T: Debug>(
157+
pub async fn launch<'a, T>(
159158
db_conn: &DatabaseConnection,
160159
// datasource_name: &str,
161160
stmt: String,
@@ -180,7 +179,7 @@ mod postgres_query_launcher {
180179
}
181180

182181
mod sqlserver_query_launcher {
183-
use std::{fmt::Debug, mem::transmute};
182+
use std::mem::transmute;
184183

185184
use canyon_connection::tiberius::Row;
186185

@@ -198,9 +197,7 @@ mod sqlserver_query_launcher {
198197
stmt: &mut String,
199198
params: Z,
200199
) -> Result<DatabaseResult<T>, Box<(dyn std::error::Error + Send + Sync + 'static)>>
201-
where
202-
T: Debug,
203-
Z: AsRef<[&'a dyn QueryParameters<'a>]> + Sync + Send + 'a,
200+
where Z: AsRef<[&'a dyn QueryParameters<'a>]> + Sync + Send + 'a
204201
{
205202
// Re-generate de insert statement to adecuate it to the SQL SERVER syntax to retrieve the PK value(s) after insert
206203
if stmt.contains("RETURNING") {

canyon_crud/src/mapper.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
use canyon_connection::{tiberius, tokio_postgres};
2-
use std::fmt::Debug;
32

43
use crate::crud::Transaction;
54

65
/// Declares functions that takes care to deserialize data incoming
76
/// from some supported database in Canyon-SQL into a user's defined
87
/// type `T`
9-
pub trait RowMapper<T: Debug + Transaction<T>>: Sized {
8+
pub trait RowMapper<T: Transaction<T>>: Sized {
109
fn deserialize_postgresql(row: &tokio_postgres::Row) -> T;
1110

1211
fn deserialize_sqlserver(row: &tiberius::Row) -> T;

canyon_crud/src/query_elements/query.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ use crate::{
88

99
/// Holds a sql sentence details
1010
#[derive(Debug, Clone)]
11-
pub struct Query<'a, T: Debug + CrudOperations<T> + Transaction<T> + RowMapper<T>> {
11+
pub struct Query<'a, T: CrudOperations<T> + Transaction<T> + RowMapper<T>> {
1212
pub sql: String,
1313
pub params: Vec<&'a dyn QueryParameters<'a>>,
1414
marker: PhantomData<T>,
1515
}
1616

1717
impl<'a, T> Query<'a, T>
1818
where
19-
T: Debug + CrudOperations<T> + Transaction<T> + RowMapper<T>,
19+
T: CrudOperations<T> + Transaction<T> + RowMapper<T>,
2020
{
2121
pub fn new(sql: String) -> Query<'a, T> {
2222
Self {sql, params: vec![], marker: PhantomData}

canyon_crud/src/query_elements/query_builder.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -119,21 +119,21 @@ pub mod ops {
119119
/// Type for construct more complex queries than the classical CRUD ones.
120120
#[derive(Debug, Clone)]
121121
pub struct QueryBuilder<'a, T> where
122-
T: Debug + CrudOperations<T> + Transaction<T> + RowMapper<T>
122+
T: CrudOperations<T> + Transaction<T> + RowMapper<T>
123123
{
124124
query: Query<'a, T>,
125125
datasource_name: &'a str
126126
}
127127

128128
unsafe impl<'a, T> Send for QueryBuilder<'a, T> where
129-
T: Debug + CrudOperations<T> + Transaction<T> + RowMapper<T> {}
129+
T: CrudOperations<T> + Transaction<T> + RowMapper<T> {}
130130
unsafe impl<'a, T> Sync for QueryBuilder<'a, T> where
131-
T: Debug + CrudOperations<T> + Transaction<T> + RowMapper<T> {}
131+
T: CrudOperations<T> + Transaction<T> + RowMapper<T> {}
132132

133133

134134
impl<'a, T> QueryBuilder<'a, T>
135135
where
136-
T: Debug + CrudOperations<T> + Transaction<T> + RowMapper<T>
136+
T: CrudOperations<T> + Transaction<T> + RowMapper<T>
137137
{
138138
/// Returns a new instance of the [`QueryBuilder`]
139139
pub fn new(query: Query<'a, T>, datasource_name: &'a str) -> Self {
@@ -270,13 +270,13 @@ where
270270

271271
#[derive(Debug, Clone)]
272272
pub struct SelectQueryBuilder<'a, T> where
273-
T: Debug + CrudOperations<T> + Transaction<T> + RowMapper<T>,
273+
T: CrudOperations<T> + Transaction<T> + RowMapper<T>,
274274
{
275275
_inner: QueryBuilder<'a, T>,
276276
}
277277

278278
impl<'a, T> SelectQueryBuilder<'a, T> where
279-
T: Debug + CrudOperations<T> + Transaction<T> + RowMapper<T>
279+
T: CrudOperations<T> + Transaction<T> + RowMapper<T>
280280
{
281281
/// Generates a new public instance of the [`SelectQueryBuilder`]
282282
pub fn new(table_schema_data: &str, datasource_name: &'a str) -> Self {
@@ -419,13 +419,13 @@ impl<'a, T> ops::QueryBuilder<'a, T> for SelectQueryBuilder<'a, T> where
419419
/// update with the provided values
420420
#[derive(Debug, Clone)]
421421
pub struct UpdateQueryBuilder<'a, T> where
422-
T: Debug + CrudOperations<T> + Transaction<T> + RowMapper<T>,
422+
T: CrudOperations<T> + Transaction<T> + RowMapper<T>,
423423
{
424424
_inner: QueryBuilder<'a, T>,
425425
}
426426

427427
impl<'a, T> UpdateQueryBuilder<'a, T> where
428-
T: Debug + CrudOperations<T> + Transaction<T> + RowMapper<T>
428+
T: CrudOperations<T> + Transaction<T> + RowMapper<T>
429429
{
430430
/// Generates a new public instance of the [`UpdateQueryBuilder`]
431431
pub fn new(table_schema_data: &str, datasource_name: &'a str) -> Self {
@@ -536,13 +536,13 @@ impl<'a, T> ops::QueryBuilder<'a, T> for UpdateQueryBuilder<'a, T> where
536536
/// update with the provided values
537537
#[derive(Debug, Clone)]
538538
pub struct DeleteQueryBuilder<'a, T> where
539-
T: Debug + CrudOperations<T> + Transaction<T> + RowMapper<T>,
539+
T: CrudOperations<T> + Transaction<T> + RowMapper<T>,
540540
{
541541
_inner: QueryBuilder<'a, T>,
542542
}
543543

544544
impl<'a, T> DeleteQueryBuilder<'a, T> where
545-
T: Debug + CrudOperations<T> + Transaction<T> + RowMapper<T>
545+
T: CrudOperations<T> + Transaction<T> + RowMapper<T>
546546
{
547547
/// Generates a new public instance of the [`DeleteQueryBuilder`]
548548
pub fn new(table_schema_data: &str, datasource_name: &'a str) -> Self {

canyon_crud/src/result.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ use std::{fmt::Debug, marker::PhantomData};
66
/// results after the query.
77
/// and providing methods to deserialize this result into a **user defined struct**
88
#[derive(Debug)]
9-
pub struct DatabaseResult<T: Debug> {
9+
pub struct DatabaseResult<T> {
1010
pub postgres: Vec<tokio_postgres::Row>,
1111
pub sqlserver: Vec<tiberius::Row>,
1212
pub active_ds: DatabaseType,
1313
_phantom_data: std::marker::PhantomData<T>,
1414
}
1515

16-
impl<T: Debug> DatabaseResult<T> {
16+
impl<T> DatabaseResult<T> {
1717
pub fn new_postgresql(result: Vec<tokio_postgres::Row>) -> Self {
1818
Self {
1919
postgres: result,
@@ -38,7 +38,7 @@ impl<T: Debug> DatabaseResult<T> {
3838
/// Also, provides a way to statically call `Z::deserialize_<db>` method,
3939
/// which it's the implementation used by the macros to automatically
4040
/// map database columns into the fields for T.
41-
pub fn get_entities<Z: RowMapper<T> + Debug>(&self) -> Vec<T>
41+
pub fn get_entities<Z: RowMapper<T>>(&self) -> Vec<T>
4242
where
4343
T: Transaction<T>,
4444
{
@@ -48,7 +48,7 @@ impl<T: Debug> DatabaseResult<T> {
4848
}
4949
}
5050

51-
fn map_from_postgresql<Z: RowMapper<T> + Debug>(&self) -> Vec<T>
51+
fn map_from_postgresql<Z: RowMapper<T>>(&self) -> Vec<T>
5252
where
5353
T: Transaction<T>,
5454
{
@@ -61,7 +61,7 @@ impl<T: Debug> DatabaseResult<T> {
6161
results
6262
}
6363

64-
fn map_from_sql_server<Z: RowMapper<T> + Debug>(&self) -> Vec<T>
64+
fn map_from_sql_server<Z: RowMapper<T>>(&self) -> Vec<T>
6565
where
6666
T: Transaction<T>,
6767
{

canyon_macros/src/query_operations/select.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -261,11 +261,9 @@ pub fn generate_find_by_pk_tokens(
261261
n if n.number_of_results() == 0 => Ok(None),
262262
_ => Ok(
263263
Some(
264-
result
265-
.ok()
266-
.unwrap()
267-
.get_entities::<#ty>()[0]
268-
.clone()
264+
result.unwrap()
265+
.get_entities::<#ty>()
266+
.remove(0)
269267
)
270268
)
271269
}
@@ -376,10 +374,9 @@ pub fn generate_find_by_foreign_key_tokens(
376374
n if n.number_of_results() == 0 => Ok(None),
377375
_ => Ok(Some(
378376
result
379-
.ok()
380377
.unwrap()
381-
.get_entities::<#fk_ty>()[0]
382-
.clone()
378+
.get_entities::<#fk_ty>()
379+
.remove(0)
383380
))
384381
}
385382
}

tests/crud/querybuilder_operations.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,10 @@ fn test_crud_update_with_querybuilder() {
8484
.r#where(LeagueFieldValue::id(&1), Comp::Gt)
8585
.and(LeagueFieldValue::id(&8), Comp::Lt);
8686

87-
// Family of QueryBuilders are clone, useful in case of need the generated SQL
88-
let qpr = q.clone();
89-
println!("PSQL: {:?}", qpr.read_sql());
87+
/* Family of QueryBuilders are clone, useful in case of need to read the generated SQL
88+
let qpr = q.clone();
89+
println!("PSQL: {:?}", qpr.read_sql());
90+
*/
9091

9192
// We can now back to the original an throw the query
9293
q.query()

tests/tests_models/league.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use canyon_sql::macros::*;
22

3-
#[derive(Debug, Clone, Fields, CanyonCrud, CanyonMapper, ForeignKeyable, Eq, PartialEq)]
3+
#[derive(Debug, Fields, CanyonCrud, CanyonMapper, ForeignKeyable, Eq, PartialEq)]
44
// #[canyon_entity(table_name = "league", schema = "public")]
55
#[canyon_entity(table_name = "league")]
66
pub struct League {

0 commit comments

Comments
 (0)