plugins-workspace: [sql] Number returns as null

When I do such request:

await db.select('SELECT COUNT(*) AS "count" FROM (SELECT * FROM "notes")');

The result is:

[{"count":null}]

I use #release branch

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 8
  • Comments: 19 (9 by maintainers)

Commits related to this issue

Most upvoted comments

This is the case with sqlite3 as well

select 1 as foo returns [{foo: null}].

Ok, i’ve pushed the feature branch

So, I dug into this a little bit. This bit of code that attempts to serialize types is likely very wrong. https://github.com/tauri-apps/tauri-plugin-sql/blob/dev/src/plugin.rs#L253

If the type_info returned from the database doesn’t match anything in this very short list of database types, you’ll get a null value back because the lib isn’t able to figure out how to serialize.

If you go check out the sqlx postgres types, you’ll see an exhaustive list https://github.com/launchbadge/sqlx/blob/061fdcabd72896d9bc3abb4ea4af6712a04bc0a8/sqlx-core/src/postgres/type_info.rs#L451

I don’t know Rust well enough to make an update, but I would hope there’s either:

  1. A way use types from upstreams like sqlx and infer and convert that way
  2. A way to use generics and convert

A couple of other notes:

  1. The broken serialization step seems like a huge performance tax to pay just to get data into TS land.
  2. This repo is misleading in the way that it presents itself as working. There needs to be a disclaimer at the top of the README.md in my opinion
  3. It would be great to have tests demonstrating some sort of functionality / stability.

we’re not checking the declared type. column.type_info() is supposed to report back the real type (affinity), at least if i understood the inner workings of sqlx correctly…

I do agree that not checking the type would be better but i didn’t find a way to do that with sqlx. This would also make it easier to support functions (like COUNT(*) from the issue description).

Edit: Actuallyyy, i just noticed that one can use try_get_raw (i tried that once but made stupid mistakes) which should allow us to check the type of the actual value instead of the column before converting it to serde_json::Value.

Edit2: Yep, looks like it works. going to work on a PR. sorry that this was unresolved for so long.

Note, this feature branch’s more explicity separation of serialization / type mapping into Rust is a step in the right direction but it still doesn’t solve the various issues people are having with null being returned.

Yeah i was noticing this when I looked at this issue and I did make this “better” in the last release I put out but I think this may need to go further. I will try and find a moment over the weekend.

Note: if anyone else any special insight I’m all ears. I inherited this Rust code and don’t have a real background in sqlx. I’d have thought the Sqlx to Rust type bindings would be automatic and from there if we need TS types we can use the rsts crate.