LuaSQLite3

Check-in [01d436858c]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:working toward Lua 5.2 compatibility
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 01d436858c06e6c449acea74e0204ad09b4f1516
User & Date: e 2011-11-10 16:08:16
References
2013-03-29
15:44 Fixed ticket [b643d638b6]: Don't use deprecated luaL_openlib() plus 5 other changes artifact: e1a87b04dc user: e
Context
2013-03-30
15:11
Add update_hook. Bump rockspec version. Builds with Lua 5.1.5 and 5.2.2 on OSX. check-in: 0d6edd6bd5 user: e tags: trunk
2011-11-10
16:08
working toward Lua 5.2 compatibility check-in: 01d436858c user: e tags: trunk
2011-02-22
01:33
Make the whole documentation file nowiki. check-in: 1569f456f7 user: e tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to Makefile.

     1      1   # Makefile for lsqlite3 library for Lua
     2      2   
     3         -ifneq "$(shell pkg-config --version)" ""
            3  +ifneq "$(shell pkg-config --version)" "0.25"
     4      4     # automagic setup (OS X fink, Linux apt-get, ..)
     5      5     #
     6      6     LUAINC= $(shell pkg-config --cflags lua)
     7      7     LUALIB= $(shell pkg-config --libs lua)
     8      8     SQLITE3INC= $(shell pkg-config --cflags sqlite3)
     9      9     SQLITE3LIB= $(shell pkg-config --libs sqlite3)
    10     10   else
    11     11     # manual setup (change these to reflect your Lua installation)
    12     12     #
    13     13     BASE= /usr/local
    14         -  LUAINC= -I$(BASE)/include
           14  +  LUAINC= -I/Users/e/Dev/unzipped/lua-5.2.0-beta/install/include
    15     15     LUALIB=
    16     16     SQLITE3INC= -I$(BASE)/include
    17     17     SQLITE3LIB= -L$(BASE)/lib -lsqlite3
    18     18   #  Windows' LUALIB is the same as the Lua executable's directory...
    19     19   #  LUALIB= -L$(BASE)/bin -llua51
    20     20     #
    21     21     POD2HTML= perl -x -S doc/pod2html.pl

Changes to lsqlite3.c.

    30     30   #include <string.h>
    31     31   #include <assert.h>
    32     32   
    33     33   #define LUA_LIB
    34     34   #include "lua.h"
    35     35   #include "lauxlib.h"
    36     36   
           37  +// Lua 5.2
           38  +//
           39  +#define lua_strlen lua_rawlen
           40  +// luaL_typerror always used with arg at ndx == NULL
           41  +#define luaL_typerror(L,ndx,str) luaL_error(L,"bad argument %d (%s expected, got nil)",ndx,str)
           42  +// luaL_register used once, so below expansion is OK for this case
           43  +#define luaL_register(L,name,reg) lua_newtable(L);luaL_setfuncs(L,reg,0)
           44  +// luaL_openlib always used with name == NULL
           45  +#define luaL_openlib(L,name,reg,nup) luaL_setfuncs(L,reg,nup)
           46  +
    37     47   #include "sqlite3.h"
    38     48   
    39     49   /* compile time features */
    40     50   #if !defined(SQLITE_OMIT_PROGRESS_CALLBACK)
    41     51       #define SQLITE_OMIT_PROGRESS_CALLBACK 0
    42     52   #endif
    43     53   
