Rename to DbErr
This commit is contained in:
parent
2a1173c174
commit
0298cfb6af
@ -1,7 +1,7 @@
|
|||||||
use super::*;
|
use super::*;
|
||||||
use sea_orm::{entity::*, error::*, query::*, DbConn};
|
use sea_orm::{entity::*, error::*, query::*, DbConn};
|
||||||
|
|
||||||
pub async fn all_about_operation(db: &DbConn) -> Result<(), SeaErr> {
|
pub async fn all_about_operation(db: &DbConn) -> Result<(), DbErr> {
|
||||||
insert_and_update(db).await?;
|
insert_and_update(db).await?;
|
||||||
|
|
||||||
println!("===== =====\n");
|
println!("===== =====\n");
|
||||||
@ -15,7 +15,7 @@ pub async fn all_about_operation(db: &DbConn) -> Result<(), SeaErr> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn insert_and_update(db: &DbConn) -> Result<(), SeaErr> {
|
pub async fn insert_and_update(db: &DbConn) -> Result<(), DbErr> {
|
||||||
let pear = fruit::ActiveModel {
|
let pear = fruit::ActiveModel {
|
||||||
name: Set("pear".to_owned()),
|
name: Set("pear".to_owned()),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
@ -41,7 +41,7 @@ pub async fn insert_and_update(db: &DbConn) -> Result<(), SeaErr> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn save_active_model(db: &DbConn) -> Result<(), SeaErr> {
|
pub async fn save_active_model(db: &DbConn) -> Result<(), DbErr> {
|
||||||
let banana = fruit::ActiveModel {
|
let banana = fruit::ActiveModel {
|
||||||
name: Set("Banana".to_owned()),
|
name: Set("Banana".to_owned()),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
@ -79,7 +79,7 @@ mod form {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn save_custom_active_model(db: &DbConn) -> Result<(), SeaErr> {
|
async fn save_custom_active_model(db: &DbConn) -> Result<(), DbErr> {
|
||||||
let pineapple = form::ActiveModel {
|
let pineapple = form::ActiveModel {
|
||||||
id: Unset(None),
|
id: Unset(None),
|
||||||
name: Set("Pineapple".to_owned()),
|
name: Set("Pineapple".to_owned()),
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
use super::*;
|
use super::*;
|
||||||
use sea_orm::{entity::*, error::*, query::*, DbConn, FromQueryResult};
|
use sea_orm::{entity::*, error::*, query::*, DbConn, FromQueryResult};
|
||||||
|
|
||||||
pub async fn all_about_select(db: &DbConn) -> Result<(), SeaErr> {
|
pub async fn all_about_select(db: &DbConn) -> Result<(), DbErr> {
|
||||||
find_all(db).await?;
|
find_all(db).await?;
|
||||||
|
|
||||||
println!("===== =====\n");
|
println!("===== =====\n");
|
||||||
@ -41,7 +41,7 @@ pub async fn all_about_select(db: &DbConn) -> Result<(), SeaErr> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn find_all(db: &DbConn) -> Result<(), SeaErr> {
|
async fn find_all(db: &DbConn) -> Result<(), DbErr> {
|
||||||
print!("find all cakes: ");
|
print!("find all cakes: ");
|
||||||
|
|
||||||
let cakes: Vec<cake::Model> = Cake::find().all(db).await?;
|
let cakes: Vec<cake::Model> = Cake::find().all(db).await?;
|
||||||
@ -63,7 +63,7 @@ async fn find_all(db: &DbConn) -> Result<(), SeaErr> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn find_together(db: &DbConn) -> Result<(), SeaErr> {
|
async fn find_together(db: &DbConn) -> Result<(), DbErr> {
|
||||||
print!("find cakes and fruits: ");
|
print!("find cakes and fruits: ");
|
||||||
|
|
||||||
let both = Cake::find().find_also_related(Fruit).all(db).await?;
|
let both = Cake::find().find_also_related(Fruit).all(db).await?;
|
||||||
@ -82,7 +82,7 @@ impl Cake {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn find_one(db: &DbConn) -> Result<(), SeaErr> {
|
async fn find_one(db: &DbConn) -> Result<(), DbErr> {
|
||||||
print!("find one by primary key: ");
|
print!("find one by primary key: ");
|
||||||
|
|
||||||
let cheese: Option<cake::Model> = Cake::find_by_id(1).one(db).await?;
|
let cheese: Option<cake::Model> = Cake::find_by_id(1).one(db).await?;
|
||||||
@ -112,7 +112,7 @@ async fn find_one(db: &DbConn) -> Result<(), SeaErr> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn count_fruits_by_cake(db: &DbConn) -> Result<(), SeaErr> {
|
async fn count_fruits_by_cake(db: &DbConn) -> Result<(), DbErr> {
|
||||||
#[derive(Debug, FromQueryResult)]
|
#[derive(Debug, FromQueryResult)]
|
||||||
struct SelectResult {
|
struct SelectResult {
|
||||||
name: String,
|
name: String,
|
||||||
@ -138,7 +138,7 @@ async fn count_fruits_by_cake(db: &DbConn) -> Result<(), SeaErr> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn find_many_to_many(db: &DbConn) -> Result<(), SeaErr> {
|
async fn find_many_to_many(db: &DbConn) -> Result<(), DbErr> {
|
||||||
print!("find cakes and fillings: ");
|
print!("find cakes and fillings: ");
|
||||||
|
|
||||||
let both: Vec<(cake::Model, Vec<filling::Model>)> =
|
let both: Vec<(cake::Model, Vec<filling::Model>)> =
|
||||||
@ -178,7 +178,7 @@ async fn find_many_to_many(db: &DbConn) -> Result<(), SeaErr> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn all_about_select_json(db: &DbConn) -> Result<(), SeaErr> {
|
async fn all_about_select_json(db: &DbConn) -> Result<(), DbErr> {
|
||||||
find_all_json(&db).await?;
|
find_all_json(&db).await?;
|
||||||
|
|
||||||
println!("===== =====\n");
|
println!("===== =====\n");
|
||||||
@ -192,7 +192,7 @@ async fn all_about_select_json(db: &DbConn) -> Result<(), SeaErr> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn find_all_json(db: &DbConn) -> Result<(), SeaErr> {
|
async fn find_all_json(db: &DbConn) -> Result<(), DbErr> {
|
||||||
print!("find all cakes: ");
|
print!("find all cakes: ");
|
||||||
|
|
||||||
let cakes = Cake::find().into_json().all(db).await?;
|
let cakes = Cake::find().into_json().all(db).await?;
|
||||||
@ -208,7 +208,7 @@ async fn find_all_json(db: &DbConn) -> Result<(), SeaErr> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn find_together_json(db: &DbConn) -> Result<(), SeaErr> {
|
async fn find_together_json(db: &DbConn) -> Result<(), DbErr> {
|
||||||
print!("find cakes and fruits: ");
|
print!("find cakes and fruits: ");
|
||||||
|
|
||||||
let cakes_fruits = Cake::find()
|
let cakes_fruits = Cake::find()
|
||||||
@ -225,7 +225,7 @@ async fn find_together_json(db: &DbConn) -> Result<(), SeaErr> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn count_fruits_by_cake_json(db: &DbConn) -> Result<(), SeaErr> {
|
async fn count_fruits_by_cake_json(db: &DbConn) -> Result<(), DbErr> {
|
||||||
print!("count fruits by cake: ");
|
print!("count fruits by cake: ");
|
||||||
|
|
||||||
let count = Cake::find()
|
let count = Cake::find()
|
||||||
@ -243,7 +243,7 @@ async fn count_fruits_by_cake_json(db: &DbConn) -> Result<(), SeaErr> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn find_all_stream(db: &DbConn) -> Result<(), SeaErr> {
|
async fn find_all_stream(db: &DbConn) -> Result<(), DbErr> {
|
||||||
use async_std::task::sleep;
|
use async_std::task::sleep;
|
||||||
use futures::TryStreamExt;
|
use futures::TryStreamExt;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
@ -291,7 +291,7 @@ async fn find_all_stream(db: &DbConn) -> Result<(), SeaErr> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn find_first_page(db: &DbConn) -> Result<(), SeaErr> {
|
async fn find_first_page(db: &DbConn) -> Result<(), DbErr> {
|
||||||
println!("fruits first page: ");
|
println!("fruits first page: ");
|
||||||
let page = fruit::Entity::find().paginate(db, 2).fetch_page(0).await?;
|
let page = fruit::Entity::find().paginate(db, 2).fetch_page(0).await?;
|
||||||
for fruit in page {
|
for fruit in page {
|
||||||
@ -301,7 +301,7 @@ async fn find_first_page(db: &DbConn) -> Result<(), SeaErr> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn find_num_pages(db: &DbConn) -> Result<(), SeaErr> {
|
async fn find_num_pages(db: &DbConn) -> Result<(), DbErr> {
|
||||||
println!("fruits number of page: ");
|
println!("fruits number of page: ");
|
||||||
let num_pages = fruit::Entity::find().paginate(db, 2).num_pages().await?;
|
let num_pages = fruit::Entity::find().paginate(db, 2).num_pages().await?;
|
||||||
println!("{:?}", num_pages);
|
println!("{:?}", num_pages);
|
||||||
|
@ -37,11 +37,11 @@ pub fn expand_derive_active_model(ident: Ident, data: Data) -> syn::Result<Token
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl ActiveModel {
|
impl ActiveModel {
|
||||||
pub async fn save(self, db: &sea_orm::DatabaseConnection) -> Result<Self, sea_orm::SeaErr> {
|
pub async fn save(self, db: &sea_orm::DatabaseConnection) -> Result<Self, sea_orm::DbErr> {
|
||||||
sea_orm::save_active_model::<Self, Entity>(self, db).await
|
sea_orm::save_active_model::<Self, Entity>(self, db).await
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn delete(self, db: &sea_orm::DatabaseConnection) -> Result<sea_orm::DeleteResult, sea_orm::SeaErr> {
|
pub async fn delete(self, db: &sea_orm::DatabaseConnection) -> Result<sea_orm::DeleteResult, sea_orm::DbErr> {
|
||||||
sea_orm::delete_active_model::<Self, Entity>(self, db).await
|
sea_orm::delete_active_model::<Self, Entity>(self, db).await
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -30,7 +30,7 @@ pub fn expand_derive_from_query_result(ident: Ident, data: Data) -> syn::Result<
|
|||||||
|
|
||||||
Ok(quote!(
|
Ok(quote!(
|
||||||
impl sea_orm::FromQueryResult for #ident {
|
impl sea_orm::FromQueryResult for #ident {
|
||||||
fn from_query_result(row: &sea_orm::QueryResult, pre: &str) -> Result<Self, sea_orm::SeaErr> {
|
fn from_query_result(row: &sea_orm::QueryResult, pre: &str) -> Result<Self, sea_orm::DbErr> {
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
#(#field: row.try_get(pre, #name)?),*
|
#(#field: row.try_get(pre, #name)?),*
|
||||||
})
|
})
|
||||||
|
@ -47,7 +47,7 @@ pub fn expand_derive_model(ident: Ident, data: Data) -> syn::Result<TokenStream>
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl sea_orm::FromQueryResult for #ident {
|
impl sea_orm::FromQueryResult for #ident {
|
||||||
fn from_query_result(row: &sea_orm::QueryResult, pre: &str) -> Result<Self, sea_orm::SeaErr> {
|
fn from_query_result(row: &sea_orm::QueryResult, pre: &str) -> Result<Self, sea_orm::DbErr> {
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
#(#field: row.try_get(pre, <<Self as ModelTrait>::Entity as EntityTrait>::Column::#name.as_str().into())?),*
|
#(#field: row.try_get(pre, <<Self as ModelTrait>::Entity as EntityTrait>::Column::#name.as_str().into())?),*
|
||||||
})
|
})
|
||||||
|
@ -77,7 +77,7 @@ impl DatabaseConnection {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn execute(&self, stmt: Statement) -> Result<ExecResult, SeaErr> {
|
pub async fn execute(&self, stmt: Statement) -> Result<ExecResult, DbErr> {
|
||||||
match self {
|
match self {
|
||||||
#[cfg(feature = "sqlx-mysql")]
|
#[cfg(feature = "sqlx-mysql")]
|
||||||
DatabaseConnection::SqlxMySqlPoolConnection(conn) => conn.execute(stmt).await,
|
DatabaseConnection::SqlxMySqlPoolConnection(conn) => conn.execute(stmt).await,
|
||||||
@ -89,7 +89,7 @@ impl DatabaseConnection {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn query_one(&self, stmt: Statement) -> Result<Option<QueryResult>, SeaErr> {
|
pub async fn query_one(&self, stmt: Statement) -> Result<Option<QueryResult>, DbErr> {
|
||||||
match self {
|
match self {
|
||||||
#[cfg(feature = "sqlx-mysql")]
|
#[cfg(feature = "sqlx-mysql")]
|
||||||
DatabaseConnection::SqlxMySqlPoolConnection(conn) => conn.query_one(stmt).await,
|
DatabaseConnection::SqlxMySqlPoolConnection(conn) => conn.query_one(stmt).await,
|
||||||
@ -101,7 +101,7 @@ impl DatabaseConnection {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn query_all(&self, stmt: Statement) -> Result<Vec<QueryResult>, SeaErr> {
|
pub async fn query_all(&self, stmt: Statement) -> Result<Vec<QueryResult>, DbErr> {
|
||||||
match self {
|
match self {
|
||||||
#[cfg(feature = "sqlx-mysql")]
|
#[cfg(feature = "sqlx-mysql")]
|
||||||
DatabaseConnection::SqlxMySqlPoolConnection(conn) => conn.query_all(stmt).await,
|
DatabaseConnection::SqlxMySqlPoolConnection(conn) => conn.query_all(stmt).await,
|
||||||
|
@ -68,14 +68,14 @@ impl MockDatabase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl MockDatabaseTrait for MockDatabase {
|
impl MockDatabaseTrait for MockDatabase {
|
||||||
fn execute(&mut self, counter: usize, statement: Statement) -> Result<ExecResult, SeaErr> {
|
fn execute(&mut self, counter: usize, statement: Statement) -> Result<ExecResult, DbErr> {
|
||||||
self.transaction_log.push(Transaction::one(statement));
|
self.transaction_log.push(Transaction::one(statement));
|
||||||
if counter < self.exec_results.len() {
|
if counter < self.exec_results.len() {
|
||||||
Ok(ExecResult {
|
Ok(ExecResult {
|
||||||
result: ExecResultHolder::Mock(std::mem::take(&mut self.exec_results[counter])),
|
result: ExecResultHolder::Mock(std::mem::take(&mut self.exec_results[counter])),
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
Err(SeaErr::Execution)
|
Err(DbErr::Execution)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -83,7 +83,7 @@ impl MockDatabaseTrait for MockDatabase {
|
|||||||
&mut self,
|
&mut self,
|
||||||
counter: usize,
|
counter: usize,
|
||||||
statement: Statement,
|
statement: Statement,
|
||||||
) -> Result<Vec<QueryResult>, SeaErr> {
|
) -> Result<Vec<QueryResult>, DbErr> {
|
||||||
self.transaction_log.push(Transaction::one(statement));
|
self.transaction_log.push(Transaction::one(statement));
|
||||||
if counter < self.query_results.len() {
|
if counter < self.query_results.len() {
|
||||||
Ok(std::mem::take(&mut self.query_results[counter])
|
Ok(std::mem::take(&mut self.query_results[counter])
|
||||||
@ -93,7 +93,7 @@ impl MockDatabaseTrait for MockDatabase {
|
|||||||
})
|
})
|
||||||
.collect())
|
.collect())
|
||||||
} else {
|
} else {
|
||||||
Err(SeaErr::Query)
|
Err(DbErr::Query)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -103,7 +103,7 @@ impl MockDatabaseTrait for MockDatabase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl MockRow {
|
impl MockRow {
|
||||||
pub fn try_get<T>(&self, col: &str) -> Result<T, SeaErr>
|
pub fn try_get<T>(&self, col: &str) -> Result<T, DbErr>
|
||||||
where
|
where
|
||||||
T: ValueType,
|
T: ValueType,
|
||||||
{
|
{
|
||||||
|
@ -10,13 +10,13 @@ pub use mock::*;
|
|||||||
pub use statement::*;
|
pub use statement::*;
|
||||||
pub use transaction::*;
|
pub use transaction::*;
|
||||||
|
|
||||||
use crate::SeaErr;
|
use crate::DbErr;
|
||||||
|
|
||||||
#[derive(Debug, Default)]
|
#[derive(Debug, Default)]
|
||||||
pub struct Database;
|
pub struct Database;
|
||||||
|
|
||||||
impl Database {
|
impl Database {
|
||||||
pub async fn connect(string: &str) -> Result<DatabaseConnection, SeaErr> {
|
pub async fn connect(string: &str) -> Result<DatabaseConnection, DbErr> {
|
||||||
#[cfg(feature = "sqlx-mysql")]
|
#[cfg(feature = "sqlx-mysql")]
|
||||||
if crate::SqlxMySqlConnector::accepts(string) {
|
if crate::SqlxMySqlConnector::accepts(string) {
|
||||||
return Ok(crate::SqlxMySqlConnector::connect(string).await?);
|
return Ok(crate::SqlxMySqlConnector::connect(string).await?);
|
||||||
@ -29,6 +29,6 @@ impl Database {
|
|||||||
if crate::MockDatabaseConnector::accepts(string) {
|
if crate::MockDatabaseConnector::accepts(string) {
|
||||||
return Ok(crate::MockDatabaseConnector::connect(string).await?);
|
return Ok(crate::MockDatabaseConnector::connect(string).await?);
|
||||||
}
|
}
|
||||||
Err(SeaErr::Connection)
|
Err(DbErr::Connection)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -15,9 +15,9 @@ pub struct MockDatabaseConnection {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub trait MockDatabaseTrait: Send {
|
pub trait MockDatabaseTrait: Send {
|
||||||
fn execute(&mut self, counter: usize, stmt: Statement) -> Result<ExecResult, SeaErr>;
|
fn execute(&mut self, counter: usize, stmt: Statement) -> Result<ExecResult, DbErr>;
|
||||||
|
|
||||||
fn query(&mut self, counter: usize, stmt: Statement) -> Result<Vec<QueryResult>, SeaErr>;
|
fn query(&mut self, counter: usize, stmt: Statement) -> Result<Vec<QueryResult>, DbErr>;
|
||||||
|
|
||||||
fn drain_transaction_log(&mut self) -> Vec<Transaction>;
|
fn drain_transaction_log(&mut self) -> Vec<Transaction>;
|
||||||
}
|
}
|
||||||
@ -27,7 +27,7 @@ impl MockDatabaseConnector {
|
|||||||
string.starts_with("mock://")
|
string.starts_with("mock://")
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn connect(_string: &str) -> Result<DatabaseConnection, SeaErr> {
|
pub async fn connect(_string: &str) -> Result<DatabaseConnection, DbErr> {
|
||||||
Ok(DatabaseConnection::MockDatabaseConnection(
|
Ok(DatabaseConnection::MockDatabaseConnection(
|
||||||
MockDatabaseConnection::new(MockDatabase::new()),
|
MockDatabaseConnection::new(MockDatabase::new()),
|
||||||
))
|
))
|
||||||
@ -49,20 +49,20 @@ impl MockDatabaseConnection {
|
|||||||
&self.mocker
|
&self.mocker
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn execute(&self, statement: Statement) -> Result<ExecResult, SeaErr> {
|
pub async fn execute(&self, statement: Statement) -> Result<ExecResult, DbErr> {
|
||||||
debug_print!("{}", statement);
|
debug_print!("{}", statement);
|
||||||
let counter = self.counter.fetch_add(1, Ordering::SeqCst);
|
let counter = self.counter.fetch_add(1, Ordering::SeqCst);
|
||||||
self.mocker.lock().unwrap().execute(counter, statement)
|
self.mocker.lock().unwrap().execute(counter, statement)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn query_one(&self, statement: Statement) -> Result<Option<QueryResult>, SeaErr> {
|
pub async fn query_one(&self, statement: Statement) -> Result<Option<QueryResult>, DbErr> {
|
||||||
debug_print!("{}", statement);
|
debug_print!("{}", statement);
|
||||||
let counter = self.counter.fetch_add(1, Ordering::SeqCst);
|
let counter = self.counter.fetch_add(1, Ordering::SeqCst);
|
||||||
let result = self.mocker.lock().unwrap().query(counter, statement)?;
|
let result = self.mocker.lock().unwrap().query(counter, statement)?;
|
||||||
Ok(result.into_iter().next())
|
Ok(result.into_iter().next())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn query_all(&self, statement: Statement) -> Result<Vec<QueryResult>, SeaErr> {
|
pub async fn query_all(&self, statement: Statement) -> Result<Vec<QueryResult>, DbErr> {
|
||||||
debug_print!("{}", statement);
|
debug_print!("{}", statement);
|
||||||
let counter = self.counter.fetch_add(1, Ordering::SeqCst);
|
let counter = self.counter.fetch_add(1, Ordering::SeqCst);
|
||||||
self.mocker.lock().unwrap().query(counter, statement)
|
self.mocker.lock().unwrap().query(counter, statement)
|
||||||
|
@ -19,13 +19,13 @@ impl SqlxMySqlConnector {
|
|||||||
string.starts_with("mysql://")
|
string.starts_with("mysql://")
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn connect(string: &str) -> Result<DatabaseConnection, SeaErr> {
|
pub async fn connect(string: &str) -> Result<DatabaseConnection, DbErr> {
|
||||||
if let Ok(pool) = MySqlPool::connect(string).await {
|
if let Ok(pool) = MySqlPool::connect(string).await {
|
||||||
Ok(DatabaseConnection::SqlxMySqlPoolConnection(
|
Ok(DatabaseConnection::SqlxMySqlPoolConnection(
|
||||||
SqlxMySqlPoolConnection { pool },
|
SqlxMySqlPoolConnection { pool },
|
||||||
))
|
))
|
||||||
} else {
|
} else {
|
||||||
Err(SeaErr::Connection)
|
Err(DbErr::Connection)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -37,7 +37,7 @@ impl SqlxMySqlConnector {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl SqlxMySqlPoolConnection {
|
impl SqlxMySqlPoolConnection {
|
||||||
pub async fn execute(&self, stmt: Statement) -> Result<ExecResult, SeaErr> {
|
pub async fn execute(&self, stmt: Statement) -> Result<ExecResult, DbErr> {
|
||||||
debug_print!("{}", stmt);
|
debug_print!("{}", stmt);
|
||||||
|
|
||||||
let query = sqlx_query(&stmt);
|
let query = sqlx_query(&stmt);
|
||||||
@ -46,10 +46,10 @@ impl SqlxMySqlPoolConnection {
|
|||||||
return Ok(res.into());
|
return Ok(res.into());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(SeaErr::Execution)
|
Err(DbErr::Execution)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn query_one(&self, stmt: Statement) -> Result<Option<QueryResult>, SeaErr> {
|
pub async fn query_one(&self, stmt: Statement) -> Result<Option<QueryResult>, DbErr> {
|
||||||
debug_print!("{}", stmt);
|
debug_print!("{}", stmt);
|
||||||
|
|
||||||
let query = sqlx_query(&stmt);
|
let query = sqlx_query(&stmt);
|
||||||
@ -60,11 +60,11 @@ impl SqlxMySqlPoolConnection {
|
|||||||
Ok(None)
|
Ok(None)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
Err(SeaErr::Query)
|
Err(DbErr::Query)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn query_all(&self, stmt: Statement) -> Result<Vec<QueryResult>, SeaErr> {
|
pub async fn query_all(&self, stmt: Statement) -> Result<Vec<QueryResult>, DbErr> {
|
||||||
debug_print!("{}", stmt);
|
debug_print!("{}", stmt);
|
||||||
|
|
||||||
let query = sqlx_query(&stmt);
|
let query = sqlx_query(&stmt);
|
||||||
@ -73,7 +73,7 @@ impl SqlxMySqlPoolConnection {
|
|||||||
return Ok(rows.into_iter().map(|r| r.into()).collect());
|
return Ok(rows.into_iter().map(|r| r.into()).collect());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(SeaErr::Query)
|
Err(DbErr::Query)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -19,13 +19,13 @@ impl SqlxSqliteConnector {
|
|||||||
string.starts_with("sqlite:")
|
string.starts_with("sqlite:")
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn connect(string: &str) -> Result<DatabaseConnection, SeaErr> {
|
pub async fn connect(string: &str) -> Result<DatabaseConnection, DbErr> {
|
||||||
if let Ok(pool) = SqlitePool::connect(string).await {
|
if let Ok(pool) = SqlitePool::connect(string).await {
|
||||||
Ok(DatabaseConnection::SqlxSqlitePoolConnection(
|
Ok(DatabaseConnection::SqlxSqlitePoolConnection(
|
||||||
SqlxSqlitePoolConnection { pool },
|
SqlxSqlitePoolConnection { pool },
|
||||||
))
|
))
|
||||||
} else {
|
} else {
|
||||||
Err(SeaErr::Connection)
|
Err(DbErr::Connection)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -37,7 +37,7 @@ impl SqlxSqliteConnector {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl SqlxSqlitePoolConnection {
|
impl SqlxSqlitePoolConnection {
|
||||||
pub async fn execute(&self, stmt: Statement) -> Result<ExecResult, SeaErr> {
|
pub async fn execute(&self, stmt: Statement) -> Result<ExecResult, DbErr> {
|
||||||
debug_print!("{}", stmt);
|
debug_print!("{}", stmt);
|
||||||
|
|
||||||
let query = sqlx_query(&stmt);
|
let query = sqlx_query(&stmt);
|
||||||
@ -46,10 +46,10 @@ impl SqlxSqlitePoolConnection {
|
|||||||
return Ok(res.into());
|
return Ok(res.into());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(SeaErr::Execution)
|
Err(DbErr::Execution)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn query_one(&self, stmt: Statement) -> Result<Option<QueryResult>, SeaErr> {
|
pub async fn query_one(&self, stmt: Statement) -> Result<Option<QueryResult>, DbErr> {
|
||||||
debug_print!("{}", stmt);
|
debug_print!("{}", stmt);
|
||||||
|
|
||||||
let query = sqlx_query(&stmt);
|
let query = sqlx_query(&stmt);
|
||||||
@ -60,11 +60,11 @@ impl SqlxSqlitePoolConnection {
|
|||||||
Ok(None)
|
Ok(None)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
Err(SeaErr::Query)
|
Err(DbErr::Query)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn query_all(&self, stmt: Statement) -> Result<Vec<QueryResult>, SeaErr> {
|
pub async fn query_all(&self, stmt: Statement) -> Result<Vec<QueryResult>, DbErr> {
|
||||||
debug_print!("{}", stmt);
|
debug_print!("{}", stmt);
|
||||||
|
|
||||||
let query = sqlx_query(&stmt);
|
let query = sqlx_query(&stmt);
|
||||||
@ -73,7 +73,7 @@ impl SqlxSqlitePoolConnection {
|
|||||||
return Ok(rows.into_iter().map(|r| r.into()).collect());
|
return Ok(rows.into_iter().map(|r| r.into()).collect());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(SeaErr::Query)
|
Err(DbErr::Query)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -66,8 +66,8 @@ pub trait ActiveModelTrait: Clone + Debug {
|
|||||||
fn default() -> Self;
|
fn default() -> Self;
|
||||||
|
|
||||||
// below is not yet possible. right now we define these methods in DeriveActiveModel
|
// below is not yet possible. right now we define these methods in DeriveActiveModel
|
||||||
// fn save(self, db: &DatabaseConnection) -> impl Future<Output = Result<Self, SeaErr>>;
|
// fn save(self, db: &DatabaseConnection) -> impl Future<Output = Result<Self, DbErr>>;
|
||||||
// fn delete(self, db: &DatabaseConnection) -> impl Future<Output = Result<DeleteResult, SeaErr>>;
|
// fn delete(self, db: &DatabaseConnection) -> impl Future<Output = Result<DeleteResult, DbErr>>;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Behaviors for users to override
|
/// Behaviors for users to override
|
||||||
@ -188,7 +188,7 @@ where
|
|||||||
|
|
||||||
/// Insert the model if primary key is unset, update otherwise.
|
/// Insert the model if primary key is unset, update otherwise.
|
||||||
/// Only works if the entity has auto increment primary key.
|
/// Only works if the entity has auto increment primary key.
|
||||||
pub async fn save_active_model<A, E>(mut am: A, db: &DatabaseConnection) -> Result<A, SeaErr>
|
pub async fn save_active_model<A, E>(mut am: A, db: &DatabaseConnection) -> Result<A, DbErr>
|
||||||
where
|
where
|
||||||
A: ActiveModelBehavior + ActiveModelTrait<Entity = E>,
|
A: ActiveModelBehavior + ActiveModelTrait<Entity = E>,
|
||||||
E::Model: IntoActiveModel<A>,
|
E::Model: IntoActiveModel<A>,
|
||||||
@ -212,7 +212,7 @@ where
|
|||||||
Ok(am)
|
Ok(am)
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn insert_and_select_active_model<A, E>(am: A, db: &DatabaseConnection) -> Result<A, SeaErr>
|
async fn insert_and_select_active_model<A, E>(am: A, db: &DatabaseConnection) -> Result<A, DbErr>
|
||||||
where
|
where
|
||||||
A: ActiveModelTrait<Entity = E>,
|
A: ActiveModelTrait<Entity = E>,
|
||||||
E::Model: IntoActiveModel<A>,
|
E::Model: IntoActiveModel<A>,
|
||||||
@ -227,14 +227,14 @@ where
|
|||||||
let model: Option<E::Model> = res?;
|
let model: Option<E::Model> = res?;
|
||||||
match model {
|
match model {
|
||||||
Some(model) => Ok(model.into_active_model()),
|
Some(model) => Ok(model.into_active_model()),
|
||||||
None => Err(SeaErr::Execution),
|
None => Err(DbErr::Execution),
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
Ok(A::default())
|
Ok(A::default())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn update_active_model<A, E>(am: A, db: &DatabaseConnection) -> Result<A, SeaErr>
|
async fn update_active_model<A, E>(am: A, db: &DatabaseConnection) -> Result<A, DbErr>
|
||||||
where
|
where
|
||||||
A: ActiveModelTrait<Entity = E>,
|
A: ActiveModelTrait<Entity = E>,
|
||||||
E: EntityTrait,
|
E: EntityTrait,
|
||||||
@ -246,7 +246,7 @@ where
|
|||||||
pub async fn delete_active_model<A, E>(
|
pub async fn delete_active_model<A, E>(
|
||||||
mut am: A,
|
mut am: A,
|
||||||
db: &DatabaseConnection,
|
db: &DatabaseConnection,
|
||||||
) -> Result<DeleteResult, SeaErr>
|
) -> Result<DeleteResult, DbErr>
|
||||||
where
|
where
|
||||||
A: ActiveModelBehavior + ActiveModelTrait<Entity = E>,
|
A: ActiveModelBehavior + ActiveModelTrait<Entity = E>,
|
||||||
E: EntityTrait,
|
E: EntityTrait,
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
use crate::{EntityTrait, SeaErr, QueryFilter, QueryResult, Related, Select};
|
use crate::{EntityTrait, DbErr, QueryFilter, QueryResult, Related, Select};
|
||||||
pub use sea_query::Value;
|
pub use sea_query::Value;
|
||||||
use std::fmt::Debug;
|
use std::fmt::Debug;
|
||||||
|
|
||||||
@ -19,11 +19,11 @@ pub trait ModelTrait: Clone + Debug {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub trait FromQueryResult {
|
pub trait FromQueryResult {
|
||||||
fn from_query_result(res: &QueryResult, pre: &str) -> Result<Self, SeaErr>
|
fn from_query_result(res: &QueryResult, pre: &str) -> Result<Self, DbErr>
|
||||||
where
|
where
|
||||||
Self: Sized;
|
Self: Sized;
|
||||||
|
|
||||||
fn from_query_result_optional(res: &QueryResult, pre: &str) -> Result<Option<Self>, SeaErr>
|
fn from_query_result_optional(res: &QueryResult, pre: &str) -> Result<Option<Self>, DbErr>
|
||||||
where
|
where
|
||||||
Self: Sized,
|
Self: Sized,
|
||||||
{
|
{
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
use std::{error, fmt};
|
use std::{error, fmt};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum SeaErr {
|
pub enum DbErr {
|
||||||
Connection,
|
Connection,
|
||||||
Execution,
|
Execution,
|
||||||
Query,
|
Query,
|
||||||
@ -9,7 +9,7 @@ pub enum SeaErr {
|
|||||||
Sqlx(sqlx::Error),
|
Sqlx(sqlx::Error),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Display for SeaErr {
|
impl fmt::Display for DbErr {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
match self {
|
match self {
|
||||||
Self::Connection => write!(f, "{:?}", "Connection Error"),
|
Self::Connection => write!(f, "{:?}", "Connection Error"),
|
||||||
@ -21,7 +21,7 @@ impl fmt::Display for SeaErr {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl error::Error for SeaErr {
|
impl error::Error for DbErr {
|
||||||
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
|
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
|
||||||
match self {
|
match self {
|
||||||
Self::Connection => None,
|
Self::Connection => None,
|
||||||
@ -34,7 +34,7 @@ impl error::Error for SeaErr {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "sqlx-dep")]
|
#[cfg(feature = "sqlx-dep")]
|
||||||
impl From<sqlx::Error> for SeaErr {
|
impl From<sqlx::Error> for DbErr {
|
||||||
fn from(sqlx_err: sqlx::Error) -> Self {
|
fn from(sqlx_err: sqlx::Error) -> Self {
|
||||||
Self::Sqlx(sqlx_err)
|
Self::Sqlx(sqlx_err)
|
||||||
}
|
}
|
||||||
|
@ -21,7 +21,7 @@ where
|
|||||||
pub fn exec(
|
pub fn exec(
|
||||||
self,
|
self,
|
||||||
db: &'a DatabaseConnection,
|
db: &'a DatabaseConnection,
|
||||||
) -> impl Future<Output = Result<DeleteResult, SeaErr>> + 'a {
|
) -> impl Future<Output = Result<DeleteResult, DbErr>> + 'a {
|
||||||
// so that self is dropped before entering await
|
// so that self is dropped before entering await
|
||||||
exec_delete_only(self.query, db)
|
exec_delete_only(self.query, db)
|
||||||
}
|
}
|
||||||
@ -34,7 +34,7 @@ where
|
|||||||
pub fn exec(
|
pub fn exec(
|
||||||
self,
|
self,
|
||||||
db: &'a DatabaseConnection,
|
db: &'a DatabaseConnection,
|
||||||
) -> impl Future<Output = Result<DeleteResult, SeaErr>> + 'a {
|
) -> impl Future<Output = Result<DeleteResult, DbErr>> + 'a {
|
||||||
// so that self is dropped before entering await
|
// so that self is dropped before entering await
|
||||||
exec_delete_only(self.query, db)
|
exec_delete_only(self.query, db)
|
||||||
}
|
}
|
||||||
@ -48,7 +48,7 @@ impl Deleter {
|
|||||||
pub fn exec(
|
pub fn exec(
|
||||||
self,
|
self,
|
||||||
db: &DatabaseConnection,
|
db: &DatabaseConnection,
|
||||||
) -> impl Future<Output = Result<DeleteResult, SeaErr>> + '_ {
|
) -> impl Future<Output = Result<DeleteResult, DbErr>> + '_ {
|
||||||
let builder = db.get_query_builder_backend();
|
let builder = db.get_query_builder_backend();
|
||||||
exec_delete(builder.build(&self.query), db)
|
exec_delete(builder.build(&self.query), db)
|
||||||
}
|
}
|
||||||
@ -57,7 +57,7 @@ impl Deleter {
|
|||||||
async fn exec_delete_only(
|
async fn exec_delete_only(
|
||||||
query: DeleteStatement,
|
query: DeleteStatement,
|
||||||
db: &DatabaseConnection,
|
db: &DatabaseConnection,
|
||||||
) -> Result<DeleteResult, SeaErr> {
|
) -> Result<DeleteResult, DbErr> {
|
||||||
Deleter::new(query).exec(db).await
|
Deleter::new(query).exec(db).await
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -65,7 +65,7 @@ async fn exec_delete_only(
|
|||||||
async fn exec_delete(
|
async fn exec_delete(
|
||||||
statement: Statement,
|
statement: Statement,
|
||||||
db: &DatabaseConnection,
|
db: &DatabaseConnection,
|
||||||
) -> Result<DeleteResult, SeaErr> {
|
) -> Result<DeleteResult, DbErr> {
|
||||||
let result = db.execute(statement).await?;
|
let result = db.execute(statement).await?;
|
||||||
Ok(DeleteResult {
|
Ok(DeleteResult {
|
||||||
rows_affected: result.rows_affected(),
|
rows_affected: result.rows_affected(),
|
||||||
|
@ -19,7 +19,7 @@ where
|
|||||||
pub fn exec(
|
pub fn exec(
|
||||||
self,
|
self,
|
||||||
db: &DatabaseConnection,
|
db: &DatabaseConnection,
|
||||||
) -> impl Future<Output = Result<InsertResult, SeaErr>> + '_ {
|
) -> impl Future<Output = Result<InsertResult, DbErr>> + '_ {
|
||||||
// so that self is dropped before entering await
|
// so that self is dropped before entering await
|
||||||
Inserter::new(self.into_query()).exec(db)
|
Inserter::new(self.into_query()).exec(db)
|
||||||
}
|
}
|
||||||
@ -33,7 +33,7 @@ impl Inserter {
|
|||||||
pub fn exec(
|
pub fn exec(
|
||||||
self,
|
self,
|
||||||
db: &DatabaseConnection,
|
db: &DatabaseConnection,
|
||||||
) -> impl Future<Output = Result<InsertResult, SeaErr>> + '_ {
|
) -> impl Future<Output = Result<InsertResult, DbErr>> + '_ {
|
||||||
let builder = db.get_query_builder_backend();
|
let builder = db.get_query_builder_backend();
|
||||||
exec_insert(builder.build(&self.query), db)
|
exec_insert(builder.build(&self.query), db)
|
||||||
}
|
}
|
||||||
@ -43,7 +43,7 @@ impl Inserter {
|
|||||||
async fn exec_insert(
|
async fn exec_insert(
|
||||||
statement: Statement,
|
statement: Statement,
|
||||||
db: &DatabaseConnection,
|
db: &DatabaseConnection,
|
||||||
) -> Result<InsertResult, SeaErr> {
|
) -> Result<InsertResult, DbErr> {
|
||||||
let result = db.execute(statement).await?;
|
let result = db.execute(statement).await?;
|
||||||
// TODO: Postgres instead use query_one + returning clause
|
// TODO: Postgres instead use query_one + returning clause
|
||||||
Ok(InsertResult {
|
Ok(InsertResult {
|
||||||
|
@ -23,7 +23,7 @@ where
|
|||||||
S: SelectorTrait + 'db,
|
S: SelectorTrait + 'db,
|
||||||
{
|
{
|
||||||
/// Fetch a specific page
|
/// Fetch a specific page
|
||||||
pub async fn fetch_page(&self, page: usize) -> Result<Vec<S::Item>, SeaErr> {
|
pub async fn fetch_page(&self, page: usize) -> Result<Vec<S::Item>, DbErr> {
|
||||||
let query = self
|
let query = self
|
||||||
.query
|
.query
|
||||||
.clone()
|
.clone()
|
||||||
@ -42,12 +42,12 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Fetch the current page
|
/// Fetch the current page
|
||||||
pub async fn fetch(&self) -> Result<Vec<S::Item>, SeaErr> {
|
pub async fn fetch(&self) -> Result<Vec<S::Item>, DbErr> {
|
||||||
self.fetch_page(self.page).await
|
self.fetch_page(self.page).await
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get the total number of pages
|
/// Get the total number of pages
|
||||||
pub async fn num_pages(&self) -> Result<usize, SeaErr> {
|
pub async fn num_pages(&self) -> Result<usize, DbErr> {
|
||||||
let builder = self.db.get_query_builder_backend();
|
let builder = self.db.get_query_builder_backend();
|
||||||
let stmt = builder.build(
|
let stmt = builder.build(
|
||||||
SelectStatement::new()
|
SelectStatement::new()
|
||||||
@ -77,7 +77,7 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Fetch one page and increment the page counter
|
/// Fetch one page and increment the page counter
|
||||||
pub async fn fetch_and_next(&mut self) -> Result<Option<Vec<S::Item>>, SeaErr> {
|
pub async fn fetch_and_next(&mut self) -> Result<Option<Vec<S::Item>>, DbErr> {
|
||||||
let vec = self.fetch().await?;
|
let vec = self.fetch().await?;
|
||||||
self.next();
|
self.next();
|
||||||
let opt = if !vec.is_empty() { Some(vec) } else { None };
|
let opt = if !vec.is_empty() { Some(vec) } else { None };
|
||||||
@ -85,7 +85,7 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Convert self into an async stream
|
/// Convert self into an async stream
|
||||||
pub fn into_stream(mut self) -> PinBoxStream<'db, Result<Vec<S::Item>, SeaErr>> {
|
pub fn into_stream(mut self) -> PinBoxStream<'db, Result<Vec<S::Item>, DbErr>> {
|
||||||
Box::pin(stream! {
|
Box::pin(stream! {
|
||||||
loop {
|
loop {
|
||||||
if let Some(vec) = self.fetch_and_next().await? {
|
if let Some(vec) = self.fetch_and_next().await? {
|
||||||
@ -148,7 +148,7 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[async_std::test]
|
#[async_std::test]
|
||||||
async fn fetch_page() -> Result<(), SeaErr> {
|
async fn fetch_page() -> Result<(), DbErr> {
|
||||||
let (db, pages) = setup();
|
let (db, pages) = setup();
|
||||||
|
|
||||||
let paginator = fruit::Entity::find().paginate(&db, 2);
|
let paginator = fruit::Entity::find().paginate(&db, 2);
|
||||||
@ -178,7 +178,7 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[async_std::test]
|
#[async_std::test]
|
||||||
async fn fetch() -> Result<(), SeaErr> {
|
async fn fetch() -> Result<(), DbErr> {
|
||||||
let (db, pages) = setup();
|
let (db, pages) = setup();
|
||||||
|
|
||||||
let mut paginator = fruit::Entity::find().paginate(&db, 2);
|
let mut paginator = fruit::Entity::find().paginate(&db, 2);
|
||||||
@ -212,7 +212,7 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[async_std::test]
|
#[async_std::test]
|
||||||
async fn num_pages() -> Result<(), SeaErr> {
|
async fn num_pages() -> Result<(), DbErr> {
|
||||||
let (db, num_rows) = setup_num_rows();
|
let (db, num_rows) = setup_num_rows();
|
||||||
|
|
||||||
let num_rows = num_rows as usize;
|
let num_rows = num_rows as usize;
|
||||||
@ -244,7 +244,7 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[async_std::test]
|
#[async_std::test]
|
||||||
async fn next_and_cur_page() -> Result<(), SeaErr> {
|
async fn next_and_cur_page() -> Result<(), DbErr> {
|
||||||
let (db, _) = setup();
|
let (db, _) = setup();
|
||||||
|
|
||||||
let mut paginator = fruit::Entity::find().paginate(&db, 2);
|
let mut paginator = fruit::Entity::find().paginate(&db, 2);
|
||||||
@ -260,7 +260,7 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[async_std::test]
|
#[async_std::test]
|
||||||
async fn fetch_and_next() -> Result<(), SeaErr> {
|
async fn fetch_and_next() -> Result<(), DbErr> {
|
||||||
let (db, pages) = setup();
|
let (db, pages) = setup();
|
||||||
|
|
||||||
let mut paginator = fruit::Entity::find().paginate(&db, 2);
|
let mut paginator = fruit::Entity::find().paginate(&db, 2);
|
||||||
@ -295,7 +295,7 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[async_std::test]
|
#[async_std::test]
|
||||||
async fn into_stream() -> Result<(), SeaErr> {
|
async fn into_stream() -> Result<(), DbErr> {
|
||||||
let (db, pages) = setup();
|
let (db, pages) = setup();
|
||||||
|
|
||||||
let mut fruit_stream = fruit::Entity::find().paginate(&db, 2).into_stream();
|
let mut fruit_stream = fruit::Entity::find().paginate(&db, 2).into_stream();
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
use crate::SeaErr;
|
use crate::DbErr;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
@ -16,7 +16,7 @@ pub(crate) enum QueryResultRow {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub trait TryGetable {
|
pub trait TryGetable {
|
||||||
fn try_get(res: &QueryResult, pre: &str, col: &str) -> Result<Self, SeaErr>
|
fn try_get(res: &QueryResult, pre: &str, col: &str) -> Result<Self, DbErr>
|
||||||
where
|
where
|
||||||
Self: Sized;
|
Self: Sized;
|
||||||
}
|
}
|
||||||
@ -24,7 +24,7 @@ pub trait TryGetable {
|
|||||||
// QueryResult //
|
// QueryResult //
|
||||||
|
|
||||||
impl QueryResult {
|
impl QueryResult {
|
||||||
pub fn try_get<T>(&self, pre: &str, col: &str) -> Result<T, SeaErr>
|
pub fn try_get<T>(&self, pre: &str, col: &str) -> Result<T, DbErr>
|
||||||
where
|
where
|
||||||
T: TryGetable,
|
T: TryGetable,
|
||||||
{
|
{
|
||||||
@ -50,7 +50,7 @@ impl fmt::Debug for QueryResultRow {
|
|||||||
macro_rules! try_getable_all {
|
macro_rules! try_getable_all {
|
||||||
( $type: ty ) => {
|
( $type: ty ) => {
|
||||||
impl TryGetable for $type {
|
impl TryGetable for $type {
|
||||||
fn try_get(res: &QueryResult, pre: &str, col: &str) -> Result<Self, SeaErr> {
|
fn try_get(res: &QueryResult, pre: &str, col: &str) -> Result<Self, DbErr> {
|
||||||
let column = format!("{}{}", pre, col);
|
let column = format!("{}{}", pre, col);
|
||||||
match &res.row {
|
match &res.row {
|
||||||
#[cfg(feature = "sqlx-mysql")]
|
#[cfg(feature = "sqlx-mysql")]
|
||||||
@ -70,7 +70,7 @@ macro_rules! try_getable_all {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl TryGetable for Option<$type> {
|
impl TryGetable for Option<$type> {
|
||||||
fn try_get(res: &QueryResult, pre: &str, col: &str) -> Result<Self, SeaErr> {
|
fn try_get(res: &QueryResult, pre: &str, col: &str) -> Result<Self, DbErr> {
|
||||||
let column = format!("{}{}", pre, col);
|
let column = format!("{}{}", pre, col);
|
||||||
match &res.row {
|
match &res.row {
|
||||||
#[cfg(feature = "sqlx-mysql")]
|
#[cfg(feature = "sqlx-mysql")]
|
||||||
@ -103,7 +103,7 @@ macro_rules! try_getable_all {
|
|||||||
macro_rules! try_getable_mysql {
|
macro_rules! try_getable_mysql {
|
||||||
( $type: ty ) => {
|
( $type: ty ) => {
|
||||||
impl TryGetable for $type {
|
impl TryGetable for $type {
|
||||||
fn try_get(res: &QueryResult, pre: &str, col: &str) -> Result<Self, SeaErr> {
|
fn try_get(res: &QueryResult, pre: &str, col: &str) -> Result<Self, DbErr> {
|
||||||
let column = format!("{}{}", pre, col);
|
let column = format!("{}{}", pre, col);
|
||||||
match &res.row {
|
match &res.row {
|
||||||
#[cfg(feature = "sqlx-mysql")]
|
#[cfg(feature = "sqlx-mysql")]
|
||||||
@ -122,7 +122,7 @@ macro_rules! try_getable_mysql {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl TryGetable for Option<$type> {
|
impl TryGetable for Option<$type> {
|
||||||
fn try_get(res: &QueryResult, pre: &str, col: &str) -> Result<Self, SeaErr> {
|
fn try_get(res: &QueryResult, pre: &str, col: &str) -> Result<Self, DbErr> {
|
||||||
let column = format!("{}{}", pre, col);
|
let column = format!("{}{}", pre, col);
|
||||||
match &res.row {
|
match &res.row {
|
||||||
#[cfg(feature = "sqlx-mysql")]
|
#[cfg(feature = "sqlx-mysql")]
|
||||||
|
@ -18,7 +18,7 @@ where
|
|||||||
pub trait SelectorTrait {
|
pub trait SelectorTrait {
|
||||||
type Item: Sized;
|
type Item: Sized;
|
||||||
|
|
||||||
fn from_raw_query_result(res: QueryResult) -> Result<Self::Item, SeaErr>;
|
fn from_raw_query_result(res: QueryResult) -> Result<Self::Item, DbErr>;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct SelectModel<M>
|
pub struct SelectModel<M>
|
||||||
@ -43,7 +43,7 @@ where
|
|||||||
{
|
{
|
||||||
type Item = M;
|
type Item = M;
|
||||||
|
|
||||||
fn from_raw_query_result(res: QueryResult) -> Result<Self::Item, SeaErr> {
|
fn from_raw_query_result(res: QueryResult) -> Result<Self::Item, DbErr> {
|
||||||
M::from_query_result(&res, "")
|
M::from_query_result(&res, "")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -55,7 +55,7 @@ where
|
|||||||
{
|
{
|
||||||
type Item = (M, Option<N>);
|
type Item = (M, Option<N>);
|
||||||
|
|
||||||
fn from_raw_query_result(res: QueryResult) -> Result<Self::Item, SeaErr> {
|
fn from_raw_query_result(res: QueryResult) -> Result<Self::Item, DbErr> {
|
||||||
Ok((
|
Ok((
|
||||||
M::from_query_result(&res, combine::SELECT_A)?,
|
M::from_query_result(&res, combine::SELECT_A)?,
|
||||||
N::from_query_result_optional(&res, combine::SELECT_B)?,
|
N::from_query_result_optional(&res, combine::SELECT_B)?,
|
||||||
@ -85,11 +85,11 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn one(self, db: &DatabaseConnection) -> Result<Option<E::Model>, SeaErr> {
|
pub async fn one(self, db: &DatabaseConnection) -> Result<Option<E::Model>, DbErr> {
|
||||||
self.into_model::<E::Model>().one(db).await
|
self.into_model::<E::Model>().one(db).await
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn all(self, db: &DatabaseConnection) -> Result<Vec<E::Model>, SeaErr> {
|
pub async fn all(self, db: &DatabaseConnection) -> Result<Vec<E::Model>, DbErr> {
|
||||||
self.into_model::<E::Model>().all(db).await
|
self.into_model::<E::Model>().all(db).await
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -129,14 +129,14 @@ where
|
|||||||
pub async fn one(
|
pub async fn one(
|
||||||
self,
|
self,
|
||||||
db: &DatabaseConnection,
|
db: &DatabaseConnection,
|
||||||
) -> Result<Option<(E::Model, Option<F::Model>)>, SeaErr> {
|
) -> Result<Option<(E::Model, Option<F::Model>)>, DbErr> {
|
||||||
self.into_model::<E::Model, F::Model>().one(db).await
|
self.into_model::<E::Model, F::Model>().one(db).await
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn all(
|
pub async fn all(
|
||||||
self,
|
self,
|
||||||
db: &DatabaseConnection,
|
db: &DatabaseConnection,
|
||||||
) -> Result<Vec<(E::Model, Option<F::Model>)>, SeaErr> {
|
) -> Result<Vec<(E::Model, Option<F::Model>)>, DbErr> {
|
||||||
self.into_model::<E::Model, F::Model>().all(db).await
|
self.into_model::<E::Model, F::Model>().all(db).await
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -168,14 +168,14 @@ where
|
|||||||
pub async fn one(
|
pub async fn one(
|
||||||
self,
|
self,
|
||||||
db: &DatabaseConnection,
|
db: &DatabaseConnection,
|
||||||
) -> Result<Option<(E::Model, Option<F::Model>)>, SeaErr> {
|
) -> Result<Option<(E::Model, Option<F::Model>)>, DbErr> {
|
||||||
self.into_model::<E::Model, F::Model>().one(db).await
|
self.into_model::<E::Model, F::Model>().one(db).await
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn all(
|
pub async fn all(
|
||||||
self,
|
self,
|
||||||
db: &DatabaseConnection,
|
db: &DatabaseConnection,
|
||||||
) -> Result<Vec<(E::Model, Vec<F::Model>)>, SeaErr> {
|
) -> Result<Vec<(E::Model, Vec<F::Model>)>, DbErr> {
|
||||||
let rows = self.into_model::<E::Model, F::Model>().all(db).await?;
|
let rows = self.into_model::<E::Model, F::Model>().all(db).await?;
|
||||||
Ok(consolidate_query_result::<E, F>(rows))
|
Ok(consolidate_query_result::<E, F>(rows))
|
||||||
}
|
}
|
||||||
@ -185,7 +185,7 @@ impl<S> Selector<S>
|
|||||||
where
|
where
|
||||||
S: SelectorTrait,
|
S: SelectorTrait,
|
||||||
{
|
{
|
||||||
pub async fn one(mut self, db: &DatabaseConnection) -> Result<Option<S::Item>, SeaErr> {
|
pub async fn one(mut self, db: &DatabaseConnection) -> Result<Option<S::Item>, DbErr> {
|
||||||
let builder = db.get_query_builder_backend();
|
let builder = db.get_query_builder_backend();
|
||||||
self.query.limit(1);
|
self.query.limit(1);
|
||||||
let row = db.query_one(builder.build(&self.query)).await?;
|
let row = db.query_one(builder.build(&self.query)).await?;
|
||||||
@ -195,7 +195,7 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn all(self, db: &DatabaseConnection) -> Result<Vec<S::Item>, SeaErr> {
|
pub async fn all(self, db: &DatabaseConnection) -> Result<Vec<S::Item>, DbErr> {
|
||||||
let builder = db.get_query_builder_backend();
|
let builder = db.get_query_builder_backend();
|
||||||
let rows = db.query_all(builder.build(&self.query)).await?;
|
let rows = db.query_all(builder.build(&self.query)).await?;
|
||||||
let mut models = Vec::new();
|
let mut models = Vec::new();
|
||||||
|
@ -21,7 +21,7 @@ where
|
|||||||
pub fn exec(
|
pub fn exec(
|
||||||
self,
|
self,
|
||||||
db: &'a DatabaseConnection,
|
db: &'a DatabaseConnection,
|
||||||
) -> impl Future<Output = Result<A, SeaErr>> + 'a {
|
) -> impl Future<Output = Result<A, DbErr>> + 'a {
|
||||||
// so that self is dropped before entering await
|
// so that self is dropped before entering await
|
||||||
exec_update_and_return_original(self.query, self.model, db)
|
exec_update_and_return_original(self.query, self.model, db)
|
||||||
}
|
}
|
||||||
@ -34,7 +34,7 @@ where
|
|||||||
pub fn exec(
|
pub fn exec(
|
||||||
self,
|
self,
|
||||||
db: &'a DatabaseConnection,
|
db: &'a DatabaseConnection,
|
||||||
) -> impl Future<Output = Result<UpdateResult, SeaErr>> + 'a {
|
) -> impl Future<Output = Result<UpdateResult, DbErr>> + 'a {
|
||||||
// so that self is dropped before entering await
|
// so that self is dropped before entering await
|
||||||
exec_update_only(self.query, db)
|
exec_update_only(self.query, db)
|
||||||
}
|
}
|
||||||
@ -48,7 +48,7 @@ impl Updater {
|
|||||||
pub fn exec(
|
pub fn exec(
|
||||||
self,
|
self,
|
||||||
db: &DatabaseConnection,
|
db: &DatabaseConnection,
|
||||||
) -> impl Future<Output = Result<UpdateResult, SeaErr>> + '_ {
|
) -> impl Future<Output = Result<UpdateResult, DbErr>> + '_ {
|
||||||
let builder = db.get_query_builder_backend();
|
let builder = db.get_query_builder_backend();
|
||||||
exec_update(builder.build(&self.query), db)
|
exec_update(builder.build(&self.query), db)
|
||||||
}
|
}
|
||||||
@ -57,7 +57,7 @@ impl Updater {
|
|||||||
async fn exec_update_only(
|
async fn exec_update_only(
|
||||||
query: UpdateStatement,
|
query: UpdateStatement,
|
||||||
db: &DatabaseConnection,
|
db: &DatabaseConnection,
|
||||||
) -> Result<UpdateResult, SeaErr> {
|
) -> Result<UpdateResult, DbErr> {
|
||||||
Updater::new(query).exec(db).await
|
Updater::new(query).exec(db).await
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -65,7 +65,7 @@ async fn exec_update_and_return_original<A>(
|
|||||||
query: UpdateStatement,
|
query: UpdateStatement,
|
||||||
model: A,
|
model: A,
|
||||||
db: &DatabaseConnection,
|
db: &DatabaseConnection,
|
||||||
) -> Result<A, SeaErr>
|
) -> Result<A, DbErr>
|
||||||
where
|
where
|
||||||
A: ActiveModelTrait,
|
A: ActiveModelTrait,
|
||||||
{
|
{
|
||||||
@ -77,7 +77,7 @@ where
|
|||||||
async fn exec_update(
|
async fn exec_update(
|
||||||
statement: Statement,
|
statement: Statement,
|
||||||
db: &DatabaseConnection,
|
db: &DatabaseConnection,
|
||||||
) -> Result<UpdateResult, SeaErr> {
|
) -> Result<UpdateResult, DbErr> {
|
||||||
let result = db.execute(statement).await?;
|
let result = db.execute(statement).await?;
|
||||||
Ok(UpdateResult {
|
Ok(UpdateResult {
|
||||||
rows_affected: result.rows_affected(),
|
rows_affected: result.rows_affected(),
|
||||||
|
16
src/lib.rs
16
src/lib.rs
@ -44,7 +44,7 @@
|
|||||||
//! ## Select
|
//! ## Select
|
||||||
//! ```
|
//! ```
|
||||||
//! # use sea_orm::{DbConn, error::*, entity::*, query::*, tests_cfg::*};
|
//! # use sea_orm::{DbConn, error::*, entity::*, query::*, tests_cfg::*};
|
||||||
//! # async fn function(db: &DbConn) -> Result<(), SeaErr> {
|
//! # async fn function(db: &DbConn) -> Result<(), DbErr> {
|
||||||
//! #
|
//! #
|
||||||
//! // find all models
|
//! // find all models
|
||||||
//! let cakes: Vec<cake::Model> = Cake::find().all(db).await?;
|
//! let cakes: Vec<cake::Model> = Cake::find().all(db).await?;
|
||||||
@ -74,7 +74,7 @@
|
|||||||
//! ## Insert
|
//! ## Insert
|
||||||
//! ```
|
//! ```
|
||||||
//! # use sea_orm::{DbConn, error::*, entity::*, query::*, tests_cfg::*};
|
//! # use sea_orm::{DbConn, error::*, entity::*, query::*, tests_cfg::*};
|
||||||
//! # async fn function(db: &DbConn) -> Result<(), SeaErr> {
|
//! # async fn function(db: &DbConn) -> Result<(), DbErr> {
|
||||||
//! #
|
//! #
|
||||||
//! let apple = fruit::ActiveModel {
|
//! let apple = fruit::ActiveModel {
|
||||||
//! name: Set("Apple".to_owned()),
|
//! name: Set("Apple".to_owned()),
|
||||||
@ -94,7 +94,7 @@
|
|||||||
//! # Ok(())
|
//! # Ok(())
|
||||||
//! # }
|
//! # }
|
||||||
//! #
|
//! #
|
||||||
//! # async fn function2(db: &DbConn) -> Result<(), SeaErr> {
|
//! # async fn function2(db: &DbConn) -> Result<(), DbErr> {
|
||||||
//! # let apple = fruit::ActiveModel {
|
//! # let apple = fruit::ActiveModel {
|
||||||
//! # name: Set("Apple".to_owned()),
|
//! # name: Set("Apple".to_owned()),
|
||||||
//! # ..Default::default() // no need to set primary key
|
//! # ..Default::default() // no need to set primary key
|
||||||
@ -117,12 +117,12 @@
|
|||||||
//! #
|
//! #
|
||||||
//! use sea_orm::sea_query::{Expr, Value};
|
//! use sea_orm::sea_query::{Expr, Value};
|
||||||
//!
|
//!
|
||||||
//! # async fn function(db: &DbConn) -> Result<(), SeaErr> {
|
//! # async fn function(db: &DbConn) -> Result<(), DbErr> {
|
||||||
//! let pear: Option<fruit::Model> = Fruit::find_by_id(1).one(db).await?;
|
//! let pear: Option<fruit::Model> = Fruit::find_by_id(1).one(db).await?;
|
||||||
//! # Ok(())
|
//! # Ok(())
|
||||||
//! # }
|
//! # }
|
||||||
//! #
|
//! #
|
||||||
//! # async fn function2(db: &DbConn) -> Result<(), SeaErr> {
|
//! # async fn function2(db: &DbConn) -> Result<(), DbErr> {
|
||||||
//! # let pear: Option<fruit::Model> = Fruit::find_by_id(1).one(db).await.unwrap();
|
//! # let pear: Option<fruit::Model> = Fruit::find_by_id(1).one(db).await.unwrap();
|
||||||
//!
|
//!
|
||||||
//! let mut pear: fruit::ActiveModel = pear.unwrap().into();
|
//! let mut pear: fruit::ActiveModel = pear.unwrap().into();
|
||||||
@ -145,7 +145,7 @@
|
|||||||
//! ```
|
//! ```
|
||||||
//! # use sea_orm::{DbConn, error::*, entity::*, query::*, tests_cfg::*};
|
//! # use sea_orm::{DbConn, error::*, entity::*, query::*, tests_cfg::*};
|
||||||
//! #
|
//! #
|
||||||
//! # async fn function(db: &DbConn) -> Result<(), SeaErr> {
|
//! # async fn function(db: &DbConn) -> Result<(), DbErr> {
|
||||||
//! let banana = fruit::ActiveModel {
|
//! let banana = fruit::ActiveModel {
|
||||||
//! id: Unset(None),
|
//! id: Unset(None),
|
||||||
//! name: Set("Banana".to_owned()),
|
//! name: Set("Banana".to_owned()),
|
||||||
@ -167,12 +167,12 @@
|
|||||||
//! ```
|
//! ```
|
||||||
//! # use sea_orm::{DbConn, error::*, entity::*, query::*, tests_cfg::*};
|
//! # use sea_orm::{DbConn, error::*, entity::*, query::*, tests_cfg::*};
|
||||||
//! #
|
//! #
|
||||||
//! # async fn function(db: &DbConn) -> Result<(), SeaErr> {
|
//! # async fn function(db: &DbConn) -> Result<(), DbErr> {
|
||||||
//! let orange: Option<fruit::Model> = Fruit::find_by_id(1).one(db).await?;
|
//! let orange: Option<fruit::Model> = Fruit::find_by_id(1).one(db).await?;
|
||||||
//! # Ok(())
|
//! # Ok(())
|
||||||
//! # }
|
//! # }
|
||||||
//! #
|
//! #
|
||||||
//! # async fn function2(db: &DbConn) -> Result<(), SeaErr> {
|
//! # async fn function2(db: &DbConn) -> Result<(), DbErr> {
|
||||||
//! # let orange: Option<fruit::Model> = Fruit::find_by_id(1).one(db).await.unwrap();
|
//! # let orange: Option<fruit::Model> = Fruit::find_by_id(1).one(db).await.unwrap();
|
||||||
//! let orange: fruit::ActiveModel = orange.unwrap().into();
|
//! let orange: fruit::ActiveModel = orange.unwrap().into();
|
||||||
//!
|
//!
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
use crate::{FromQueryResult, SeaErr, QueryResult, QueryResultRow};
|
use crate::{FromQueryResult, DbErr, QueryResult, QueryResultRow};
|
||||||
use serde_json::Map;
|
use serde_json::Map;
|
||||||
pub use serde_json::Value as JsonValue;
|
pub use serde_json::Value as JsonValue;
|
||||||
|
|
||||||
impl FromQueryResult for JsonValue {
|
impl FromQueryResult for JsonValue {
|
||||||
fn from_query_result(res: &QueryResult, pre: &str) -> Result<Self, SeaErr> {
|
fn from_query_result(res: &QueryResult, pre: &str) -> Result<Self, DbErr> {
|
||||||
match &res.row {
|
match &res.row {
|
||||||
#[cfg(feature = "sqlx-mysql")]
|
#[cfg(feature = "sqlx-mysql")]
|
||||||
QueryResultRow::SqlxMySql(row) => {
|
QueryResultRow::SqlxMySql(row) => {
|
||||||
|
@ -31,7 +31,7 @@ async fn setup_schema(db: &DbConn) {
|
|||||||
println!("Create table cake: {:?}", result);
|
println!("Create table cake: {:?}", result);
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn crud_cake(db: &DbConn) -> Result<(), SeaErr> {
|
async fn crud_cake(db: &DbConn) -> Result<(), DbErr> {
|
||||||
let apple = cake::ActiveModel {
|
let apple = cake::ActiveModel {
|
||||||
name: Set("Apple Pie".to_owned()),
|
name: Set("Apple Pie".to_owned()),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user