Skip to main content

Role

Enum Role 

Source
#[non_exhaustive]
pub enum Role { Id, Group, PartitionKey, Priority, RetryAfter, Attempt, LockedUntil, ProcessedAt, Headers, Payload, }
Expand description

What a column does for the queue, named as #[field(..)] spells it.

A struct marks the columns that run its queue with roles; every other column is the message’s own data.

§Examples

use ruststream_sqlx_dialect::{Dialect, Param, Role, Statement, StatementError, TableSpec};

/// SQL Server, a database without a built-in dialect.
#[derive(Debug)]
pub struct Mssql;

impl Dialect for Mssql {
    fn name(&self) -> &'static str {
        "mssql"
    }

    fn retry_after(&self, spec: &TableSpec<'_>) -> Result<Statement, StatementError> {
        // A delayed retry writes the column that plays `retry_after`.
        let Some(until) = spec.column(Role::RetryAfter) else {
            return Err(StatementError::MissingRole {
                statement: "retry_after",
                role: Role::RetryAfter,
            });
        };
        let mut sql = String::from("UPDATE ");
        self.quote_into(spec.table(), &mut sql);
        sql.push_str(" SET ");
        self.quote_into(until.name(), &mut sql);
        sql.push_str(" = @p1 WHERE ");
        self.quote_into(spec.id().name(), &mut sql);
        sql.push_str(" = @p2");
        Ok(Statement::new(sql, [Param::RetryAfter, Param::Id]))
    }
}

Variants (Non-exhaustive)§

This enum is marked as non-exhaustive
Non-exhaustive enums could have additional variants added in future. Therefore, when matching against variants of non-exhaustive enums, an extra wildcard arm must be added to account for any future variants.
§

Id

The row’s identity, for settlement and for matching fetched rows to claimed ids.

§

Group

The group a subscription reads: the rows of one table split into groups.

§

PartitionKey

The key of the delivery’s lane under workers(n, by_key) or threads(n, by_key).

§

Priority

The claim order: a smaller value is claimed first.

§

RetryAfter

The time before which a row is not claimed.

§

Attempt

The attempt number; the first delivery reads 1.

§

LockedUntil

The expiry of a lease; a column playing it selects the lease form.

§

ProcessedAt

The time a row was finished; acknowledgement sets it instead of deleting the row.

§

Headers

The delivery’s headers.

§

Payload

The message bytes a handler decodes.

Implementations§

Source§

impl Role

Source

pub const fn attribute(self) -> &'static str

The role’s name inside #[field(..)].

§Examples
use ruststream_sqlx_dialect::{
    ClaimShape, Dialect, Param, Role, RowLock, Statement, StatementError, TableSpec,
};

/// SQL Server, a database without a built-in dialect.
#[derive(Debug)]
pub struct Mssql;

impl RowLock for Mssql {
    // The service's queue tables have no groups and no claim order of their own.
    fn lock_claim(
        &self,
        spec: &TableSpec<'_>,
        shape: ClaimShape,
    ) -> Result<Statement, StatementError> {
        let mut selected = String::new();
        match shape {
            // Whole rows, for a struct the broker reads.
            ClaimShape::Rows => {
                for column in spec.columns() {
                    if !selected.is_empty() {
                        selected.push_str(", ");
                    }
                    self.quote_into(column.name(), &mut selected);
                }
            }
            // Only the ids, for a struct that fetches its rows itself.
            ClaimShape::Ids => self.quote_into(spec.id().name(), &mut selected),
            // The columns that run the queue, each under the name of its role.
            ClaimShape::Roles => {
                let roles = [
                    Role::Id,
                    Role::PartitionKey,
                    Role::Attempt,
                    Role::Headers,
                    Role::Payload,
                ];
                for role in roles {
                    let Some(column) = spec.column(role) else {
                        continue;
                    };
                    if !selected.is_empty() {
                        selected.push_str(", ");
                    }
                    self.quote_into(column.name(), &mut selected);
                    selected.push_str(" AS ");
                    self.quote_into(role.attribute(), &mut selected);
                }
            }
        }
        let mut sql = format!("SELECT TOP (@p1) {selected} FROM ");
        self.quote_into(spec.table(), &mut sql);
        sql.push_str(" WITH (UPDLOCK, READPAST) ORDER BY ");
        self.quote_into(spec.id().name(), &mut sql);
        Ok(Statement::new(sql, [Param::Limit]))
    }
}

Trait Implementations§

Source§

impl Clone for Role

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for Role

Source§

impl Debug for Role

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for Role

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for Role

Source§

impl Hash for Role

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for Role

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Role

Auto Trait Implementations§

§

impl Freeze for Role

§

impl RefUnwindSafe for Role

§

impl Send for Role

§

impl Sync for Role

§

impl Unpin for Role

§

impl UnsafeUnpin for Role

§

impl UnwindSafe for Role

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.