Skip to content

Commit 1ff6a2a

Browse files
bgeronabonander
authored andcommitted
feat: support calling PostgreSQL procedures with the macros
Fixes #1449 (I think). I verified that the code fixes the new test. I used INOUT in setup.sql because older versions of Postgres don't support OUT parameters.
1 parent bb2baf2 commit 1ff6a2a

File tree

3 files changed

+31
-5
lines changed

3 files changed

+31
-5
lines changed

sqlx-postgres/src/connection/describe.rs

+15-5
Original file line numberDiff line numberDiff line change
@@ -451,9 +451,15 @@ WHERE rngtypid = $1
451451

452452
let mut nullables = Vec::new();
453453

454-
if let Some(outputs) = &explain.plan.output {
454+
if let Explain::Plan(
455+
plan @ Plan {
456+
output: Some(outputs),
457+
..
458+
},
459+
) = &explain
460+
{
455461
nullables.resize(outputs.len(), None);
456-
visit_plan(&explain.plan, outputs, &mut nullables);
462+
visit_plan(&plan, outputs, &mut nullables);
457463
}
458464

459465
Ok(nullables)
@@ -486,9 +492,13 @@ fn visit_plan(plan: &Plan, outputs: &[String], nullables: &mut Vec<Option<bool>>
486492
}
487493

488494
#[derive(serde::Deserialize)]
489-
struct Explain {
490-
#[serde(rename = "Plan")]
491-
plan: Plan,
495+
enum Explain {
496+
/// {"Plan": ...} -- returned for most statements
497+
Plan(Plan),
498+
/// The string "Utility Statement" -- returned for
499+
/// a CALL statement
500+
#[serde(rename = "Utility Statement")]
501+
UtilityStatement,
492502
}
493503

494504
#[derive(serde::Deserialize)]

tests/postgres/macros.rs

+13
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,19 @@ async fn test_void() -> anyhow::Result<()> {
9696
Ok(())
9797
}
9898

99+
#[sqlx_macros::test]
100+
async fn test_call_procedure() -> anyhow::Result<()> {
101+
let mut conn = new::<Postgres>().await?;
102+
103+
let row = sqlx::query!(r#"CALL forty_two(null)"#)
104+
.fetch_one(&mut conn)
105+
.await?;
106+
107+
assert_eq!(row.forty_two, Some(42));
108+
109+
Ok(())
110+
}
111+
99112
#[sqlx_macros::test]
100113
async fn test_query_file() -> anyhow::Result<()> {
101114
let mut conn = new::<Postgres>().await?;

tests/postgres/setup.sql

+3
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,6 @@ CREATE TABLE products (
4141
name TEXT,
4242
price NUMERIC CHECK (price > 0)
4343
);
44+
45+
CREATE OR REPLACE PROCEDURE forty_two(INOUT forty_two INT = NULL)
46+
LANGUAGE plpgsql AS 'begin forty_two := 42; end;';

0 commit comments

Comments
 (0)