Skip to main content

Opens

Trait Opens 

Source
pub trait Opens<Level>: Dialect { }
Expand description

A dialect that opens transactions at Level: an isolation level or a SQLite mode as one of the level types, or (), which a table that names neither carries.

A subscription requires Opens<Level> of its table’s dialect, so a table that names a level its dialect lacks does not compile. Every dialect opens (). A dialect implements Opens<Level> for each level whose Opening its begin opens: Postgres READ COMMITTED, REPEATABLE READ and SERIALIZABLE; MySql those and READ UNCOMMITTED; Sqlite its three modes.

§Examples

A dialect of the service’s own opens the levels its begin opens:

use ruststream_sqlx_dialect::{
    Dialect, Isolation, Opening, Opens, Statement, StatementError, TableSpec, level,
};

/// 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 begin(&self, opening: Opening) -> Result<Option<&'static str>, StatementError> {
        match opening {
            Opening::Default | Opening::Isolation(Isolation::ReadCommitted) => Ok(Some(
                "SET TRANSACTION ISOLATION LEVEL READ COMMITTED; BEGIN TRANSACTION",
            )),
            Opening::Isolation(Isolation::RepeatableRead) => Ok(Some(
                "SET TRANSACTION ISOLATION LEVEL REPEATABLE READ; BEGIN TRANSACTION",
            )),
            other => Err(StatementError::UnsupportedOpening {
                dialect: self.name(),
                opening: other.name(),
            }),
        }
    }
}

// A table at one of these levels mounts on `Mssql`; one at any other level does not compile.
impl Opens<level::ReadCommitted> for Mssql {}
impl Opens<level::RepeatableRead> for Mssql {}

SQLite opens no transaction at an isolation level, so the same subscription on SQLite does not compile:

ⓘ
use ruststream_sqlx_dialect::{Isolation, Opening, Opens, Sqlite, StatementError, level};

fn serializable<D: Opens<level::Serializable>>(
    dialect: &D,
) -> Result<Option<&'static str>, StatementError> {
    dialect.begin(Opening::Isolation(Isolation::Serializable))
}

fn subscribe() -> Result<Option<&'static str>, StatementError> {
    serializable(&Sqlite)
}

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§