use crate::SqlGraphEntity;
use crate::pgrx_sql::PgrxSql;
#[derive(Default, Clone)]
pub struct ToSqlConfigEntity<'a> {
pub enabled: bool,
pub content: Option<&'a str>,
}
impl ToSqlConfigEntity<'_> {
#[inline]
fn fields(&self) -> (bool, Option<&str>) {
(self.enabled, self.content)
}
pub fn to_sql(
&self,
entity: &SqlGraphEntity<'_>,
context: &PgrxSql<'_>,
) -> Option<eyre::Result<String>> {
if !self.enabled {
return Some(Ok(format!(
"\n\
{sql_anchor_comment}\n\
-- Skipped due to `#[pgrx(sql = false)]`\n",
sql_anchor_comment = entity.sql_anchor_comment(),
)));
}
if let Some(content) = self.content {
let module_pathname = context.get_module_pathname();
let content = content.replace("@MODULE_PATHNAME@", &module_pathname);
return Some(Ok(format!(
"\n\
{sql_anchor_comment}\n\
{content}\n\
",
content = content,
sql_anchor_comment = entity.sql_anchor_comment()
)));
}
None
}
}
impl std::cmp::PartialOrd for ToSqlConfigEntity<'_> {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl std::cmp::Ord for ToSqlConfigEntity<'_> {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.fields().cmp(&other.fields())
}
}
impl std::cmp::PartialEq for ToSqlConfigEntity<'_> {
fn eq(&self, other: &Self) -> bool {
self.fields() == other.fields()
}
}
impl std::cmp::Eq for ToSqlConfigEntity<'_> {}
impl std::hash::Hash for ToSqlConfigEntity<'_> {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.fields().hash(state);
}
}
impl std::fmt::Debug for ToSqlConfigEntity<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
let (enabled, content) = self.fields();
f.debug_struct("ToSqlConfigEntity")
.field("enabled", &enabled)
.field("content", &content)
.finish()
}
}