Shock Jiang | 3c72318 | 2014-09-10 16:41:18 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014, Regents of the University of California. |
| 4 | * |
| 5 | * This file is part of NDNS (Named Data Networking Domain Name Service). |
| 6 | * See AUTHORS.md for complete list of NDNS authors and contributors. |
| 7 | * |
| 8 | * NDNS is free software: you can redistribute it and/or modify it under the terms |
| 9 | * of the GNU General Public License as published by the Free Software Foundation, |
| 10 | * either version 3 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * NDNS is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 13 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 | * PURPOSE. See the GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along with |
| 17 | * NDNS, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
Shock Jiang | 4e0ab7c | 2014-09-11 16:23:21 -0700 | [diff] [blame] | 20 | #ifndef NDNS_DB_MGR_HPP |
| 21 | #define NDNS_DB_MGR_HPP |
Shock Jiang | 3c72318 | 2014-09-10 16:41:18 -0700 | [diff] [blame] | 22 | |
| 23 | #include "config.hpp" |
Shock Jiang | 4e0ab7c | 2014-09-11 16:23:21 -0700 | [diff] [blame] | 24 | #include "zone.hpp" |
Shock Jiang | 3c72318 | 2014-09-10 16:41:18 -0700 | [diff] [blame] | 25 | |
| 26 | #include <ndn-cxx/common.hpp> |
| 27 | #include <sqlite3.h> |
| 28 | |
| 29 | namespace ndn { |
| 30 | namespace ndns { |
| 31 | |
Shock Jiang | 4e0ab7c | 2014-09-11 16:23:21 -0700 | [diff] [blame] | 32 | #define DEFINE_ERROR(Name, Base) \ |
| 33 | class Name : public Base \ |
| 34 | { \ |
| 35 | public: \ |
| 36 | explicit \ |
| 37 | Name(const std::string& what) \ |
| 38 | : Base(what) \ |
| 39 | { \ |
| 40 | } \ |
| 41 | }; |
| 42 | |
Shock Jiang | 3c72318 | 2014-09-10 16:41:18 -0700 | [diff] [blame] | 43 | /** |
| 44 | * @brief Database Manager, which provides some common DB functionalities |
| 45 | */ |
| 46 | class DbMgr : noncopyable |
| 47 | { |
| 48 | public: |
| 49 | /** |
| 50 | * @brief The Database Status |
| 51 | */ |
| 52 | enum DbStatus { |
| 53 | DB_CONNECTED, |
| 54 | DB_CLOSED, |
| 55 | DB_ERROR |
| 56 | }; |
| 57 | |
Shock Jiang | 4e0ab7c | 2014-09-11 16:23:21 -0700 | [diff] [blame] | 58 | DEFINE_ERROR(Error, std::runtime_error); |
| 59 | DEFINE_ERROR(PrepareError, Error); |
| 60 | DEFINE_ERROR(ExecuteError, Error); |
| 61 | |
Shock Jiang | 3c72318 | 2014-09-10 16:41:18 -0700 | [diff] [blame] | 62 | public: |
| 63 | /** |
| 64 | * @brief constructor |
| 65 | */ |
| 66 | explicit |
| 67 | DbMgr(const std::string& dbFile = DEFAULT_CONFIG_PATH "/" "ndns.conf"); |
| 68 | |
| 69 | /** |
| 70 | * @brief destructor |
| 71 | */ |
| 72 | ~DbMgr(); |
| 73 | |
| 74 | /** |
| 75 | * @brief connect to the database |
| 76 | */ |
| 77 | void |
| 78 | open(); |
| 79 | |
| 80 | /** |
| 81 | * @brief close the database connection |
| 82 | */ |
| 83 | void |
| 84 | close(); |
| 85 | |
| 86 | /** |
| 87 | * @brief get error message |
| 88 | * |
| 89 | * only valid when some db-related error happens, |
| 90 | * such as db file does not exist, wrong SQL, etc |
| 91 | */ |
| 92 | const std::string& |
| 93 | getErr() const |
| 94 | { |
| 95 | return m_err; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * @brief get Database connection status |
| 100 | */ |
| 101 | DbStatus |
| 102 | getStatus() const |
| 103 | { |
| 104 | return m_status; |
| 105 | } |
| 106 | |
Shock Jiang | 4e0ab7c | 2014-09-11 16:23:21 -0700 | [diff] [blame] | 107 | public: // Zone manipulation |
| 108 | DEFINE_ERROR(ZoneError, Error); |
| 109 | |
| 110 | /** |
| 111 | * @brief lookup the zone by name, fill the m_id and m_ttl |
| 112 | * @post whatever the previous id is |
| 113 | * @return true if the record exist |
| 114 | */ |
| 115 | bool |
| 116 | lookup(Zone& zone); |
| 117 | |
| 118 | /** |
| 119 | * @brief remove the zone |
| 120 | * @pre m_zone.getId() > 0 |
| 121 | * @post m_zone.getId() == 0 |
| 122 | */ |
| 123 | void |
| 124 | remove(Zone& zone); |
| 125 | |
| 126 | /** |
| 127 | * @brief insert the m_zone to the database, and set the zone's id. |
| 128 | * If the zone is already in the db, handle the exception without leaving it to upper level, |
| 129 | * meanwhile, set the zone's id too. |
| 130 | * @pre m_zone.getId() == 0 |
| 131 | * @post m_zone.getId() > 0 |
| 132 | */ |
| 133 | void |
| 134 | insert(Zone& zone); |
| 135 | |
Shock Jiang | 3c72318 | 2014-09-10 16:41:18 -0700 | [diff] [blame] | 136 | private: |
| 137 | /** |
| 138 | * @brief set error message |
| 139 | * |
| 140 | * only valid when some db-related error happens, |
| 141 | * such as db file does not exist, wrong SQL, etc |
| 142 | */ |
| 143 | void |
| 144 | setErr(const std::string& err) |
| 145 | { |
| 146 | this->m_err = err; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * @brief set Database connection status |
| 151 | */ |
| 152 | void |
| 153 | setStatus(DbStatus status) |
| 154 | { |
| 155 | this->m_status = status; |
| 156 | } |
| 157 | |
| 158 | private: |
| 159 | std::string m_dbFile; |
| 160 | sqlite3* m_conn; |
| 161 | |
| 162 | std::string m_err; |
| 163 | DbStatus m_status; |
| 164 | }; |
| 165 | |
| 166 | } // namespace ndns |
| 167 | } // namespace ndn |
| 168 | |
Shock Jiang | 4e0ab7c | 2014-09-11 16:23:21 -0700 | [diff] [blame] | 169 | #endif // NDNS_DB_MGR_HPP |