pub struct Database { /* private fields */ }db only.Expand description
A database connection structure that holds the connection to the database.
It is used to execute queries and interact with the database. The connection
is established when the structure is created and closed when
Self::close() is called.
Implementations§
Source§impl Database
impl Database
Sourcepub async fn new<T: Into<String>>(url: T) -> Result<Self>
pub async fn new<T: Into<String>>(url: T) -> Result<Self>
Creates a new database connection. The connection string should be in the format of the database URL.
§Errors
This method can return an error if the connection to the database could not be established.
This method can return an error if the database URL is invalid.
§Panics
This method will panic if the database URL is not supported.
§Examples
use cot::db::Database;
#[tokio::main]
async fn main() {
let db = Database::new("sqlite::memory:").await.unwrap();
}Sourcepub async fn close(&self) -> Result<()>
pub async fn close(&self) -> Result<()>
Closes the database connection.
This method should be called when the database connection is no longer needed. The connection is closed and the resources are released.
§Errors
This method can return an error if the connection to the database could not be closed gracefully, for instance because the connection has already been dropped.
§Examples
use cot::db::Database;
#[tokio::main]
async fn main() {
let db = Database::new("sqlite::memory:").await.unwrap();
db.close().await.unwrap();
}Sourcepub async fn insert<T: Model>(&self, data: &mut T) -> Result<()>
pub async fn insert<T: Model>(&self, data: &mut T) -> Result<()>
Inserts a new row into the database.
§Errors
This method can return an error if the row could not be inserted into the database, for instance because the migrations haven’t been applied, or there was a problem with the database connection.
Sourcepub async fn insert_or_update<T: Model>(&self, data: &mut T) -> Result<()>
pub async fn insert_or_update<T: Model>(&self, data: &mut T) -> Result<()>
Inserts a new row into the database, or updates it if a row with the same primary key already exists.
§Errors
This method can return an error if the row could not be inserted into the database, for instance because the migrations haven’t been applied, or there was a problem with the database connection.
Sourcepub async fn update<T: Model>(&self, data: &mut T) -> Result<()>
pub async fn update<T: Model>(&self, data: &mut T) -> Result<()>
Updates an existing row in a database.
§Errors
This method can return an error if the row could not be updated in the database, for instance because the migrations haven’t been applied, or there was a problem with the database connection.
This method can return an error if the row with the given primary key could not be found in the database.
Sourcepub async fn query<T: Model>(&self, query: &Query<T>) -> Result<Vec<T>>
pub async fn query<T: Model>(&self, query: &Query<T>) -> Result<Vec<T>>
Executes the given query and returns the results converted to the model type.
§Errors
This method can return an error if the query is invalid.
This method can return an error if the data in the database is not compatible with the model (usually meaning the migrations haven’t been generated or applied).
Can return an error if the database connection is lost.
Sourcepub async fn get<T: Model>(&self, query: &Query<T>) -> Result<Option<T>>
pub async fn get<T: Model>(&self, query: &Query<T>) -> Result<Option<T>>
Returns the first row that matches the given query. If no rows match the
query, returns None.
§Errors
This method can return an error if the query is invalid.
This method can return an error if the model doesn’t exist in the database (usually meaning the migrations haven’t been generated or applied).
Can return an error if the database connection is lost.
Sourcepub async fn exists<T: Model>(&self, query: &Query<T>) -> Result<bool>
pub async fn exists<T: Model>(&self, query: &Query<T>) -> Result<bool>
Returns whether a row exists that matches the given query.
§Errors
This method can return an error if the query is invalid.
This method can return an error if the model doesn’t exist in the database (usually meaning the migrations haven’t been generated or applied).
Can return an error if the database connection is lost.
Sourcepub async fn delete<T: Model>(
&self,
query: &Query<T>,
) -> Result<StatementResult>
pub async fn delete<T: Model>( &self, query: &Query<T>, ) -> Result<StatementResult>
Deletes all rows that match the given query.
§Errors
This method can return an error if the query is invalid.
This method can return an error if the model doesn’t exist in the database (usually meaning the migrations haven’t been generated or applied).
Can return an error if the database connection is lost.
Sourcepub async fn raw(&self, query: &str) -> Result<StatementResult>
pub async fn raw(&self, query: &str) -> Result<StatementResult>
Executes a raw SQL query.
§Errors
This method can return an error if the query is invalid.
Can return an error if the database connection is lost.
§Examples
use cot::db::Database;
let db = Database::new("sqlite::memory:").await?;
db.raw("CREATE TABLE test (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT)")
.await?;Sourcepub async fn raw_with(
&self,
query: &str,
values: &[&dyn ToDbValue],
) -> Result<StatementResult>
pub async fn raw_with( &self, query: &str, values: &[&dyn ToDbValue], ) -> Result<StatementResult>
Executes a raw SQL query with parameters.
§Errors
This method can return an error if the query is invalid.
Can return an error if the database connection is lost.
§Examples
use cot::db::Database;
let db = Database::new("sqlite::memory:").await?;
db.raw("CREATE TABLE test (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT)")
.await?;
db.raw_with("SELECT * FROM test WHERE id = ?", &[&1])
.await?;Trait Implementations§
Source§impl DatabaseBackend for Database
impl DatabaseBackend for Database
Source§fn insert_or_update<'life0, 'life1, 'async_trait, T>(
&'life0 self,
data: &'life1 mut T,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
T: 'async_trait + Model,
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn insert_or_update<'life0, 'life1, 'async_trait, T>(
&'life0 self,
data: &'life1 mut T,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
T: 'async_trait + Model,
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Source§fn insert<'life0, 'life1, 'async_trait, T>(
&'life0 self,
data: &'life1 mut T,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
T: 'async_trait + Model,
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn insert<'life0, 'life1, 'async_trait, T>(
&'life0 self,
data: &'life1 mut T,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
T: 'async_trait + Model,
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Source§fn update<'life0, 'life1, 'async_trait, T>(
&'life0 self,
data: &'life1 mut T,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
T: 'async_trait + Model,
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn update<'life0, 'life1, 'async_trait, T>(
&'life0 self,
data: &'life1 mut T,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
T: 'async_trait + Model,
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Source§fn query<'life0, 'life1, 'async_trait, T>(
&'life0 self,
query: &'life1 Query<T>,
) -> Pin<Box<dyn Future<Output = Result<Vec<T>>> + Send + 'async_trait>>where
T: 'async_trait + Model,
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn query<'life0, 'life1, 'async_trait, T>(
&'life0 self,
query: &'life1 Query<T>,
) -> Pin<Box<dyn Future<Output = Result<Vec<T>>> + Send + 'async_trait>>where
T: 'async_trait + Model,
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Source§fn get<'life0, 'life1, 'async_trait, T>(
&'life0 self,
query: &'life1 Query<T>,
) -> Pin<Box<dyn Future<Output = Result<Option<T>>> + Send + 'async_trait>>where
T: 'async_trait + Model,
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn get<'life0, 'life1, 'async_trait, T>(
&'life0 self,
query: &'life1 Query<T>,
) -> Pin<Box<dyn Future<Output = Result<Option<T>>> + Send + 'async_trait>>where
T: 'async_trait + Model,
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
None. Read moreSource§fn exists<'life0, 'life1, 'async_trait, T>(
&'life0 self,
query: &'life1 Query<T>,
) -> Pin<Box<dyn Future<Output = Result<bool>> + Send + 'async_trait>>where
T: 'async_trait + Model,
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn exists<'life0, 'life1, 'async_trait, T>(
&'life0 self,
query: &'life1 Query<T>,
) -> Pin<Box<dyn Future<Output = Result<bool>> + Send + 'async_trait>>where
T: 'async_trait + Model,
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Source§fn delete<'life0, 'life1, 'async_trait, T>(
&'life0 self,
query: &'life1 Query<T>,
) -> Pin<Box<dyn Future<Output = Result<StatementResult>> + Send + 'async_trait>>where
T: 'async_trait + Model,
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn delete<'life0, 'life1, 'async_trait, T>(
&'life0 self,
query: &'life1 Query<T>,
) -> Pin<Box<dyn Future<Output = Result<StatementResult>> + Send + 'async_trait>>where
T: 'async_trait + Model,
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Auto Trait Implementations§
impl Freeze for Database
impl !RefUnwindSafe for Database
impl Send for Database
impl Sync for Database
impl Unpin for Database
impl !UnwindSafe for Database
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
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> IntoField<Auto<T>> for T
impl<T> IntoField<Auto<T>> for T
Source§fn into_field(self) -> Auto<T>
fn into_field(self) -> Auto<T>
db only.