lunarmodules / luasql

LuaSQL is a simple interface from Lua to a DBMS.
http://lunarmodules.github.io/luasql
539 stars 191 forks source link

Handle boolean fields correctly in Postgres #17

Closed RenaKunisaki closed 8 years ago

RenaKunisaki commented 10 years ago

LuaSQL doesn't convert boolean fields to Lua boolean values when reading from a Postgres database but instead leaves them as strings "t" or "f". This makes for awkward code because both of those values evaluate as true in Lua.

It's not safe either to loop through the results and convert "f" to false because there can be string columns with the value "f". The only safe way to handle boolean columns is to query the column types for every table you select from, then loop through the results and convert "f" to false only for boolean columns. This is a lot of extra work to make types map natively like they ought to...

tomasguisasola commented 10 years ago
Hi Rena

This is the expected behaviour.
Each LuaSQL driver "decides" whether to convert the values or not.

In the case of Postgres, all data are provided as strings by the client interface and converted by the programmer if necessary. The driver does the same, which favors performance. The only database type converted to Lua is NULL, which became nil. But there are other reasons: there are no standard way to convert all types from the database to Lua (what to do with a date type?); many times the programmer needs a string (to store in a data file or to produce output), thus two conversions would be done; and in most circumstances, the programmer knows the type of the column, then it doesn't matter if the test is against the boolean true (or false) or the string 't' (or 'f').

Regards,
    Tomás

On Fri, 9 May 2014, Rena Kunisaki wrote:

LuaSQL doesn't convert boolean fields to Lua boolean values when reading from a Postgres database but instead leaves them as strings "t" or "f". This makes for awkward code because both of those values evaluate as true in Lua.

It's not safe either to loop through the results and convert "f" to false because there can be string columns with the value "f". The only safe way to handle boolean columns is to query the column types for every table you select from, then loop through the results and convert "f" to false only for boolean columns. This is a lot of extra work to make types map natively like they ought to...


Reply to this email directly or view it on GitHub: https://github.com/keplerproject/luasql/issues/17