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