pub enum KeyPart<'a> {
Literal(&'a str),
Column(&'a str),
}Expand description
One piece of an advisory lock key: literal text, or the value of a column of the row.
#[inbox(advisory_lock = "jobs-{job_id}")] becomes
[KeyPart::Literal("jobs-"), KeyPart::Column("job_id")]. The database renders each row’s key
from the parts, in the claim that selects the row: a literal as it is, a column’s value as
text, a column without a value as empty text.
§Examples
use ruststream_sqlx_dialect::{
Advisory, ClaimShape, Dialect, Form, KeyPart, Param, Statement, StatementError, TableSpec,
};
/// SQL Server, a database without a built-in dialect; the service's advisory tables keep no
/// groups, no claim order, no delays and no finished rows.
#[derive(Debug)]
pub struct Mssql;
impl Advisory for Mssql {
// The candidates of a table read in id order, with their keys.
fn advisory_claim(&self, spec: &TableSpec<'_>) -> Result<Statement, StatementError> {
let Form::Advisory(key) = spec.form() else {
return Err(StatementError::FormMismatch {
statement: "advisory_claim",
form: spec.form().name(),
});
};
// `advisory_lock = "jobs-{job_id}"` arrives as `[Literal("jobs-"), Column("job_id")]`,
// and the database renders each row's key from it. `CONCAT` reads a column without a
// value as empty text, and the leading empty literal gives it the two arguments it
// takes at least.
let mut rendered = String::from("N''");
for part in key {
rendered.push_str(", ");
match part {
KeyPart::Literal(text) => {
rendered.push_str("N'");
rendered.push_str(&text.replace('\'', "''"));
rendered.push('\'');
}
KeyPart::Column(column) => self.quote_into(column, &mut rendered),
}
}
let mut table = String::new();
self.quote_into(spec.table(), &mut table);
let mut id = String::new();
self.quote_into(spec.id().name(), &mut id);
Ok(Statement::new(
format!(
"SELECT TOP (@p1) {id}, CONCAT({rendered}) AS [__lock] \
FROM {table} ORDER BY {id}"
),
[Param::Limit],
))
}
}Variants§
Trait Implementations§
impl<'a> Copy for KeyPart<'a>
impl<'a> Eq for KeyPart<'a>
impl<'a> StructuralPartialEq for KeyPart<'a>
Auto Trait Implementations§
impl<'a> Freeze for KeyPart<'a>
impl<'a> RefUnwindSafe for KeyPart<'a>
impl<'a> Send for KeyPart<'a>
impl<'a> Sync for KeyPart<'a>
impl<'a> Unpin for KeyPart<'a>
impl<'a> UnsafeUnpin for KeyPart<'a>
impl<'a> UnwindSafe for KeyPart<'a>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more