SOCI / soci

Official repository of the SOCI - The C++ Database Access Library
http://soci.sourceforge.net/
Boost Software License 1.0
1.37k stars 472 forks source link

Reported datatype can be wrong (using SQLite) #1033

Open Krzmbrzl opened 1 year ago

Krzmbrzl commented 1 year ago

The datatype that soci reports for a given entry in our query can be completely wrong, when using SQLite. Based on the code (and in accordance to my experience) https://github.com/SOCI/soci/blob/d7af3212ef3ef1a0a367d3879e7fdc7d43314bfe/src/backends/sqlite3/statement.cpp#L484-L489 the reported type is that of the column the fetched value has originated from. This is okay, if the value is fetched as-is (though there is still the issue that SQLite doesn't really have proper types - e.g. it will probably be impossible to figure out that a given column was a date). However, if the queried value has been transformed (e.g. via a cast), then the reported type can end up being completely wrong.

Example:

CREATE TABLE my_table(my_col DATE);
INSERT INTO my_table(my_col) VALUES ("2002-06-23");

SELECT CAST(strftime('%1', my_col) AS INTEGER) FROM my_table;

soci will think that the fetched value is of type date, whereas in reality the type is integer. However, when asked SQLite will even report the correct type in this particular case.

Fiddle

Krzmbrzl commented 1 year ago

@vadz this issue is not actually solved by my commit. I only encountered the timestamp mismatch while skimming through the related code parts. For this to be fixed, I guess we'd somehow have to leverage SQLite's typeof capability programmatically through the API in order for SQLite to at least tell us integers, reals and text apart. Maybe it is enough to perform the check that in the current code is only used as a fallback, first and only if that returns text, would we move on to the column-type lookup :thinking:

vadz commented 1 year ago

Oh sorry for misunderstanding.