JoinQuery

Struct JoinQuery 

Source
pub struct JoinQuery<'a, L: 'static, R: 'static> { /* private fields */ }
Expand description

A query builder for joining two collections.

Supports inner joins, left joins, and filtered joins using key-paths for type-safe join conditions.

§Type Parameters

  • 'a - The lifetime of the data being joined
  • L - The type of items in the left collection
  • R - The type of items in the right collection

§Example

let user_orders = JoinQuery::new(&users, &orders)
    .inner_join(
        User::id(),
        Order::user_id(),
        |user, order| (user.name.clone(), order.total)
    );

Implementations§

Source§

impl<'a, L: 'static, R: 'static> JoinQuery<'a, L, R>

Source

pub fn new(left: &'a [L], right: &'a [R]) -> Self

Creates a new join query from two collections.

Note: No Clone required on L or R. The mapper function handles any cloning needed for the result type.

§Arguments
  • left - The left collection to join
  • right - The right collection to join
§Example
let join = JoinQuery::new(&users, &orders);
Source

pub fn inner_join<K, O, F>( &self, left_key: KeyPaths<L, K>, right_key: KeyPaths<R, K>, mapper: F, ) -> Vec<O>
where K: Eq + Hash + Clone + 'static, F: Fn(&L, &R) -> O,

Performs an inner join between two collections.

Returns only the pairs where the join keys match. Uses a hash-based algorithm for O(n + m) performance.

§Arguments
  • left_key - Key-path to the join field in the left collection
  • right_key - Key-path to the join field in the right collection
  • mapper - Function to transform matching pairs into the result type
§Example
let results = JoinQuery::new(&users, &orders)
    .inner_join(
        User::id(),
        Order::user_id(),
        |user, order| UserOrder {
            user_name: user.name.clone(),
            order_total: order.total,
        }
    );
Source

pub fn left_join<K, O, F>( &self, left_key: KeyPaths<L, K>, right_key: KeyPaths<R, K>, mapper: F, ) -> Vec<O>
where K: Eq + Hash + Clone + 'static, F: Fn(&L, Option<&R>) -> O,

Performs a left join between two collections.

Returns all items from the left collection with optional matching items from the right collection. If no match is found, the right item is None.

§Arguments
  • left_key - Key-path to the join field in the left collection
  • right_key - Key-path to the join field in the right collection
  • mapper - Function to transform pairs into the result type (right item may be None)
§Example
let results = JoinQuery::new(&users, &orders)
    .left_join(
        User::id(),
        Order::user_id(),
        |user, order| match order {
            Some(o) => format!("{} has order {}", user.name, o.id),
            None => format!("{} has no orders", user.name),
        }
    );
Source

pub fn inner_join_where<K, O, F, P>( &self, left_key: KeyPaths<L, K>, right_key: KeyPaths<R, K>, predicate: P, mapper: F, ) -> Vec<O>
where K: Eq + Hash + Clone + 'static, F: Fn(&L, &R) -> O, P: Fn(&L, &R) -> bool,

Performs an inner join with an additional filter predicate.

Like inner_join, but only includes pairs that satisfy both the join condition and the additional predicate.

§Arguments
  • left_key - Key-path to the join field in the left collection
  • right_key - Key-path to the join field in the right collection
  • predicate - Additional condition that must be true for pairs to be included
  • mapper - Function to transform matching pairs into the result type
§Example
// Join orders with products, but only high-value orders
let results = JoinQuery::new(&orders, &products)
    .inner_join_where(
        Order::product_id(),
        Product::id(),
        |order, _product| order.total > 100.0,
        |order, product| (product.name.clone(), order.total)
    );
Source

pub fn right_join<K, O, F>( &self, left_key: KeyPaths<L, K>, right_key: KeyPaths<R, K>, mapper: F, ) -> Vec<O>
where K: Eq + Hash + Clone + 'static, F: Fn(Option<&L>, &R) -> O,

Performs a right join between two collections.

Returns all items from the right collection with optional matching items from the left collection. If no match is found, the left item is None.

§Arguments
  • left_key - Key-path to the join field in the left collection
  • right_key - Key-path to the join field in the right collection
  • mapper - Function to transform pairs into the result type (left item may be None)
§Example
let results = JoinQuery::new(&users, &orders)
    .right_join(
        User::id(),
        Order::user_id(),
        |user, order| match user {
            Some(u) => format!("Order {} by {}", order.id, u.name),
            None => format!("Order {} by unknown user", order.id),
        }
    );
Source

pub fn cross_join<O, F>(&self, mapper: F) -> Vec<O>
where F: Fn(&L, &R) -> O,

Performs a cross join (Cartesian product) between two collections.

Returns all possible pairs of items from both collections. Warning: This can produce very large result sets (size = left.len() * right.len()).

§Arguments
  • mapper - Function to transform pairs into the result type
§Example
let all_combinations = JoinQuery::new(&colors, &sizes)
    .cross_join(|color, size| ProductVariant {
        color: color.clone(),
        size: size.clone(),
    });

Auto Trait Implementations§

§

impl<'a, L, R> Freeze for JoinQuery<'a, L, R>

§

impl<'a, L, R> RefUnwindSafe for JoinQuery<'a, L, R>

§

impl<'a, L, R> Send for JoinQuery<'a, L, R>
where L: Sync, R: Sync,

§

impl<'a, L, R> Sync for JoinQuery<'a, L, R>
where L: Sync, R: Sync,

§

impl<'a, L, R> Unpin for JoinQuery<'a, L, R>

§

impl<'a, L, R> UnwindSafe for JoinQuery<'a, L, R>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.