Skip to content

Commit

Permalink
fix(clinvoice_adapter_postgres): workaround for rust-lang/rust#39959
Browse files Browse the repository at this point in the history
  • Loading branch information
Iron-E committed Jun 20, 2022
1 parent 2a74a0c commit 95e55a4
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 53 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
mod display;

use core::fmt::Display;

use clinvoice_adapter::fmt::SnakeCase;
Expand All @@ -12,54 +10,34 @@ use clinvoice_adapter::fmt::SnakeCase;
/// Created to avoid using `format!` every time this pattern was required, thus eagerly allocating
/// a [`String`] even if it was only needed for pushing to another [`String`].
#[derive(Copy, Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub(crate) struct PgLocationRecursiveCte<TCurrent, TPrev>(SnakeCase<TPrev, TCurrent>)
where
TCurrent: Display,
TPrev: Display;
pub(crate) struct PgLocationRecursiveCte;

impl<TCurrent, TPrev> PgLocationRecursiveCte<TCurrent, TPrev>
where
TCurrent: Display,
TPrev: Display,
impl PgLocationRecursiveCte
{
/// # Summary
///
/// Return the previous occurance of the [`PgLocationRecursiveCte`], if there is one.
pub(crate) const fn prev(&self) -> Option<&TPrev>
/// Create a new recursive CTE identifier for a [`PgLocation`].
pub(crate) const fn new() -> SnakeCase<&'static str, &'static str>
{
if let Some((left, _)) = self.0.slice_end()
{
return Some(left);
}

None
SnakeCase::new("location")
}

/// # Summary
///
/// Get the [`PgLocationRecursiveCte`] representing the [`Location`](clinvoice_schema::Location) this one.
pub(crate) fn outer(self) -> PgLocationRecursiveCte<&'static str, SnakeCase<TPrev, TCurrent>>
{
PgLocationRecursiveCte(self.0.push("outer"))
}
}

impl PgLocationRecursiveCte<&'static str, &'static str>
{
/// # Summary
///
/// Create a new recursive CTE identifier for a [`PgLocation`].
pub(crate) const fn new() -> Self
pub(crate) const fn outer<T>(t: T) -> SnakeCase<T, &'static str>
where
T: Display,
{
Self(SnakeCase::new("location"))
SnakeCase::Body(t, "outer")
}

/// # Summary
///
/// The ident used to refer to the rows matching some [`MatchLocation`] at the end of a `WITH
/// RECURSIVE`.
pub(crate) const fn report() -> Self
pub(crate) const fn report() -> SnakeCase<&'static str, &'static str>
{
Self(SnakeCase::new("location_report"))
SnakeCase::new("location_report")
}
}

This file was deleted.

22 changes: 17 additions & 5 deletions crates/adapters/clinvoice_adapter_postgres/src/schema/location.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ impl PgLocation
/// Generate multiple Common Table Expressions for a recursive query.
fn generate_cte<TCurrent, TPrev, const FIRST: bool>(
query: &mut QueryBuilder<Postgres>,
ident: PgLocationRecursiveCte<TCurrent, TPrev>,
ident: TCurrent, /* HACK: this parameter should be `ident: SnakeCase<TPrev, TCurrent>` after rust-lang/rust#39959 */
match_condition: &MatchLocation,
prev: Option<TPrev>, /* HACK: this parameter is necessary because we can't use `ident: SnakeCase` because of rust-lang/rust#39959 */
) where
TCurrent: Display,
TPrev: Display,
Expand All @@ -58,11 +59,11 @@ impl PgLocation
.push("FROM locations")
.push(ALIAS_OUTER);

if let Some(prev) = ident.prev()
if let Some(p) = prev
{
separated
.push("JOIN")
.push(prev)
.push(p)
.push(ALIAS_INNER)
.push("ON (")
.push_unseparated(outer_columns.id)
Expand Down Expand Up @@ -103,7 +104,13 @@ impl PgLocation
MatchOuterLocation::Some(ref outer) =>
{
query.push(',');
generate_cte::<_, _, false>(query, ident.outer(), outer)
generate_cte::<_, _, false>(
query,
// HACK: remove `.to_string()` after rust-lang/rust#39959
PgLocationRecursiveCte::outer(&ident).to_string(),
outer,
Some(&ident),
)
},
MatchOuterLocation::Any | MatchOuterLocation::None =>
{
Expand Down Expand Up @@ -148,7 +155,12 @@ impl PgLocation

let mut query = QueryBuilder::new("WITH RECURSIVE ");

generate_cte::<_, _, true>(&mut query, PgLocationRecursiveCte::new(), match_condition);
generate_cte::<_, _, true>(
&mut query,
PgLocationRecursiveCte::new(),
match_condition,
None::<&str>,
);

query.push(' ');
query
Expand Down

0 comments on commit 95e55a4

Please sign in to comment.