blob: baabad89505cd1fd0fef5fe579aa22d8b9508073 [file] [log] [blame]
shockjianga5ae48c2014-07-27 23:21:41 -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 <iostream>
21#include <fstream>
22
23#include "db-mgr.hpp"
24
25namespace ndn {
26namespace ndns {
27
28DBMgr::DBMgr()
shockjiang99ad3892014-08-03 14:56:13 -070029 : m_dbfile("src/db/ndns-local.db")
30 , m_conn(0)
31 , m_reCode(-1)
32 , m_status(DBClosed)
33 , m_resultNum(0)
shockjianga5ae48c2014-07-27 23:21:41 -070034{
35 std::fstream file;
shockjiang99ad3892014-08-03 14:56:13 -070036 file.open(m_dbfile, std::ios::in);
37 if (file) {
38 std::cout << "database file " << m_dbfile << " does exist" << std::endl;
39 } else {
40 std::cout << "database file " << m_dbfile << " does not exist" << std::endl;
shockjianga5ae48c2014-07-27 23:21:41 -070041 }
42
43}
44
shockjianga5ae48c2014-07-27 23:21:41 -070045DBMgr::~DBMgr()
46{
47}
48
49void DBMgr::open()
50{
51 if (m_status == DBConnected)
52 return;
53
54 m_reCode = sqlite3_open(this->m_dbfile.c_str(), &(this->m_conn));
shockjiang99ad3892014-08-03 14:56:13 -070055 if (m_reCode != SQLITE_OK) {
56 m_err = "Cannot connect to the db: " + this->m_dbfile;
shockjianga5ae48c2014-07-27 23:21:41 -070057 m_status = DBError;
58 //exit(1);
shockjiang99ad3892014-08-03 14:56:13 -070059 } else {
60 std::cout << "connect to the db: " << m_dbfile << std::endl;
shockjianga5ae48c2014-07-27 23:21:41 -070061 }
62 m_status = DBConnected;
63}
64
65void DBMgr::close()
66{
67 if (m_status == DBClosed)
68 return;
69
70 m_reCode = sqlite3_close(this->m_conn);
shockjiang99ad3892014-08-03 14:56:13 -070071 if (m_reCode != SQLITE_OK) {
72 m_err = "Cannot close the db: " + this->m_dbfile;
73 m_status = DBError;
shockjianga5ae48c2014-07-27 23:21:41 -070074 }
75 m_status = DBClosed;
76}
77
shockjiang99ad3892014-08-03 14:56:13 -070078void DBMgr::execute(std::string sql,
79 int (*callback)(void*, int, char**, char**), void * paras)
shockjianga5ae48c2014-07-27 23:21:41 -070080{
81 if (m_status == DBClosed)
82 this->open();
83
84 clearResultNum();
85 char *err_msg;
86 m_reCode = sqlite3_exec(m_conn, sql.c_str(), callback, paras, &err_msg);
shockjiang99ad3892014-08-03 14:56:13 -070087 if (m_reCode != SQLITE_OK) {
shockjianga5ae48c2014-07-27 23:21:41 -070088 m_status = DBError;
89 this->m_err.append(err_msg);
shockjiang99ad3892014-08-03 14:56:13 -070090 std::cout << this->m_err << std::endl;
shockjianga5ae48c2014-07-27 23:21:41 -070091 }
92 this->close();
93}
94
shockjiang99ad3892014-08-03 14:56:13 -070095} //namespace ndns
96} //namespace ndn
shockjianga5ae48c2014-07-27 23:21:41 -070097