-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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(sqlite): add json_array_length fn #4479
feat(sqlite): add json_array_length fn #4479
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for working on this. It's already looking like a good start, but we want to improve some things to get the CI passing + make it more robust in terms of how types are represented.
/// assert_eq!(major, 3); | ||
/// assert!(minor >= 46); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would rather skip the test in this case instead of panicing, as we do for the other tests as well: https://github.com/diesel-rs/diesel/blob/master/diesel/src/sqlite/expression/functions.rs#L82-L87
/// assert_eq!(major, 3); | ||
/// assert!(minor >= 46); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would rather skip the test in this case instead of panicing, as we do for the other tests as well: https://github.com/diesel-rs/diesel/blob/master/diesel/src/sqlite/expression/functions.rs#L82-L87
/// | ||
/// assert_eq!(json!(0), result); | ||
/// | ||
/// # Ok(()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We also want a test here that checks what happens if you pass a SQL NULL
value to the function. So something like
let result = diesel::select(json_array_length::<Nullable<Json, _>>(None::<serde_json::Value>())
Possibly we also want variants that use Jsonb
instead of Json
as SQL type.
/// # } | ||
/// ``` | ||
#[sql_name = "json_array_length"] | ||
fn json_array_length_with_path<J: JsonOrNullableJsonOrJsonbOrNullableJsonb + MaybeNullableValue<Json>>(j: J, path: Text) -> J::Out; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This must unconditionally return a Nullable<Json>
or Nullable<Jsonb>
type as the nullablity of the return value depends on runtime constraints (whether path exists or not).
fn json_array_length_with_path<J: JsonOrNullableJsonOrJsonbOrNullableJsonb + MaybeNullableValue<Json>>(j: J, path: Text) -> J::Out; | |
fn json_array_length_with_path<J: JsonOrNullableJsonOrJsonbOrNullableJsonb + IntoNullable>(j: J, path: Text) -> J::Nullable; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The J::Nullable
requires me to add + SingleValue
to J
which gets me this error:
the trait `sql_types::SingleValue` is not implemented for `<J as sql_types::IntoNullable>::Nullable`
Haven't been able to find a solution yet. Any insights?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had another look and it turns out that the suggestion was not correct. This function needs to return Nullable<Integer>
instead. I just push a fix in 4e181aa for that + rebased the PR to incorporate recent changes from master branch.
This should be good to merge now. Thanks again for contributing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you!
As I am still fairly unfamiliar to the project, I hope to be even more helpful next time.
8352d8a
to
4e181aa
Compare
Add json_array_length to sqlite json functions
Notes
use diesel::dsl::json_array_length
was the postgres version ?!use diesel::dsl::json_array_length_with_path
was not foundjson_pretty
in the same file below a certain versionInstall sqlite3 lib
sqlite-autoconf-<VERSION>.tar.gz
from https://www.sqlite.org/download.htmltar xvfz ../sqlite-autoconf-<VERSION>.tar.gz
./configure
make
sudo make install
libsqlite3.so
(find / -name libsqlite3.so ...
=> in my case/usr/lib/x86_64-linux-gnu
) and copy the file (in my case had to make a symbolic link too)