mwild1 / luadbi

Multi-backend SQL database library for Lua
MIT License
38 stars 14 forks source link

colnames #1

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Hi Neil, this is a feature request. It would be nice to have access to column 
names after a select. 
LuaSQL provides a colnames function IIRC. This is handy for dealing with 
metadata.

Not related, but in the MySQL statement.c, there is this code:

    if (!statement->metadata) {
    luaL_error(L, DBI_ERR_FETCH_NO_EXECUTE);
    return 0;
    }

    if (!statement->metadata) {
    lua_pushnil(L);
    return 1;
    }

I am not sure the second test is needed.

Original issue reported on code.google.com by golg...@gmail.com on 7 Mar 2009 at 8:15

GoogleCodeExporter commented 8 years ago
Hi golgote, you can retrieve column names from a select by passing true as a
parameter to the 'fetch' or 'rows' method of a statement handle. By default 
'fetch'
or 'rows' returns a numerically index table (AKA an array), but by passing true 
to
these methods the result is return as a table indexed by column names.

See the examples on http://code.google.com/p/luadbi/wiki/DBDDriverStatement for 
more
details.

Original comment by nr...@ii.net on 5 Apr 2009 at 5:14

GoogleCodeExporter commented 8 years ago
Yes, I noticed that, but columns in the table can be in any order (since that's 
the way tables work in Lua). To 
build specific functions like "getAssoc" (and some others for my ORM), I need 
to know in which order the 
columns were fetched, that's why a function to get the column names is useful. 
Thanks for your reply :)

Original comment by golg...@gmail.com on 6 Apr 2009 at 10:26

GoogleCodeExporter commented 8 years ago
I've had a look at the source code, and the functionality should be relatively 
easy
to add. The implementation will look something like:

local sth = dbh:prepare('SELECT id,num,timestamp FROM sometable')
sth:execute()
-- fields will be equal to {'id','num','timestamp'}
local fields = sth:columns()

I'll look at adding this in the next week.

Original comment by nr...@ii.net on 9 Apr 2009 at 12:48

GoogleCodeExporter commented 8 years ago
I've committed changes to implement the 'columns' method for statement handles 
into
SVN. I'll be uploading a new release source package shortly. 

Original comment by nr...@ii.net on 17 Apr 2009 at 11:49

GoogleCodeExporter commented 8 years ago
Thank you Neil :)

Original comment by golg...@gmail.com on 19 Apr 2009 at 8:45