lua-users home
lua-l archive

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


Hello,

Here is a little functable [1] wrapper around LuaSQL [2]. Just because :o)

http://dev.alt.textdrive.com/browser/HTTP/DB.lua

Usage example:

local DB = require( 'DB' )

local aDB = DB( 'mysql://localhost/test' )

aDB( 'drop table contact' )
aDB( 'create table contact( name varchar( 32 ), email varchar( 32 ) )' )

local someContacts = {
    { name = 'Jose das Couves', email = 'jose@couves.com' },
    { name = 'Manoel Joaquim', email = 'manoel.joaquim@cafundo.com' },
    { name = 'Maria das Dores', email = 'maria@dores.com' }
}

for _, aContact in ipairs( someContacts ) do
aDB( 'insert into contact value ( %s, %s )', aContact.name, aContact.email )
end

for aContact in aDB( 'select name, email from contact' ) do
    print( aContact.name, aContact.email )
end

> Jose das Couves jose@couves.com
> Manoel Joaquim  manoel.joaquim@cafundo.com
> Maria das Dores maria@dores.com

Main differences from plain LuaSQL:

- It's a functable! 8^)
- The environment and connection are abstracted behind a URL syntax
- Pseudo prepared statement
- The cursor is an iterator

Thanks to Rici Lake for providing the original inspiration for those funky FuncTables!

Cheers,

PA.

[1] http://lua-users.org/wiki/FuncTables
[2] http://www.keplerproject.org/luasql/