[Kepler-Project] LuaSQL usage patterns?
Petite Abeille
petite.abeille at gmail.com
Fri Apr 25 16:06:46 GMT+2 2008
Hello,
Out of curiosity, how are people using LuaSQL?
Are you using it 'as is'?
Wrap it in your own module?
Hide it behind some sort of ORM?
Something else altogether?
After multitudinous experimentations, I have settled on the following
usage pattern:
(1) Lightweight LuaSQL wrapper to make the library fit into an
existing code base
This is mostly for consistency sake as it just wrap LuaSQL's
functionalities behind a couple of metamethods packaged as a module,
e.g.:
local DB = require( 'DB' )
local aDB = DB( 'sqlite3://localhost/test.db' )
for aContact in aDB( 'select * from contact' ) do
print( aContact.name, aContact.email )
end
http://dev.alt.textdrive.com/browser/HTTP/DB.lua
(2) Use of named DML
Instead of mixing SQL and Lua, the SQL is defined separately and
referred to by name, e.g.:
aDB( DML[ 'InsertDocument' ], aDocument )
http://dev.alt.textdrive.com/browser/HTTP/Finder.ddl
http://dev.alt.textdrive.com/browser/HTTP/Finder.dml
(3) Use of template to generate complex DML
For complex, dynamically build DML, e.g.:
for anIndex, aToken in anIterator do
local aTokenTemplate = aTemplate[ 'tokens' ]
if aCount == 0 then
aTokenTemplate[ 'union' ] = nil
else
aTokenTemplate[ 'union' ] = 'union all'
end
http://dev.alt.textdrive.com/browser/HTTP/Finder.lua#L119
http://dev.alt.textdrive.com/browser/HTTP/Finder.dml#L70
How are you using LuaSQL?
Cheers,
--
PA.
http://alt.textdrive.com/nanoki/
More information about the Kepler-Project
mailing list