Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add Sqllite json_valid #4465

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

dhirensr
Copy link

Related to #4366

also I couldn't test the functionality fully, and I've asked a question related to testing in #4463

Copy link
Member

@weiznich weiznich left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for opening this PR and sorry for taking some days to get back with hints what's wrong here.

The implementation is already on a good way, there are only minor nits that needs to be fixed to run the tests + make this good to merge.

/// # }
/// ```
#[sql_name = "json_valid"]
fn json_valid<J: JsonOrNullableJsonOrJsonbOrNullableJsonb + MaybeNullableValue<Text>>(j: J) -> Integer;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fn json_valid<J: JsonOrNullableJsonOrJsonbOrNullableJsonb + MaybeNullableValue<Text>>(j: J) -> Integer;
fn json_valid<J: JsonOrNullableJsonOrJsonbOrNullableJsonb + MaybeNullableValue<Bool>>(j: J) -> J::Out;

Diesel maps such flag integers to a boolean for sqlite so we use Bool as return type. That would usually be -> Bool. For this particular function sqlite returns NULL if a null value is passed in, which means we want to return Nullable<Bool> if the input value is nullable and Bool otherwise. The MaybeNullableValue<Bool> part takes care of that by transforming the generic parameter into the right variant. That variant then can be accessed via the Out associated type.

/// }
///
/// let result = diesel::select(json_valid::<Json>(json!({"x":35})))
/// .get_result::<Integer>(connection)?;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// .get_result::<Integer>(connection)?;
/// .get_result::<bool>(connection)?;

/// #
/// # #[cfg(feature = "serde_json")]
/// # fn run_test() -> QueryResult<()> {
/// # use diesel::dsl::{sql, json_pretty_with_indentation};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// # use diesel::dsl::{sql, json_pretty_with_indentation};
/// # use diesel::dsl::{sql, json_valid};


#[cfg(feature = "sqlite")]
define_sql_function! {
/// Returns 1 if the argument is well-formed JSON, or returns 0 if is not well-formed.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// Returns 1 if the argument is well-formed JSON, or returns 0 if is not well-formed.
/// Returns `true` if the argument is well-formed JSON, or returns `false` if is not well-formed.

/// Return type of [`json_valid(json)`](super::functions::json_valid())
#[allow(non_camel_case_types)]
#[cfg(feature = "sqlite")]
pub type json_valid<E> = super::functions::json_valid<SqlTypeOf<E>, Integer>;
Copy link
Member

@weiznich weiznich Feb 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pub type json_valid<E> = super::functions::json_valid<SqlTypeOf<E>, Integer>;
pub type json_valid<E> = super::functions::json_valid<SqlTypeOf<E>, E>;

That needs to be just E and not Integer as the helper type accepts one generic argument for the SQL type (the SqlTypeOf<E> part) and one generic argument for the expression itself (the E))

(That also means removing the use crate::sql_types::Integer; line again)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants