From f69f370f25f099fd5732a5383ceffc76f724c482 Mon Sep 17 00:00:00 2001 From: Leon Lux <52079984+Lachstec@users.noreply.github.com> Date: Sun, 25 Aug 2024 00:59:40 +0200 Subject: [PATCH] Clarify usage of Json/Jsonb in query macros (#3447) * add information and example on using json in query macros * run cargo format * add missing bracket to docs of json * wrap docs in hidden async function * change no_run to ignore --- sqlx-core/src/types/json.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/sqlx-core/src/types/json.rs b/sqlx-core/src/types/json.rs index 5e85092b..fa187f4a 100644 --- a/sqlx-core/src/types/json.rs +++ b/sqlx-core/src/types/json.rs @@ -51,6 +51,35 @@ use crate::types::Type; /// dewey_decimal: sqlx::types::Json> /// } /// ``` +/// +/// If the query macros are used, it is necessary to tell the macro to use +/// the `Json` adapter by using the type override syntax +/// ```rust,ignore +/// # async fn example3() -> sqlx::Result<()> { +/// # let mut conn: sqlx::PgConnection = unimplemented!(); +/// #[derive(sqlx::FromRow)] +/// struct Book { +/// title: String, +/// } +/// +/// #[derive(sqlx::FromRow)] +/// struct Author { +/// name: String, +/// books: sqlx::types::Json, +/// } +/// // Note the type override in the query string +/// let authors = sqlx::query_as!( +/// Author, +/// r#" +/// SELECT name, books as "books: Json" +/// FROM authors +/// "# +/// ) +/// .fetch_all(&mut conn) +/// .await?; +/// # Ok(()) +/// # } +/// ``` #[derive( Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Serialize, Deserialize, )]