#[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
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
impl Role
Sourcepub const fn attribute(self) -> &'static str
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]))
}
}