Skip to main content

Database

Struct Database 

Source
pub struct Database { /* private fields */ }
Expand description

A database handle: a shared Connection, cheap to clone.

Lives as a container singleton and is cloned into repositories, jobs and console commands freely.

Implementations§

Source§

impl Database

Source

pub fn new(connection: impl Connection) -> Self

Wrap a concrete connection.

Source

pub fn from_arc(connection: Arc<dyn Connection>) -> Self

Wrap an already-shared connection.

Source

pub fn connection(&self) -> &Arc<dyn Connection>

The underlying connection.

Source

pub fn dialect(&self) -> Dialect

The dialect the backend speaks.

Source

pub fn is_sharded(&self) -> bool

Whether the backend is a sharded fleet.

Source

pub fn shard_family(&self) -> Option<String>

The shard family, if any.

Source

pub async fn fetch( &self, prepared: Prepared, columns: Vec<ColumnRequest>, ) -> Result<Vec<OwnedRow>>

Run a prepared query, reading back the named columns.

Source

pub async fn fetch_all<E: Entity>(&self, prepared: Prepared) -> Result<Vec<E>>

Run a prepared query and decode every row into E.

Source

pub async fn fetch_one<E: Entity>( &self, prepared: Prepared, ) -> Result<Option<E>>

Run a prepared query and decode the first row into E.

Source

pub async fn fetch_count(&self, prepared: Prepared) -> Result<u64>

Run a prepared COUNT(*) AS cnt and read the total.

Source

pub async fn execute(&self, prepared: Prepared) -> Result<ExecOutcome>

Run a prepared write.

Source

pub async fn statement(&self, sql: &str) -> Result<ExecOutcome>

Run a raw statement with no bindings — for DDL.

Source§

impl Database

Source

pub fn query(&self, sql: impl Into<String>) -> RawQuery<'_>

Start a raw statement.

ⓘ
database.query("UPDATE posts SET views = views + 1 WHERE id = ?")
    .bind(post_id)
    .execute()
    .await?;

See the module docs for what is and is not safe to put in the statement itself.

Trait Implementations§

Source§

impl Clone for Database

Source§

fn clone(&self) -> Database

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 Debug for Database

Source§

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

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

impl Executor for Database

Implementing Executor is what keeps Rainier ORM’s whole surface available.

repo::insert, repo::query::<E>(), Cursor and Migrator::run are all generic over X: Executor, so they accept a Database directly. Their futures are !Send (see the module docs), so use them from a console command, a migration, or a Worker — not from a request handler, which needs the Send path above.

Source§

fn dialect(&self) -> Dialect

The SQL dialect this backend speaks, so callers render matching SQL.
Source§

fn shard_family(&self) -> Option<&str>

The shard family this connector routes within, or None for a single, unsharded database. Read more
Source§

fn is_sharded(&self) -> bool

true when this connector is sharded — sugar for shard_family().is_some().
Source§

fn allocate_id(&self, shard_key: u64) -> Option<u64>

Mint a shard-encoded primary key for a row whose shard-key value is shard_key (e.g. the owning user_id). repo::insert calls this when an entity’s primary key is itself a shard key, is not auto-increment, and is unset — so id assignment for the sharded data tier happens here, in the connector, transparently, not in the service. The default is None (no allocation: the caller’s id or the DB’s auto-increment stands) so single, unsharded executors are unaffected.
Source§

async fn fetch_all( &self, sql: &str, params: Vec<Value>, ) -> Result<Vec<Box<dyn Row>>>

Run a query and return every row. Parameters are bound positionally in the order sea-query emitted them.
Source§

async fn execute(&self, sql: &str, params: Vec<Value>) -> Result<ExecOutcome>

Run a write and report its ExecOutcome.
Source§

async fn fetch_all_routed( &self, route: ShardRoute, sql: &str, params: Vec<Value>, ) -> Result<Vec<Box<dyn Row>>>

Run a query against the physical database the ShardRoute selects. Read more
Source§

async fn execute_routed( &self, route: ShardRoute, sql: &str, params: Vec<Value>, ) -> Result<ExecOutcome>

Routed counterpart of execute. Defaults to ignoring the route — see fetch_all_routed.

Auto Trait Implementations§

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> Event for T
where T: Send + Sync + 'static,

Source§

fn event_name() -> &'static str
where Self: Sized,

A human-readable name, used by wildcard listeners and log output. Always the Rust type path; see the trait docs.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more