Skip to content

Commit

Permalink
factorize the Jsonb SQLite/Pg example
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean-Marc Le Roux committed Sep 27, 2024
1 parent 268a3df commit def9cee
Showing 1 changed file with 19 additions and 42 deletions.
61 changes: 19 additions & 42 deletions diesel/src/sql_types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -467,21 +467,32 @@ pub struct Json;
///
/// ## Examples
///
/// ### PostgreSQL
///
/// ```rust
/// # #![allow(dead_code)]
/// # include!("../doctest_setup.rs");
/// #
/// # #[cfg(all(feature = "postgres_backend", feature = "serde_json"))]
/// table! {
/// contacts {
/// id -> Integer,
/// name -> VarChar,
/// address -> Jsonb,
/// }
/// }
/// # #[cfg(all(
/// # feature = "sqlite",
/// # feature = "serde_json",
/// # feature = "returning_clauses_for_sqlite_3_35"
/// # ))]
/// table! {
/// contacts {
/// id -> Integer,
/// name -> Text,
/// address -> Jsonb,
/// }
/// }
///
/// # #[cfg(feature = "serde_json")]
/// # #[cfg(all(feature = "postgres_backend", feature = "serde_json"))]
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # use diesel::insert_into;
/// # use self::contacts::dsl::*;
Expand All @@ -491,52 +502,18 @@ pub struct Json;
/// # name VARCHAR NOT NULL,
/// # address JSONB NOT NULL
/// # )").execute(connection)?;
///
/// let santas_address: serde_json::Value = serde_json::from_str(r#"{
/// "street": "Article Circle Expressway 1",
/// "city": "North Pole",
/// "postcode": "99705",
/// "state": "Alaska"
/// }"#)?;
/// let inserted_address = insert_into(contacts)
/// .values((name.eq("Claus"), address.eq(&santas_address)))
/// .returning(address)
/// .get_result::<serde_json::Value>(connection)?;
/// assert_eq!(santas_address, inserted_address);
/// # Ok(())
/// # }
/// # #[cfg(not(feature = "serde_json"))]
/// # fn main() {}
/// ```
///
/// ### SQLite
///
/// ```rust
/// # #![allow(dead_code)]
/// # include!("../doctest_setup.rs");
/// #
/// # #[cfg(all(feature = "sqlite", feature = "serde_json"))]
/// table! {
/// contacts {
/// id -> Integer,
/// name -> Text,
/// address -> Jsonb,
/// }
/// }
///
/// # #[cfg(all(feature = "sqlite", feature = "serde_json"))]
/// # #[cfg(all(
/// # feature = "sqlite",
/// # feature = "serde_json",
/// # feature = "returning_clauses_for_sqlite_3_35"
/// # ))]
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # use diesel::insert_into;
/// # use diesel::query_dsl::RunQueryDsl;
/// # use diesel::test_helpers::connection;
/// # use diesel::ExpressionMethods;
/// # use diesel::dsl::*;
/// # let connection = &mut connection();
/// # diesel::sql_query("CREATE TABLE contacts (
/// # id INT PRIMARY KEY,
/// # name TEXT NOT NULL,
/// # address BLOB NOT NULL
/// # )").execute(connection)?;
/// let santas_address: serde_json::Value = serde_json::from_str(r#"{
/// "street": "Article Circle Expressway 1",
/// "city": "North Pole",
Expand Down

0 comments on commit def9cee

Please sign in to comment.