Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 2 | /* |
Davide Pesavento | 093ce1c | 2019-01-21 19:22:42 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2019, Regents of the University of California. |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 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 | |
| 20 | #include "db-mgr.hpp" |
| 21 | #include "logger.hpp" |
| 22 | #include "clients/response.hpp" |
Davide Pesavento | d01c1a4 | 2019-01-21 21:42:45 -0500 | [diff] [blame] | 23 | #include "util/util.hpp" |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 24 | |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 25 | namespace ndn { |
| 26 | namespace ndns { |
| 27 | |
Alexander Afanasyev | 08d1874 | 2018-03-15 16:31:28 -0400 | [diff] [blame] | 28 | NDNS_LOG_INIT(DbMgr); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 29 | |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 30 | static const std::string NDNS_SCHEMA = R"VALUE( |
| 31 | CREATE TABLE IF NOT EXISTS zones ( |
| 32 | id INTEGER NOT NULL PRIMARY KEY, |
| 33 | name blob NOT NULL UNIQUE, |
| 34 | ttl integer(10) NOT NULL); |
| 35 | |
| 36 | CREATE TABLE IF NOT EXISTS zone_info ( |
| 37 | zone_id INTEGER NOT NULL, |
| 38 | key VARCHAR(10) NOT NULL, |
| 39 | value blob NOT NULL, |
| 40 | PRIMARY KEY (zone_id, key), |
| 41 | FOREIGN KEY(zone_id) REFERENCES zones(id) ON UPDATE Cascade ON DELETE Cascade); |
| 42 | |
| 43 | CREATE TABLE IF NOT EXISTS rrsets ( |
| 44 | id INTEGER NOT NULL PRIMARY KEY, |
| 45 | zone_id integer(10) NOT NULL, |
| 46 | label blob NOT NULL, |
| 47 | type blob NOT NULL, |
| 48 | version blob NOT NULL, |
| 49 | ttl integer(10) NOT NULL, |
| 50 | data blob NOT NULL, |
| 51 | FOREIGN KEY(zone_id) REFERENCES zones(id) ON UPDATE Cascade ON DELETE Cascade); |
| 52 | |
| 53 | CREATE UNIQUE INDEX rrsets_zone_id_label_type_version |
| 54 | ON rrsets (zone_id, label, type, version); |
| 55 | )VALUE"; |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 56 | |
Davide Pesavento | d01c1a4 | 2019-01-21 21:42:45 -0500 | [diff] [blame] | 57 | DbMgr::DbMgr(const std::string& dbFile) |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 58 | : m_dbFile(dbFile) |
Davide Pesavento | 093ce1c | 2019-01-21 19:22:42 -0500 | [diff] [blame] | 59 | , m_conn(nullptr) |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 60 | { |
Davide Pesavento | d01c1a4 | 2019-01-21 21:42:45 -0500 | [diff] [blame] | 61 | if (m_dbFile.empty()) |
| 62 | m_dbFile = getDefaultDatabaseFile(); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 63 | |
Davide Pesavento | 093ce1c | 2019-01-21 19:22:42 -0500 | [diff] [blame] | 64 | open(); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 65 | |
| 66 | NDNS_LOG_INFO("open database: " << m_dbFile); |
| 67 | } |
| 68 | |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 69 | DbMgr::~DbMgr() |
| 70 | { |
Davide Pesavento | 093ce1c | 2019-01-21 19:22:42 -0500 | [diff] [blame] | 71 | close(); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | void |
| 75 | DbMgr::open() |
| 76 | { |
Davide Pesavento | 093ce1c | 2019-01-21 19:22:42 -0500 | [diff] [blame] | 77 | int res = sqlite3_open_v2(m_dbFile.data(), &m_conn, |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 78 | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, |
| 79 | #ifdef DISABLE_SQLITE3_FS_LOCKING |
| 80 | "unix-dotfile" |
| 81 | #else |
Davide Pesavento | 093ce1c | 2019-01-21 19:22:42 -0500 | [diff] [blame] | 82 | nullptr |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 83 | #endif |
| 84 | ); |
| 85 | |
| 86 | if (res != SQLITE_OK) { |
| 87 | NDNS_LOG_FATAL("Cannot open the db file: " << m_dbFile); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 88 | BOOST_THROW_EXCEPTION(ConnectError("Cannot open the db file: " + m_dbFile)); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 89 | } |
Davide Pesavento | 093ce1c | 2019-01-21 19:22:42 -0500 | [diff] [blame] | 90 | |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 91 | // ignore any errors from DB creation (command will fail for the existing database, which is ok) |
Davide Pesavento | 093ce1c | 2019-01-21 19:22:42 -0500 | [diff] [blame] | 92 | sqlite3_exec(m_conn, NDNS_SCHEMA.data(), nullptr, nullptr, nullptr); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | void |
| 96 | DbMgr::close() |
| 97 | { |
Davide Pesavento | 093ce1c | 2019-01-21 19:22:42 -0500 | [diff] [blame] | 98 | if (m_conn == nullptr) |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 99 | return; |
| 100 | |
| 101 | int ret = sqlite3_close(m_conn); |
| 102 | if (ret != SQLITE_OK) { |
| 103 | NDNS_LOG_FATAL("Cannot close the db: " << m_dbFile); |
| 104 | } |
| 105 | else { |
Davide Pesavento | 093ce1c | 2019-01-21 19:22:42 -0500 | [diff] [blame] | 106 | m_conn = nullptr; |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 107 | NDNS_LOG_INFO("Close database: " << m_dbFile); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | void |
| 112 | DbMgr::clearAllData() |
| 113 | { |
| 114 | const char* sql = "DELETE FROM zones; DELETE FROM rrsets;"; |
| 115 | |
Davide Pesavento | 093ce1c | 2019-01-21 19:22:42 -0500 | [diff] [blame] | 116 | // sqlite3_step cannot execute multiple SQL statements |
| 117 | int rc = sqlite3_exec(m_conn, sql, nullptr, nullptr, nullptr); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 118 | if (rc != SQLITE_OK) { |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 119 | BOOST_THROW_EXCEPTION(ExecuteError(sql)); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | NDNS_LOG_INFO("clear all the data in the database: " << m_dbFile); |
| 123 | } |
| 124 | |
Yumin Xia | 55a7cc4 | 2017-05-14 18:43:34 -0700 | [diff] [blame] | 125 | void |
| 126 | DbMgr::saveName(const Name& name, sqlite3_stmt* stmt, int iCol, bool isStatic) |
| 127 | { |
| 128 | const Block& wire = name.wireEncode(); |
| 129 | sqlite3_bind_blob(stmt, iCol, wire.value(), wire.value_size(), isStatic ? SQLITE_STATIC : SQLITE_TRANSIENT); |
| 130 | } |
| 131 | |
| 132 | Name |
| 133 | DbMgr::restoreName(sqlite3_stmt* stmt, int iCol) |
| 134 | { |
| 135 | Name name; |
| 136 | |
| 137 | const uint8_t* buffer = static_cast<const uint8_t*>(sqlite3_column_blob(stmt, iCol)); |
| 138 | size_t nBytesLeft = sqlite3_column_bytes(stmt, iCol); |
| 139 | |
| 140 | while (nBytesLeft > 0) { |
| 141 | bool hasDecodingSucceeded; |
| 142 | name::Component component; |
| 143 | std::tie(hasDecodingSucceeded, component) = Block::fromBuffer(buffer, nBytesLeft); |
| 144 | if (!hasDecodingSucceeded) { |
| 145 | BOOST_THROW_EXCEPTION(Error("Error while decoding name from the database")); |
| 146 | } |
| 147 | name.append(component); |
| 148 | buffer += component.size(); |
| 149 | nBytesLeft -= component.size(); |
| 150 | } |
| 151 | |
| 152 | return name; |
| 153 | } |
| 154 | |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 155 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 156 | // Zone |
| 157 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 158 | |
| 159 | void |
| 160 | DbMgr::insert(Zone& zone) |
| 161 | { |
| 162 | if (zone.getId() > 0) |
| 163 | return; |
| 164 | |
| 165 | sqlite3_stmt* stmt; |
| 166 | const char* sql = "INSERT INTO zones (name, ttl) VALUES (?, ?)"; |
Davide Pesavento | 093ce1c | 2019-01-21 19:22:42 -0500 | [diff] [blame] | 167 | int rc = sqlite3_prepare_v2(m_conn, sql, -1, &stmt, nullptr); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 168 | if (rc != SQLITE_OK) { |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 169 | BOOST_THROW_EXCEPTION(PrepareError(sql)); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 170 | } |
| 171 | |
Yumin Xia | 55a7cc4 | 2017-05-14 18:43:34 -0700 | [diff] [blame] | 172 | saveName(zone.getName(), stmt, 1); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 173 | sqlite3_bind_int(stmt, 2, zone.getTtl().count()); |
| 174 | |
| 175 | rc = sqlite3_step(stmt); |
| 176 | if (rc != SQLITE_DONE) { |
| 177 | sqlite3_finalize(stmt); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 178 | BOOST_THROW_EXCEPTION(ExecuteError(sql)); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | zone.setId(sqlite3_last_insert_rowid(m_conn)); |
| 182 | sqlite3_finalize(stmt); |
| 183 | } |
| 184 | |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 185 | void |
| 186 | DbMgr::setZoneInfo(Zone& zone, |
| 187 | const std::string& key, |
| 188 | const Block& value) |
| 189 | { |
| 190 | if (zone.getId() == 0) { |
| 191 | BOOST_THROW_EXCEPTION(Error("zone has not been initialized")); |
| 192 | } |
| 193 | |
| 194 | if (key.length() > 10) { |
| 195 | BOOST_THROW_EXCEPTION(Error("key length should not exceed 10")); |
| 196 | } |
| 197 | |
| 198 | sqlite3_stmt* stmt; |
| 199 | const char* sql = "INSERT OR REPLACE INTO zone_info (zone_id, key, value) VALUES (?, ?, ?)"; |
Davide Pesavento | 093ce1c | 2019-01-21 19:22:42 -0500 | [diff] [blame] | 200 | int rc = sqlite3_prepare_v2(m_conn, sql, -1, &stmt, nullptr); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 201 | if (rc != SQLITE_OK) { |
| 202 | BOOST_THROW_EXCEPTION(PrepareError(sql)); |
| 203 | } |
| 204 | |
| 205 | sqlite3_bind_int(stmt, 1, zone.getId()); |
Davide Pesavento | 093ce1c | 2019-01-21 19:22:42 -0500 | [diff] [blame] | 206 | sqlite3_bind_text(stmt, 2, key.data(), key.length(), SQLITE_STATIC); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 207 | sqlite3_bind_blob(stmt, 3, value.wire(), value.size(), SQLITE_STATIC); |
| 208 | |
| 209 | rc = sqlite3_step(stmt); |
| 210 | if (rc != SQLITE_DONE) { |
| 211 | sqlite3_finalize(stmt); |
| 212 | BOOST_THROW_EXCEPTION(ExecuteError(sql)); |
| 213 | } |
| 214 | |
| 215 | sqlite3_finalize(stmt); |
| 216 | } |
| 217 | |
| 218 | std::map<std::string, Block> |
| 219 | DbMgr::getZoneInfo(Zone& zone) |
| 220 | { |
| 221 | using std::string; |
| 222 | std::map<string, Block> rtn; |
| 223 | |
| 224 | if (zone.getId() == 0) { |
| 225 | find(zone); |
| 226 | } |
| 227 | |
| 228 | if (zone.getId() == 0) { |
| 229 | BOOST_THROW_EXCEPTION(Error("zone has not been initialized")); |
| 230 | } |
| 231 | |
| 232 | sqlite3_stmt* stmt; |
| 233 | const char* sql = "SELECT key, value FROM zone_info WHERE zone_id=?"; |
Davide Pesavento | 093ce1c | 2019-01-21 19:22:42 -0500 | [diff] [blame] | 234 | int rc = sqlite3_prepare_v2(m_conn, sql, -1, &stmt, nullptr); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 235 | if (rc != SQLITE_OK) { |
| 236 | BOOST_THROW_EXCEPTION(PrepareError(sql)); |
| 237 | } |
| 238 | |
| 239 | sqlite3_bind_int(stmt, 1, zone.getId()); |
| 240 | |
| 241 | while (sqlite3_step(stmt) == SQLITE_ROW) { |
| 242 | const char* key = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 0)); |
| 243 | rtn[string(key)] = Block(static_cast<const uint8_t*>(sqlite3_column_blob(stmt, 1)), |
| 244 | sqlite3_column_bytes(stmt, 1)); |
| 245 | } |
| 246 | |
| 247 | sqlite3_finalize(stmt); |
| 248 | return rtn; |
| 249 | } |
| 250 | |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 251 | bool |
| 252 | DbMgr::find(Zone& zone) |
| 253 | { |
| 254 | sqlite3_stmt* stmt; |
| 255 | const char* sql = "SELECT id, ttl FROM zones WHERE name=?"; |
Davide Pesavento | 093ce1c | 2019-01-21 19:22:42 -0500 | [diff] [blame] | 256 | int rc = sqlite3_prepare_v2(m_conn, sql, -1, &stmt, nullptr); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 257 | if (rc != SQLITE_OK) { |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 258 | BOOST_THROW_EXCEPTION(PrepareError(sql)); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 259 | } |
| 260 | |
Yumin Xia | 55a7cc4 | 2017-05-14 18:43:34 -0700 | [diff] [blame] | 261 | saveName(zone.getName(), stmt, 1); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 262 | |
| 263 | if (sqlite3_step(stmt) == SQLITE_ROW) { |
| 264 | zone.setId(sqlite3_column_int64(stmt, 0)); |
| 265 | zone.setTtl(time::seconds(sqlite3_column_int(stmt, 1))); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 266 | } |
| 267 | else { |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 268 | zone.setId(0); |
| 269 | } |
| 270 | |
| 271 | sqlite3_finalize(stmt); |
| 272 | |
| 273 | return zone.getId() != 0; |
| 274 | } |
| 275 | |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 276 | std::vector<Zone> |
| 277 | DbMgr::listZones() |
| 278 | { |
| 279 | sqlite3_stmt* stmt; |
| 280 | const char* sql = "SELECT id, name, ttl FROM zones"; |
Davide Pesavento | 093ce1c | 2019-01-21 19:22:42 -0500 | [diff] [blame] | 281 | int rc = sqlite3_prepare_v2(m_conn, sql, -1, &stmt, nullptr); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 282 | if (rc != SQLITE_OK) { |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 283 | BOOST_THROW_EXCEPTION(PrepareError(sql)); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | std::vector<Zone> vec; |
| 287 | |
| 288 | while (sqlite3_step(stmt) == SQLITE_ROW) { |
| 289 | vec.emplace_back(); |
| 290 | Zone& zone = vec.back(); |
| 291 | zone.setId(sqlite3_column_int64(stmt, 0)); |
| 292 | zone.setTtl(time::seconds(sqlite3_column_int(stmt, 2))); |
Yumin Xia | 55a7cc4 | 2017-05-14 18:43:34 -0700 | [diff] [blame] | 293 | zone.setName(restoreName(stmt, 1)); |
Jiewen Tan | 870b29b | 2014-11-17 19:09:49 -0800 | [diff] [blame] | 294 | } |
| 295 | sqlite3_finalize(stmt); |
| 296 | |
| 297 | return vec; |
| 298 | } |
| 299 | |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 300 | void |
| 301 | DbMgr::remove(Zone& zone) |
| 302 | { |
| 303 | if (zone.getId() == 0) |
| 304 | return; |
| 305 | |
| 306 | sqlite3_stmt* stmt; |
| 307 | const char* sql = "DELETE FROM zones where id=?"; |
Davide Pesavento | 093ce1c | 2019-01-21 19:22:42 -0500 | [diff] [blame] | 308 | int rc = sqlite3_prepare_v2(m_conn, sql, -1, &stmt, nullptr); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 309 | if (rc != SQLITE_OK) { |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 310 | BOOST_THROW_EXCEPTION(PrepareError(sql)); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | sqlite3_bind_int64(stmt, 1, zone.getId()); |
| 314 | |
| 315 | rc = sqlite3_step(stmt); |
| 316 | if (rc != SQLITE_DONE) { |
| 317 | sqlite3_finalize(stmt); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 318 | BOOST_THROW_EXCEPTION(ExecuteError(sql)); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | sqlite3_finalize(stmt); |
| 322 | |
| 323 | zone = Zone(); |
| 324 | } |
| 325 | |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 326 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 327 | // Rrset |
| 328 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 329 | |
| 330 | void |
| 331 | DbMgr::insert(Rrset& rrset) |
| 332 | { |
| 333 | if (rrset.getId() != 0) |
| 334 | return; |
| 335 | |
Davide Pesavento | 093ce1c | 2019-01-21 19:22:42 -0500 | [diff] [blame] | 336 | if (rrset.getZone() == nullptr) { |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 337 | BOOST_THROW_EXCEPTION(RrsetError("Rrset has not been assigned to a zone")); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | if (rrset.getZone()->getId() == 0) { |
| 341 | insert(*rrset.getZone()); |
| 342 | } |
| 343 | |
| 344 | const char* sql = |
| 345 | "INSERT INTO rrsets (zone_id, label, type, version, ttl, data)" |
| 346 | " VALUES (?, ?, ?, ?, ?, ?)"; |
| 347 | |
| 348 | sqlite3_stmt* stmt; |
Davide Pesavento | 093ce1c | 2019-01-21 19:22:42 -0500 | [diff] [blame] | 349 | int rc = sqlite3_prepare_v2(m_conn, sql, -1, &stmt, nullptr); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 350 | if (rc != SQLITE_OK) { |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 351 | BOOST_THROW_EXCEPTION(PrepareError(sql)); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | sqlite3_bind_int64(stmt, 1, rrset.getZone()->getId()); |
| 355 | |
Yumin Xia | 55a7cc4 | 2017-05-14 18:43:34 -0700 | [diff] [blame] | 356 | saveName(rrset.getLabel(), stmt, 2); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 357 | sqlite3_bind_blob(stmt, 3, rrset.getType().wire(), rrset.getType().size(), SQLITE_STATIC); |
| 358 | sqlite3_bind_blob(stmt, 4, rrset.getVersion().wire(), rrset.getVersion().size(), SQLITE_STATIC); |
| 359 | sqlite3_bind_int64(stmt, 5, rrset.getTtl().count()); |
| 360 | sqlite3_bind_blob(stmt, 6, rrset.getData().wire(), rrset.getData().size(), SQLITE_STATIC); |
| 361 | |
| 362 | rc = sqlite3_step(stmt); |
| 363 | if (rc != SQLITE_DONE) { |
| 364 | sqlite3_finalize(stmt); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 365 | BOOST_THROW_EXCEPTION(ExecuteError(sql)); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | rrset.setId(sqlite3_last_insert_rowid(m_conn)); |
| 369 | sqlite3_finalize(stmt); |
| 370 | } |
| 371 | |
| 372 | bool |
| 373 | DbMgr::find(Rrset& rrset) |
| 374 | { |
Davide Pesavento | 093ce1c | 2019-01-21 19:22:42 -0500 | [diff] [blame] | 375 | if (rrset.getZone() == nullptr) { |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 376 | BOOST_THROW_EXCEPTION(RrsetError("Rrset has not been assigned to a zone")); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | if (rrset.getZone()->getId() == 0) { |
| 380 | bool isFound = find(*rrset.getZone()); |
| 381 | if (!isFound) { |
| 382 | return false; |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | sqlite3_stmt* stmt; |
| 387 | const char* sql = |
| 388 | "SELECT id, ttl, version, data FROM rrsets" |
| 389 | " WHERE zone_id=? and label=? and type=?"; |
Davide Pesavento | 093ce1c | 2019-01-21 19:22:42 -0500 | [diff] [blame] | 390 | int rc = sqlite3_prepare_v2(m_conn, sql, -1, &stmt, nullptr); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 391 | if (rc != SQLITE_OK) { |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 392 | BOOST_THROW_EXCEPTION(PrepareError(sql)); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | sqlite3_bind_int64(stmt, 1, rrset.getZone()->getId()); |
| 396 | |
Yumin Xia | 55a7cc4 | 2017-05-14 18:43:34 -0700 | [diff] [blame] | 397 | saveName(rrset.getLabel(), stmt, 2); |
| 398 | sqlite3_bind_blob(stmt, 3, rrset.getType().wire(), rrset.getType().size(), SQLITE_STATIC); |
| 399 | |
| 400 | if (sqlite3_step(stmt) == SQLITE_ROW) { |
| 401 | rrset.setId(sqlite3_column_int64(stmt, 0)); |
| 402 | rrset.setTtl(time::seconds(sqlite3_column_int64(stmt, 1))); |
| 403 | rrset.setVersion(Block(static_cast<const uint8_t*>(sqlite3_column_blob(stmt, 2)), |
| 404 | sqlite3_column_bytes(stmt, 2))); |
| 405 | rrset.setData(Block(static_cast<const uint8_t*>(sqlite3_column_blob(stmt, 3)), |
| 406 | sqlite3_column_bytes(stmt, 3))); |
| 407 | } |
| 408 | else { |
| 409 | rrset.setId(0); |
| 410 | } |
| 411 | sqlite3_finalize(stmt); |
| 412 | |
| 413 | return rrset.getId() != 0; |
| 414 | } |
| 415 | |
| 416 | bool |
| 417 | DbMgr::findLowerBound(Rrset& rrset) |
| 418 | { |
Davide Pesavento | 093ce1c | 2019-01-21 19:22:42 -0500 | [diff] [blame] | 419 | if (rrset.getZone() == nullptr) { |
Yumin Xia | 55a7cc4 | 2017-05-14 18:43:34 -0700 | [diff] [blame] | 420 | BOOST_THROW_EXCEPTION(RrsetError("Rrset has not been assigned to a zone")); |
| 421 | } |
| 422 | |
| 423 | if (rrset.getZone()->getId() == 0) { |
| 424 | bool isFound = find(*rrset.getZone()); |
| 425 | if (!isFound) { |
| 426 | return false; |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | sqlite3_stmt* stmt; |
| 431 | const char* sql = |
| 432 | "SELECT id, ttl, version, data FROM rrsets" |
| 433 | " WHERE zone_id=? and label<? and type=? ORDER BY label DESC"; |
Davide Pesavento | 093ce1c | 2019-01-21 19:22:42 -0500 | [diff] [blame] | 434 | int rc = sqlite3_prepare_v2(m_conn, sql, -1, &stmt, nullptr); |
Yumin Xia | 55a7cc4 | 2017-05-14 18:43:34 -0700 | [diff] [blame] | 435 | if (rc != SQLITE_OK) { |
| 436 | BOOST_THROW_EXCEPTION(PrepareError(sql)); |
| 437 | } |
| 438 | |
| 439 | sqlite3_bind_int64(stmt, 1, rrset.getZone()->getId()); |
| 440 | |
| 441 | saveName(rrset.getLabel(), stmt, 2); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 442 | sqlite3_bind_blob(stmt, 3, rrset.getType().wire(), rrset.getType().size(), SQLITE_STATIC); |
| 443 | |
| 444 | if (sqlite3_step(stmt) == SQLITE_ROW) { |
| 445 | rrset.setId(sqlite3_column_int64(stmt, 0)); |
| 446 | rrset.setTtl(time::seconds(sqlite3_column_int64(stmt, 1))); |
| 447 | rrset.setVersion(Block(static_cast<const uint8_t*>(sqlite3_column_blob(stmt, 2)), |
| 448 | sqlite3_column_bytes(stmt, 2))); |
| 449 | rrset.setData(Block(static_cast<const uint8_t*>(sqlite3_column_blob(stmt, 3)), |
| 450 | sqlite3_column_bytes(stmt, 3))); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 451 | } |
| 452 | else { |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 453 | rrset.setId(0); |
| 454 | } |
| 455 | sqlite3_finalize(stmt); |
| 456 | |
| 457 | return rrset.getId() != 0; |
| 458 | } |
| 459 | |
| 460 | std::vector<Rrset> |
| 461 | DbMgr::findRrsets(Zone& zone) |
| 462 | { |
| 463 | if (zone.getId() == 0) |
| 464 | find(zone); |
| 465 | |
| 466 | if (zone.getId() == 0) |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 467 | BOOST_THROW_EXCEPTION(RrsetError("Attempting to find all the rrsets with a zone does not in the database")); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 468 | |
| 469 | std::vector<Rrset> vec; |
| 470 | sqlite3_stmt* stmt; |
| 471 | const char* sql = "SELECT id, ttl, version, data, label, type " |
Yumin Xia | 55a7cc4 | 2017-05-14 18:43:34 -0700 | [diff] [blame] | 472 | "FROM rrsets where zone_id=? ORDER BY label"; |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 473 | |
Davide Pesavento | 093ce1c | 2019-01-21 19:22:42 -0500 | [diff] [blame] | 474 | int rc = sqlite3_prepare_v2(m_conn, sql, -1, &stmt, nullptr); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 475 | if (rc != SQLITE_OK) { |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 476 | BOOST_THROW_EXCEPTION(PrepareError(sql)); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 477 | } |
| 478 | sqlite3_bind_int64(stmt, 1, zone.getId()); |
| 479 | |
| 480 | while (sqlite3_step(stmt) == SQLITE_ROW) { |
| 481 | vec.emplace_back(&zone); |
| 482 | Rrset& rrset = vec.back(); |
| 483 | |
| 484 | rrset.setId(sqlite3_column_int64(stmt, 0)); |
| 485 | rrset.setTtl(time::seconds(sqlite3_column_int64(stmt, 1))); |
| 486 | rrset.setVersion(Block(static_cast<const uint8_t*>(sqlite3_column_blob(stmt, 2)), |
| 487 | sqlite3_column_bytes(stmt, 2))); |
| 488 | rrset.setData(Block(static_cast<const uint8_t*>(sqlite3_column_blob(stmt, 3)), |
| 489 | sqlite3_column_bytes(stmt, 3))); |
Yumin Xia | 55a7cc4 | 2017-05-14 18:43:34 -0700 | [diff] [blame] | 490 | rrset.setLabel(restoreName(stmt, 4)); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 491 | rrset.setType(Block(static_cast<const uint8_t*>(sqlite3_column_blob(stmt, 5)), |
| 492 | sqlite3_column_bytes(stmt, 5))); |
| 493 | } |
| 494 | sqlite3_finalize(stmt); |
| 495 | |
| 496 | return vec; |
| 497 | } |
| 498 | |
Yumin Xia | 55a7cc4 | 2017-05-14 18:43:34 -0700 | [diff] [blame] | 499 | void |
| 500 | DbMgr::removeRrsetsOfZoneByType(Zone& zone, const name::Component& type) |
| 501 | { |
| 502 | if (zone.getId() == 0) |
| 503 | find(zone); |
| 504 | |
| 505 | if (zone.getId() == 0) |
| 506 | BOOST_THROW_EXCEPTION(RrsetError("Attempting to find all the rrsets with a zone does not in the database")); |
| 507 | |
| 508 | sqlite3_stmt* stmt; |
| 509 | const char* sql = "DELETE FROM rrsets WHERE zone_id = ? AND type = ?"; |
Davide Pesavento | 093ce1c | 2019-01-21 19:22:42 -0500 | [diff] [blame] | 510 | int rc = sqlite3_prepare_v2(m_conn, sql, -1, &stmt, nullptr); |
Yumin Xia | 55a7cc4 | 2017-05-14 18:43:34 -0700 | [diff] [blame] | 511 | if (rc != SQLITE_OK) { |
| 512 | BOOST_THROW_EXCEPTION(PrepareError(sql)); |
| 513 | } |
| 514 | |
| 515 | sqlite3_bind_int64(stmt, 1, zone.getId()); |
| 516 | sqlite3_bind_blob(stmt, 2, type.wire(), type.size(), SQLITE_STATIC); |
| 517 | |
| 518 | rc = sqlite3_step(stmt); |
| 519 | if (rc != SQLITE_DONE) { |
| 520 | sqlite3_finalize(stmt); |
| 521 | BOOST_THROW_EXCEPTION(ExecuteError(sql)); |
| 522 | } |
| 523 | sqlite3_finalize(stmt); |
| 524 | } |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 525 | |
| 526 | void |
| 527 | DbMgr::remove(Rrset& rrset) |
| 528 | { |
| 529 | if (rrset.getId() == 0) |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 530 | BOOST_THROW_EXCEPTION(RrsetError("Attempting to remove Rrset that has no assigned id")); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 531 | |
| 532 | sqlite3_stmt* stmt; |
| 533 | const char* sql = "DELETE FROM rrsets WHERE id=?"; |
Davide Pesavento | 093ce1c | 2019-01-21 19:22:42 -0500 | [diff] [blame] | 534 | int rc = sqlite3_prepare_v2(m_conn, sql, -1, &stmt, nullptr); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 535 | if (rc != SQLITE_OK) { |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 536 | BOOST_THROW_EXCEPTION(PrepareError(sql)); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | sqlite3_bind_int64(stmt, 1, rrset.getId()); |
| 540 | |
| 541 | rc = sqlite3_step(stmt); |
| 542 | if (rc != SQLITE_DONE) { |
| 543 | sqlite3_finalize(stmt); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 544 | BOOST_THROW_EXCEPTION(ExecuteError(sql)); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | sqlite3_finalize(stmt); |
| 548 | |
| 549 | rrset = Rrset(rrset.getZone()); |
| 550 | } |
| 551 | |
| 552 | void |
| 553 | DbMgr::update(Rrset& rrset) |
| 554 | { |
| 555 | if (rrset.getId() == 0) { |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 556 | BOOST_THROW_EXCEPTION(RrsetError("Attempting to replace Rrset that has no assigned id")); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 557 | } |
| 558 | |
Davide Pesavento | 093ce1c | 2019-01-21 19:22:42 -0500 | [diff] [blame] | 559 | if (rrset.getZone() == nullptr) { |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 560 | BOOST_THROW_EXCEPTION(RrsetError("Rrset has not been assigned to a zone")); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | sqlite3_stmt* stmt; |
| 564 | const char* sql = "UPDATE rrsets SET ttl=?, version=?, data=? WHERE id=?"; |
Davide Pesavento | 093ce1c | 2019-01-21 19:22:42 -0500 | [diff] [blame] | 565 | int rc = sqlite3_prepare_v2(m_conn, sql, -1, &stmt, nullptr); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 566 | if (rc != SQLITE_OK) { |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 567 | BOOST_THROW_EXCEPTION(PrepareError(sql)); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | sqlite3_bind_int64(stmt, 1, rrset.getTtl().count()); |
| 571 | sqlite3_bind_blob(stmt, 2, rrset.getVersion().wire(), rrset.getVersion().size(), SQLITE_STATIC); |
| 572 | sqlite3_bind_blob(stmt, 3, rrset.getData().wire(), rrset.getData().size(), SQLITE_STATIC); |
| 573 | sqlite3_bind_int64(stmt, 4, rrset.getId()); |
| 574 | |
| 575 | sqlite3_step(stmt); |
| 576 | sqlite3_finalize(stmt); |
| 577 | } |
| 578 | |
| 579 | } // namespace ndns |
| 580 | } // namespace ndn |