db-mgr: add Zone lookup/insert/remove

Change-Id: I9c0beab970f4508ba6523d7c2a3280fecbdd6cc1
diff --git a/src/db/db-mgr.cpp b/src/db-mgr.cpp
similarity index 62%
rename from src/db/db-mgr.cpp
rename to src/db-mgr.cpp
index 27d7db4..1cd0d3a 100644
--- a/src/db/db-mgr.cpp
+++ b/src/db-mgr.cpp
@@ -18,7 +18,7 @@
  */
 
 #include "db-mgr.hpp"
-#include "../logger.hpp"
+#include "logger.hpp"
 
 #include <boost/algorithm/string/predicate.hpp>
 
@@ -108,5 +108,87 @@
   m_status = DB_CLOSED;
 }
 
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+bool
+DbMgr::lookup(Zone& zone)
+{
+  sqlite3_stmt* stmt;
+  const char* sql = "SELECT id, ttl FROM zones WHERE name=?";
+  int rc = sqlite3_prepare_v2(m_conn, sql, strlen(sql), &stmt, 0);
+  if (rc != SQLITE_OK) {
+    throw PrepareError(sql);
+  }
+
+  const Block& zoneName = zone.getName().wireEncode();
+  sqlite3_bind_blob(stmt, 1, zoneName.wire(), zoneName.size(), SQLITE_STATIC);
+
+  if (sqlite3_step(stmt) == SQLITE_ROW) {
+    zone.setId(sqlite3_column_int64(stmt, 0));
+    zone.setTtl(time::seconds(sqlite3_column_int(stmt, 1)));
+  } else {
+    zone.setId(0);
+  }
+
+  sqlite3_finalize(stmt);
+
+  return zone.getId() != 0;
+}
+
+void
+DbMgr::insert(Zone& zone)
+{
+  if (zone.getId() > 0)
+    return;
+
+  sqlite3_stmt* stmt;
+  const char* sql = "INSERT INTO zones (name, ttl) VALUES (?, ?)";
+  int rc = sqlite3_prepare_v2(m_conn, sql, strlen(sql), &stmt, 0);
+  if (rc != SQLITE_OK) {
+    throw PrepareError(sql);
+  }
+
+  const Block& zoneName = zone.getName().wireEncode();
+  sqlite3_bind_blob(stmt, 1, zoneName.wire(), zoneName.size(), SQLITE_STATIC);
+  sqlite3_bind_int(stmt,  2, zone.getTtl().count());
+
+  rc = sqlite3_step(stmt);
+  if (rc != SQLITE_DONE) {
+    sqlite3_finalize(stmt);
+    throw ExecuteError(sql);
+  }
+
+  zone.setId(sqlite3_last_insert_rowid(m_conn));
+  sqlite3_finalize(stmt);
+}
+
+void
+DbMgr::remove(Zone& zone)
+{
+  if (zone.getId() == 0)
+    return;
+
+  sqlite3_stmt* stmt;
+  const char* sql = "DELETE FROM zones where id=?";
+  int rc = sqlite3_prepare_v2(m_conn, sql, strlen(sql), &stmt, 0);
+  if (rc != SQLITE_OK) {
+    throw PrepareError(sql);
+  }
+
+  sqlite3_bind_int64(stmt, 1, zone.getId());
+
+  rc = sqlite3_step(stmt);
+  if (rc != SQLITE_DONE) {
+    sqlite3_finalize(stmt);
+    throw ExecuteError(sql);
+  }
+
+  sqlite3_finalize(stmt);
+
+  zone.setId(0);
+  zone.setTtl(time::seconds(3600));
+}
+
+
 } // namespace ndns
 } // namespace ndn
diff --git a/src/db/db-mgr.hpp b/src/db-mgr.hpp
similarity index 64%
rename from src/db/db-mgr.hpp
rename to src/db-mgr.hpp
index 6c5eafd..26041c6 100644
--- a/src/db/db-mgr.hpp
+++ b/src/db-mgr.hpp
@@ -17,10 +17,11 @@
  * NDNS, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-#ifndef NDNS_DB_DB_MGR_HPP
