Skip to content

Commit 08828b7

Browse files
authored
Fix @FUNCTION_NAME@ not being handled in sql attributes (#443)
* Fix @FUNCTION_NAME@ not being handled in sql attributes Signed-off-by: Ana Hobden <[email protected]> * Fixup test Signed-off-by: Ana Hobden <[email protected]> * Fix test name Signed-off-by: Ana Hobden <[email protected]>
1 parent b527960 commit 08828b7

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

pgx-tests/src/tests/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ mod log_tests;
2121
mod memcxt_tests;
2222
mod name_tests;
2323
mod numeric_tests;
24-
mod pg_extern_args_tests;
24+
mod pg_extern_tests;
2525
mod pg_try_tests;
2626
mod pgbox_tests;
2727
mod postgres_type_tests;

pgx-tests/src/tests/pg_extern_args_tests.rs renamed to pgx-tests/src/tests/pg_extern_tests.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,25 @@ mod tests {
3030
.expect("failed to get SPI result");
3131
assert!(result)
3232
}
33+
34+
35+
// Ensures `@FUNCTION_NAME@` is handled.
36+
#[pg_extern(sql = r#"
37+
CREATE OR REPLACE FUNCTION tests."overridden_sql_with_fn_name"() RETURNS void
38+
STRICT
39+
LANGUAGE c /* Rust */
40+
AS 'MODULE_PATHNAME', '@FUNCTION_NAME@';
41+
"#)]
42+
fn overridden_sql_with_fn_name() -> bool {
43+
true
44+
}
45+
46+
#[pg_test]
47+
fn test_overridden_sql_with_fn_name() {
48+
let result = Spi::get_one::<bool>(
49+
r#"SELECT tests."overridden_sql_with_fn_name"()"#,
50+
)
51+
.expect("failed to get SPI result");
52+
assert!(result)
53+
}
3354
}

pgx-utils/src/sql_entity_graph/pg_extern/mod.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,18 @@ impl PgExtern {
205205
}
206206

207207
let func = syn::parse2::<syn::ItemFn>(item)?;
208+
209+
if let Some(ref mut to_sql_config) = to_sql_config {
210+
if let Some(ref mut content) = to_sql_config.content {
211+
let value = content.value();
212+
let updated_value = value.replace(
213+
"@FUNCTION_NAME@",
214+
&*(func.sig.ident.to_string() + "_wrapper"),
215+
) + "\n";
216+
*content = syn::LitStr::new(&updated_value, Span::call_site());
217+
}
218+
}
219+
208220
Ok(Self {
209221
attrs,
210222
func,

0 commit comments

Comments
 (0)