LuaSQLite3

Check-in [3a1789e967]
Login

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

Overview
Comment:Handle backup_init returning NULL, and add unit test for it.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 3a1789e967535498f8c6515b5d0d28421acacc6a
User & Date: e 2016-11-03 21:41:04
Context
2016-11-03
22:31
New rockspec for 0.9.4 with second module 'lsqlite3complete' that statically links sqlite.c check-in: 29da4fe833 user: e tags: trunk
21:41
Handle backup_init returning NULL, and add unit test for it. check-in: 3a1789e967 user: e tags: trunk
21:20
Update wiki doc for new Online Backup API support. check-in: 80be7a8591 user: e tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to lsqlite3.c.

  1577   1577       sdb *target_db = lsqlite_checkdb(L, 1);
  1578   1578       const char *target_nm = luaL_checkstring(L, 2);
  1579   1579       sdb *source_db = lsqlite_checkdb(L, 3);
  1580   1580       const char *source_nm = luaL_checkstring(L, 4);
  1581   1581   
  1582   1582       sqlite3_backup *bu = sqlite3_backup_init(target_db->db, target_nm, source_db->db, source_nm);
  1583   1583   
  1584         -    sdb_bu *sbu = (sdb_bu*)lua_newuserdata(L, sizeof(sdb_bu));
  1585         -
  1586         -    luaL_getmetatable(L, sqlite_bu_meta);
  1587         -    lua_setmetatable(L, -2);        /* set metatable */
  1588         -    sbu->bu = bu;
  1589         -
  1590         -    /* create table from registry */
  1591         -    /* to prevent referenced databases from being garbage collected while bu is live */
  1592         -    lua_pushlightuserdata(L, bu);
  1593         -    lua_createtable(L, 2, 0);
  1594         -    /* add source and target dbs to table at indices 1 and 2 */
  1595         -    lua_pushvalue(L, 1); /* target db */
  1596         -    lua_rawseti(L, -2, 1);
  1597         -    lua_pushvalue(L, 3); /* source db */
  1598         -    lua_rawseti(L, -2, 2);
  1599         -    /* put table in registry with key lightuserdata bu */
  1600         -    lua_rawset(L, LUA_REGISTRYINDEX);
  1601         -
  1602         -    return 1;
         1584  +    if (NULL != bu) {
         1585  +        sdb_bu *sbu = (sdb_bu*)lua_newuserdata(L, sizeof(sdb_bu));
         1586  +
         1587  +        luaL_getmetatable(L, sqlite_bu_meta);
         1588  +        lua_setmetatable(L, -2);        /* set metatable */
         1589  +        sbu->bu = bu;
         1590  +
         1591  +        /* create table from registry */
         1592  +        /* to prevent referenced databases from being garbage collected while bu is live */
         1593  +        lua_pushlightuserdata(L, bu);
         1594  +        lua_createtable(L, 2, 0);
         1595  +        /* add source and target dbs to table at indices 1 and 2 */
         1596  +        lua_pushvalue(L, 1); /* target db */
         1597  +        lua_rawseti(L, -2, 1);
         1598  +        lua_pushvalue(L, 3); /* source db */
         1599  +        lua_rawseti(L, -2, 2);
         1600  +        /* put table in registry with key lightuserdata bu */
         1601  +        lua_rawset(L, LUA_REGISTRYINDEX);
         1602  +
         1603  +        return 1;
         1604  +    }
         1605  +    else {
         1606  +        return 0;
         1607  +    }
  1603   1608   }
  1604   1609   
  1605   1610   static sdb_bu *lsqlite_getbu(lua_State *L, int index) {
  1606   1611       sdb_bu *sbu = (sdb_bu*)luaL_checkudata(L, index, sqlite_bu_meta);
  1607   1612       if (sbu == NULL) luaL_typerror(L, index, "sqlite database backup");
  1608   1613       return sbu;
  1609   1614   }

Changes to test/tests-sqlite3.lua.

  1175   1175     for row in db:nrows("SELECT name as val FROM test WHERE id = 3") do
  1176   1176       assert_equal (row.val, 'Hello SQLite')
  1177   1177       assert_equal (#row.val, 12)
  1178   1178     end
  1179   1179   
  1180   1180     assert( db:close() )
  1181   1181   
         1182  +end
         1183  +
         1184  +local db_bu_null = lunit_TestCase("Online Backup API NULL")
         1185  +
         1186  +function db_bu_null.setup()
         1187  +  db_bu_null.db = assert( sqlite3.open_memory() )
         1188  +end
         1189  +
         1190  +function db_bu_null.teardown()
         1191  +  assert( db_bu_null.db:close() )
         1192  +end
         1193  +
         1194  +function db_bu_null.test()
         1195  +
         1196  +  local bu = assert_nil( sqlite3.backup_init(db_bu_null.db, 'main', db_bu_null.db, 'main') )
         1197  +
  1182   1198   end
  1183   1199   
  1184   1200   lunit.main(arg)
  1185   1201