Skip to content

Commit 2d1531f

Browse files
committed
de-duplicate type aliases
1 parent e168d4a commit 2d1531f

File tree

3 files changed

+62
-183
lines changed

3 files changed

+62
-183
lines changed

nexus/src/db/collection_attach.rs

Lines changed: 50 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
1515
use super::cte_utils::{
1616
BoxableTable, BoxableUpdateStatement, BoxedQuery, ExprSqlType, FilterBy,
17-
QueryFromClause, QuerySqlType,
17+
QueryFromClause, QuerySqlType, TableDefaultWhereClause,
1818
};
1919
use super::pool::DbConnection;
2020
use async_bb8_diesel::{AsyncRunQueryDsl, ConnectionManager, PoolError};
@@ -29,39 +29,53 @@ use diesel::query_source::Table;
2929
use diesel::sql_types::{BigInt, Nullable, SingleValue};
3030
use std::fmt::Debug;
3131

32-
/// The table representing the collection. The resource references
33-
/// this table.
34-
type CollectionTable<ResourceType, C> = <<C as DatastoreAttachTarget<
35-
ResourceType,
36-
>>::CollectionIdColumn as Column>::Table;
37-
/// The table representing the resource. This table contains an
38-
/// ID acting as a foreign key into the collection table.
39-
type ResourceTable<ResourceType, C> = <<C as DatastoreAttachTarget<
40-
ResourceType,
41-
>>::ResourceIdColumn as Column>::Table;
42-
/// The default WHERE clause of the resource table.
43-
type ResourceTableWhereClause<ResourceType, C> =
44-
<ResourceTable<ResourceType, C> as IntoUpdateTarget>::WhereClause;
45-
type CollectionIdColumn<ResourceType, C> =
46-
<C as DatastoreAttachTarget<ResourceType>>::CollectionIdColumn;
47-
type ResourceIdColumn<ResourceType, C> =
48-
<C as DatastoreAttachTarget<ResourceType>>::ResourceIdColumn;
49-
50-
// Representation of Primary Key in Rust.
51-
type CollectionPrimaryKey<ResourceType, C> =
52-
<CollectionTable<ResourceType, C> as Table>::PrimaryKey;
53-
type ResourcePrimaryKey<ResourceType, C> =
54-
<ResourceTable<ResourceType, C> as Table>::PrimaryKey;
55-
type ResourceForeignKey<ResourceType, C> =
56-
<C as DatastoreAttachTarget<ResourceType>>::ResourceCollectionIdColumn;
57-
58-
// Representation of Primary Key in SQL.
59-
type SerializedCollectionPrimaryKey<ResourceType, C> =
60-
<CollectionPrimaryKey<ResourceType, C> as diesel::Expression>::SqlType;
61-
type SerializedResourcePrimaryKey<ResourceType, C> =
62-
<ResourcePrimaryKey<ResourceType, C> as diesel::Expression>::SqlType;
63-
type SerializedResourceForeignKey<ResourceType, C> =
64-
<ResourceForeignKey<ResourceType, C> as diesel::Expression>::SqlType;
32+
/// A collection of type aliases particularly relevant to collection-based CTEs.
33+
pub(crate) mod aliases {
34+
use super::{
35+
Column, DatastoreAttachTarget, Table, TableDefaultWhereClause,
36+
};
37+
38+
/// The table representing the collection. The resource references
39+
/// this table.
40+
pub type CollectionTable<ResourceType, C> = <<C as DatastoreAttachTarget<
41+
ResourceType,
42+
>>::CollectionIdColumn as Column>::Table;
43+
/// The table representing the resource. This table contains an
44+
/// ID acting as a foreign key into the collection table.
45+
pub type ResourceTable<ResourceType, C> = <<C as DatastoreAttachTarget<
46+
ResourceType,
47+
>>::ResourceIdColumn as Column>::Table;
48+
49+
/// The default WHERE clause of the collection table.
50+
pub type CollectionTableDefaultWhereClause<ResourceType, C> =
51+
TableDefaultWhereClause<CollectionTable<ResourceType, C>>;
52+
/// The default WHERE clause of the resource table.
53+
pub type ResourceTableDefaultWhereClause<ResourceType, C> =
54+
TableDefaultWhereClause<ResourceTable<ResourceType, C>>;
55+
56+
pub type CollectionIdColumn<ResourceType, C> =
57+
<C as DatastoreAttachTarget<ResourceType>>::CollectionIdColumn;
58+
pub type ResourceIdColumn<ResourceType, C> =
59+
<C as DatastoreAttachTarget<ResourceType>>::ResourceIdColumn;
60+
61+
/// Representation of Primary Key in Rust.
62+
pub type CollectionPrimaryKey<ResourceType, C> =
63+
<CollectionTable<ResourceType, C> as Table>::PrimaryKey;
64+
pub type ResourcePrimaryKey<ResourceType, C> =
65+
<ResourceTable<ResourceType, C> as Table>::PrimaryKey;
66+
pub type ResourceForeignKey<ResourceType, C> =
67+
<C as DatastoreAttachTarget<ResourceType>>::ResourceCollectionIdColumn;
68+
69+
/// Representation of Primary Key in SQL.
70+
pub type SerializedCollectionPrimaryKey<ResourceType, C> =
71+
<CollectionPrimaryKey<ResourceType, C> as diesel::Expression>::SqlType;
72+
pub type SerializedResourcePrimaryKey<ResourceType, C> =
73+
<ResourcePrimaryKey<ResourceType, C> as diesel::Expression>::SqlType;
74+
pub type SerializedResourceForeignKey<ResourceType, C> =
75+
<ResourceForeignKey<ResourceType, C> as diesel::Expression>::SqlType;
76+
}
77+
78+
use aliases::*;
6579

