pgrx_sql_entity_graph/pg_trigger/
entity.rs1use crate::{PgrxSql, SqlGraphEntity, SqlGraphIdentifier, ToSql, ToSqlConfigEntity};
19
20#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
21pub struct PgTriggerEntity {
22 pub function_name: &'static str,
23 pub to_sql_config: ToSqlConfigEntity,
24 pub file: &'static str,
25 pub line: u32,
26 pub module_path: &'static str,
27 pub full_path: &'static str,
28}
29
30impl PgTriggerEntity {
31 fn wrapper_function_name(&self) -> String {
32 self.function_name.to_string() + "_wrapper"
33 }
34}
35
36impl From<PgTriggerEntity> for SqlGraphEntity {
37 fn from(val: PgTriggerEntity) -> Self {
38 SqlGraphEntity::Trigger(val)
39 }
40}
41
42impl ToSql for PgTriggerEntity {
43 fn to_sql(&self, context: &PgrxSql) -> eyre::Result<String> {
44 let self_index = context.triggers[self];
45 let schema = context.schema_prefix_for(&self_index);
46
47 let PgTriggerEntity { file, line, full_path, function_name, .. } = self;
48 let sql = format!(
49 "\n\
50 -- {file}:{line}\n\
51 -- {full_path}\n\
52 CREATE FUNCTION {schema}\"{function_name}\"()\n\
53 \tRETURNS TRIGGER\n\
54 \tLANGUAGE c\n\
55 \tAS 'MODULE_PATHNAME', '{wrapper_function_name}';",
56 wrapper_function_name = self.wrapper_function_name(),
57 );
58 Ok(sql)
59 }
60}
61
62impl SqlGraphIdentifier for PgTriggerEntity {
63 fn dot_identifier(&self) -> String {
64 format!("trigger fn {}", self.full_path)
65 }
66 fn rust_identifier(&self) -> String {
67 self.full_path.to_string()
68 }
69
70 fn file(&self) -> Option<&'static str> {
71 Some(self.file)
72 }
73
74 fn line(&self) -> Option<u32> {
75 Some(self.line)
76 }
77}