Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
#include "location.h"
#include "as.h"
#include "location.h"
#include "as.h"
#include "database.h"
#include "network.h"
#include "database.h"
#include "network.h"
+static int Database_get_country(lua_State* L) {
+ struct loc_country* country = NULL;
+ int r;
+
+ Database* self = luaL_checkdatabase(L, 1);
+
+ // Fetch code
+ const char* code = luaL_checkstring(L, 2);
+
+ // Fetch the country
+ r = loc_database_get_country(self->db, &country, code);
+ if (r) {
+ lua_pushnil(L);
+ return 1;
+ }
+
+ // Create a new country object
+ r = create_country(L, country);
+ loc_country_unref(country);
+
+ return r;
+}
+
static int Database_lookup(lua_State* L) {
struct loc_network* network = NULL;
int r;
static int Database_lookup(lua_State* L) {
struct loc_network* network = NULL;
int r;
static const struct luaL_Reg database_functions[] = {
{ "get_as", Database_get_as },
static const struct luaL_Reg database_functions[] = {
{ "get_as", Database_get_as },
+ { "get_country", Database_get_country },
{ "open", Database_open },
{ "lookup", Database_lookup },
{ "__gc", Database_gc },
{ "open", Database_open },
{ "lookup", Database_lookup },
{ "__gc", Database_gc },
+function test_fetch_country()
+ location = require("location")
+
+ -- Open the database
+ db = location.Database.open(ENV_TEST_DATABASE)
+
+ -- Fetch an invalid country
+ c = db:get_country("XX")
+ luaunit.assertNil(c)
+
+ -- Fetch something that exists
+ c = db:get_country("DE")
+ luaunit.assertEquals(c:get_code(), "DE")
+ luaunit.assertEquals(c:get_name(), "Germany")
+end
+
-- This test is not very deterministic but should help to test the GC methods
function test_gc()
print("GC: " .. collectgarbage("collect"))
-- This test is not very deterministic but should help to test the GC methods
function test_gc()
print("GC: " .. collectgarbage("collect"))