-#define NDNS_DB_DB_MGR_HPP
+#ifndef NDNS_DB_MGR_HPP
+#define NDNS_DB_MGR_HPP
 
 #include "config.hpp"
+#include "zone.hpp"
 
 #include <ndn-cxx/common.hpp>
 #include <sqlite3.h>
@@ -28,6 +29,17 @@
 namespace ndn {
 namespace ndns {
 
+#define DEFINE_ERROR(Name, Base)                \
+class Name : public Base                        \
+{                                               \
+ public:                                        \
+  explicit                                      \
+  Name(const std::string& what)                 \
+    : Base(what)                                \
+  {                                             \
+  }                                             \
+};
+
 /**
  * @brief Database Manager, which provides some common DB functionalities
  */
@@ -43,6 +55,10 @@
     DB_ERROR
   };
 
+  DEFINE_ERROR(Error, std::runtime_error);
+  DEFINE_ERROR(PrepareError, Error);
+  DEFINE_ERROR(ExecuteError, Error);
+
 public:
   /**
    * @brief constructor
@@ -88,6 +104,35 @@
     return m_status;
   }
 
+public: // Zone manipulation
+  DEFINE_ERROR(ZoneError, Error);
+
+  /**
+   * @brief lookup the zone by name, fill the m_id and m_ttl
+   * @post whatever the previous id is
+   * @return true if the record exist
+   */
+  bool
+  lookup(Zone& zone);
+
+  /**
+   * @brief remove the zone
+   * @pre m_zone.getId() > 0
+   * @post m_zone.getId() == 0
+   */
+  void
+  remove(Zone& zone);
+
+  /**
+   * @brief insert the m_zone to the database, and set the zone's id.
+   * If the zone is already in the db, handle the exception without leaving it to upper level,
+   * meanwhile, set the zone's id too.
+   * @pre m_zone.getId() == 0
+   * @post m_zone.getId() > 0
+   */
+  void
+  insert(Zone& zone);
+
 private:
   /**
    * @brief set error message
@@ -121,4 +166,4 @@
 } // namespace ndns
 } // namespace ndn
 
-#endif // NDNS_DB_DB_MGR_HPP
+#endif // NDNS_DB_MGR_HPP
diff --git a/tests/unit/db-mgr.cpp b/tests/unit/db-mgr.cpp
new file mode 100644
index 0000000..ad382a1
--- /dev/null
+++ b/tests/unit/db-mgr.cpp
@@ -0,0 +1,95 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014, Regents of the University of California.
+ *
+ * This file is part of NDNS (Named Data Networking Domain Name Service).
+ * See AUTHORS.md for complete list of NDNS authors and contributors.
+ *
+ * NDNS is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation,
+ * either version 3 of the License, or (at your option) any later version.
+ *
+ * NDNS is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
+ * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * PURPOSE.  See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * NDNS, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "db-mgr.hpp"
+#include "../boost-test.hpp"
+
+#include <boost/filesystem.hpp>
+
+namespace ndn {
+namespace ndns {
+namespace tests {
+
+BOOST_AUTO_TEST_SUITE(DbMgr)
+
+static const boost::filesystem::path TEST_DATABASE = BUILDDIR "/tests/unit/db-mgr-ndns.db";
+
+class DbMgrFixture
+{
+public:
+  DbMgrFixture()
+    : session(TEST_DATABASE.string().c_str())
+  {
+  }
+
+  ~DbMgrFixture()
+  {
+    session.close();
+    boost::filesystem::remove(TEST_DATABASE);
+  }
+
+public:
+  ndns::DbMgr session;
+};
+
+
+BOOST_FIXTURE_TEST_CASE(Basic, DbMgrFixture)
+{
+  BOOST_CHECK_EQUAL(session.getStatus(), ndns::DbMgr::DB_CONNECTED);
+
+  session.close();
+  BOOST_CHECK_EQUAL(session.getStatus(), ndns::DbMgr::DB_CLOSED);
+
+  // reopen
+  session.open();
+  BOOST_CHECK_EQUAL(session.getStatus(), ndns::DbMgr::DB_CONNECTED);
+}
+
+BOOST_FIXTURE_TEST_CASE(Zones, DbMgrFixture)
+{
+  Zone zone1;
+  zone1.setName("/net");
+  zone1.setTtl(time::seconds(4600));
+  BOOST_CHECK_NO_THROW(session.insert(zone1));
+  BOOST_CHECK_GT(zone1.getId(), 0);
+
+  Zone zone2;
+  zone2.setName("/net");
+  session.lookup(zone2);
+  BOOST_CHECK_EQUAL(zone2.getId(), zone1.getId());
+  BOOST_CHECK_EQUAL(zone2.getTtl(), zone1.getTtl());
+
+  BOOST_CHECK_NO_THROW(session.insert(zone2)); // zone2 already has id.  Nothing to execute
+
+  zone2.setId(0);
+  BOOST_CHECK_THROW(session.insert(zone2), ndns::DbMgr::ExecuteError);
+
+  BOOST_CHECK_NO_THROW(session.remove(zone1));
+  BOOST_CHECK_EQUAL(zone1.getId(), 0);
+
+  // record shouldn't exist at this point
+  BOOST_CHECK_NO_THROW(session.lookup(zone2));
+  BOOST_CHECK_EQUAL(zone2.getId(), 0);
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+
+} // namespace tests
+} // namespace ndns
+} // namespace ndn
diff --git a/tests/unit/db/db-mgr.cpp b/tests/unit/db/db-mgr.cpp
deleted file mode 100644
index 198a851..0000000
--- a/tests/unit/db/db-mgr.cpp
+++ /dev/null
@@ -1,46 +0,0 @@
-/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
- * Copyright (c) 2014, Regents of the University of California.
- *
- * This file is part of NDNS (Named Data Networking Domain Name Service).
- * See AUTHORS.md for complete list of NDNS authors and contributors.
- *
- * NDNS is free software: you can redistribute it and/or modify it under the terms
- * of the GNU General Public License as published by the Free Software Foundation,
- * either version 3 of the License, or (at your option) any later version.
- *
- * NDNS is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
- * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
- * PURPOSE.  See the GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along with
- * NDNS, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
- */
-
-#include "db/db-mgr.hpp"
-#include "../../boost-test.hpp"
-
-namespace ndn {
-namespace ndns {
-namespace tests {
-
-BOOST_AUTO_TEST_SUITE(DbMgr)
-
-BOOST_AUTO_TEST_CASE(Basic)
-{
-  ndns::DbMgr mgr(BUILDDIR "/tests/unit/db/dbmgr-ndns.db");
-  BOOST_CHECK_EQUAL(mgr.getStatus(), ndns::DbMgr::DB_CONNECTED);
-
-  mgr.close();
-  BOOST_CHECK_EQUAL(mgr.getStatus(), ndns::DbMgr::DB_CLOSED);
-
-  // reopen
-  mgr.open();
-  BOOST_CHECK_EQUAL(mgr.getStatus(), ndns::DbMgr::DB_CONNECTED);
-}
-
-BOOST_AUTO_TEST_SUITE_END()
-
-} // namespace tests
-} // namespace ndns
-} // namespace ndn
diff --git a/wscript b/wscript
index 9c5bd1f..8963265 100644
--- a/wscript
+++ b/wscript
@@ -35,7 +35,7 @@
     if conf.options.with_tests:
         conf.env['WITH_TESTS'] = True
 
-    USED_BOOST_LIBS = ['system']
+    USED_BOOST_LIBS = ['system', 'filesystem']
     if conf.env['WITH_TESTS']:
         USED_BOOST_LIBS += ['unit_test_framework']
     conf.check_boost(lib=USED_BOOST_LIBS, mandatory=True)