|
3 | 3 | use super::expression_methods::InetOrCidr;
|
4 | 4 | use crate::expression::functions::define_sql_function;
|
5 | 5 | use crate::pg::expression::expression_methods::ArrayOrNullableArray;
|
| 6 | +use crate::pg::expression::expression_methods::JsonOrNullableJson; |
6 | 7 | use crate::pg::expression::expression_methods::JsonbOrNullableJsonb;
|
7 | 8 | use crate::pg::expression::expression_methods::MaybeNullableValue;
|
8 | 9 | use crate::pg::expression::expression_methods::MultirangeOrNullableMultirange;
|
@@ -1636,6 +1637,126 @@ define_sql_function! {
|
1636 | 1637 | ) -> Arr::Out;
|
1637 | 1638 | }
|
1638 | 1639 |
|
| 1640 | +#[cfg(feature = "postgres_backend")] |
| 1641 | +define_sql_function! { |
| 1642 | + /// Returns the type of the top-level json value as a text-string |
| 1643 | + /// |
| 1644 | + /// # Example |
| 1645 | + /// |
| 1646 | + /// ```rust |
| 1647 | + /// # include!("../../doctest_setup.rs"); |
| 1648 | + /// # |
| 1649 | + /// # fn main() { |
| 1650 | + /// # #[cfg(feature = "serde_json")] |
| 1651 | + /// # run_test().unwrap(); |
| 1652 | + /// # } |
| 1653 | + /// # |
| 1654 | + /// # #[cfg(feature = "serde_json")] |
| 1655 | + /// # fn run_test() -> QueryResult<()> { |
| 1656 | + /// # use diesel::dsl::json_typeof; |
| 1657 | + /// # use serde_json::{json, Value}; |
| 1658 | + /// # use diesel::sql_types::{Json, Nullable}; |
| 1659 | + /// # let connection = &mut establish_connection(); |
| 1660 | + /// let result = diesel::select(json_typeof::<Json, _>(json!({"a": "b", "c": 1}))) |
| 1661 | + /// .get_result::<String>(connection)?; |
| 1662 | + /// |
| 1663 | + /// assert_eq!("object".to_string(), result); |
| 1664 | + /// |
| 1665 | + /// let result = diesel::select(json_typeof::<Json, _>(json!([1,2,3]))) |
| 1666 | + /// .get_result::<String>(connection)?; |
| 1667 | + /// |
| 1668 | + /// assert_eq!("array".to_string(), result); |
| 1669 | + /// |
| 1670 | + /// let result = diesel::select(json_typeof::<Json, _>(json!("abc"))) |
| 1671 | + /// .get_result::<String>(connection)?; |
| 1672 | + /// |
| 1673 | + /// assert_eq!("string".to_string(), result); |
| 1674 | + /// |
| 1675 | + /// let result = diesel::select(json_typeof::<Json, _>(json!(-123.4))) |
| 1676 | + /// .get_result::<String>(connection)?; |
| 1677 | + /// |
| 1678 | + /// assert_eq!("number".to_string(), result); |
| 1679 | + /// |
| 1680 | + /// let result = diesel::select(json_typeof::<Json, _>(json!(true))) |
| 1681 | + /// .get_result::<String>(connection)?; |
| 1682 | + /// |
| 1683 | + /// assert_eq!("boolean".to_string(), result); |
| 1684 | + /// |
| 1685 | + /// let result = diesel::select(json_typeof::<Json, _>(json!(null))) |
| 1686 | + /// .get_result::<String>(connection)?; |
| 1687 | + /// |
| 1688 | + /// assert_eq!("null".to_string(), result); |
| 1689 | + /// |
| 1690 | + /// let result = diesel::select(json_typeof::<Nullable<Json>, _>(None::<Value>)) |
| 1691 | + /// .get_result::<Option<String>>(connection)?; |
| 1692 | + /// |
| 1693 | + /// assert!(result.is_none()); |
| 1694 | + /// # Ok(()) |
| 1695 | + /// # } |
| 1696 | + /// ``` |
| 1697 | + fn json_typeof<E: JsonOrNullableJson + SingleValue + MaybeNullableValue<Text>>(e: E) -> E::Out; |
| 1698 | +} |
| 1699 | + |
| 1700 | +#[cfg(feature = "postgres_backend")] |
| 1701 | +define_sql_function! { |
| 1702 | + /// Returns the type of the top-level jsonb value as a text-string |
| 1703 | + /// |
| 1704 | + /// # Example |
| 1705 | + /// |
| 1706 | + /// ```rust |
| 1707 | + /// # include!("../../doctest_setup.rs"); |
| 1708 | + /// # |
| 1709 | + /// # fn main() { |
| 1710 | + /// # #[cfg(feature = "serde_json")] |
| 1711 | + /// # run_test().unwrap(); |
| 1712 | + /// # } |
| 1713 | + /// # |
| 1714 | + /// # #[cfg(feature = "serde_json")] |
| 1715 | + /// # fn run_test() -> QueryResult<()> { |
| 1716 | + /// # use diesel::dsl::jsonb_typeof; |
| 1717 | + /// # use serde_json::{json, Value}; |
| 1718 | + /// # use diesel::sql_types::{Jsonb, Nullable}; |
| 1719 | + /// # let connection = &mut establish_connection(); |
| 1720 | + /// let result = diesel::select(jsonb_typeof::<Jsonb, _>(json!({"a": "b", "c": 1}))) |
| 1721 | + /// .get_result::<String>(connection)?; |
| 1722 | + /// |
| 1723 | + /// assert_eq!("object".to_string(), result); |
| 1724 | + /// |
| 1725 | + /// let result = diesel::select(jsonb_typeof::<Jsonb, _>(json!([1,2,3]))) |
| 1726 | + /// .get_result::<String>(connection)?; |
| 1727 | + /// |
| 1728 | + /// assert_eq!("array".to_string(), result); |
| 1729 | + /// |
| 1730 | + /// let result = diesel::select(jsonb_typeof::<Jsonb, _>(json!("abc"))) |
| 1731 | + /// .get_result::<String>(connection)?; |
| 1732 | + /// |
| 1733 | + /// assert_eq!("string".to_string(), result); |
| 1734 | + /// |
| 1735 | + /// let result = diesel::select(jsonb_typeof::<Jsonb, _>(json!(-123.4))) |
| 1736 | + /// .get_result::<String>(connection)?; |
| 1737 | + /// |
| 1738 | + /// assert_eq!("number".to_string(), result); |
| 1739 | + /// |
| 1740 | + /// let result = diesel::select(jsonb_typeof::<Jsonb, _>(json!(true))) |
| 1741 | + /// .get_result::<String>(connection)?; |
| 1742 | + /// |
| 1743 | + /// assert_eq!("boolean".to_string(), result); |
| 1744 | + /// |
| 1745 | + /// let result = diesel::select(jsonb_typeof::<Jsonb, _>(json!(null))) |
| 1746 | + /// .get_result::<String>(connection)?; |
| 1747 | + /// |
| 1748 | + /// assert_eq!("null".to_string(), result); |
| 1749 | + /// |
| 1750 | + /// let result = diesel::select(jsonb_typeof::<Nullable<Jsonb>, _>(None::<Value>)) |
| 1751 | + /// .get_result::<Option<String>>(connection)?; |
| 1752 | + /// |
| 1753 | + /// assert!(result.is_none()); |
| 1754 | + /// # Ok(()) |
| 1755 | + /// # } |
| 1756 | + /// ``` |
| 1757 | + fn jsonb_typeof<E: JsonbOrNullableJsonb + SingleValue + MaybeNullableValue<Text>>(e: E) -> E::Out; |
| 1758 | +} |
| 1759 | + |
1639 | 1760 | #[cfg(feature = "postgres_backend")]
|
1640 | 1761 | define_sql_function! {
|
1641 | 1762 | /// Converts the given json value to pretty-printed, indented text
|
|
0 commit comments