lua-users home
lua-l archive

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


Hi Viacheslav

As one of the maintainers of LuaSQL, I have to explain two characteristics of LuaSQL drivers with respect to this thread.

Observe that there is only one local variable. The rest are globals.
Yes, this was by choice: the example was developed with the command line interpreter in mind.  That code was not supposed to be copied as a pattern.  Anyway, I'll try to improve it or at least to make it clear.
 
function rows (connection, sql_statement)
  local cursor = assert (connection:execute (sql_statement))
  return function ()
    return cursor:fetch()
  end
end

The variable is local; but it is an upvalue, too, so it can only be collected when the closure is finalised, which means the return value of rows() needs to be a scoped variable.
I think all LuaSQL drivers were carefully written to release the resources when the cursor reach the end of the result set.
 
But here is how it is supposed to be used:

env = assert (require"luasql.mysql".mysql())
con = assert (env:connect"my_db")
for id, name, address in rows (con, "select * from contacts") do
  print (string.format ("%s: %s", name, address))
end
Unless there is an error during the for-loop, the resources will be release at the end regardless of the garbage collector.

Note that I am just pointing out some details in the "defense" of LuaSQL.  I not against your argument in general.

Regards,
Tomás