lua-users home
lua-l archive

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


Hello,
       I have made this lua module that I wanted to share with anyone who is interested.

http://www.amved.com/milindsweb/tableDB

The module as the name suggest is a mapping of Lua tables to a database, specifically sqlite as of now, but extending it to other databases should be easy. The module only requires luasql sqlite3.

The module works as follows:
1. Module creates a database by taking in a template table (which is just a sample of the Lua table you want to use). For example a recursive table shown below:

templateT = {
            Name = "name",
            Title = "title",
            TrialPeriod = true,
            NumOfReportees = 4
}
templateT[1] = templateT    -- 1st reporting employee table
templateT[2] = templateT    --  2nd reporting employee table
templateT[3] = templateT    --  3rd reporting employee table
templateT[4] = templateT    --  4th reporting employee table

------------------------------------------------------
tdb = require("tableDB")
emp = tdb.createDB("testdb2",templateT,true)
2.Now with the database created the module returns a table object (emp) which can be used as a normal Lua table and the module takes care of all the backend data mapping.

emp.Name = "ABC"
emp.Title = "CEO"
emp.TrialPeriod = false
emp.NumOfReportees = 2

emp[1] = {
		Name = "DEF",
		Title = "VP",
		TrialPeriod = false,
		NumOfReportees = 1
}
emp[1][1] = {
		Name = "GHI",
		Title = "Engineer",
		TrialPeriod = false,
		NumOfReportees = 0
}
emp[2] = {
		Name = "JKL",
		Title = "VP",
		TrialPeriod = false,
		NumOfReportees = 1
}
emp[2][1] = {
		Name = "MNO",
		Title = "Engineer",
		TrialPeriod = true,
		NumOfReportees = 0
}

print(emp[2][1].Name)	-- Would print MNO
-- Lets add something not defined in the original template
emp[2].Address = "123 Fortune Street"

3. In the end just do a save and the data is saved to the database

tdb.saveDB(emp) -- This saves all the data into the database
tdb.closeDB(emp) -- close the file


For more information and downloading the module see the link above.

Regards,
Milind