lua-users home
lua-l archive

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


On 1/17/12, Jak Sprats <jaksprats@gmail.com> wrote:
> So here is a "semi" finished solution for my relational table triggering
> (just for the record, in case someone peruses these logs later)
>
> I used some Lua eval's because I am not clever enough to do it any other
> way. I am not worried about the eval's performance, because such action are
> only done on "CREATE INDEX" operations (which dont happen much)
>
> ...

You do not need 'eval' at all.

I believe you want someting like this:

    function createLuaObjectColumn(n)
      local n = {}
      local n_Iel  = {}
      local n_Stbl = {}

      local function n_setter(tbl,k,v)
        if not n_Iel[k] and n_Stbl[k] then
          setIndex(n,k,v)
        else
          if v == nil then
            deleteIndex(n,k)
          else
            updateIndex(n,k,n_Stbl[k],v)
          end
        end
        rawset(n_Stbl,k,v)
      end
      setmetatable(n,{
        __index    = n_Stbl;
        __newindex = n_setter;
      })
      return n
    end

I'm not sure what is the point of "a_Iel". In your code you're
creating it as a global. If that is what you want, you can replace
your line 14 with:

    _G[n .. '_Iel'] = {} -- in Lua 5.1

or

    _ENV[n .. '_Iel'] = {} -- in Lua 5.2

Also, the name 'a', is hardcoded in lines 31 and 32, you should be using 'n'.

Also also, when you do need to use loadstring(), you should consider
using string.format() instead of all the string concatenation.