sqlx icon indicating copy to clipboard operation
sqlx copied to clipboard

feat: support json in `Any` driver

Open JosiahParry opened this issue 5 months ago • 14 comments

Does your PR solve an issue?

Fixes: https://github.com/launchbadge/sqlx/issues/3997

This PR adds implementations for Decode and Encode for Json<T> with support for Any.

Is this a breaking change?

No, this is not a breaking change. It adds new functionality only.

JosiahParry avatar Aug 25 '25 18:08 JosiahParry

@abonander thank you for your feedback! I've gone ahead and made your suggested changes. However, when the thorough Postgres test suite starts there appears to be a syntax error or something similar.

My knowledge of postgres is fairly limited unfortunately.

---- it_encodes_decodes_json stdout ---- Error: error returned from database: syntax error at or near ")" Caused by: syntax error at or near ")"

The test uses the following queries:

    sqlx::query("create temporary table json_test (data TEXT)")
    sqlx::query("insert into json_test (data) values (?)")

should (?) be replaced with ($1)? Or do you suspect something different?

I'll look into spinning up my own postgres server for testing

JosiahParry avatar Aug 25 '25 23:08 JosiahParry

@abonander thank you for your feedback! I've gone ahead and made your suggested changes. However, when the thorough Postgres test suite starts there appears to be a syntax error or something similar.

My knowledge of postgres is fairly limited unfortunately.

---- it_encodes_decodes_json stdout ---- Error: error returned from database: syntax error at or near ")" Caused by: syntax error at or near ")"

The test uses the following queries:

    sqlx::query("create temporary table json_test (data TEXT)")
    sqlx::query("insert into json_test (data) values (?)")

should (?) be replaced with ($1)? Or do you suspect something different?

I'll look into spinning up my own postgres server for testing

That's likely fixed by using $x syntax instead of ? on postgres.

Here's an example from https://github.com/launchbadge/sqlx/pull/3960:

    #[cfg(feature = "postgres")]
    const SQL: &str =
        "SELECT 'Hello, world!' as string where 'Hello, world!' in ($1, $2, $3, $4, $5, $6, $7)";

    #[cfg(not(feature = "postgres"))]
    const SQL: &str =
        "SELECT 'Hello, world!' as string where 'Hello, world!' in (?, ?, ?, ?, ?, ?, ?)";

iamjpotts avatar Aug 26 '25 12:08 iamjpotts

CI is now failing due to a conflict with main as https://github.com/launchbadge/sqlx/pull/3859 was merged yesterday.

Does #3859 supersede this?

Edit: i've had to revert one of the impls from #3859 for compatibility

JosiahParry avatar Aug 26 '25 14:08 JosiahParry

Was the Cargo.toml file reformatting intentional?

iamjpotts avatar Aug 26 '25 15:08 iamjpotts

Was the Cargo.toml file reformatting intentional?

@iamjpotts nope! Zed formatting my apologies. I'll revert.

JosiahParry avatar Aug 26 '25 15:08 JosiahParry

Reverted and showing no change! Thanks everyone for your help and patience:)

JosiahParry avatar Aug 26 '25 15:08 JosiahParry

Please let me know if any other changes are needed for this pr! Thanks :)

JosiahParry avatar Aug 28 '25 17:08 JosiahParry

Please let me know if any other changes are needed for this pr! Thanks :)

@josiahparry

There are now some minor code conflicts due to recent changes to simplify arguments - removal of the lifetime parameter on the Arguments trait and some related changes.

S/b easy fix, mostly lifetime parameter deletion.

iamjpotts avatar Sep 14 '25 16:09 iamjpotts

Synced with main! There is one change that had to be made to the Cargo.toml. I've added a comment and hope it was clear.

If there are suggested alternative approaches I'm happy to make the change!

JosiahParry avatar Sep 24 '25 22:09 JosiahParry

Please let me know if any further action is needed for this PR! Thanks :)

JosiahParry avatar Oct 01 '25 13:10 JosiahParry

@iamjpotts please let me know if the changes to address Arguments changes in main look good for y'all!

JosiahParry avatar Oct 06 '25 19:10 JosiahParry

@iamjpotts please let me know if the changes to address Arguments changes in main look good for y'all!

You're good from an Arguments perspective.

Though I can't speak for @abonander I did skim thru the current version of the changes and nothing jumped out at me for adding feedback on.

I did see your question about the json feature on the cli crate, but that's a @abonander question.

iamjpotts avatar Oct 07 '25 00:10 iamjpotts

@abonander please let me know what other changes are needed to the PR :)

JosiahParry avatar Oct 10 '25 17:10 JosiahParry

I wouldn't ping Bonander. The pr does have visibility to him.

If you need to incorporate this change right away into some project you're working on, it's possible to configure your own Cargo.toml to point to your branch on your fork instead of pointing to a version on crates.io.

iamjpotts avatar Oct 10 '25 22:10 iamjpotts