Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove Changeset #1486

Merged
merged 1 commit into from
Jan 17, 2018
Merged

Remove Changeset #1486

merged 1 commit into from
Jan 17, 2018

Conversation

sgrif
Copy link
Member

@sgrif sgrif commented Jan 17, 2018

This is a long overdue refactoring. I'm going to do a similar
refactoring for InsertValues as part of #1106, but this shares a lot
of groundwork for that and is much smaller in scope. Changeset has
always been entirely redundant with QueryFragment, but we had to keep
it as a separate trait for two places where the implementation changed:

  • Eq doesn't qualify the column name
  • tuples skip commas when is_noop returned true (for Changeset this
    was only the case for None or a tuple where all elements were None)

The first one is easy to solve, we just return a type other than Eq in
AsChangeset (there was very little reason to use Eq in the first
place. We weren't able to re-use any of its code.)

The second one is a bit trickier, since we need to replicate the
is_noop method in QueryFragment. I'm fine with doing this, since
InsertValues has the same method (and tuples implement walk_ast
identically for InsertValues and Changeset), so it makes sense to
share them.

Originally I added an explicit noop method to AstPass which we would
call. However, it felt weird that the impl for () which literally just
does Ok(()) didn't call noop. At that point I realized I could just
generalize this to "did a method that generates SQL get called?" This
works fine for updates. I'm not 100% sure if it'll work for inserts, but
it's worth a shot.

I should note that the semantics of the new is_noop are slightly
different than the one on InsertValues, which explicitly states that
an empty array will return false. This will make it return true when
we switch inserts to use this. Since we already have to explicitly
handle empty arrays long before we start checking is_noop, I think
that's fine.

This is a long overdue refactoring. I'm going to do a similar
refactoring for `InsertValues` as part of #1106, but this shares a lot
of groundwork for that and is much smaller in scope. `Changeset` has
always been entirely redundant with `QueryFragment`, but we had to keep
it as a separate trait for two places where the implementation changed:

- `Eq` doesn't qualify the column name
- tuples skip commas when `is_noop` returned true (for `Changeset` this
  was only the case for `None` or a tuple where all elements were `None`)

The first one is easy to solve, we just return a type other than `Eq` in
`AsChangeset` (there was very little reason to use `Eq` in the first
place. We weren't able to re-use any of its code.)

The second one is a bit trickier, since we need to replicate the
`is_noop` method in `QueryFragment`. I'm fine with doing this, since
`InsertValues` has the same method (and tuples implement `walk_ast`
identically for `InsertValues` and `Changeset`), so it makes sense to
share them.

Originally I added an explicit `noop` method to `AstPass` which we would
call. However, it felt weird that the impl for `()` which literally just
does `Ok(())` didn't call `noop`. At that point I realized I could just
generalize this to "did a method that generates SQL get called?" This
works fine for updates. I'm not 100% sure if it'll work for inserts, but
it's worth a shot.

I should note that the semantics of the new `is_noop` are slightly
different than the one on `InsertValues`, which explicitly states that
an empty array will return `false`. This will make it return `true` when
we switch inserts to use this. Since we already have to explicitly
handle empty arrays long before we start checking `is_noop`, I think
that's fine.
@sgrif sgrif requested a review from a team January 17, 2018 17:39
Copy link
Member

@weiznich weiznich left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, modulo the underscore name

None => Ok(()),
fn as_changeset(self) -> Self::Changeset {
Assign {
_column: self.left,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this use a underscore name?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's never actually used. Only the type is. I didn't feel like using PhantomData for it since we actually have the value at the point of construction.

@sgrif sgrif merged commit 5d5e068 into master Jan 17, 2018
@sgrif sgrif deleted the sg-rm-changeset branch January 17, 2018 20:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants