lunarmodules / luasql

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

Decimal Number returns as string in lua #162

Closed klavanya19 closed 7 months ago

klavanya19 commented 7 months ago

Hi,

I am using luarocks and fetching data from mysql. And have a column with data type as Decimal(12). When we connect to mySQL using luasql library (require "luasql.mysql"), the field type returns as "undefined" and the value fetches in string format "12.34"

P.S: Doesn't want to use typecasting. Any other way other than converting the every row value to tonumber().

Any help here appreciated.

tomasguisasola commented 7 months ago

As stated in the manual (https://lunarmodules.github.io/luasql/manual.html#cur_fetch):

"There is no guarantee about the types of the results: they may or may not be converted to adequate Lua types by the driver. In the current implementation, the PostgreSQL and MySQL drivers return all values as strings while the ODBC and Oracle drivers convert them to Lua types."

Note that there is no "undefined" type in Lua; maybe you are mixing systems/languages.

The above policy is a design decision. It seems that some systems will have to convert values to an appropriate Lua type, while others don't (Web systems, for instance, most of the time produce HTML (text) pages). So each driver implements what have less cost. As stated in the manual, PostgreSQL and MySQL return values as strings because that is what the DBMS API provides. If your Lua program nees a number, it will have to convert it when necessary; when your program needs to retrieve a value just to insert it again in the database, it won't have to convert it.