add DbMgr

Change-Id: I799f6a066ef60ecd42d64de7d8d04be8996564ed
diff --git a/src/db/db-mgr.cpp b/src/db/db-mgr.cpp
new file mode 100644
index 0000000..27d7db4
--- /dev/null
+++ b/src/db/db-mgr.cpp
@@ -0,0 +1,112 @@
+/* -*- 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 "../logger.hpp"
+
+#include <boost/algorithm/string/predicate.hpp>
+
+#include <iostream>
+#include <fstream>
+
+namespace ndn {
+namespace ndns {
+
+NDNS_LOG_INIT("DbMgr");
+
+static const std::string NDNS_SCHEMA = "\
+CREATE TABLE IF NOT EXISTS zones (      \n\
+  id    INTEGER NOT NULL PRIMARY KEY,   \n\
+  name  blob NOT NULL UNIQUE,           \n\
+  ttl   integer(10) NOT NULL);          \n\
+                                        \n\
+CREATE TABLE IF NOT EXISTS rrsets (     \n\
+  id      INTEGER NOT NULL PRIMARY KEY, \n\
+  zone_id integer(10) NOT NULL,         \n\
+  label   blob NOT NULL,                \n\
+  type    blob NOT NULL,                \n\
+  version blob NOT NULL,                \n\
+  ttl     integer(10) NOT NULL,         \n\
+  data    blob NOT NULL,                                                \n\
+  FOREIGN KEY(zone_id) REFERENCES zones(id) ON UPDATE Cascade ON DELETE Cascade); \n\
+                                                                        \n\
+CREATE UNIQUE INDEX rrsets_zone_id_label_type_version                   \n\
+  ON rrsets (zone_id, label, type, version);                            \n\
+";
+
+DbMgr::DbMgr(const std::string& dbFile/* = DEFAULT_CONFIG_PATH "/" "ndns.conf"*/)
+  : m_dbFile(dbFile)
+  , m_conn(0)
+  , m_status(DB_CLOSED)
+{
+  this->open();
+}
+
+
+DbMgr::~DbMgr()
+{
+  if (m_status != DB_CLOSED) {
+    this->close();
+  }
+}
+
+void
+DbMgr::open()
+{
+  if (m_status == DB_CONNECTED)
+    return;
+
+  int res = sqlite3_open_v2(m_dbFile.c_str(), &m_conn,
+                            SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,
+#ifdef DISABLE_SQLITE3_FS_LOCKING
+                            "unix-dotfile"
+#else
+                            0
+#endif
+                            );
+
+  if (res != SQLITE_OK) {
+    m_err = "Cannot open the db file: " + m_dbFile;
+    NDNS_LOG_FATAL(m_err);
+    m_status = DB_ERROR;
+  }
+
+  // ignore any errors from DB creation (command will fail for the existing database, which is ok)
+  sqlite3_exec(m_conn, NDNS_SCHEMA.c_str(), 0, 0, 0);
+
+  m_status = DB_CONNECTED;
+}
+
+void
+DbMgr::close()
+{
+  if (m_status == DB_CLOSED)
+    return;
+
+  int ret = sqlite3_close(m_conn);
+  if (ret != SQLITE_OK) {
+    m_err = "Cannot close the db: " + m_dbFile;
+    NDNS_LOG_FATAL(m_err);
+    m_status = DB_ERROR;
+  }
+  m_status = DB_CLOSED;
+}
+
+} // namespace ndns
+} // namespace ndn
diff --git a/src/db/db-mgr.hpp b/src/db/db-mgr.hpp
new file mode 100644
index 0000000..6c5eafd
--- /dev/null
+++ b/src/db/db-mgr.hpp
@@ -0,0 +1,124 @@
+/* -*- 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/>.
+ */
+
+#ifndef NDNS_DB_DB_MGR_HPP
+#define NDNS_DB_DB_MGR_HPP
+
+#include "config.hpp"
+
+#include <ndn-cxx/common.hpp>
+#include <sqlite3.h>
+
+namespace ndn {
+namespace ndns {
+
+/**
+ * @brief Database Manager, which provides some common DB functionalities
+ */
+class DbMgr : noncopyable
+{
+public:
+  /**
+   * @brief The Database Status
+   */
+  enum DbStatus {
+    DB_CONNECTED,
+    DB_CLOSED,
+    DB_ERROR
+  };
+
+public:
+  /**
+   * @brief constructor
+   */
+  explicit
+  DbMgr(const std::string& dbFile = DEFAULT_CONFIG_PATH "/" "ndns.conf");
+
+  /**
+   * @brief destructor
+   */
+  ~DbMgr();
+
+  /**
+   * @brief connect to the database
+   */
+  void
+  open();
+
+  /**
+   * @brief close the database connection
+   */
+  void
+  close();
+
+  /**
+   * @brief get error message
+   *
+   * only valid when some db-related error happens,
+   * such as db file does not exist, wrong SQL, etc
+   */
+  const std::string&
+  getErr() const
+  {
+    return m_err;
+  }
+
+  /**
+   * @brief get Database connection status
+   */
+  DbStatus
+  getStatus() const
+  {
+    return m_status;
+  }
+
+private:
+  /**
+   * @brief set error message
+   *
+   * only valid when some db-related error happens,
+   * such as db file does not exist, wrong SQL, etc
+   */
+  void
+  setErr(const std::string& err)
+  {
+    this->m_err = err;
+  }
+
+  /**
+   * @brief set Database connection status
+   */
+  void
+  setStatus(DbStatus status)
+  {
+    this->m_status = status;
+  }
+
+private:
+  std::string m_dbFile;
+  sqlite3* m_conn;
+
+  std::string m_err;
+  DbStatus m_status;
+};
+
+} // namespace ndns
+} // namespace ndn
+
+#endif // NDNS_DB_DB_MGR_HPP