shockjiang | a5ae48c | 2014-07-27 23:21:41 -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 | |
| 20 | #include <iostream> |
| 21 | #include <fstream> |
| 22 | |
| 23 | #include "db-mgr.hpp" |
| 24 | |
| 25 | namespace ndn { |
| 26 | namespace ndns { |
| 27 | |
| 28 | DBMgr::DBMgr() |
shockjiang | 99ad389 | 2014-08-03 14:56:13 -0700 | [diff] [blame] | 29 | : m_dbfile("src/db/ndns-local.db") |
| 30 | , m_conn(0) |
| 31 | , m_reCode(-1) |
| 32 | , m_status(DBClosed) |
| 33 | , m_resultNum(0) |
shockjiang | a5ae48c | 2014-07-27 23:21:41 -0700 | [diff] [blame] | 34 | { |
| 35 | std::fstream file; |
shockjiang | 99ad389 | 2014-08-03 14:56:13 -0700 | [diff] [blame] | 36 | 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; |
shockjiang | a5ae48c | 2014-07-27 23:21:41 -0700 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | } |
| 44 | |
shockjiang | a5ae48c | 2014-07-27 23:21:41 -0700 | [diff] [blame] | 45 | DBMgr::~DBMgr() |
| 46 | { |
| 47 | } |
| 48 | |
| 49 | void DBMgr::open() |
| 50 | { |
| 51 | if (m_status == DBConnected) |
| 52 | return; |
| 53 | |
| 54 | m_reCode = sqlite3_open(this->m_dbfile.c_str(), &(this->m_conn)); |
shockjiang | 99ad389 | 2014-08-03 14:56:13 -0700 | [diff] [blame] | 55 | if (m_reCode != SQLITE_OK) { |
| 56 | m_err = "Cannot connect to the db: " + this->m_dbfile; |
shockjiang | a5ae48c | 2014-07-27 23:21:41 -0700 | [diff] [blame] | 57 | m_status = DBError; |
| 58 | //exit(1); |
shockjiang | 99ad389 | 2014-08-03 14:56:13 -0700 | [diff] [blame] | 59 | } else { |
| 60 | std::cout << "connect to the db: " << m_dbfile << std::endl; |
shockjiang | a5ae48c | 2014-07-27 23:21:41 -0700 | [diff] [blame] | 61 | } |
| 62 | m_status = DBConnected; |
| 63 | } |
| 64 | |
| 65 | void DBMgr::close() |
| 66 | { |
| 67 | if (m_status == DBClosed) |
| 68 | return; |
| 69 | |
| 70 | m_reCode = sqlite3_close(this->m_conn); |
shockjiang | 99ad389 | 2014-08-03 14:56:13 -0700 | [diff] [blame] | 71 | if (m_reCode != SQLITE_OK) { |
| 72 | m_err = "Cannot close the db: " + this->m_dbfile; |
| 73 | m_status = DBError; |
shockjiang | a5ae48c | 2014-07-27 23:21:41 -0700 | [diff] [blame] | 74 | } |
| 75 | m_status = DBClosed; |
| 76 | } |
| 77 | |
shockjiang | 99ad389 | 2014-08-03 14:56:13 -0700 | [diff] [blame] | 78 | void DBMgr::execute(std::string sql, |
| 79 | int (*callback)(void*, int, char**, char**), void * paras) |
shockjiang | a5ae48c | 2014-07-27 23:21:41 -0700 | [diff] [blame] | 80 | { |
| 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); |
shockjiang | 99ad389 | 2014-08-03 14:56:13 -0700 | [diff] [blame] | 87 | if (m_reCode != SQLITE_OK) { |
shockjiang | a5ae48c | 2014-07-27 23:21:41 -0700 | [diff] [blame] | 88 | m_status = DBError; |
| 89 | this->m_err.append(err_msg); |
shockjiang | 99ad389 | 2014-08-03 14:56:13 -0700 | [diff] [blame] | 90 | std::cout << this->m_err << std::endl; |
shockjiang | a5ae48c | 2014-07-27 23:21:41 -0700 | [diff] [blame] | 91 | } |
| 92 | this->close(); |
| 93 | } |
| 94 | |
shockjiang | 99ad389 | 2014-08-03 14:56:13 -0700 | [diff] [blame] | 95 | } //namespace ndns |
| 96 | } //namespace ndn |
shockjiang | a5ae48c | 2014-07-27 23:21:41 -0700 | [diff] [blame] | 97 | |