pub struct TableName<'a> { /* private fields */ }Expand description
A table outside the queue’s own description, such as the table dead-lettered rows move to.
It is read from table or schema.table, and checked when it is read: no segment is empty,
and there is at most one schema.
§Examples
use ruststream_sqlx_dialect::{
Dialect, Param, Statement, StatementError, TableName, 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"
}
// A dead email of the service's one queue table moves with the time it died.
fn dead_letter_table(
&self,
_spec: &TableSpec<'_>,
target: TableName<'_>,
) -> Result<Vec<Statement>, StatementError> {
let mut into = String::new();
self.quote_into(target.schema().unwrap_or("dbo"), &mut into);
into.push('.');
self.quote_into(target.table(), &mut into);
Ok(vec![
Statement::new(
format!(
"INSERT INTO {into} ([job_id], [payload], [died_at]) \
SELECT [job_id], [payload], @p1 FROM [email_jobs] WHERE [job_id] = @p2"
),
[Param::Now, Param::Id],
),
Statement::new("DELETE FROM [email_jobs] WHERE [job_id] = @p1", [Param::Id]),
])
}
fn quote_into(&self, ident: &str, out: &mut String) {
out.push('[');
out.push_str(&ident.replace(']', "]]"));
out.push(']');
}
}Implementations§
Source§impl<'a> TableName<'a>
impl<'a> TableName<'a>
Sourcepub fn parse(name: &'a str) -> Result<Self, ParseTableNameError>
pub fn parse(name: &'a str) -> Result<Self, ParseTableNameError>
Reads table or schema.table.
§Errors
ParseTableNameError::EmptySegment when the name or one of its segments is empty;
ParseTableNameError::TooManySegments when the name has more than one dot.
§Examples
use ruststream_sqlx_dialect::{Column, Dialect, Form, Postgres, TableName, TableSpec};
const EMAILS: TableSpec<'static> =
TableSpec::new("email_jobs", Column::new("job_id"), Form::RowLock)
.payload(Column::new("payload"));
// A dead email moves to the table the configuration names, in the `archive` schema.
let target = TableName::parse("archive.email_jobs_dead")?;
let moves = Postgres.dead_letter_table(&EMAILS, target)?;
assert_eq!(
moves[0].sql(),
r#"WITH moved AS (DELETE FROM "email_jobs" WHERE "job_id" = $1 RETURNING "job_id", "payload") INSERT INTO "archive"."email_jobs_dead" ("job_id", "payload") SELECT "job_id", "payload" FROM moved"#
);Sourcepub const fn schema(&self) -> Option<&'a str>
pub const fn schema(&self) -> Option<&'a str>
The schema, or None for the connection’s default.
§Examples
use ruststream_sqlx_dialect::{
Dialect, Param, Statement, StatementError, TableName, 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 dead_letter_table(
&self,
spec: &TableSpec<'_>,
target: TableName<'_>,
) -> Result<Vec<Statement>, StatementError> {
// A struct that flattens another hides columns, so its row moves by position.
let mut columns = String::new();
if spec.selects_all() {
columns.push('*');
} else {
for column in spec.columns() {
if !columns.is_empty() {
columns.push_str(", ");
}
self.quote_into(column.name(), &mut columns);
}
}
// A target that names no schema lives in SQL Server's default one, `dbo`.
let mut into = String::new();
self.quote_into(target.schema().unwrap_or("dbo"), &mut into);
into.push('.');
self.quote_into(target.table(), &mut into);
let mut from = String::new();
self.quote_into(spec.table(), &mut from);
let mut id = String::new();
self.quote_into(spec.id().name(), &mut id);
let into_columns = if spec.selects_all() {
String::new()
} else {
format!(" ({columns})")
};
Ok(vec![
Statement::new(
format!(
"INSERT INTO {into}{into_columns} SELECT {columns} FROM {from} \
WHERE {id} = @p1"
),
[Param::Id],
),
Statement::new(format!("DELETE FROM {from} WHERE {id} = @p1"), [Param::Id]),
])
}
}Sourcepub const fn table(&self) -> &'a str
pub const fn table(&self) -> &'a str
The table’s name, without its schema.
§Examples
use ruststream_sqlx_dialect::{
Dialect, Param, Statement, StatementError, TableName, 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 dead_letter_table(
&self,
spec: &TableSpec<'_>,
target: TableName<'_>,
) -> Result<Vec<Statement>, StatementError> {
// A struct that flattens another hides columns, so its row moves by position.
let mut columns = String::new();
if spec.selects_all() {
columns.push('*');
} else {
for column in spec.columns() {
if !columns.is_empty() {
columns.push_str(", ");
}
self.quote_into(column.name(), &mut columns);
}
}
// A target that names no schema lives in SQL Server's default one, `dbo`.
let mut into = String::new();
self.quote_into(target.schema().unwrap_or("dbo"), &mut into);
into.push('.');
self.quote_into(target.table(), &mut into);
let mut from = String::new();
self.quote_into(spec.table(), &mut from);
let mut id = String::new();
self.quote_into(spec.id().name(), &mut id);
let into_columns = if spec.selects_all() {
String::new()
} else {
format!(" ({columns})")
};
Ok(vec![
Statement::new(
format!(
"INSERT INTO {into}{into_columns} SELECT {columns} FROM {from} \
WHERE {id} = @p1"
),
[Param::Id],
),
Statement::new(format!("DELETE FROM {from} WHERE {id} = @p1"), [Param::Id]),
])
}
}Trait Implementations§
impl<'a> Copy for TableName<'a>
impl<'a> Eq for TableName<'a>
impl<'a> StructuralPartialEq for TableName<'a>
Auto Trait Implementations§
impl<'a> Freeze for TableName<'a>
impl<'a> RefUnwindSafe for TableName<'a>
impl<'a> Send for TableName<'a>
impl<'a> Sync for TableName<'a>
impl<'a> Unpin for TableName<'a>
impl<'a> UnsafeUnpin for TableName<'a>
impl<'a> UnwindSafe for TableName<'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