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
impl Database
Sourcepub fn new(connection: impl Connection) -> Self
pub fn new(connection: impl Connection) -> Self
Wrap a concrete connection.
Sourcepub fn from_arc(connection: Arc<dyn Connection>) -> Self
pub fn from_arc(connection: Arc<dyn Connection>) -> Self
Wrap an already-shared connection.
Sourcepub fn connection(&self) -> &Arc<dyn Connection> ⓘ
pub fn connection(&self) -> &Arc<dyn Connection> ⓘ
The underlying connection.
Sourcepub fn is_sharded(&self) -> bool
pub fn is_sharded(&self) -> bool
Whether the backend is a sharded fleet.
Sourcepub fn shard_family(&self) -> Option<String>
pub fn shard_family(&self) -> Option<String>
The shard family, if any.
Sourcepub async fn fetch(
&self,
prepared: Prepared,
columns: Vec<ColumnRequest>,
) -> Result<Vec<OwnedRow>>
pub async fn fetch( &self, prepared: Prepared, columns: Vec<ColumnRequest>, ) -> Result<Vec<OwnedRow>>
Run a prepared query, reading back the named columns.
Sourcepub async fn fetch_all<E: Entity>(&self, prepared: Prepared) -> Result<Vec<E>>
pub async fn fetch_all<E: Entity>(&self, prepared: Prepared) -> Result<Vec<E>>
Run a prepared query and decode every row into E.
Sourcepub async fn fetch_one<E: Entity>(
&self,
prepared: Prepared,
) -> Result<Option<E>>
pub async fn fetch_one<E: Entity>( &self, prepared: Prepared, ) -> Result<Option<E>>
Run a prepared query and decode the first row into E.
Sourcepub async fn fetch_count(&self, prepared: Prepared) -> Result<u64>
pub async fn fetch_count(&self, prepared: Prepared) -> Result<u64>
Run a prepared COUNT(*) AS cnt and read the total.
Sourcepub async fn execute(&self, prepared: Prepared) -> Result<ExecOutcome>
pub async fn execute(&self, prepared: Prepared) -> Result<ExecOutcome>
Run a prepared write.
Sourcepub async fn statement(&self, sql: &str) -> Result<ExecOutcome>
pub async fn statement(&self, sql: &str) -> Result<ExecOutcome>
Run a raw statement with no bindings — for DDL.
Trait Implementations§
Source§impl Executor for Database
Implementing Executor is what keeps Rainier ORM’s whole surface available.
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
fn dialect(&self) -> Dialect
Source§fn shard_family(&self) -> Option<&str>
fn shard_family(&self) -> Option<&str>
None for a
single, unsharded database. Read moreSource§fn is_sharded(&self) -> bool
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>
fn allocate_id(&self, shard_key: u64) -> Option<u64>
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>>>
async fn fetch_all( &self, sql: &str, params: Vec<Value>, ) -> Result<Vec<Box<dyn Row>>>
Source§async fn execute(&self, sql: &str, params: Vec<Value>) -> Result<ExecOutcome>
async fn execute(&self, sql: &str, params: Vec<Value>) -> Result<ExecOutcome>
ExecOutcome.Source§async fn fetch_all_routed(
&self,
route: ShardRoute,
sql: &str,
params: Vec<Value>,
) -> Result<Vec<Box<dyn Row>>>
async fn fetch_all_routed( &self, route: ShardRoute, sql: &str, params: Vec<Value>, ) -> Result<Vec<Box<dyn Row>>>
ShardRoute selects. Read moreSource§async fn execute_routed(
&self,
route: ShardRoute,
sql: &str,
params: Vec<Value>,
) -> Result<ExecOutcome>
async fn execute_routed( &self, route: ShardRoute, sql: &str, params: Vec<Value>, ) -> Result<ExecOutcome>
execute. Defaults to ignoring
the route — see fetch_all_routed.