-
Notifications
You must be signed in to change notification settings - Fork 40
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
[nexus] Make 'update_and_check' CTE explicitly request columns #4572
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
|
||
//! CTE utility for iterating over all columns in a table. | ||
|
||
use diesel::prelude::*; | ||
use std::marker::PhantomData; | ||
|
||
/// Used to iterate over a tuple of columns ("T"). | ||
/// | ||
/// Diesel exposes "AllColumns" as a tuple, which is difficult to iterate over | ||
/// -- after all, all the types are distinct. However, each of these types | ||
/// implements "Column", so we can use a macro to provide a | ||
/// "convertion-to-iterator" implemenation for our expected tuples. | ||
pub(crate) struct ColumnWalker<T> { | ||
remaining: PhantomData<T>, | ||
} | ||
|
||
impl<T> ColumnWalker<T> { | ||
pub fn new() -> Self { | ||
Self { remaining: PhantomData } | ||
} | ||
} | ||
|
||
macro_rules! impl_column_walker { | ||
( $($column:ident)+ ) => ( | ||
impl<$($column: Column),+> IntoIterator for ColumnWalker<($($column,)+)> { | ||
type Item = &'static str; | ||
type IntoIter = std::vec::IntoIter<Self::Item>; | ||
|
||
fn into_iter(self) -> Self::IntoIter { | ||
// TODO: don't convert to vec? You'll need to figure | ||
// out how to state the type of IntoIter. | ||
[$($column::NAME,)+].to_vec().into_iter() | ||
} | ||
} | ||
); | ||
} | ||
|
||
// implementations for 1 - 32 columns | ||
impl_column_walker! { A } | ||
impl_column_walker! { A B } | ||
impl_column_walker! { A B C } | ||
impl_column_walker! { A B C D } | ||
impl_column_walker! { A B C D E } | ||
impl_column_walker! { A B C D E F } | ||
impl_column_walker! { A B C D E F G } | ||
impl_column_walker! { A B C D E F G H } | ||
impl_column_walker! { A B C D E F G H I } | ||
impl_column_walker! { A B C D E F G H I J } | ||
impl_column_walker! { A B C D E F G H I J K } | ||
impl_column_walker! { A B C D E F G H I J K L } | ||
impl_column_walker! { A B C D E F G H I J K L M } | ||
impl_column_walker! { A B C D E F G H I J K L M N } | ||
impl_column_walker! { A B C D E F G H I J K L M N O } | ||
impl_column_walker! { A B C D E F G H I J K L M N O P } | ||
impl_column_walker! { A B C D E F G H I J K L M N O P Q } | ||
impl_column_walker! { A B C D E F G H I J K L M N O P Q R } | ||
impl_column_walker! { A B C D E F G H I J K L M N O P Q R S } | ||
impl_column_walker! { A B C D E F G H I J K L M N O P Q R S T } | ||
impl_column_walker! { A B C D E F G H I J K L M N O P Q R S T U } | ||
impl_column_walker! { A B C D E F G H I J K L M N O P Q R S T U V } | ||
impl_column_walker! { A B C D E F G H I J K L M N O P Q R S T U V W } | ||
impl_column_walker! { A B C D E F G H I J K L M N O P Q R S T U V W X } | ||
impl_column_walker! { A B C D E F G H I J K L M N O P Q R S T U V W X Y } | ||
impl_column_walker! { A B C D E F G H I J K L M N O P Q R S T U V W X Y Z } | ||
impl_column_walker! { A B C D E F G H I J K L M N O P Q R S T U V W X Y Z A1 } | ||
impl_column_walker! { A B C D E F G H I J K L M N O P Q R S T U V W X Y Z A1 B1 } | ||
impl_column_walker! { A B C D E F G H I J K L M N O P Q R S T U V W X Y Z A1 B1 C1 } | ||
impl_column_walker! { A B C D E F G H I J K L M N O P Q R S T U V W X Y Z A1 B1 C1 D1 } | ||
impl_column_walker! { A B C D E F G H I J K L M N O P Q R S T U V W X Y Z A1 B1 C1 D1 E1 } | ||
impl_column_walker! { A B C D E F G H I J K L M N O P Q R S T U V W X Y Z A1 B1 C1 D1 E1 F1 } | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
|
||
table! { | ||
test_schema.test_table (id) { | ||
id -> Uuid, | ||
value -> Int4, | ||
time_deleted -> Nullable<Timestamptz>, | ||
} | ||
} | ||
|
||
// We can convert all a tables columns into an iteratable format. | ||
#[test] | ||
fn test_walk_table() { | ||
let all_columns = | ||
ColumnWalker::<<test_table::table as Table>::AllColumns>::new(); | ||
|
||
let mut iter = all_columns.into_iter(); | ||
assert_eq!(iter.next(), Some("id")); | ||
assert_eq!(iter.next(), Some("value")); | ||
assert_eq!(iter.next(), Some("time_deleted")); | ||
assert_eq!(iter.next(), None); | ||
} | ||
|
||
// We can, if we want to, also make a ColumnWalker out of an arbitrary tuple | ||
// of columns. | ||
#[test] | ||
fn test_walk_columns() { | ||
let all_columns = ColumnWalker::<( | ||
test_table::columns::id, | ||
test_table::columns::value, | ||
)>::new(); | ||
|
||
let mut iter = all_columns.into_iter(); | ||
assert_eq!(iter.next(), Some("id")); | ||
assert_eq!(iter.next(), Some("value")); | ||
assert_eq!(iter.next(), None); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would like to turn this into the following
... but this would require changing:
Into:
I don't know how to specify the type of "N" here, even though it should be knowable -- it's the length of
$column
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think either of the first two options in https://danielkeep.github.io/tlborm/book/blk-counting.html would be fine. A better approach would be rust-lang/rust#83527 which isn't stable yet.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually in this case you can just pass in the length as an argument to
impl_column_walker
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, good call, I'll pass the length explicitly.