use crate::control::security::identity::AuthenticatedIdentity;
use crate::control::sql_dispatch::dispatch_sql;
use crate::control::state::SharedState;
use super::super::super::result::{DdlError, DdlResult};
use super::super::auth_support::status;
pub async fn handle_publish(
state: &SharedState,
identity: &AuthenticatedIdentity,
sql: &str,
) -> Result<Vec<DdlResult>, DdlError> {
match dispatch_sql(state, identity, sql).await {
Some(Ok(_)) => Ok(status("PUBLISH")),
Some(Err(e)) => {
let sqlstate = match &e {
crate::Error::CollectionNotFound { .. } => "42704",
crate::Error::BadRequest { .. } => "42601",
crate::Error::Dispatch { .. } => "58000",
_ => "XX000",
};
Err(DdlError {
sqlstate: sqlstate.to_string(),
message: e.to_string(),
})
}
None => Err(DdlError {
sqlstate: "42601".to_string(),
message: "expected PUBLISH TO <topic> '<payload>'".to_string(),
}),
}
}