use super::AgentStoreOperations;
use crate::agents::store::diesel::{
schema::{agent, role},
Agent, AgentStoreError,
};
use crate::agents::store::diesel::models::{AgentModel, RoleModel};
use crate::commits::MAX_COMMIT_NUM;
use crate::error::InternalError;
use diesel::{prelude::*, result::Error::NotFound};
pub(in crate::agents::store::diesel) trait AgentStoreFetchAgentOperation {
fn fetch_agent(
&self,
pub_key: &str,
service_id: Option<&str>,
) -> Result<Option<Agent>, AgentStoreError>;
}
#[cfg(feature = "postgres")]
impl<'a> AgentStoreFetchAgentOperation for AgentStoreOperations<'a, diesel::pg::PgConnection> {
fn fetch_agent(
&self,
pub_key: &str,
service_id: Option<&str>,
) -> Result<Option<Agent>, AgentStoreError> {
self.conn
.build_transaction()
.read_write()
.run::<_, AgentStoreError, _>(|| {
let mut query = agent::table.into_boxed().select(agent::all_columns).filter(
agent::public_key
.eq(&pub_key)
.and(agent::end_commit_num.eq(MAX_COMMIT_NUM)),
);
if let Some(service_id) = service_id {
query = query.filter(agent::service_id.eq(service_id));
} else {
query = query.filter(agent::service_id.is_null());
}
let agent = query
.first::<AgentModel>(self.conn)
.map(Some)
.or_else(|err| if err == NotFound { Ok(None) } else { Err(err) })
.map_err(|err| {
AgentStoreError::InternalError(InternalError::from_source(Box::new(err)))
})?;
let mut query = role::table
.select(role::all_columns)
.into_boxed()
.select(role::all_columns)
.filter(
role::public_key
.eq(&pub_key)
.and(role::end_commit_num.eq(MAX_COMMIT_NUM)),
);
if let Some(service_id) = service_id {
query = query.filter(role::service_id.eq(service_id));
} else {
query = query.filter(role::service_id.is_null());
}
let roles = query.load::<RoleModel>(self.conn).map_err(|err| {
AgentStoreError::InternalError(InternalError::from_source(Box::new(err)))
})?;
Ok(agent.map(|agent| Agent::from((agent, roles))))
})
}
}
#[cfg(feature = "sqlite")]
impl<'a> AgentStoreFetchAgentOperation
for AgentStoreOperations<'a, diesel::sqlite::SqliteConnection>
{
fn fetch_agent(
&self,
pub_key: &str,
service_id: Option<&str>,
) -> Result<Option<Agent>, AgentStoreError> {
self.conn
.immediate_transaction::<_, AgentStoreError, _>(|| {
let mut query = agent::table.into_boxed().select(agent::all_columns).filter(
agent::public_key
.eq(&pub_key)
.and(agent::end_commit_num.eq(MAX_COMMIT_NUM)),
);
if let Some(service_id) = service_id {
query = query.filter(agent::service_id.eq(service_id));
} else {
query = query.filter(agent::service_id.is_null());
}
let agent = query
.first::<AgentModel>(self.conn)
.map(Some)
.or_else(|err| if err == NotFound { Ok(None) } else { Err(err) })
.map_err(|err| {
AgentStoreError::InternalError(InternalError::from_source(Box::new(err)))
})?;
let mut query = role::table
.select(role::all_columns)
.into_boxed()
.select(role::all_columns)
.filter(
role::public_key
.eq(&pub_key)
.and(role::end_commit_num.eq(MAX_COMMIT_NUM)),
);
if let Some(service_id) = service_id {
query = query.filter(role::service_id.eq(service_id));
} else {
query = query.filter(role::service_id.is_null());
}
let roles = query.load::<RoleModel>(self.conn).map_err(|err| {
AgentStoreError::InternalError(InternalError::from_source(Box::new(err)))
})?;
Ok(agent.map(|agent| Agent::from((agent, roles))))
})
}
}