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 Unified Diffs Show Whitespace Changes Patch

Changes to lsqlite3.c.

1577
1578
1579
1580
1581
1582
1583

1584
1585
1586
1587
1588
1589
1590
....
1596
1597
1598
1599
1600
1601
1602




1603
1604
1605
1606
1607
1608
1609
    sdb *target_db = lsqlite_checkdb(L, 1);
    const char *target_nm = luaL_checkstring(L, 2);
    sdb *source_db = lsqlite_checkdb(L, 3);
    const char *source_nm = luaL_checkstring(L, 4);

    sqlite3_backup *bu = sqlite3_backup_init(target_db->db, target_nm, source_db->db, source_nm);


    sdb_bu *sbu = (sdb_bu*)lua_newuserdata(L, sizeof(sdb_bu));

    luaL_getmetatable(L, sqlite_bu_meta);
    lua_setmetatable(L, -2);        /* set metatable */
    sbu->bu = bu;

    /* create table from registry */
................................................................................
    lua_rawseti(L, -2, 1);
    lua_pushvalue(L, 3); /* source db */
    lua_rawseti(L, -2, 2);
    /* put table in registry with key lightuserdata bu */
    lua_rawset(L, LUA_REGISTRYINDEX);

    return 1;




}

static sdb_bu *lsqlite_getbu(lua_State *L, int index) {
    sdb_bu *sbu = (sdb_bu*)luaL_checkudata(L, index, sqlite_bu_meta);
    if (sbu == NULL) luaL_typerror(L, index, "sqlite database backup");
    return sbu;
}







>







 







>
>
>
>







1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
....
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
    sdb *target_db = lsqlite_checkdb(L, 1);
    const char *target_nm = luaL_checkstring(L, 2);
    sdb *source_db = lsqlite_checkdb(L, 3);
    const char *source_nm = luaL_checkstring(L, 4);

    sqlite3_backup *bu = sqlite3_backup_init(target_db->db, target_nm, source_db->db, source_nm);

    if (NULL != bu) {
        sdb_bu *sbu = (sdb_bu*)lua_newuserdata(L, sizeof(sdb_bu));

        luaL_getmetatable(L, sqlite_bu_meta);
        lua_setmetatable(L, -2);        /* set metatable */
        sbu->bu = bu;

        /* create table from registry */
................................................................................
        lua_rawseti(L, -2, 1);
        lua_pushvalue(L, 3); /* source db */
        lua_rawseti(L, -2, 2);
        /* put table in registry with key lightuserdata bu */
        lua_rawset(L, LUA_REGISTRYINDEX);

        return 1;
    }
    else {
        return 0;
    }
}

static sdb_bu *lsqlite_getbu(lua_State *L, int index) {
    sdb_bu *sbu = (sdb_bu*)luaL_checkudata(L, index, sqlite_bu_meta);
    if (sbu == NULL) luaL_typerror(L, index, "sqlite database backup");
    return sbu;
}

Changes to test/tests-sqlite3.lua.

1175
1176
1177
1178
1179
1180
1181
















1182
1183
1184
1185
  for row in db:nrows("SELECT name as val FROM test WHERE id = 3") do
    assert_equal (row.val, 'Hello SQLite')
    assert_equal (#row.val, 12)
  end

  assert( db:close() )

















end

lunit.main(arg)








>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>




1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
  for row in db:nrows("SELECT name as val FROM test WHERE id = 3") do
    assert_equal (row.val, 'Hello SQLite')
    assert_equal (#row.val, 12)
  end

  assert( db:close() )

end

local db_bu_null = lunit_TestCase("Online Backup API NULL")

function db_bu_null.setup()
  db_bu_null.db = assert( sqlite3.open_memory() )
end

function db_bu_null.teardown()
  assert( db_bu_null.db:close() )
end

function db_bu_null.test()

  local bu = assert_nil( sqlite3.backup_init(db_bu_null.db, 'main', db_bu_null.db, 'main') )

end

lunit.main(arg)