lua-users home
lua-l archive

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


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)

-- START CODE
function setIndex(tbl, key, val)
  print ('setIndex: tbl: ' .. tbl .. ' key: ' .. key .. ' val: ' .. val);
end
function deleteIndex(tbl, key)
  print ('deleteIndex: tbl: ' .. tbl .. ' key: ' .. key);
end
function updateIndex(tbl, key, old, new)
  print ('updateIndex: tbl: ' .. tbl .. ' key: ' .. key .. ' old: ' .. old ..
                     ' new: ' .. new);
end
function createLuaObjectColumn(n)
  local cmd =
    n .. '      = {}; ' ..
    n .. '_Iel  = { }; ' ..
    n .. '_Stbl = { }; ' ..
    'function ' .. n .. '_setter(tbl, k, v) ' ..
    '  if (' .. n .. '_Iel[k] ~= nil) then ' ..
    '    if (' .. n .. '_Stbl[k] == nil) then ' ..
    '      setIndex(\'' .. n .. '\',k,v); ' ..
    '    else ' ..
    '      if (v == nil) then ' ..
    '        deleteIndex(\'' .. n .. '\',k); ' ..
    '      else ' ..
    '        updateIndex(\'' .. n .. '\',k,' .. n .. '_Stbl[k],v); ' ..
    '      end ' ..
    '    end ' ..
    '  end ' ..
    '  rawset(' .. n .. '_Stbl, k, v);' ..
    'end'; --print (cmd);
  assert(loadstring(cmd))()
  cmd = 'setmetatable(a, {__index='    .. n .. '_Stbl, ' ..
                         '__newindex=' .. n .. '_setter});'; --print (cmd);
  assert(loadstring(cmd))()
  return a;
end
function addIndexToLuaObjectColumn(n, i)
  local cmd =
    'if (' .. n .. '_Stbl.' .. i .. ' ~= nil) then ' ..
    '  setIndex(\'' .. n .. '\',\'' .. i .. '\',' .. n .. '_Stbl.' .. i .. '); ' ..
    'end ' ..
    n .. '_Iel.' .. i .. ' = true;' --print (cmd);
  assert(loadstring(cmd))()
end

a=createLuaObjectColumn("a");
addIndexToLuaObjectColumn("a", "x");

a.x=44;  -- SET
a.x=999; -- UPDATE
a.x=nil; -- DELETE

print ('NOOP: Set NonIndexed Field');
a.y=22;

print('addIndexToLuaObjectColumn("a", "y")');
addIndexToLuaObjectColumn("a", "y"); -- will AddIndexSet
print ('SET INDEXED Field');
a.y=888;

-- END CODE