blob: bf98fe8d26eb7b588827bc2374a99224a038a55f [file] [log] [blame]
Alexander Afanasyev71b43e72012-12-27 01:03:43 -08001/* -*- 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#ifndef SQLITE_HELPER_H
23#define SQLITE_HELPER_H
24
Zhenkai Zhu2ac6cf42013-01-02 12:37:36 -080025#include <stdint.h>
Alexander Afanasyev71b43e72012-12-27 01:03:43 -080026#include <sqlite3.h>
27#include <openssl/evp.h>
28#include <boost/exception/all.hpp>
29#include <string>
Alexander Afanasyevae43c502012-12-29 17:26:37 -080030#include "hash-string-converter.h"
Alexander Afanasyev71b43e72012-12-27 01:03:43 -080031
32// temporarily ignoring all potential database errors
33
34class DbHelper
35{
36public:
37 DbHelper (const std::string &path);
38 ~DbHelper ();
39
Alexander Afanasyevae43c502012-12-29 17:26:37 -080040 // done
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080041 void
Alexander Afanasyevae43c502012-12-29 17:26:37 -080042 UpdateDeviceSeqno (const std::string &name, uint64_t seqNo);
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080043
Alexander Afanasyevae43c502012-12-29 17:26:37 -080044 // std::string
45 // LookupForwardingAlias (const std::string &deviceName);
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080046
Alexander Afanasyevae43c502012-12-29 17:26:37 -080047 // void
48 // UpdateForwardingAlias ();
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080049
Alexander Afanasyevae43c502012-12-29 17:26:37 -080050 // done
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080051 /**
52 * Create an entry in SyncLog and SyncStateNodes corresponding to the current state of SyncNodes
53 */
Alexander Afanasyevae43c502012-12-29 17:26:37 -080054 HashPtr
55 RememberStateInStateLog ();
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080056
Alexander Afanasyevae43c502012-12-29 17:26:37 -080057 // done
58 sqlite3_int64
59 LookupSyncLog (const std::string &stateHash);
60
61 // done
62 sqlite3_int64
63 LookupSyncLog (const Hash &stateHash);
64
65 // How difference is exposed will be determined later by the actual protocol
66 void
67 FindStateDifferences (const std::string &oldHash, const std::string &newHash);
68
69 void
70 FindStateDifferences (const Hash &oldHash, const Hash &newHash);
71
Alexander Afanasyev71b43e72012-12-27 01:03:43 -080072private:
73 static void
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080074 hash_xStep (sqlite3_context *context, int argc, sqlite3_value **argv);
Alexander Afanasyev71b43e72012-12-27 01:03:43 -080075
76 static void
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080077 hash_xFinal (sqlite3_context *context);
78
79 static void
80 hash2str_Func (sqlite3_context *context, int argc, sqlite3_value **argv);
81
82 static void
83 str2hash_Func (sqlite3_context *context, int argc, sqlite3_value **argv);
Alexander Afanasyev71b43e72012-12-27 01:03:43 -080084
85private:
86 sqlite3 *m_db;
87};
88
89namespace Error {
90struct Db : virtual boost::exception, virtual std::exception { };
91}
92
93
94#endif // SQLITE_HELPER_H