blob: 27d7db403a02c9451a8719babe3a9377276af205 [file] [log] [blame]
Shock Jiang3c723182014-09-10 16:41:18 -07001/* -*- 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
20#include "db-mgr.hpp"
21#include "../logger.hpp"
22
23#include <boost/algorithm/string/predicate.hpp>
24
25#include <iostream>
26#include <fstream>
27
28namespace ndn {
29namespace ndns {
30
31NDNS_LOG_INIT("DbMgr");
32
33static const std::string NDNS_SCHEMA = "\
34CREATE TABLE IF NOT EXISTS zones ( \n\
35 id INTEGER NOT NULL PRIMARY KEY, \n\
36 name blob NOT NULL UNIQUE, \n\
37 ttl integer(10) NOT NULL); \n\
38 \n\
39CREATE TABLE IF NOT EXISTS rrsets ( \n\
40 id INTEGER NOT NULL PRIMARY KEY, \n\
41 zone_id integer(10) NOT NULL, \n\
42 label blob NOT NULL, \n\
43 type blob NOT NULL, \n\
44 version blob NOT NULL, \n\
45 ttl integer(10) NOT NULL, \n\
46 data blob NOT NULL, \n\
47 FOREIGN KEY(zone_id) REFERENCES zones(id) ON UPDATE Cascade ON DELETE Cascade); \n\
48 \n\
49CREATE UNIQUE INDEX rrsets_zone_id_label_type_version \n\
50 ON rrsets (zone_id, label, type, version); \n\
51";
52
53DbMgr::DbMgr(const std::string& dbFile/* = DEFAULT_CONFIG_PATH "/" "ndns.conf"*/)
54 : m_dbFile(dbFile)
55 , m_conn(0)
56 , m_status(DB_CLOSED)
57{
58 this->open();
59}
60
61
62DbMgr::~DbMgr()
63{
64 if (m_status != DB_CLOSED) {
65 this->close();
66 }
67}
68
69void
70DbMgr::open()
71{
72 if (m_status == DB_CONNECTED)
73 return;
74
75 int res = sqlite3_open_v2(m_dbFile.c_str(), &m_conn,
76 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,
77#ifdef DISABLE_SQLITE3_FS_LOCKING
78 "unix-dotfile"
79#else
80 0
81#endif
82 );
83
84 if (res != SQLITE_OK) {
85 m_err = "Cannot open the db file: " + m_dbFile;
86 NDNS_LOG_FATAL(m_err);
87 m_status = DB_ERROR;
88 }
89
90 // ignore any errors from DB creation (command will fail for the existing database, which is ok)
91 sqlite3_exec(m_conn, NDNS_SCHEMA.c_str(), 0, 0, 0);
92
93 m_status = DB_CONNECTED;
94}
95
96void
97DbMgr::close()
98{
99 if (m_status == DB_CLOSED)
100 return;
101
102 int ret = sqlite3_close(m_conn);
103 if (ret != SQLITE_OK) {
104 m_err = "Cannot close the db: " + m_dbFile;
105 NDNS_LOG_FATAL(m_err);
106 m_status = DB_ERROR;
107 }
108 m_status = DB_CLOSED;
109}
110
111} // namespace ndns
112} // namespace ndn