6680
/// Trait to be implemented by structs representing an attachable collection.
6781
///
@@ -181,7 +195,7 @@ pub trait DatastoreAttachTarget<ResourceType>: Selectable<Pg> + Sized {
181195
// value.
182196
update: UpdateStatement<
183197
ResourceTable<ResourceType, Self>,
184-
ResourceTableWhereClause<ResourceType, Self>,
198+
ResourceTableDefaultWhereClause<ResourceType, Self>,
185199
V,
186200
>,
187201
) -> AttachToCollectionStatement<ResourceType, V, Self>
@@ -212,7 +226,7 @@ pub trait DatastoreAttachTarget<ResourceType>: Selectable<Pg> + Sized {
212226
// Allows calling "update.into_boxed()"
213227
UpdateStatement<
214228
ResourceTable<ResourceType, Self>,
215-
ResourceTableWhereClause<ResourceType, Self>,
229+
ResourceTableDefaultWhereClause<ResourceType, Self>,
216230
V,
217231
>: BoxableUpdateStatement<ResourceTable<ResourceType, Self>, V>,
218232
// Allows calling

nexus/src/db/collection_detach.rs

Lines changed: 7 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@
1010
//! - Validates conditions on both the collection and resource
1111
//! - Updates the resource row
1212
13+
use super::collection_attach::aliases::*;
1314
use super::cte_utils::{
1415
BoxableTable, BoxableUpdateStatement, BoxedQuery, ExprSqlType, FilterBy,
15-
QueryFromClause, QuerySqlType, TableDefaultWhereClause,
16+
QueryFromClause, QuerySqlType,
1617
};
1718
use super::pool::DbConnection;
1819
use crate::db::collection_attach::DatastoreAttachTarget;
@@ -28,74 +29,13 @@ use diesel::query_source::Table;
2829
use diesel::sql_types::{Nullable, SingleValue};
2930
use std::fmt::Debug;
3031

31-
/// The table representing the collection. The resource references
32-
/// this table.
33-
type CollectionTable<ResourceType, C> = <<C as DatastoreDetachTarget<
34-
ResourceType,
35-
>>::CollectionIdColumn as Column>::Table;
36-
37-
/// The table representing the resource. This table contains an
38-
/// ID acting as a foreign key into the collection table.
39-
type ResourceTable<ResourceType, C> = <<C as DatastoreDetachTarget<
40-
ResourceType,
41-
>>::ResourceIdColumn as Column>::Table;
42-
43-
/// The default WHERE clause of the resource table.
44-
type ResourceTableDefaultWhereClause<ResourceType, C> =
45-
TableDefaultWhereClause<ResourceTable<ResourceType, C>>;
46-
47-
/// Helper to access column type.
48-
type CollectionIdColumn<ResourceType, C> =
49-
<C as DatastoreDetachTarget<ResourceType>>::CollectionIdColumn;
50-
/// Helper to access column type.
51-
type ResourceIdColumn<ResourceType, C> =
52-
<C as DatastoreDetachTarget<ResourceType>>::ResourceIdColumn;
53-
54-
// Representation of Primary Key in Rust.
55-
type CollectionPrimaryKey<ResourceType, C> =
56-
<CollectionTable<ResourceType, C> as Table>::PrimaryKey;
57-
type ResourcePrimaryKey<ResourceType, C> =
58-
<ResourceTable<ResourceType, C> as Table>::PrimaryKey;
59-
type ResourceForeignKey<ResourceType, C> =
60-
<C as DatastoreDetachTarget<ResourceType>>::ResourceCollectionIdColumn;
61-
62-
// Representation of Primary Key in SQL.
63-
type SerializedCollectionPrimaryKey<ResourceType, C> =
64-
<CollectionPrimaryKey<ResourceType, C> as diesel::Expression>::SqlType;
65-
type SerializedResourcePrimaryKey<ResourceType, C> =
66-
<ResourcePrimaryKey<ResourceType, C> as diesel::Expression>::SqlType;
67-
type SerializedResourceForeignKey<ResourceType, C> =
68-
<ResourceForeignKey<ResourceType, C> as diesel::Expression>::SqlType;
69-
7032
/// Trait to be implemented by structs representing a detachable collection.
7133
///
7234
/// A blanket implementation is provided for traits that implement
7335
/// [`DatastoreAttachTarget`].
74-
pub trait DatastoreDetachTarget<ResourceType>: Selectable<Pg> + Sized {
75-
/// The Rust type of the collection and resource ids (typically Uuid).
76-
type Id: Copy + Debug + PartialEq + Send + 'static;
77-
78-
/// The primary key column of the collection.
79-
type CollectionIdColumn: Column;
80-
81-
/// The time deleted column in the CollectionTable
82-
type CollectionTimeDeletedColumn: Column<Table = <Self::CollectionIdColumn as Column>::Table>
83-
+ Default
84-
+ ExpressionMethods;
85-
86-
/// The primary key column of the resource
87-
type ResourceIdColumn: Column;
88-
89-
/// The column in the resource acting as a foreign key into the Collection
90-
type ResourceCollectionIdColumn: Column<Table = <Self::ResourceIdColumn as Column>::Table>
91-
+ Default
92-
+ ExpressionMethods;
93-
94-
/// The time deleted column in the ResourceTable
95-
type ResourceTimeDeletedColumn: Column<Table = <Self::ResourceIdColumn as Column>::Table>
96-
+ Default
97-
+ ExpressionMethods;
98-
36+
pub trait DatastoreDetachTarget<ResourceType>:
37+
DatastoreAttachTarget<ResourceType>
38+
{
9939
/// Creates a statement for detaching a resource from the given collection.
10040
///
10141
/// This statement allows callers to atomically check the state of a
@@ -250,16 +190,9 @@ pub trait DatastoreDetachTarget<ResourceType>: Selectable<Pg> + Sized {
250190
}
251191
}
252192

253-
impl<T, ResourceType> DatastoreDetachTarget<ResourceType> for T
254-
where
255-
T: DatastoreAttachTarget<ResourceType>,
193+
impl<T, ResourceType> DatastoreDetachTarget<ResourceType> for T where
194+
T: DatastoreAttachTarget<ResourceType>
256195
{
257-
type Id = T::Id;
258-
type CollectionIdColumn = T::CollectionIdColumn;
259-
type CollectionTimeDeletedColumn = T::CollectionTimeDeletedColumn;
260-
type ResourceIdColumn = T::ResourceIdColumn;
261-
type ResourceCollectionIdColumn = T::ResourceCollectionIdColumn;
262-
type ResourceTimeDeletedColumn = T::ResourceTimeDeletedColumn;
263196
}
264197

265198
/// The CTE described in the module docs

nexus/src/db/collection_detach_many.rs

Lines changed: 5 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@
1010
//! - Updates the collection row
1111
//! - Updates the resource rows
1212
13+
use super::collection_attach::aliases::*;
1314
use super::cte_utils::{
1415
BoxableTable, BoxableUpdateStatement, BoxedQuery, ExprSqlType, FilterBy,
15-
QueryFromClause, QuerySqlType, TableDefaultWhereClause,
16+
QueryFromClause, QuerySqlType,
1617
};
1718
use super::pool::DbConnection;
1819
use crate::db::collection_attach::DatastoreAttachTarget;
@@ -28,75 +29,13 @@ use diesel::query_source::Table;
2829
use diesel::sql_types::{Nullable, SingleValue};
2930
use std::fmt::Debug;
3031

31-
/// The table representing the collection. The resource references
32-
/// this table.
33-
type CollectionTable<ResourceType, C> = <<C as DatastoreDetachManyTarget<
34-
ResourceType,
35-
>>::CollectionIdColumn as Column>::Table;
36-
37-
/// The table representing the resource. This table contains an
38-
/// ID acting as a foreign key into the collection table.
39-
type ResourceTable<ResourceType, C> = <<C as DatastoreDetachManyTarget<
40-
ResourceType,
41-
>>::ResourceIdColumn as Column>::Table;
42-
43-
/// The default WHERE clause of the collection table.
44-
type CollectionTableDefaultWhereClause<ResourceType, C> =
45-
TableDefaultWhereClause<CollectionTable<ResourceType, C>>;
46-
/// The default WHERE clause of the resource table.
47-
type ResourceTableDefaultWhereClause<ResourceType, C> =
48-
TableDefaultWhereClause<ResourceTable<ResourceType, C>>;
49-
/// Helper to access column type.
50-
type CollectionIdColumn<ResourceType, C> =
51-
<C as DatastoreDetachManyTarget<ResourceType>>::CollectionIdColumn;
52-
53-
// Representation of Primary Key in Rust.
54-
type CollectionPrimaryKey<ResourceType, C> =
55-
<CollectionTable<ResourceType, C> as Table>::PrimaryKey;
56-
type ResourcePrimaryKey<ResourceType, C> =
57-
<ResourceTable<ResourceType, C> as Table>::PrimaryKey;
58-
type ResourceForeignKey<ResourceType, C> =
59-
<C as DatastoreDetachManyTarget<ResourceType>>::ResourceCollectionIdColumn;
60-
61-
// Representation of Primary Key in SQL.
62-
type SerializedCollectionPrimaryKey<ResourceType, C> =
63-
<CollectionPrimaryKey<ResourceType, C> as diesel::Expression>::SqlType;
64-
type SerializedResourcePrimaryKey<ResourceType, C> =
65-
<ResourcePrimaryKey<ResourceType, C> as diesel::Expression>::SqlType;
66-
type SerializedResourceForeignKey<ResourceType, C> =
67-
<ResourceForeignKey<ResourceType, C> as diesel::Expression>::SqlType;
68-
6932
/// Trait to be implemented by structs representing a detachable collection.
7033
///
7134
/// A blanket implementation is provided for traits that implement
7235
/// [`DatastoreAttachTarget`].
7336
pub trait DatastoreDetachManyTarget<ResourceType>:
74-
Selectable<Pg> + Sized
37+
DatastoreAttachTarget<ResourceType>
7538
{
76-
/// The Rust type of the collection and resource ids (typically Uuid).
77-
type Id: Copy + Debug + PartialEq + Send + 'static;
78-
79-
/// The primary key column of the collection.
80-
type CollectionIdColumn: Column;
81-
82-
/// The time deleted column in the CollectionTable
83-
type CollectionTimeDeletedColumn: Column<Table = <Self::CollectionIdColumn as Column>::Table>
84-
+ Default
85-
+ ExpressionMethods;
86-
87-
/// The primary key column of the resource
88-
type ResourceIdColumn: Column;
89-
90-
/// The column in the resource acting as a foreign key into the Collection
91-
type ResourceCollectionIdColumn: Column<Table = <Self::ResourceIdColumn as Column>::Table>
92-
+ Default
93-
+ ExpressionMethods;
94-
95-
/// The time deleted column in the ResourceTable
96-
type ResourceTimeDeletedColumn: Column<Table = <Self::ResourceIdColumn as Column>::Table>
97-
+ Default
98-
+ ExpressionMethods;
99-
10039
/// Creates a statement for detaching a resource from the given collection.
10140
///
10241
/// This statement allows callers to atomically check the state of a
@@ -259,16 +198,9 @@ pub trait DatastoreDetachManyTarget<ResourceType>:
259198
}
260199
}
261200

262-
impl<T, ResourceType> DatastoreDetachManyTarget<ResourceType> for T
263-
where
264-
T: DatastoreAttachTarget<ResourceType>,
201+
impl<T, ResourceType> DatastoreDetachManyTarget<ResourceType> for T where
202+
T: DatastoreAttachTarget<ResourceType>
265203
{
266-
type Id = T::Id;
267-
type CollectionIdColumn = T::CollectionIdColumn;
268-
type CollectionTimeDeletedColumn = T::CollectionTimeDeletedColumn;
269-
type ResourceIdColumn = T::ResourceIdColumn;
270-
type ResourceCollectionIdColumn = T::ResourceCollectionIdColumn;
271-
type ResourceTimeDeletedColumn = T::ResourceTimeDeletedColumn;
272204
}
273205

274206
/// The CTE described in the module docs

0 commit comments

Comments
 (0)