-
-
Notifications
You must be signed in to change notification settings - Fork 568
Add proxy transaction impl #2573
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
Open
logicin
wants to merge
1
commit into
SeaQL:master
Choose a base branch
from
logicin:proxy-transaction
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
+40
−0
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//! Proxy connection example.
mod entity;
mod system_users;
use std::{
collections::BTreeMap,
sync::{Arc, Mutex},
};
use std::error::Error;
use std::future::Future;
use std::pin::Pin;
use gluesql::{memory_storage::MemoryStorage, prelude::Glue};
use gluesql::core::store::Transaction;
use sea_orm::{AccessMode, ActiveValue, ActiveValue::Set, ConnectOptions, ConnectionTrait, Database, DatabaseBackend, DatabaseConnection, DatabaseTransaction, DbBackend, DbErr, EntityTrait, ExecResult, IsolationLevel, ProxyDatabaseTrait, ProxyExecResult, ProxyRow, QueryResult, Statement, TransactionError, TransactionTrait};
use entity::post::{ActiveModel, Entity};
use sea_orm::prelude::DateTime;
use crate::system_users::prelude::{SystemUsers, SystemUsersActiveModel};
#[derive(Debug)]
pub struct DatabaseTransactionProxyNew {
inner: DatabaseConnection,
}
#[async_trait::async_trait]
impl ProxyDatabaseTrait for DatabaseTransactionProxyNew {
async fn query(&self, statement: Statement) -> Result<Vec<ProxyRow>, DbErr> {
println!("ProxyDatabaseTrait-----query--------A{:?}", statement);
Ok(vec![])
}
async fn execute(&self, statement: Statement) -> Result<ProxyExecResult, DbErr> {
println!("ProxyDatabaseTrait-----query--------B{:?}", statement);
Ok(ProxyExecResult { last_insert_id: 0, rows_affected: 0 })
}
}
impl DatabaseTransactionProxyNew {
pub async fn new(opt: DatabaseConnection) -> Result<DatabaseTransactionProxyNew, DbErr>
{
Ok(DatabaseTransactionProxyNew {
inner: opt,
})
}
pub async fn connect<C>(opt: C) -> Result<DatabaseTransactionProxyNew, DbErr>
where
C: Into<ConnectOptions>,
{
Ok(DatabaseTransactionProxyNew {
inner: Database::connect(opt).await?,
})
}
}
#[async_std::main]
async fn main()-> Result<(), Box<dyn Error>> {
let dc =Database::connect("mysql://*********/test").await?;
let proxy = DatabaseTransactionProxyNew::new(dc).await?;
let db_proxy = Database::connect_proxy(DatabaseBackend::MySql, Arc::new(Box::new(proxy))).await?;
// begin ok
db_proxy.transaction(
|txn| {
Box::pin(async move {
let am = SystemUsersActiveModel{
id: ActiveValue::Set(333),
username: ActiveValue::Set("1".to_string()),
password: ActiveValue::Set("1".to_string()),
nickname: ActiveValue::Set("1".to_string()),
remark: ActiveValue::Set(Some("1".to_string())),
dept_id: Default::default(),
post_ids: Default::default(),
email: Default::default(),
mobile: Default::default(),
sex: Default::default(),
avatar: Default::default(),
status: ActiveValue::Set(1),
login_ip: Default::default(),
login_date: Default::default(),
creator: Default::default(),
create_time: ActiveValue::Set(DateTime::default()),
updater: Default::default(),
update_time:ActiveValue::Set(DateTime::default()),
deleted: ActiveValue::Set(false),
tenant_id: ActiveValue::Set(22),
};
SystemUsers::insert(am).exec(txn).await
})
}
).await?;
Ok(())
}
#[cfg(test)]
mod tests {
#[smol_potat::test]
async fn try_run() {
crate::main()
}
} Error: Transaction(RecordNotInserted) |
i think this error cause by " fn execute " rows_affected return 0; @oulover
|
Yes, I forgot to implement execute, thank you @logicin async fn execute(&self, statement: Statement) -> Result<ProxyExecResult, DbErr> {
self.inner.execute(statement).await.map(|r|
ProxyExecResult {
last_insert_id: r.last_insert_id(),
rows_affected: r.rows_affected(),
}
)
}
// successfully execute |
@tyt2y3 hello, Cloud you merge this PR? I want to use this feature. 😂 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
PR Info
New Features
Bug Fixes
Breaking Changes
Changes