Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Documentation for sea-orm #280

Merged
merged 21 commits into from
Nov 1, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Documetation for the executor module
  • Loading branch information
charleschege committed Oct 29, 2021
commit 817e9bdd2313658c78e4205391768ffd9fec4689
7 changes: 7 additions & 0 deletions src/executor/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,24 @@ use crate::{
use sea_query::DeleteStatement;
use std::future::Future;

/// Handles DELETE operations in a ActiveModel using [DeleteStatement]
#[derive(Clone, Debug)]
pub struct Deleter {
query: DeleteStatement,
}

/// The result of a DELETE operation
#[derive(Clone, Debug)]
pub struct DeleteResult {
/// The number of rows affected by the DELETE operation
pub rows_affected: u64,
}

impl<'a, A: 'a> DeleteOne<A>
where
A: ActiveModelTrait,
{
/// Execute a DELETE operation on one ActiveModel
pub fn exec<C>(self, db: &'a C) -> impl Future<Output = Result<DeleteResult, DbErr>> + '_
where
C: ConnectionTrait<'a>,
Expand All @@ -31,6 +35,7 @@ impl<'a, E> DeleteMany<E>
where
E: EntityTrait,
{
/// Execute a DELETE operation on many ActiveModels
pub fn exec<C>(self, db: &'a C) -> impl Future<Output = Result<DeleteResult, DbErr>> + '_
where
C: ConnectionTrait<'a>,
Expand All @@ -41,10 +46,12 @@ where
}

impl Deleter {
/// Instantiate a new [Deleter] by passing it a [DeleteStatement]
pub fn new(query: DeleteStatement) -> Self {
Self { query }
}

/// Execute a DELETE operation
pub fn exec<'a, C>(self, db: &'a C) -> impl Future<Output = Result<DeleteResult, DbErr>> + '_
where
C: ConnectionTrait<'a>,
Expand Down
10 changes: 10 additions & 0 deletions src/executor/execute.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,32 @@
/// Defines the result of executing an operation
#[derive(Debug)]
pub struct ExecResult {
/// The type of result from the execution depending on the feature flag enabled
/// to choose a database backend
pub(crate) result: ExecResultHolder,
}

/// Holds a result depending on the database backend chosen by the feature flag
#[derive(Debug)]
pub(crate) enum ExecResultHolder {
/// Holds the result of executing an operation on a MySQL database
#[cfg(feature = "sqlx-mysql")]
SqlxMySql(sqlx::mysql::MySqlQueryResult),
/// Holds the result of executing an operation on a PostgreSQL database
#[cfg(feature = "sqlx-postgres")]
SqlxPostgres(sqlx::postgres::PgQueryResult),
/// Holds the result of executing an operation on a SQLite database
#[cfg(feature = "sqlx-sqlite")]
SqlxSqlite(sqlx::sqlite::SqliteQueryResult),
/// Holds the result of executing an operation on the Mock database
#[cfg(feature = "mock")]
Mock(crate::MockExecResult),
}

// ExecResult //

impl ExecResult {
/// Get the last id after `AUTOINCREMENT` is done on the primary key
pub fn last_insert_id(&self) -> u64 {
match &self.result {
#[cfg(feature = "sqlx-mysql")]
Expand All @@ -40,6 +49,7 @@ impl ExecResult {
}
}

/// Get the number of rows affedted by the operation
pub fn rows_affected(&self) -> u64 {
match &self.result {
#[cfg(feature = "sqlx-mysql")]
Expand Down
6 changes: 6 additions & 0 deletions src/executor/insert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::{
use sea_query::{FromValueTuple, InsertStatement, ValueTuple};
use std::{future::Future, marker::PhantomData};

/// Defines a structure to perform INSERT operations in an ActiveModel
#[derive(Debug)]
pub struct Inserter<A>
where
Expand All @@ -15,18 +16,21 @@ where
model: PhantomData<A>,
}

/// The result of an INSERT operation on an ActiveModel
#[derive(Debug)]
pub struct InsertResult<A>
where
A: ActiveModelTrait,
{
/// The id performed when AUTOINCREMENT was performed on the PrimaryKey
pub last_insert_id: <<<A as ActiveModelTrait>::Entity as EntityTrait>::PrimaryKey as PrimaryKeyTrait>::ValueType,
}

impl<A> Insert<A>
where
A: ActiveModelTrait,
{
/// Execute an insert operation
#[allow(unused_mut)]
pub fn exec<'a, C>(self, db: &'a C) -> impl Future<Output = Result<InsertResult<A>, DbErr>> + '_
where
Expand All @@ -53,6 +57,7 @@ impl<A> Inserter<A>
where
A: ActiveModelTrait,
{
/// Instantiate a new insert operation
pub fn new(primary_key: Option<ValueTuple>, query: InsertStatement) -> Self {
Self {
primary_key,
Expand All @@ -61,6 +66,7 @@ where
}
}

/// Execute an insert operation
pub fn exec<'a, C>(self, db: &'a C) -> impl Future<Output = Result<InsertResult<A>, DbErr>> + '_
where
C: ConnectionTrait<'a>,
Expand Down
2 changes: 2 additions & 0 deletions src/executor/paginator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ use futures::Stream;
use sea_query::{Alias, Expr, SelectStatement};
use std::{marker::PhantomData, pin::Pin};

/// Pin a Model so that stream operations can be performed on the model
pub type PinBoxStream<'db, Item> = Pin<Box<dyn Stream<Item = Item> + 'db>>;

/// Defined a structure to handle pagination of a result from a query operation on a Model
#[derive(Clone, Debug)]
pub struct Paginator<'db, C, S>
where
Expand Down
13 changes: 12 additions & 1 deletion src/executor/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::debug_print;
use crate::{DbErr, SelectGetableValue, SelectorRaw, Statement};
use std::fmt;

/// Defines the result of a query operation on a Model
#[derive(Debug)]
pub struct QueryResult {
pub(crate) row: QueryResultRow,
Expand All @@ -19,13 +20,18 @@ pub(crate) enum QueryResultRow {
Mock(crate::MockRow),
}

/// Constrain any type trying to get a Row in a database
pub trait TryGetable: Sized {
/// Ensure the type implements this method
fn try_get(res: &QueryResult, pre: &str, col: &str) -> Result<Self, TryGetError>;
}

/// An error from trying to get a row from a Model
#[derive(Debug)]
pub enum TryGetError {
/// A database error was encountered as defined in [crate::DbErr]
DbErr(DbErr),
/// A null value was encountered
Null,
}

Expand All @@ -41,13 +47,15 @@ impl From<TryGetError> for DbErr {
// QueryResult //

impl QueryResult {
/// Get a Row from a Column
pub fn try_get<T>(&self, pre: &str, col: &str) -> Result<T, DbErr>
where
T: TryGetable,
{
Ok(T::try_get(self, pre, col)?)
}

/// Perform query operations on multiple Columns
pub fn try_get_many<T>(&self, pre: &str, cols: &[String]) -> Result<T, DbErr>
where
T: TryGetableMany,
Expand Down Expand Up @@ -306,7 +314,9 @@ try_getable_all!(uuid::Uuid);

// TryGetableMany //

/// Perform a query on multiple columns
pub trait TryGetableMany: Sized {
/// THe method to perform a query on multiple columns
fn try_get_many(res: &QueryResult, pre: &str, cols: &[String]) -> Result<Self, TryGetError>;

/// ```
Expand Down Expand Up @@ -453,8 +463,9 @@ fn try_get_many_with_slice_len_of(len: usize, cols: &[String]) -> Result<(), Try
}

// TryFromU64 //

/// Try to convert a type to a u64
pub trait TryFromU64: Sized {
/// The method to convert the type to a u64
fn try_from_u64(n: u64) -> Result<Self, DbErr>;
}

Expand Down
Loading