lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On 2015-12-23, at 9:20 AM, Marc Balmer <marc@msys.ch> wrote:
> 
> I am currently working on making the Lua PostgreSQL interface more user friendly, more Lua-ish, and I would appreciate comments...
> 
> The iterator sets a metatable on the returned tuple to define the __index metamethod for direct field access, so it can be further simplified:
> 
> 	p.acct = {}
> 	for tuple, row in  res:tuples() do
> 		p.acct[row] = {
> 			account_nr = tuple.account_nr,
> 			account = tuple.account,
> 			class_nr = tuple.class_nr,
> 			group_nr = tuple.group_nr,
> 			subgroup_nr = tuple.subgroup_nr,
> 			class = tuple.class,
> 			group = tuple.group,
> 			subgroup = tuple.subgroup
> 		}
> 	end
> 
> In the last step I added the __call metamethod to result set, to create the nested table structure in one single call
> 
> 	p.acct = res()

Doesn't make much difference, I think. A lot of uses would just use the tuples() like a cursor.

If row always starts from 1, users can do something like this generic function to get an array:

function list_from_iterator()
    local l = {}
    for v in iterator do
        l[#l+1] = v
    end
    return l
end

p.accts = list_from_iterator(res:tuples())

People seem to treat Lua code as less convenient than C code though....

Jay