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
- fix: use bundle identifier in dbus path and name (#10) — committed to tauri-apps/plugins-workspace by lucasfernog 2 years ago
- Merge pull request #10 from tauri-apps/renovate/tokio-1.x — committed to tauri-apps/plugins-workspace by lucasfernog 3 years ago
This is the case with sqlite3 as well
select 1 as fooreturns[{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_inforeturned from the database doesn’t match anything in this very short list of database types, you’ll get anullvalue 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:
A couple of other notes:
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
nullbeing 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.