Alexander Afanasyev | dfe5819 | 2013-01-17 17:34:04 -0800 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2012 University of California, Los Angeles |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation; |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | * |
| 18 | * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 19 | * Zhenkai Zhu <zhenkai@cs.ucla.edu> |
| 20 | */ |
| 21 | |
| 22 | #include "fetcher.h" |
| 23 | #include <boost/make_shared.hpp> |
| 24 | #include <boost/ref.hpp> |
| 25 | #include <boost/throw_exception.hpp> |
| 26 | |
| 27 | using namespace boost; |
| 28 | using namespace std; |
| 29 | namespace fs = boost::filesystem; |
| 30 | |
| 31 | const std::string INIT_DATABASE = "\ |
| 32 | PRAGMA foreign_keys = ON; \ |
| 33 | \ |
| 34 | CREATE TABLE \ |
| 35 | Queue( \ |
| 36 | name BLOB NOT NULL PRIMARY KEY, \ |
| 37 | seq_no_rcvd INTEGER, \ |
| 38 | seq_no_available INTEGER, \ |
| 39 | ); \ |
| 40 | "; |
| 41 | |
| 42 | Fetcher::Fetcher (const fs::path &path, const string &name) |
| 43 | { |
| 44 | fs::path chronoshareDirectory = path / ".chronoshare"; |
| 45 | fs::create_directories (chronoshareDirectory); |
| 46 | |
| 47 | int res = sqlite3_open((chronoshareDirectory / (name+".db")).c_str (), &m_db); |
| 48 | if (res != SQLITE_OK) |
| 49 | { |
| 50 | BOOST_THROW_EXCEPTION (Error::Fetcher () |
| 51 | << errmsg_info_str ("Cannot open/create dabatabase: [" + (chronoshareDirectory / (name+".db")).string () + "]")); |
| 52 | } |
| 53 | |
| 54 | char *errmsg = 0; |
| 55 | res = sqlite3_exec (m_db, INIT_DATABASE.c_str (), NULL, NULL, &errmsg); |
| 56 | if (res != SQLITE_OK && errmsg != 0) |
| 57 | { |
| 58 | // std::cerr << "DEBUG: " << errmsg << std::endl; |
| 59 | sqlite3_free (errmsg); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | Fetcher::~Fetcher () |
| 64 | { |
| 65 | int res = sqlite3_close (m_db); |
| 66 | if (res != SQLITE_OK) |
| 67 | { |
| 68 | // complain |
| 69 | } |
| 70 | } |