Skip to content

Commit 3885ae6

Browse files
izik1mehcode
authored andcommitted
fix: allow queries that select just void to pass through macros as () instead of struct { _1: () }
1 parent 6714225 commit 3885ae6

File tree

5 files changed

+14
-7
lines changed

5 files changed

+14
-7
lines changed

sqlx-core/src/postgres/type_info.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -755,6 +755,10 @@ impl TypeInfo for PgTypeInfo {
755755
fn is_null(&self) -> bool {
756756
false
757757
}
758+
759+
fn is_void(&self) -> bool {
760+
matches!(self.0, PgType::Void)
761+
}
758762
}
759763

760764
impl PartialEq<PgCustomType> for PgCustomType {

sqlx-core/src/type_info.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,9 @@ pub trait TypeInfo: Debug + Display + Clone + PartialEq<Self> + Send + Sync {
88
/// Common type names are `VARCHAR`, `TEXT`, or `INT`. Type names should be uppercase. They
99
/// should be a rough approximation of how they are written in SQL in the given database.
1010
fn name(&self) -> &str;
11+
12+
#[doc(hidden)]
13+
fn is_void(&self) -> bool {
14+
false
15+
}
1116
}

sqlx-macros/src/query/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ pub use input::QueryMacroInput;
99
use quote::{format_ident, quote};
1010
use sqlx_core::connection::Connection;
1111
use sqlx_core::database::Database;
12-
use sqlx_core::describe::Describe;
12+
use sqlx_core::{column::Column, describe::Describe, type_info::TypeInfo};
1313
use sqlx_rt::block_on;
1414

1515
use crate::database::DatabaseExt;
@@ -208,7 +208,7 @@ where
208208

209209
let query_args = format_ident!("query_args");
210210

211-
let output = if data.describe.columns().is_empty() {
211+
let output = if data.describe.columns().iter().all(|it| it.type_info().is_void()) {
212212
let db_path = DB::db_path();
213213
let sql = &input.src;
214214

src/macros.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/// Statically checked SQL query with `println!()` style syntax.
22
///
33
/// This expands to an instance of [`query::Map`][crate::query::Map] that outputs an ad-hoc anonymous
4-
/// struct type, if the query has output columns, or `()` (unit) otherwise:
4+
/// struct type, if the query has at least one output column that is not `Void`, or `()` (unit) otherwise:
55
///
66
/// ```rust,ignore
77
/// # use sqlx::Connect;

tests/postgres/macros.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,10 @@ async fn test_text_var_char_char_n() -> anyhow::Result<()> {
8989
async fn test_void() -> anyhow::Result<()> {
9090
let mut conn = new::<Postgres>().await?;
9191

92-
let record = sqlx::query!(r#"select pg_notify('chan', 'message') as _1"#)
93-
.fetch_one(&mut conn)
92+
let _ = sqlx::query!(r#"select pg_notify('chan', 'message')"#)
93+
.execute(&mut conn)
9494
.await?;
9595

96-
assert_eq!(record._1, Some(()));
97-
9896
Ok(())
9997
}
10098

0 commit comments

Comments
 (0)