lua-users home
lua-l archive

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


Hi,

I have tried to implement something like an adapter pattern[1] with
Lua and I am not sure I did it correctly.

I am writing a wrapper to make working with LuaSQL more efficient. My
wrapper supports functions like prepare, execute, getOne, getAll,
getAssoc, limitQuery, etc. As you know, LuaSQL supports multiple
database drivers and so does my wrapper.

So I have a base module 'connection' and the specialized adapters
modules 'mysql', 'sqlite', ...
And here is what my connection initialization function looks like:

module('connection', package.seeall)

function new(driver)
  local db = {
    driver = driver -- luasql driver type
  }
  -- loads the SQL adapter for the given driver
  local adapter = require("adapter." .. driver)
  for k, v in pairs(adapter) do
    if k ~= "_M" then
      db[k] = v
    end
  end

  setmetatable(db, {__index = _M})
  return db
end

It loads the adapter and steals everything in it, and adds that to the
db table...
Is this considered ok or harmful ?
Any other ideas on how to solve my needs for an "adapter" pattern
implementation ?

Thanks,

[1] http://en.wikipedia.org/wiki/Adapter_pattern
-----
Bertrand Mansion
Mamasam