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