| /* -*- 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_MGR_HPP |
| #define NDNS_DB_MGR_HPP |
| |
| #include "config.hpp" |
| #include "zone.hpp" |
| |
| #include <ndn-cxx/common.hpp> |
| #include <sqlite3.h> |
| |
| 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 |
| */ |
| class DbMgr : noncopyable |
| { |
| public: |
| /** |
| * @brief The Database Status |
| */ |
| enum DbStatus { |
| DB_CONNECTED, |
| DB_CLOSED, |
| DB_ERROR |
| }; |
| |
| DEFINE_ERROR(Error, std::runtime_error); |
| DEFINE_ERROR(PrepareError, Error); |
| DEFINE_ERROR(ExecuteError, 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; |
| } |
| |
| 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 |
| * |
| * 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_MGR_HPP |