................................................................................
  1723   1733   
  1724   1734       /* terminator */
  1725   1735       { NULL, 0 }
  1726   1736   };
  1727   1737   
  1728   1738   /* ======================================================= */
  1729   1739   
  1730         -static const luaL_reg dblib[] = {
         1740  +static const luaL_Reg dblib[] = {
  1731   1741       {"isopen",              db_isopen               },
  1732   1742       {"last_insert_rowid",   db_last_insert_rowid    },
  1733   1743       {"changes",             db_changes              },
  1734   1744       {"total_changes",       db_total_changes        },
  1735   1745       {"errcode",             db_errcode              },
  1736   1746       {"error_code",          db_errcode              },
  1737   1747       {"errmsg",              db_errmsg               },
................................................................................
  1759   1769   
  1760   1770       {"__tostring",          db_tostring             },
  1761   1771       {"__gc",                db_gc                   },
  1762   1772   
  1763   1773       {NULL, NULL}
  1764   1774   };
  1765   1775   
  1766         -static const luaL_reg vmlib[] = {
         1776  +static const luaL_Reg vmlib[] = {
  1767   1777       {"isopen",              dbvm_isopen             },
  1768   1778   
  1769   1779       {"step",                dbvm_step               },
  1770   1780       {"reset",               dbvm_reset              },
  1771   1781       {"finalize",            dbvm_finalize           },
  1772   1782   
  1773   1783       {"columns",             dbvm_columns            },
................................................................................
  1805   1815   
  1806   1816       {"__tostring",          dbvm_tostring           },
  1807   1817       {"__gc",                dbvm_gc                 },
  1808   1818   
  1809   1819       { NULL, NULL }
  1810   1820   };
  1811   1821   
  1812         -static const luaL_reg ctxlib[] = {
         1822  +static const luaL_Reg ctxlib[] = {
  1813   1823       {"user_data",               lcontext_user_data              },
  1814   1824   
  1815   1825       {"get_aggregate_data",      lcontext_get_aggregate_context  },
  1816   1826       {"set_aggregate_data",      lcontext_set_aggregate_context  },
  1817   1827       {"aggregate_count",         lcontext_aggregate_count        },
  1818   1828   
  1819   1829       {"result",                  lcontext_result                 },
................................................................................
  1825   1835       {"result_blob",             lcontext_result_blob            },
  1826   1836       {"result_error",            lcontext_result_error           },
  1827   1837   
  1828   1838       {"__tostring",              lcontext_tostring               },
  1829   1839       {NULL, NULL}
  1830   1840   };
  1831   1841   
  1832         -static const luaL_reg sqlitelib[] = {
         1842  +static const luaL_Reg sqlitelib[] = {
  1833   1843       {"version",         lsqlite_version         },
  1834   1844       {"complete",        lsqlite_complete        },
  1835   1845   #ifndef WIN32
  1836   1846       {"temp_directory",  lsqlite_temp_directory  },
  1837   1847   #endif
  1838   1848       {"open",            lsqlite_open            },
  1839   1849       {"open_memory",     lsqlite_open_memory     },
  1840   1850   
  1841   1851       {"__newindex",      lsqlite_newindex        },
  1842   1852       {NULL, NULL}
  1843   1853   };
  1844   1854   
  1845         -static void create_meta(lua_State *L, const char *name, const luaL_reg *lib) {
         1855  +static void create_meta(lua_State *L, const char *name, const luaL_Reg *lib) {
  1846   1856       luaL_newmetatable(L, name);
  1847   1857       lua_pushstring(L, "__index");
  1848   1858       lua_pushvalue(L, -2);               /* push metatable */
  1849   1859       lua_rawset(L, -3);                  /* metatable.__index = metatable */
  1850   1860   
  1851   1861       /* register metatable functions */
  1852   1862       luaL_openlib(L, NULL, lib, 0);

Changes to lunit.lua.

    33     33   
    34     34   
    35     35   
    36     36   -----------------------
    37     37   -- Intialize package --
    38     38   -----------------------
    39     39   
    40         -local P = { }
    41         -lunit = P
           40  +--e52 local P = { }
           41  +--e52 lunit = P
    42     42   
    43     43   -- Import
    44     44   local type = type
    45     45   local print = print
    46     46   local ipairs = ipairs
    47     47   local pairs = pairs
    48     48   local string = string
................................................................................
    50     50   local pcall = pcall
    51     51   local xpcall = xpcall
    52     52   local traceback = debug.traceback
    53     53   local error = error
    54     54   local setmetatable = setmetatable
    55     55   local rawset = rawset
    56     56   local orig_assert = assert
    57         -local getfenv = getfenv
    58         -local setfenv = setfenv
           57  +--e52 local getfenv = getfenv
           58  +--e52 local setfenv = setfenv
    59     59   local tostring = tostring
    60     60   
    61     61   
    62     62   -- Start package scope
    63         -setfenv(1, P)
           63  +--e52 setfenv(1, P)
    64     64   
    65         -
           65  +--e52 
           66  +local _ENV = {}
    66     67   
    67     68   
    68     69   --------------------------------
    69     70   -- Private data and functions --
    70     71   --------------------------------
    71     72   
    72     73   local run_testcase
................................................................................
   448    449     stats.passed = 0	-- Number of Test passed
   449    450     stats.assertions = 0	-- Number of all assertions made in all Test in all Test Cases
   450    451     
   451    452     --------------------------------
   452    453     -- Count Test Cases and Tests --
   453    454     --------------------------------
   454    455     
   455         -  stats.testcases = table.getn(testcases)
          456  +  --e52 stats.testcases = table.getn(testcases)
          457  +  stats.testcases = #testcases
   456    458     
   457    459     for _, tc in ipairs(testcases) do
   458         -    stats_inc("tests" , table.getn(tc.__lunit_tests))
          460  +    --e52 stats_inc("tests" , table.getn(tc.__lunit_tests))
          461  +    stats_inc("tests" , #tc.__lunit_tests)
   459    462     end
   460    463     
   461    464     ------------------
   462    465     -- Print Header --
   463    466     ------------------
   464    467     
   465    468     print()
................................................................................
   571    574     end
   572    575     
   573    576     ---------------------------------
   574    577     -- Run all Tests on a TestCase --
   575    578     ---------------------------------
   576    579     
   577    580     print()
   578         -  print("#### Running '"..tc.__lunit_name.."' ("..table.getn(tc.__lunit_tests).." Tests)...")
          581  +  --e52 print("#### Running '"..tc.__lunit_name.."' ("..table.getn(tc.__lunit_tests).." Tests)...")
          582  +  print("#### Running '"..tc.__lunit_name.."' ("..#tc.__lunit_tests.." Tests)...")
   579    583     
   580    584     for _, testname in ipairs(tc.__lunit_tests) do
   581    585       if setup(testname) then
   582    586         run(testname)
   583    587         stats_inc("run")
   584    588         teardown(testname)
   585    589       else
................................................................................
   593    597   
   594    598   
   595    599   
   596    600   ---------------------
   597    601   -- Import function --
   598    602   ---------------------
   599    603   
   600         -function import(name)
          604  +
          605  +function import(name, user_env)
   601    606     
   602    607     do_assert(is_string(name), "lunit.import() expects a single string as argument")
   603    608     
   604         -  local user_env = getfenv(2)
          609  +  --e52 local user_env = getfenv(2)
   605    610     
   606    611     --------------------------------------------------
   607    612     -- Installs a specific function in the user env --
   608    613     --------------------------------------------------
   609    614     
   610    615     local function install(funcname)
   611         -    user_env[funcname] = P[funcname]
          616  +    user_env[funcname] = _ENV[funcname]
   612    617     end
   613    618     
   614    619     
   615    620     ----------------------------------------------------------
   616    621     -- Install functions matching a pattern in the user env --
   617    622     ----------------------------------------------------------
   618    623     
   619    624     local function install_pattern(pattern)
   620         -    for funcname, _ in pairs(P) do
          625  +    for funcname, _ in pairs(_ENV) do
   621    626         if string.find(funcname, pattern) then
   622    627           install(funcname)
   623    628         end
   624    629       end
   625    630     end
   626    631     
   627    632     ------------------------------------------------------------
................................................................................
   662    667   
   663    668   
   664    669   
   665    670   --------------------------------------------------
   666    671   -- Installs a private environment on the caller --
   667    672   --------------------------------------------------
   668    673   
   669         -function setprivfenv()
          674  +function makeprivfenv()
   670    675     local new_env = { }
   671         -  local new_env_mt = { __index = getfenv(2) }
          676  +  local new_env_mt = { __index = _ENV }
   672    677     setmetatable(new_env, new_env_mt)
   673         -  setfenv(2, new_env)
          678  +  return new_env
   674    679   end
   675         -
   676         -
   677    680   
   678    681   
   679    682   --------------------------------------------------
   680    683   -- Increments a counter in the statistics table --  
   681    684   --------------------------------------------------
   682    685   
   683    686   function stats_inc(varname, value)
................................................................................
   684    687     orig_assert(is_table(stats))
   685    688     orig_assert(is_string(varname))
   686    689     orig_assert(is_nil(value) or is_number(value))
   687    690     if not stats[varname] then return end
   688    691     stats[varname] = stats[varname] + (value or 1)
   689    692   end
   690    693   
   691         -
   692         -
          694  +--e52 
          695  +return _ENV
   693    696   

Changes to test.lua.

     1         -require("lsqlite3")
            1  +local sqlite3 = require("lsqlite3")
     2      2   
     3      3   local width = 78
     4      4   local function line(pref, suff)
     5      5       pref = pref or ''
     6      6       suff = suff or ''
     7      7       local len = width - 2 - string.len(pref) - string.len(suff)
     8      8       print(pref .. string.rep('-', len) .. suff)

Changes to tests-sqlite3.lua.

    23     23       CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
    24     24       TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
    25     25       SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    26     26   
    27     27   --]]--------------------------------------------------------------------------
    28     28   
    29     29   
    30         -require "lsqlite3"
           30  +local sqlite3 = require "lsqlite3"
    31     31   
    32         -require "lunit"
           32  +local os = os
    33     33   
    34         -lunit.setprivfenv()
    35         -lunit.import "assertions"
    36         -lunit.import "checks"
           34  +local lunit = require "lunitx"
    37     35   
           36  +local tests_sqlite3
    38     37   
           38  +if _VERSION >= 'Lua 5.2' then 
    39     39   
           40  +    tests_sqlite3 = lunit.module('tests-sqlite3','seeall')
           41  +    _ENV = tests_sqlite3
           42  +    
           43  +else
    40     44   
           45  +    module('tests_sqlite3', lunit.testcase, package.seeall)
           46  +    tests_sqlite3 = _M
           47  +    
           48  +end
           49  +
           50  +-- compat
           51  +
           52  +function lunit_wrap (name, fcn)
           53  +   tests_sqlite3['test_o_'..name] = fcn
           54  +end
           55  +
           56  +function lunit_TestCase (name)
           57  +   return lunit.module(name,'seeall')
           58  +end
    41     59   
    42     60   
    43     61   -------------------------------
    44     62   -- Basic open and close test --
    45     63   -------------------------------
    46     64   
    47         -lunit.wrap("open_memory", function()
           65  +lunit_wrap("open_memory", function()
    48     66     local db = assert_userdata( sqlite3.open_memory() )
    49     67     assert( db:close() )
    50     68   end)
    51     69   
    52         -lunit.wrap("open", function()
           70  +lunit_wrap("open", function()
    53     71     local filename = "/tmp/__lua-sqlite3-20040906135849." .. os.time()
    54     72     local db = assert_userdata( sqlite3.open(filename) )
    55     73     assert( db:close() )
    56     74     os.remove(filename)
    57     75   end)
    58     76   
    59     77   
    60     78   
    61     79   -------------------------------------
    62     80   -- Presence of db member functions --
    63     81   -------------------------------------
    64     82   
    65         -local db_funcs = lunit.TestCase("Database Member Functions")
           83  +local db_funcs = lunit_TestCase("Database Member Functions")
    66     84   
    67         -function db_funcs:setup()
    68         -  self.db = assert( sqlite3.open_memory() )
           85  +function db_funcs.setup()
           86  +  db_funcs.db = assert( sqlite3.open_memory() )
    69     87   end
    70     88   
    71         -function db_funcs:teardown()
    72         -  assert( self.db:close() )
           89  +function db_funcs.teardown()
           90  +  assert( db_funcs.db:close() )
    73     91   end
    74     92   
    75         -function db_funcs:test()
    76         -  local db = self.db
           93  +function db_funcs.test()
           94  +  local db = db_funcs.db
    77     95     assert_function( db.close )
    78     96     assert_function( db.exec )
    79     97   --e  assert_function( db.irows )
    80     98     assert_function( db.rows )
    81     99   --e  assert_function( db.cols )
    82    100   --e  assert_function( db.first_irow )
    83    101   --e  assert_function( db.first_row )
................................................................................
    91    109   
    92    110   
    93    111   
    94    112   ---------------------------------------
    95    113   -- Presence of stmt member functions --
    96    114   ---------------------------------------
    97    115   
    98         -local stmt_funcs = lunit.TestCase("Statement Member Functions")
          116  +local stmt_funcs = lunit_TestCase("Statement Member Functions")
    99    117   
   100         -function stmt_funcs:setup()
   101         -  self.db = assert( sqlite3.open_memory() )
   102         -  self.stmt = assert( self.db:prepare("CREATE TABLE test (id, content)") )
          118  +function stmt_funcs.setup()
          119  +  stmt_funcs.db = assert( sqlite3.open_memory() )
          120  +  stmt_funcs.stmt = assert( stmt_funcs.db:prepare("CREATE TABLE test (id, content)") )
   103    121   end
   104    122   
   105         -function stmt_funcs:teardown()
   106         ---e-  assert( self.stmt:close() )
   107         -  assert( self.stmt:finalize() ) --e+
   108         -  assert( self.db:close() )
          123  +function stmt_funcs.teardown()
          124  +--e-  assert( stmt_funcs.stmt:close() )
          125  +  assert( stmt_funcs.stmt:finalize() ) --e+
          126  +  assert( stmt_funcs.db:close() )
   109    127   end
   110    128   
   111         -function stmt_funcs:test()
   112         -  local stmt = self.stmt
          129  +function stmt_funcs.test()
          130  +  local stmt = stmt_funcs.stmt
   113    131   --e  assert_function( stmt.close )
   114    132     assert_function( stmt.reset )
   115    133   --e  assert_function( stmt.exec )
   116    134     assert_function( stmt.bind )
   117    135   --e  assert_function( stmt.irows )
   118    136   --e  assert_function( stmt.rows )
   119    137   --e  assert_function( stmt.cols )
................................................................................
   156    174   
   157    175   
   158    176   
   159    177   ------------------
   160    178   -- Tests basics --
   161    179   ------------------
   162    180   
   163         -local basics = lunit.TestCase("Basics")
   164         -
   165         -function basics:setup()
   166         -  self.db = assert_userdata( sqlite3.open_memory() )
   167         -end
   168         -
   169         -function basics:teardown()
   170         -  assert_number( self.db:close() )
   171         -end
   172         -
   173         -function basics:create_table()
   174         -  assert_number( self.db:exec("CREATE TABLE test (id, name)") )
   175         -end
   176         -
   177         -function basics:drop_table()
   178         -  assert_number( self.db:exec("DROP TABLE test") )
   179         -end
   180         -
   181         -function basics:insert(id, name)
   182         -  assert_number( self.db:exec("INSERT INTO test VALUES ("..id..", '"..name.."')") )
   183         -end
   184         -
   185         -function basics:update(id, name)
   186         -  assert_number( self.db:exec("UPDATE test SET name = '"..name.."' WHERE id = "..id) )
   187         -end
   188         -
   189         -function basics:test_create_drop()
   190         -  self:create_table()
   191         -  self:drop_table()
   192         -end
   193         -
   194         -function basics:test_multi_create_drop()
   195         -  self:create_table()
   196         -  self:drop_table()
   197         -  self:create_table()
   198         -  self:drop_table()
   199         -end
   200         -
   201         -function basics:test_insert()
   202         -  self:create_table()
   203         -  self:insert(1, "Hello World")
   204         -  self:insert(2, "Hello Lua")
   205         -  self:insert(3, "Hello sqlite3")
   206         -end
   207         -
   208         -function basics:test_update()
   209         -  self:create_table()
   210         -  self:insert(1, "Hello Home")
   211         -  self:insert(2, "Hello Lua")
   212         -  self:update(1, "Hello World")
          181  +local basics = lunit_TestCase("Basics")
          182  +
          183  +function basics.setup()
          184  +  basics.db = assert_userdata( sqlite3.open_memory() )
          185  +end
          186  +
          187  +function basics.teardown()
          188  +  assert_number( basics.db:close() )
          189  +end
          190  +
          191  +function basics.create_table()
          192  +  assert_number( basics.db:exec("CREATE TABLE test (id, name)") )
          193  +end
          194  +
          195  +function basics.drop_table()
          196  +  assert_number( basics.db:exec("DROP TABLE test") )
          197  +end
          198  +
          199  +function basics.insert(id, name)
          200  +  assert_number( basics.db:exec("INSERT INTO test VALUES ("..id..", '"..name.."')") )
          201  +end
          202  +
          203  +function basics.update(id, name)
          204  +  assert_number( basics.db:exec("UPDATE test SET name = '"..name.."' WHERE id = "..id) )
          205  +end
          206  +
          207  +function basics.test_create_drop()
          208  +  basics.create_table()
          209  +  basics.drop_table()
          210  +end
          211  +
          212  +function basics.test_multi_create_drop()
          213  +  basics.create_table()
          214  +  basics.drop_table()
          215  +  basics.create_table()
          216  +  basics.drop_table()
          217  +end
          218  +
          219  +function basics.test_insert()
          220  +  basics.create_table()
          221  +  basics.insert(1, "Hello World")
          222  +  basics.insert(2, "Hello Lua")
          223  +  basics.insert(3, "Hello sqlite3")
          224  +end
          225  +
          226  +function basics.test_update()
          227  +  basics.create_table()
          228  +  basics.insert(1, "Hello Home")
          229  +  basics.insert(2, "Hello Lua")
          230  +  basics.update(1, "Hello World")
   213    231   end
   214    232   
   215    233   
   216    234   ---------------------------------
   217    235   -- Statement Column Info Tests --
   218    236   ---------------------------------
   219    237   
   220         -lunit.wrap("Column Info Test", function()
          238  +lunit_wrap("Column Info Test", function()
   221    239     local db = assert_userdata( sqlite3.open_memory() )
   222    240     assert_number( db:exec("CREATE TABLE test (id INTEGER, name TEXT)") )
   223    241     local stmt = assert_userdata( db:prepare("SELECT * FROM test") )
   224    242     
   225    243     assert_equal(2, stmt:columns(), "Wrong number of columns." )
   226    244     
   227    245     local names = assert_table( stmt:get_names() )
................................................................................
   240    258   
   241    259   
   242    260   
   243    261   ---------------------
   244    262   -- Statement Tests --
   245    263   ---------------------
   246    264   
   247         -st = lunit.TestCase("Statement Tests")
          265  +st = lunit_TestCase("Statement Tests")
   248    266   
   249         -function st:setup()
   250         -  self.db = assert( sqlite3.open_memory() )
   251         -  assert_equal( sqlite3.OK, self.db:exec("CREATE TABLE test (id, name)") )
   252         -  assert_equal( sqlite3.OK, self.db:exec("INSERT INTO test VALUES (1, 'Hello World')") )
   253         -  assert_equal( sqlite3.OK, self.db:exec("INSERT INTO test VALUES (2, 'Hello Lua')") )
   254         -  assert_equal( sqlite3.OK, self.db:exec("INSERT INTO test VALUES (3, 'Hello sqlite3')") )
          267  +function st.setup()
          268  +  st.db = assert( sqlite3.open_memory() )
          269  +  assert_equal( sqlite3.OK, st.db:exec("CREATE TABLE test (id, name)") )
          270  +  assert_equal( sqlite3.OK, st.db:exec("INSERT INTO test VALUES (1, 'Hello World')") )
          271  +  assert_equal( sqlite3.OK, st.db:exec("INSERT INTO test VALUES (2, 'Hello Lua')") )
          272  +  assert_equal( sqlite3.OK, st.db:exec("INSERT INTO test VALUES (3, 'Hello sqlite3')") )
   255    273   end
   256    274   
   257         -function st:teardown()
   258         -  assert_equal( sqlite3.OK, self.db:close() )
          275  +function st.teardown()
          276  +  assert_equal( sqlite3.OK, st.db:close() )
   259    277   end
   260    278   
   261         -function st:check_content(expected)
   262         -  local stmt = assert( self.db:prepare("SELECT * FROM test ORDER BY id") )
          279  +function st.check_content(expected)
          280  +  local stmt = assert( st.db:prepare("SELECT * FROM test ORDER BY id") )
   263    281     local i = 0
   264    282     for row in stmt:rows() do
   265    283       i = i + 1
   266    284       assert( i <= #(expected), "Too many rows." )
   267    285       assert_equal(2, #(row), "Two result column expected.")
   268    286       assert_equal(i, row[1], "Wrong 'id'.")
   269    287       assert_equal(expected[i], row[2], "Wrong 'name'.")
   270    288     end
   271    289     assert_equal( #(expected), i, "Too few rows." )
   272    290     assert_number( stmt:finalize() )
   273    291   end
   274    292   
   275         -function st:test_setup()
   276         -  assert_pass(function() self:check_content{ "Hello World", "Hello Lua", "Hello sqlite3" } end)
   277         -  assert_error(function() self:check_content{ "Hello World", "Hello Lua" } end)
   278         -  assert_error(function() self:check_content{ "Hello World", "Hello Lua", "Hello sqlite3", "To much" } end)
   279         -  assert_error(function() self:check_content{ "Hello World", "Hello Lua", "Wrong" } end)
   280         -  assert_error(function() self:check_content{ "Hello World", "Wrong", "Hello sqlite3" } end)
   281         -  assert_error(function() self:check_content{ "Wrong", "Hello Lua", "Hello sqlite3" } end)
          293  +function st.test_setup()
          294  +  assert_pass(function() st.check_content{ "Hello World", "Hello Lua", "Hello sqlite3" } end)
          295  +  assert_error(function() st.check_content{ "Hello World", "Hello Lua" } end)
          296  +  assert_error(function() st.check_content{ "Hello World", "Hello Lua", "Hello sqlite3", "To much" } end)
          297  +  assert_error(function() st.check_content{ "Hello World", "Hello Lua", "Wrong" } end)
          298  +  assert_error(function() st.check_content{ "Hello World", "Wrong", "Hello sqlite3" } end)
          299  +  assert_error(function() st.check_content{ "Wrong", "Hello Lua", "Hello sqlite3" } end)
   282    300   end
   283    301   
   284         -function st:test_questionmark_args()
   285         -  local stmt = assert_userdata( self.db:prepare("INSERT INTO test VALUES (?, ?)")  )
          302  +function st.test_questionmark_args()
          303  +  local stmt = assert_userdata( st.db:prepare("INSERT INTO test VALUES (?, ?)")  )
   286    304     assert_number( stmt:bind_values(0, "Test") )
   287    305     assert_error(function() stmt:bind_values("To few") end)
   288    306     assert_error(function() stmt:bind_values(0, "Test", "To many") end)
   289    307   end
   290    308   
   291         -function st:test_questionmark()
   292         -  local stmt = assert_userdata( self.db:prepare("INSERT INTO test VALUES (?, ?)")  )
          309  +function st.test_questionmark()
          310  +  local stmt = assert_userdata( st.db:prepare("INSERT INTO test VALUES (?, ?)")  )
   293    311     assert_number( stmt:bind_values(4, "Good morning") )
   294    312     assert_number( stmt:step() )
   295    313     assert_number( stmt:reset() )
   296         -  self:check_content{ "Hello World", "Hello Lua", "Hello sqlite3", "Good morning" }
          314  +  st.check_content{ "Hello World", "Hello Lua", "Hello sqlite3", "Good morning" }
   297    315     assert_number( stmt:bind_values(5, "Foo Bar") )
   298    316     assert_number( stmt:step() )
   299    317     assert_number( stmt:reset() )
   300         -  self:check_content{ "Hello World", "Hello Lua", "Hello sqlite3", "Good morning", "Foo Bar" }
          318  +  st.check_content{ "Hello World", "Hello Lua", "Hello sqlite3", "Good morning", "Foo Bar" }
   301    319     assert_number( stmt:finalize() )
   302    320   end
   303    321   
   304    322   --[===[
   305         -function st:test_questionmark_multi()
   306         -  local stmt = assert_userdata( self.db:prepare([[
          323  +function st.test_questionmark_multi()
          324  +  local stmt = assert_userdata( st.db:prepare([[
   307    325       INSERT INTO test VALUES (?, ?); INSERT INTO test VALUES (?, ?) ]]))
   308    326     assert( stmt:bind_values(5, "Foo Bar", 4, "Good morning") )
   309    327     assert_number( stmt:step() )
   310    328     assert_number( stmt:reset() )
   311         -  self:check_content{ "Hello World", "Hello Lua", "Hello sqlite3", "Good morning", "Foo Bar" }
          329  +  st.check_content{ "Hello World", "Hello Lua", "Hello sqlite3", "Good morning", "Foo Bar" }
   312    330     assert_number( stmt:finalize() )
   313    331   end
   314    332   ]===]
   315    333   
   316         -function st:test_identifiers()
   317         -  local stmt = assert_userdata( self.db:prepare("INSERT INTO test VALUES (:id, :name)")  )
          334  +function st.test_identifiers()
          335  +  local stmt = assert_userdata( st.db:prepare("INSERT INTO test VALUES (:id, :name)")  )
   318    336     assert_number( stmt:bind_values(4, "Good morning") )
   319    337     assert_number( stmt:step() )
   320    338     assert_number( stmt:reset() )
   321         -  self:check_content{ "Hello World", "Hello Lua", "Hello sqlite3", "Good morning" }
          339  +  st.check_content{ "Hello World", "Hello Lua", "Hello sqlite3", "Good morning" }
   322    340     assert_number( stmt:bind_values(5, "Foo Bar") )
   323    341     assert_number( stmt:step() )
   324    342     assert_number( stmt:reset() )
   325         -  self:check_content{ "Hello World", "Hello Lua", "Hello sqlite3", "Good morning", "Foo Bar" }
          343  +  st.check_content{ "Hello World", "Hello Lua", "Hello sqlite3", "Good morning", "Foo Bar" }
   326    344     assert_number( stmt:finalize() )
   327    345   end
   328    346   
   329    347   --[===[
   330         -function st:test_identifiers_multi()
   331         -  local stmt = assert_table( self.db:prepare([[
          348  +function st.test_identifiers_multi()
          349  +  local stmt = assert_table( st.db:prepare([[
   332    350       INSERT INTO test VALUES (:id1, :name1); INSERT INTO test VALUES (:id2, :name2) ]]))
   333    351     assert( stmt:bind_values(5, "Foo Bar", 4, "Good morning") )
   334    352     assert( stmt:exec() )
   335         -  self:check_content{ "Hello World", "Hello Lua", "Hello sqlite3", "Good morning", "Foo Bar" }
          353  +  st.check_content{ "Hello World", "Hello Lua", "Hello sqlite3", "Good morning", "Foo Bar" }
   336    354   end
   337    355   ]===]
   338    356   
   339         -function st:test_identifiers_names()
   340         -  --local stmt = assert_userdata( self.db:prepare({"name", "id"}, "INSERT INTO test VALUES (:id, $name)")  )
   341         -  local stmt = assert_userdata( self.db:prepare("INSERT INTO test VALUES (:id, $name)")  )
          357  +function st.test_identifiers_names()
          358  +  --local stmt = assert_userdata( st.db:prepare({"name", "id"}, "INSERT INTO test VALUES (:id, $name)")  )
          359  +  local stmt = assert_userdata( st.db:prepare("INSERT INTO test VALUES (:id, $name)")  )
   342    360     assert_number( stmt:bind_names({name="Good morning", id=4}) )
   343    361     assert_number( stmt:step() )
   344    362     assert_number( stmt:reset() )
   345         -  self:check_content{ "Hello World", "Hello Lua", "Hello sqlite3", "Good morning" }
          363  +  st.check_content{ "Hello World", "Hello Lua", "Hello sqlite3", "Good morning" }
   346    364     assert_number( stmt:bind_names({name="Foo Bar", id=5}) )
   347    365     assert_number( stmt:step() )
   348    366     assert_number( stmt:reset() )
   349         -  self:check_content{ "Hello World", "Hello Lua", "Hello sqlite3", "Good morning", "Foo Bar" }
          367  +  st.check_content{ "Hello World", "Hello Lua", "Hello sqlite3", "Good morning", "Foo Bar" }
   350    368     assert_number( stmt:finalize() )
   351    369   end
   352    370   
   353    371   --[===[
   354    372   function st:test_identifiers_multi_names()
   355         -  local stmt = assert_table( self.db:prepare( {"name", "id1", "id2"},[[
          373  +  local stmt = assert_table( st.db:prepare( {"name", "id1", "id2"},[[
   356    374       INSERT INTO test VALUES (:id1, $name); INSERT INTO test VALUES ($id2, :name) ]]))
   357    375     assert( stmt:bind_values("Hoho", 4, 5) )
   358    376     assert( stmt:exec() )
   359         -  self:check_content{ "Hello World", "Hello Lua", "Hello sqlite3", "Hoho", "Hoho" }
          377  +  st.check_content{ "Hello World", "Hello Lua", "Hello sqlite3", "Hoho", "Hoho" }
   360    378   end
   361    379   ]===]
   362    380   
   363         -function st:test_colon_identifiers_names()
   364         -  local stmt = assert_userdata( self.db:prepare("INSERT INTO test VALUES (:id, :name)")  )
          381  +function st.test_colon_identifiers_names()
          382  +  local stmt = assert_userdata( st.db:prepare("INSERT INTO test VALUES (:id, :name)")  )
   365    383     assert_number( stmt:bind_names({name="Good morning", id=4}) )
   366    384     assert_number( stmt:step() )
   367    385     assert_number( stmt:reset() )
   368         -  self:check_content{ "Hello World", "Hello Lua", "Hello sqlite3", "Good morning" }
          386  +  st.check_content{ "Hello World", "Hello Lua", "Hello sqlite3", "Good morning" }
   369    387     assert_number( stmt:bind_names({name="Foo Bar", id=5}) )
   370    388     assert_number( stmt:step() )
   371    389     assert_number( stmt:reset() )
   372         -  self:check_content{ "Hello World", "Hello Lua", "Hello sqlite3", "Good morning", "Foo Bar" }
          390  +  st.check_content{ "Hello World", "Hello Lua", "Hello sqlite3", "Good morning", "Foo Bar" }
   373    391     assert_number( stmt:finalize() )
   374    392   end
   375    393   
   376    394   --[===[
   377    395   function st:test_colon_identifiers_multi_names()
   378         -  local stmt = assert_table( self.db:prepare( {":name", ":id1", ":id2"},[[
          396  +  local stmt = assert_table( st.db:prepare( {":name", ":id1", ":id2"},[[
   379    397       INSERT INTO test VALUES (:id1, $name); INSERT INTO test VALUES ($id2, :name) ]]))
   380    398     assert( stmt:bind_values("Hoho", 4, 5) )
   381    399     assert( stmt:exec() )
   382         -  self:check_content{ "Hello World", "Hello Lua", "Hello sqlite3", "Hoho", "Hoho" }
          400  +  st.check_content{ "Hello World", "Hello Lua", "Hello sqlite3", "Hoho", "Hoho" }
   383    401   end
   384    402   
   385    403   
   386         -function st:test_dollar_identifiers_names()
   387         -  local stmt = assert_table( self.db:prepare({"$name", "$id"}, "INSERT INTO test VALUES (:id, $name)")  )
          404  +function st.test_dollar_identifiers_names()
          405  +  local stmt = assert_table( st.db:prepare({"$name", "$id"}, "INSERT INTO test VALUES (:id, $name)")  )
   388    406     assert_table( stmt:bind_values("Good morning", 4) )
   389    407     assert_table( stmt:exec() )
   390         -  self:check_content{ "Hello World", "Hello Lua", "Hello sqlite3", "Good morning" }
          408  +  st.check_content{ "Hello World", "Hello Lua", "Hello sqlite3", "Good morning" }
   391    409     assert_table( stmt:bind_values("Foo Bar", 5) )
   392    410     assert_table( stmt:exec() )
   393         -  self:check_content{ "Hello World", "Hello Lua", "Hello sqlite3", "Good morning", "Foo Bar" }
          411  +  st.check_content{ "Hello World", "Hello Lua", "Hello sqlite3", "Good morning", "Foo Bar" }
   394    412   end
   395    413   
   396         -function st:test_dollar_identifiers_multi_names()
   397         -  local stmt = assert_table( self.db:prepare( {"$name", "$id1", "$id2"},[[
          414  +function st.test_dollar_identifiers_multi_names()
          415  +  local stmt = assert_table( st.db:prepare( {"$name", "$id1", "$id2"},[[
   398    416       INSERT INTO test VALUES (:id1, $name); INSERT INTO test VALUES ($id2, :name) ]]))
   399    417     assert( stmt:bind_values("Hoho", 4, 5) )
   400    418     assert( stmt:exec() )
   401         -  self:check_content{ "Hello World", "Hello Lua", "Hello sqlite3", "Hoho", "Hoho" }
          419  +  st.check_content{ "Hello World", "Hello Lua", "Hello sqlite3", "Hoho", "Hoho" }
   402    420   end
   403    421   ]===]
   404    422   
   405         -function st:test_bind_by_names()
   406         -  local stmt = assert_userdata( self.db:prepare("INSERT INTO test VALUES (:id, :name)")  )
          423  +function st.test_bind_by_names()
          424  +  local stmt = assert_userdata( st.db:prepare("INSERT INTO test VALUES (:id, :name)")  )
   407    425     local args = { }
   408    426     args.id = 5
   409    427     args.name = "Hello girls"
   410    428     assert( stmt:bind_names(args) )
   411    429     assert_number( stmt:step() )
   412    430     assert_number( stmt:reset() )
   413    431     args.id = 4
   414    432     args.name = "Hello boys"
   415    433     assert( stmt:bind_names(args) )
   416    434     assert_number( stmt:step() )
   417    435     assert_number( stmt:reset() )
   418         -  self:check_content{ "Hello World", "Hello Lua", "Hello sqlite3",  "Hello boys", "Hello girls" }
          436  +  st.check_content{ "Hello World", "Hello Lua", "Hello sqlite3",  "Hello boys", "Hello girls" }
   419    437     assert_number( stmt:finalize() )
   420    438   end
   421    439   
   422    440   
   423    441   
   424    442   --------------------------------
   425    443   -- Tests binding of arguments --
   426    444   --------------------------------
   427    445   
   428         -b = lunit.TestCase("Binding Tests")
          446  +b = lunit_TestCase("Binding Tests")
   429    447   
   430         -function b:setup()
   431         -  self.db = assert( sqlite3.open_memory() )
   432         -  assert_number( self.db:exec("CREATE TABLE test (id, name, u, v, w, x, y, z)") )
          448  +function b.setup()
          449  +  b.db = assert( sqlite3.open_memory() )
          450  +  assert_number( b.db:exec("CREATE TABLE test (id, name, u, v, w, x, y, z)") )
          451  +end
          452  +
          453  +function b.teardown()
          454  +  assert_number( b.db:close() )
   433    455   end
   434    456   
   435         -function b:teardown()
   436         -  assert_number( self.db:close() )
   437         -end
   438         -
   439         -function b:test_auto_parameter_names()
   440         -  local stmt = assert_userdata( self.db:prepare("INSERT INTO test VALUES(:a, $b, :a2, :b2, $a, :b, $a3, $b3)") )
          457  +function b.test_auto_parameter_names()
          458  +  local stmt = assert_userdata( b.db:prepare("INSERT INTO test VALUES(:a, $b, :a2, :b2, $a, :b, $a3, $b3)") )
   441    459     local parameters = assert_number( stmt:bind_parameter_count() )
   442    460     assert_equal( 8, parameters )
   443    461     assert_equal( ":a", stmt:bind_parameter_name(1) )
   444    462     assert_equal( "$b", stmt:bind_parameter_name(2) )
   445    463     assert_equal( ":a2", stmt:bind_parameter_name(3) )
   446    464     assert_equal( ":b2", stmt:bind_parameter_name(4) )
   447    465     assert_equal( "$a", stmt:bind_parameter_name(5) )
   448    466     assert_equal( ":b", stmt:bind_parameter_name(6) )
   449    467     assert_equal( "$a3", stmt:bind_parameter_name(7) )
   450    468     assert_equal( "$b3", stmt:bind_parameter_name(8) )
   451    469   end
   452    470   
   453         -function b:test_auto_parameter_names()
   454         -  local stmt = assert_userdata( self.db:prepare("INSERT INTO test VALUES($a, $b, $a2, $b2, $a, $b, $a3, $b3)") )
          471  +function b.test_auto_parameter_names()
          472  +  local stmt = assert_userdata( b.db:prepare("INSERT INTO test VALUES($a, $b, $a2, $b2, $a, $b, $a3, $b3)") )
   455    473     local parameters = assert_number( stmt:bind_parameter_count() )
   456    474     assert_equal( 6, parameters )
   457    475     assert_equal( "$a", stmt:bind_parameter_name(1) )
   458    476     assert_equal( "$b", stmt:bind_parameter_name(2) )
   459    477     assert_equal( "$a2", stmt:bind_parameter_name(3) )
   460    478     assert_equal( "$b2", stmt:bind_parameter_name(4) )
   461    479     assert_equal( "$a3", stmt:bind_parameter_name(5) )
   462    480     assert_equal( "$b3", stmt:bind_parameter_name(6) )
   463    481   end
   464    482   
   465         -function b:test_no_parameter_names_1()
   466         -  local stmt = assert_userdata( self.db:prepare([[ SELECT * FROM test ]]))
          483  +function b.test_no_parameter_names_1()
          484  +  local stmt = assert_userdata( b.db:prepare([[ SELECT * FROM test ]]))
   467    485     local parameters = assert_number( stmt:bind_parameter_count() )
   468    486     assert_equal( 0, (parameters) )
   469    487   end
   470    488   
   471         -function b:test_no_parameter_names_2()
   472         -  local stmt = assert_userdata( self.db:prepare([[ INSERT INTO test VALUES(?, ?, ?, ?, ?, ?, ?, ?) ]]))
          489  +function b.test_no_parameter_names_2()
          490  +  local stmt = assert_userdata( b.db:prepare([[ INSERT INTO test VALUES(?, ?, ?, ?, ?, ?, ?, ?) ]]))
   473    491     local parameters = assert_number( stmt:bind_parameter_count() )
   474    492     assert_equal( 8, (parameters) )
   475    493     assert_nil( stmt:bind_parameter_name(1) )
   476    494   end
   477    495   
   478    496   
   479    497   
................................................................................
   487    505   
   488    506   
   489    507   
   490    508   ----------------------------
   491    509   -- Test for bugs reported --
   492    510   ----------------------------
   493    511   
   494         -bug = lunit.TestCase("Bug-Report Tests")
          512  +bug = lunit_TestCase("Bug-Report Tests")
   495    513   
   496         -function bug:setup()
   497         -  self.db = assert( sqlite3.open_memory() )
          514  +function bug.setup()
          515  +  bug.db = assert( sqlite3.open_memory() )
   498    516   end
   499    517   
   500         -function bug:teardown()
   501         -  assert_number( self.db:close() )
          518  +function bug.teardown()
          519  +  assert_number( bug.db:close() )
   502    520   end
   503    521   
   504    522   --[===[
   505         -function bug:test_1()
   506         -  self.db:exec("CREATE TABLE test (id INTEGER PRIMARY KEY, value TEXT)")
          523  +function bug.test_1()
          524  +  bug.db:exec("CREATE TABLE test (id INTEGER PRIMARY KEY, value TEXT)")
   507    525     
   508         -  local query = assert_userdata( self.db:prepare("SELECT id FROM test WHERE value=?") )
          526  +  local query = assert_userdata( bug.db:prepare("SELECT id FROM test WHERE value=?") )
   509    527     
   510    528     assert_table ( query:bind_values("1") )
   511    529     assert_nil   ( query:first_cols() )
   512    530     assert_table ( query:bind_values("2") )
   513    531     assert_nil   ( query:first_cols() )
   514    532   end
   515    533   ]===]
   516    534   
   517         -function bug:test_nils()   -- appeared in lua-5.1 (holes in arrays)
          535  +function bug.test_nils()   -- appeared in lua-5.1 (holes in arrays)
   518    536     local function check(arg1, arg2, arg3, arg4, arg5)
   519    537       assert_equal(1, arg1)
   520    538       assert_equal(2, arg2)
   521    539       assert_nil(arg3)
   522    540       assert_equal(4, arg4)
   523    541       assert_nil(arg5)
   524    542     end
   525    543     
   526         -  self.db:create_function("test_nils", 5, function(arg1, arg2, arg3, arg4, arg5)
          544  +  bug.db:create_function("test_nils", 5, function(arg1, arg2, arg3, arg4, arg5)
   527    545       check(arg1, arg2, arg3, arg4, arg5)
   528    546     end, {})
   529    547     
   530         -  assert_number( self.db:exec([[ SELECT test_nils(1, 2, NULL, 4, NULL) ]]) )
          548  +  assert_number( bug.db:exec([[ SELECT test_nils(1, 2, NULL, 4, NULL) ]]) )
   531    549     
   532         -  for arg1, arg2, arg3, arg4, arg5 in self.db:urows([[ SELECT 1, 2, NULL, 4, NULL ]])
          550  +  for arg1, arg2, arg3, arg4, arg5 in bug.db:urows([[ SELECT 1, 2, NULL, 4, NULL ]])
   533    551     do check(arg1, arg2, arg3, arg4, arg5) 
   534    552     end
   535    553     
   536         -  for row in self.db:rows([[ SELECT 1, 2, NULL, 4, NULL ]])
          554  +  for row in bug.db:rows([[ SELECT 1, 2, NULL, 4, NULL ]])
   537    555     do assert_table( row ) 
   538    556        check(row[1], row[2], row[3], row[4], row[5])
   539    557     end
   540    558   end
   541    559   
   542    560   ----------------------------
   543    561   -- Test for collation fun --
   544    562   ----------------------------
   545    563   
   546         -colla = lunit.TestCase("Collation Tests")
          564  +colla = lunit_TestCase("Collation Tests")
   547    565   
   548         -function colla:setup()
          566  +function colla.setup()
   549    567       local function collate(s1,s2)
   550    568           -- if p then print("collation callback: ",s1,s2) end
   551    569           s1=s1:lower()
   552    570           s2=s2:lower()
   553    571           if s1==s2 then return 0
   554    572           elseif s1<s2 then return -1
   555    573           else return 1 end
   556    574       end
   557         -    self.db = assert( sqlite3.open_memory() )
   558         -    assert_nil(self.db:create_collation('CINSENS',collate))
   559         -    self.db:exec[[
          575  +    colla.db = assert( sqlite3.open_memory() )
          576  +    assert_nil(colla.db:create_collation('CINSENS',collate))
          577  +    colla.db:exec[[
   560    578         CREATE TABLE test(id INTEGER PRIMARY KEY,content COLLATE CINSENS);
   561    579         INSERT INTO test VALUES(NULL,'hello world');
   562    580         INSERT INTO test VALUES(NULL,'Buenos dias');
   563    581         INSERT INTO test VALUES(NULL,'HELLO WORLD');
   564    582         INSERT INTO test VALUES(NULL,'Guten Tag');
   565    583         INSERT INTO test VALUES(NULL,'HeLlO WoRlD');
   566    584         INSERT INTO test VALUES(NULL,'Bye for now');
   567    585       ]]
   568    586   end
   569    587   
   570         -function colla:teardown()
   571         -  assert_number( self.db:close() )
          588  +function colla.teardown()
          589  +  assert_number( colla.db:close() )
   572    590   end
   573    591   
   574         -function colla:test()
          592  +function colla.test()
   575    593       --for row in db:nrows('SELECT * FROM test') do
   576    594       --  print(row.id,row.content)
   577    595       --end
   578    596       local n = 0
   579         -    for row in self.db:nrows('SELECT * FROM test WHERE content="hElLo wOrLd"') do
          597  +    for row in colla.db:nrows('SELECT * FROM test WHERE content="hElLo wOrLd"') do
   580    598         -- print(row.id,row.content)
   581    599         assert_equal (row.content:lower(), "hello world")
   582    600         n = n + 1
   583    601       end
   584    602       assert_equal (n, 3)
   585    603   end
   586    604   
   587         -lunit.run()