blob: 384e74e0ffff537eebde285d84e035bff2050257 [file] [log] [blame]
Yingdi Yuede8eaf2013-10-14 14:07:03 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2013, Regents of the University of California
4 * Yingdi Yu
5 *
6 * BSD license, See the LICENSE file for more information
7 *
8 * Author: Yingdi Yu <yingdi@cs.ucla.edu>
9 */
10
11#include "contact-storage.h"
Yingdi Yuede8eaf2013-10-14 14:07:03 -070012
Yingdi Yuede8eaf2013-10-14 14:07:03 -070013#include <boost/filesystem.hpp>
Yingdi Yu0a6b6c52013-10-15 17:54:00 -070014#include "logging.h"
Yingdi Yuede8eaf2013-10-14 14:07:03 -070015
16using namespace std;
17using namespace ndn;
Yingdi Yu64206112013-12-24 11:16:32 +080018using namespace ndn::ptr_lib;
Yingdi Yuede8eaf2013-10-14 14:07:03 -070019namespace fs = boost::filesystem;
20
Yingdi Yu0a6b6c52013-10-15 17:54:00 -070021INIT_LOGGER ("ContactStorage");
22
23const string INIT_SP_TABLE = "\
24CREATE TABLE IF NOT EXISTS \n \
25 SelfProfile( \n \
Yingdi Yu49eea9f2013-10-17 15:07:17 -070026 profile_identity BLOB NOT NULL, \n \
Yingdi Yu0a6b6c52013-10-15 17:54:00 -070027 profile_type BLOB NOT NULL, \n \
Yingdi Yu49eea9f2013-10-17 15:07:17 -070028 profile_value BLOB NOT NULL, \n \
29 \
30 PRIMARY KEY (profile_identity, profile_type) \n \
Yingdi Yu0a6b6c52013-10-15 17:54:00 -070031 ); \n \
32 \
Yingdi Yu49eea9f2013-10-17 15:07:17 -070033CREATE INDEX sp_index ON SelfProfile(profile_identity,profile_type); \n \
Yingdi Yu0a6b6c52013-10-15 17:54:00 -070034";
35
Yingdi Yu590fa5d2013-10-18 18:35:09 -070036const string INIT_SE_TABLE = "\
Yingdi Yu0a6b6c52013-10-15 17:54:00 -070037CREATE TABLE IF NOT EXISTS \n \
Yingdi Yu590fa5d2013-10-18 18:35:09 -070038 SelfEndorse( \n \
Yingdi Yu0a6b6c52013-10-15 17:54:00 -070039 identity BLOB NOT NULL UNIQUE, \n \
Yingdi Yu590fa5d2013-10-18 18:35:09 -070040 endorse_data BLOB NOT NULL, \n \
Yingdi Yu0a6b6c52013-10-15 17:54:00 -070041 \
42 PRIMARY KEY (identity) \n \
43 ); \n \
Yingdi Yu590fa5d2013-10-18 18:35:09 -070044CREATE INDEX se_index ON SelfEndorse(identity); \n \
Yingdi Yu0a6b6c52013-10-15 17:54:00 -070045";
Yingdi Yuede8eaf2013-10-14 14:07:03 -070046
Yingdi Yu813d4e92013-11-03 16:22:05 -080047const string INIT_CONTACT_TABLE = "\
Yingdi Yuede8eaf2013-10-14 14:07:03 -070048CREATE TABLE IF NOT EXISTS \n \
Yingdi Yu813d4e92013-11-03 16:22:05 -080049 Contact( \n \
Yingdi Yuede8eaf2013-10-14 14:07:03 -070050 contact_namespace BLOB NOT NULL, \n \
51 contact_alias BLOB NOT NULL, \n \
52 self_certificate BLOB NOT NULL, \n \
Yingdi Yu813d4e92013-11-03 16:22:05 -080053 is_introducer INTEGER DEFAULT 0, \n \
Yingdi Yuede8eaf2013-10-14 14:07:03 -070054 \
55 PRIMARY KEY (contact_namespace) \n \
56 ); \n \
57 \
Yingdi Yu8dacdf22013-11-05 23:06:43 -080058CREATE INDEX contact_index ON Contact(contact_namespace); \n \
Yingdi Yuede8eaf2013-10-14 14:07:03 -070059";
60
Yingdi Yu813d4e92013-11-03 16:22:05 -080061const string INIT_TS_TABLE = "\
Yingdi Yuede8eaf2013-10-14 14:07:03 -070062CREATE TABLE IF NOT EXISTS \n \
Yingdi Yu813d4e92013-11-03 16:22:05 -080063 TrustScope( \n \
Yingdi Yu8dacdf22013-11-05 23:06:43 -080064 id INTEGER PRIMARY KEY AUTOINCREMENT, \n \
Yingdi Yuede8eaf2013-10-14 14:07:03 -070065 contact_namespace BLOB NOT NULL, \n \
Yingdi Yu8dacdf22013-11-05 23:06:43 -080066 trust_scope BLOB NOT NULL \n \
Yingdi Yuede8eaf2013-10-14 14:07:03 -070067 ); \n \
68 \
Yingdi Yu8dacdf22013-11-05 23:06:43 -080069CREATE INDEX ts_index ON TrustScope(contact_namespace); \n \
70";
71
72const string INIT_CP_TABLE = "\
73CREATE TABLE IF NOT EXISTS \n \
74 ContactProfile( \n \
75 profile_identity BLOB NOT NULL, \n \
76 profile_type BLOB NOT NULL, \n \
77 profile_value BLOB NOT NULL, \n \
78 endorse INTEGER NOT NULL, \n \
79 \
80 PRIMARY KEY (profile_identity, profile_type) \n \
81 ); \n \
82 \
83CREATE INDEX cp_index ON ContactProfile(profile_identity); \n \
84";
85
86const string INIT_PE_TABLE = "\
87CREATE TABLE IF NOT EXISTS \n \
88 ProfileEndorse( \n \
89 identity BLOB NOT NULL UNIQUE, \n \
90 endorse_data BLOB NOT NULL, \n \
91 \
92 PRIMARY KEY (identity) \n \
93 ); \n \
94 \
95CREATE INDEX pe_index ON ProfileEndorse(identity); \n \
96";
97
98const string INIT_CE_TABLE = "\
99CREATE TABLE IF NOT EXISTS \n \
100 CollectEndorse( \n \
101 endorsee BLOB NOT NULL, \n \
102 endorser BLOB NOT NULL, \n \
103 endorse_name BLOB NOT NULL, \n \
104 endorse_data BLOB NOT NULL, \n \
105 \
106 PRIMARY KEY (endorsee, endorser) \n \
107 ); \n \
108 \
109CREATE INDEX ce_index ON CollectEndorse(endorsee); \n \
Yingdi Yuede8eaf2013-10-14 14:07:03 -0700110";
111
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700112ContactStorage::ContactStorage()
Yingdi Yuede8eaf2013-10-14 14:07:03 -0700113{
114 fs::path chronosDir = fs::path(getenv("HOME")) / ".chronos";
115 fs::create_directories (chronosDir);
116
117 int res = sqlite3_open((chronosDir / "chronos.db").c_str (), &m_db);
118 if (res != SQLITE_OK)
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800119 throw Error("Chronos DB cannot be open/created");
Yingdi Yuede8eaf2013-10-14 14:07:03 -0700120
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700121 // Check if SelfProfile table exists
Yingdi Yuede8eaf2013-10-14 14:07:03 -0700122 sqlite3_stmt *stmt;
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700123 sqlite3_prepare_v2 (m_db, "SELECT name FROM sqlite_master WHERE type='table' And name='SelfProfile'", -1, &stmt, 0);
124 res = sqlite3_step (stmt);
125
126 bool spTableExist = false;
127 if (res == SQLITE_ROW)
128 spTableExist = true;
129 sqlite3_finalize (stmt);
130
131 if(!spTableExist)
132 {
133 char *errmsg = 0;
134 res = sqlite3_exec (m_db, INIT_SP_TABLE.c_str (), NULL, NULL, &errmsg);
135 if (res != SQLITE_OK && errmsg != 0)
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800136 throw Error("Init \"error\" in SelfProfile");
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700137 }
138
Yingdi Yu590fa5d2013-10-18 18:35:09 -0700139 // Check if SelfEndorse table exists
140 sqlite3_prepare_v2 (m_db, "SELECT name FROM sqlite_master WHERE type='table' And name='SelfEndorse'", -1, &stmt, 0);
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700141 res = sqlite3_step (stmt);
142
Yingdi Yu590fa5d2013-10-18 18:35:09 -0700143 bool seTableExist = false;
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700144 if (res == SQLITE_ROW)
Yingdi Yu590fa5d2013-10-18 18:35:09 -0700145 seTableExist = true;
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700146 sqlite3_finalize (stmt);
147
Yingdi Yu590fa5d2013-10-18 18:35:09 -0700148 if(!seTableExist)
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700149 {
150 char *errmsg = 0;
Yingdi Yu590fa5d2013-10-18 18:35:09 -0700151 res = sqlite3_exec (m_db, INIT_SE_TABLE.c_str (), NULL, NULL, &errmsg);
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700152 if (res != SQLITE_OK && errmsg != 0)
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800153 throw Error("Init \"error\" in SelfEndorse");
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700154 }
155
156
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800157 // Check if Contact table exists
Yingdi Yu813d4e92013-11-03 16:22:05 -0800158 sqlite3_prepare_v2 (m_db, "SELECT name FROM sqlite_master WHERE type='table' And name='Contact'", -1, &stmt, 0);
Yingdi Yuede8eaf2013-10-14 14:07:03 -0700159 res = sqlite3_step (stmt);
160
Yingdi Yu813d4e92013-11-03 16:22:05 -0800161 bool contactTableExist = false;
Yingdi Yuede8eaf2013-10-14 14:07:03 -0700162 if (res == SQLITE_ROW)
Yingdi Yu813d4e92013-11-03 16:22:05 -0800163 contactTableExist = true;
Yingdi Yuede8eaf2013-10-14 14:07:03 -0700164 sqlite3_finalize (stmt);
165
Yingdi Yu813d4e92013-11-03 16:22:05 -0800166 if(!contactTableExist)
Yingdi Yuede8eaf2013-10-14 14:07:03 -0700167 {
168 char *errmsg = 0;
Yingdi Yu813d4e92013-11-03 16:22:05 -0800169 res = sqlite3_exec (m_db, INIT_CONTACT_TABLE.c_str (), NULL, NULL, &errmsg);
Yingdi Yuede8eaf2013-10-14 14:07:03 -0700170 if (res != SQLITE_OK && errmsg != 0)
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800171 throw Error("Init \"error\" in Contact");
Yingdi Yuede8eaf2013-10-14 14:07:03 -0700172 }
Yingdi Yu813d4e92013-11-03 16:22:05 -0800173
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800174 // Check if TrustScope table exists
Yingdi Yu813d4e92013-11-03 16:22:05 -0800175 sqlite3_prepare_v2 (m_db, "SELECT name FROM sqlite_master WHERE type='table' And name='TrustScope'", -1, &stmt, 0);
Yingdi Yuede8eaf2013-10-14 14:07:03 -0700176 res = sqlite3_step (stmt);
177
Yingdi Yu813d4e92013-11-03 16:22:05 -0800178 bool tsTableExist = false;
Yingdi Yuede8eaf2013-10-14 14:07:03 -0700179 if (res == SQLITE_ROW)
Yingdi Yu813d4e92013-11-03 16:22:05 -0800180 tsTableExist = true;
Yingdi Yuede8eaf2013-10-14 14:07:03 -0700181 sqlite3_finalize (stmt);
182
Yingdi Yu813d4e92013-11-03 16:22:05 -0800183 if(!tsTableExist)
Yingdi Yuede8eaf2013-10-14 14:07:03 -0700184 {
185 char *errmsg = 0;
Yingdi Yu813d4e92013-11-03 16:22:05 -0800186 res = sqlite3_exec (m_db, INIT_TS_TABLE.c_str (), NULL, NULL, &errmsg);
Yingdi Yuede8eaf2013-10-14 14:07:03 -0700187 if (res != SQLITE_OK && errmsg != 0)
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800188 throw Error("Init \"error\" in TrustScope");
Yingdi Yuede8eaf2013-10-14 14:07:03 -0700189 }
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800190
191 // Check if ContactProfile table exists
192 sqlite3_prepare_v2 (m_db, "SELECT name FROM sqlite_master WHERE type='table' And name='ContactProfile'", -1, &stmt, 0);
193 res = sqlite3_step (stmt);
194
195 bool cpTableExist = false;
196 if (res == SQLITE_ROW)
197 cpTableExist = true;
198 sqlite3_finalize (stmt);
199
200 if(!cpTableExist)
201 {
202 char *errmsg = 0;
203 res = sqlite3_exec (m_db, INIT_CP_TABLE.c_str (), NULL, NULL, &errmsg);
204 if (res != SQLITE_OK && errmsg != 0)
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800205 throw Error("Init \"error\" in ContactProfile");
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800206 }
207
208 // Check if ProfileEndorse table exists
209 sqlite3_prepare_v2 (m_db, "SELECT name FROM sqlite_master WHERE type='table' And name='ProfileEndorse'", -1, &stmt, 0);
210 res = sqlite3_step (stmt);
211
212 bool peTableExist = false;
213 if (res == SQLITE_ROW)
214 peTableExist = true;
215 sqlite3_finalize (stmt);
216
217 if(!peTableExist)
218 {
219 char *errmsg = 0;
220 res = sqlite3_exec (m_db, INIT_PE_TABLE.c_str (), NULL, NULL, &errmsg);
221 if (res != SQLITE_OK && errmsg != 0)
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800222 throw Error("Init \"error\" in ProfileEndorse");
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800223 }
224
225 // Check if CollectEndorse table exists
226 sqlite3_prepare_v2 (m_db, "SELECT name FROM sqlite_master WHERE type='table' And name='CollectEndorse'", -1, &stmt, 0);
227 res = sqlite3_step (stmt);
228
229 bool ceTableExist = false;
230 if (res == SQLITE_ROW)
231 ceTableExist = true;
232 sqlite3_finalize (stmt);
233
234 if(!ceTableExist)
235 {
236 char *errmsg = 0;
237 res = sqlite3_exec (m_db, INIT_CE_TABLE.c_str (), NULL, NULL, &errmsg);
238 if (res != SQLITE_OK && errmsg != 0)
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800239 throw Error("Init \"error\" in CollectEndorse");
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800240 }
Yingdi Yuede8eaf2013-10-14 14:07:03 -0700241}
242
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700243bool
Yingdi Yu49eea9f2013-10-17 15:07:17 -0700244ContactStorage::doesSelfEntryExist(const Name& identity, const string& profileType)
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700245{
246 bool result = false;
247
248 sqlite3_stmt *stmt;
Yingdi Yu49eea9f2013-10-17 15:07:17 -0700249 sqlite3_prepare_v2 (m_db, "SELECT count(*) FROM SelfProfile WHERE profile_type=? and profile_identity=?", -1, &stmt, 0);
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700250 sqlite3_bind_text(stmt, 1, profileType.c_str(), profileType.size(), SQLITE_TRANSIENT);
Yingdi Yu49eea9f2013-10-17 15:07:17 -0700251 sqlite3_bind_text(stmt, 2, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT);
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700252
253 int res = sqlite3_step (stmt);
254
255 if (res == SQLITE_ROW)
256 {
257 int countAll = sqlite3_column_int (stmt, 0);
258 if (countAll > 0)
259 result = true;
260 }
261 sqlite3_finalize (stmt);
262 return result;
263}
264
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700265void
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800266ContactStorage::setSelfProfileEntry(const Name& identity, const string& profileType, const Buffer &profileValue)
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700267{
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700268 sqlite3_stmt *stmt;
Yingdi Yu49eea9f2013-10-17 15:07:17 -0700269 if(doesSelfEntryExist(identity, profileType))
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700270 {
Yingdi Yu49eea9f2013-10-17 15:07:17 -0700271 sqlite3_prepare_v2 (m_db, "UPDATE SelfProfile SET profile_value=? WHERE profile_type=? and profile_identity=?", -1, &stmt, 0);
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800272 sqlite3_bind_text(stmt, 1, reinterpret_cast<const char*>(profileValue.buf()), profileValue.size(), SQLITE_TRANSIENT);
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700273 sqlite3_bind_text(stmt, 2, profileType.c_str(), profileType.size(), SQLITE_TRANSIENT);
Yingdi Yu49eea9f2013-10-17 15:07:17 -0700274 sqlite3_bind_text(stmt, 3, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT);
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700275 }
276 else
277 {
Yingdi Yu49eea9f2013-10-17 15:07:17 -0700278 sqlite3_prepare_v2 (m_db, "INSERT INTO SelfProfile (profile_identity, profile_type, profile_value) values (?, ?, ?)", -1, &stmt, 0);
279 sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT);
280 sqlite3_bind_text(stmt, 2, profileType.c_str(), profileType.size(), SQLITE_TRANSIENT);
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800281 sqlite3_bind_text(stmt, 3, reinterpret_cast<const char*>(profileValue.buf()), profileValue.size(), SQLITE_TRANSIENT);
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700282 }
283 sqlite3_step (stmt);
284 sqlite3_finalize (stmt);
285}
286
Yingdi Yu64206112013-12-24 11:16:32 +0800287shared_ptr<Profile>
Yingdi Yu49eea9f2013-10-17 15:07:17 -0700288ContactStorage::getSelfProfile(const Name& identity)
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700289{
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700290 sqlite3_stmt *stmt;
Yingdi Yu64206112013-12-24 11:16:32 +0800291 shared_ptr<Profile> profile = make_shared<Profile>(identity);
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700292
Yingdi Yu79c25a22013-10-21 13:38:38 -0700293 sqlite3_prepare_v2(m_db, "SELECT profile_type, profile_value FROM SelfProfile WHERE profile_identity=?", -1, &stmt, 0);
Yingdi Yu49eea9f2013-10-17 15:07:17 -0700294 sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT);
Yingdi Yu79c25a22013-10-21 13:38:38 -0700295
296 while(sqlite3_step (stmt) == SQLITE_ROW)
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700297 {
298 string profileType(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0));
Yingdi Yu64206112013-12-24 11:16:32 +0800299 string profileValue(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 1)), sqlite3_column_bytes (stmt, 1));
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700300
301 profile->setProfileEntry(profileType, profileValue );
302 }
303
Yingdi Yu6a5b9f62013-11-06 23:00:21 -0800304 sqlite3_finalize(stmt);
305
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700306 return profile;
307}
Yingdi Yu908f8412013-11-09 00:03:26 -0800308
309void
310ContactStorage::removeContact(const Name& contactNameSpace)
311{
Yingdi Yu64206112013-12-24 11:16:32 +0800312 shared_ptr<ContactItem> contact = getContact(contactNameSpace);
Yingdi Yu908f8412013-11-09 00:03:26 -0800313 string identity = contactNameSpace.toUri();
314
315 if(contact == NULL)
316 return;
317
318 sqlite3_stmt *stmt;
319 sqlite3_prepare_v2 (m_db, "DELETE FROM Contact WHERE contact_namespace=?", -1, &stmt, 0);
320 sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size (), SQLITE_TRANSIENT);
321 int res = sqlite3_step (stmt);
322 sqlite3_finalize (stmt);
323
324 sqlite3_prepare_v2 (m_db, "DELETE FROM ContactProfile WHERE profile_identity=?", -1, &stmt, 0);
325 sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size (), SQLITE_TRANSIENT);
326 res = sqlite3_step (stmt);
327 sqlite3_finalize (stmt);
328
329 if(contact->isIntroducer())
330 {
331 sqlite3_prepare_v2 (m_db, "DELETE FROM TrustScope WHERE contact_namespace=?", -1, &stmt, 0);
332 sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size (), SQLITE_TRANSIENT);
333 res = sqlite3_step (stmt);
334 sqlite3_finalize (stmt);
335 }
336}
Yingdi Yuede8eaf2013-10-14 14:07:03 -0700337
338void
Yingdi Yu813d4e92013-11-03 16:22:05 -0800339ContactStorage::addContact(const ContactItem& contact)
Yingdi Yuede8eaf2013-10-14 14:07:03 -0700340{
Yingdi Yu813d4e92013-11-03 16:22:05 -0800341 if(doesContactExist(contact.getNameSpace()))
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800342 throw Error("Normal Contact has already existed");
Yingdi Yuede8eaf2013-10-14 14:07:03 -0700343
Yingdi Yu813d4e92013-11-03 16:22:05 -0800344 bool isIntroducer = contact.isIntroducer();
345
Yingdi Yuede8eaf2013-10-14 14:07:03 -0700346 sqlite3_stmt *stmt;
347 sqlite3_prepare_v2 (m_db,
Yingdi Yu813d4e92013-11-03 16:22:05 -0800348 "INSERT INTO Contact (contact_namespace, contact_alias, self_certificate, is_introducer) values (?, ?, ?, ?)",
Yingdi Yuede8eaf2013-10-14 14:07:03 -0700349 -1,
350 &stmt,
351 0);
352
Yingdi Yu813d4e92013-11-03 16:22:05 -0800353 sqlite3_bind_text(stmt, 1, contact.getNameSpace().toUri().c_str(), contact.getNameSpace().toUri().size (), SQLITE_TRANSIENT);
354 sqlite3_bind_text(stmt, 2, contact.getAlias().c_str(), contact.getAlias().size(), SQLITE_TRANSIENT);
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800355 const Block& selfCertificateBlock = contact.getSelfEndorseCertificate().wireEncode();
356 sqlite3_bind_text(stmt, 3, reinterpret_cast<const char*>(selfCertificateBlock.wire()), selfCertificateBlock.size(), SQLITE_TRANSIENT);
Yingdi Yu813d4e92013-11-03 16:22:05 -0800357 sqlite3_bind_int(stmt, 4, (isIntroducer ? 1 : 0));
Yingdi Yuede8eaf2013-10-14 14:07:03 -0700358
359 int res = sqlite3_step (stmt);
360 sqlite3_finalize (stmt);
Yingdi Yu813d4e92013-11-03 16:22:05 -0800361
Yingdi Yu64206112013-12-24 11:16:32 +0800362 const Profile& profile = contact.getSelfEndorseCertificate().getProfileData().getProfile();
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800363 Profile::const_iterator it = profile.begin();
364 string identity = contact.getNameSpace().toUri();
365 for(; it != profile.end(); it++)
366 {
367 sqlite3_prepare_v2 (m_db,
368 "INSERT INTO ContactProfile (profile_identity, profile_type, profile_value, endorse) values (?, ?, ?, 0)",
369 -1,
370 &stmt,
371 0);
372 sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size (), SQLITE_TRANSIENT);
373 sqlite3_bind_text(stmt, 2, it->first.c_str(), it->first.size(), SQLITE_TRANSIENT);
Yingdi Yu64206112013-12-24 11:16:32 +0800374 sqlite3_bind_text(stmt, 3, it->second.c_str(), it->second.size(), SQLITE_TRANSIENT);
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800375 res = sqlite3_step (stmt);
376 sqlite3_finalize (stmt);
377 }
378
Yingdi Yu813d4e92013-11-03 16:22:05 -0800379 if(isIntroducer)
380 {
381 const vector<Name>& scopeList = contact.getTrustScopeList();
382 vector<Name>::const_iterator it = scopeList.begin();
383 string nameSpace = contact.getNameSpace().toUri();
384
385 while(it != scopeList.end())
386 {
387 sqlite3_prepare_v2 (m_db,
388 "INSERT INTO TrustScope (contact_namespace, trust_scope) values (?, ?)",
389 -1,
390 &stmt,
391 0);
392 sqlite3_bind_text(stmt, 1, nameSpace.c_str(), nameSpace.size (), SQLITE_TRANSIENT);
393 sqlite3_bind_text(stmt, 2, it->toUri().c_str(), it->toUri().size(), SQLITE_TRANSIENT);
394 res = sqlite3_step (stmt);
395 sqlite3_finalize (stmt);
396 it++;
397 }
398 }
Yingdi Yuede8eaf2013-10-14 14:07:03 -0700399}
400
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800401void
402ContactStorage::updateIsIntroducer(const ndn::Name& identity, bool isIntroducer)
403{
404 sqlite3_stmt *stmt;
405 sqlite3_prepare_v2 (m_db, "UPDATE Contact SET is_introducer=? WHERE contact_namespace=?", -1, &stmt, 0);
406 sqlite3_bind_int(stmt, 1, (isIntroducer ? 1 : 0));
407 sqlite3_bind_text(stmt, 2, identity.toUri().c_str(), identity.toUri().size (), SQLITE_TRANSIENT);
408 int res = sqlite3_step (stmt);
409 sqlite3_finalize (stmt);
410 return;
411}
412
Yingdi Yu79c25a22013-10-21 13:38:38 -0700413void
414ContactStorage::updateAlias(const ndn::Name& identity, std::string alias)
415{
Yingdi Yu813d4e92013-11-03 16:22:05 -0800416 sqlite3_stmt *stmt;
417 sqlite3_prepare_v2 (m_db, "UPDATE Contact SET contact_alias=? WHERE contact_namespace=?", -1, &stmt, 0);
418 sqlite3_bind_text(stmt, 1, alias.c_str(), alias.size(), SQLITE_TRANSIENT);
419 sqlite3_bind_text(stmt, 2, identity.toUri().c_str(), identity.toUri().size (), SQLITE_TRANSIENT);
420 int res = sqlite3_step (stmt);
421 sqlite3_finalize (stmt);
422 return;
Yingdi Yu79c25a22013-10-21 13:38:38 -0700423}
424
Yingdi Yuede8eaf2013-10-14 14:07:03 -0700425bool
Yingdi Yu813d4e92013-11-03 16:22:05 -0800426ContactStorage::doesContactExist(const Name& name)
Yingdi Yuede8eaf2013-10-14 14:07:03 -0700427{
428 bool result = false;
429
430 sqlite3_stmt *stmt;
Yingdi Yu813d4e92013-11-03 16:22:05 -0800431 sqlite3_prepare_v2 (m_db, "SELECT count(*) FROM Contact WHERE contact_namespace=?", -1, &stmt, 0);
Yingdi Yuede8eaf2013-10-14 14:07:03 -0700432 sqlite3_bind_text(stmt, 1, name.toUri().c_str(), name.toUri().size(), SQLITE_TRANSIENT);
433
434 int res = sqlite3_step (stmt);
435
436 if (res == SQLITE_ROW)
437 {
438 int countAll = sqlite3_column_int (stmt, 0);
439 if (countAll > 0)
440 result = true;
441 }
442 sqlite3_finalize (stmt);
443 return result;
444}
445
Yingdi Yu64206112013-12-24 11:16:32 +0800446void
447ContactStorage::getAllContacts(vector<shared_ptr<ContactItem> >& contacts) const
Yingdi Yuede8eaf2013-10-14 14:07:03 -0700448{
Yingdi Yuede8eaf2013-10-14 14:07:03 -0700449 sqlite3_stmt *stmt;
Yingdi Yu813d4e92013-11-03 16:22:05 -0800450 sqlite3_prepare_v2 (m_db, "SELECT contact_alias, self_certificate, is_introducer FROM Contact", -1, &stmt, 0);
Yingdi Yuede8eaf2013-10-14 14:07:03 -0700451
452 while( sqlite3_step (stmt) == SQLITE_ROW)
453 {
454 string alias(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0));
Yingdi Yu64206112013-12-24 11:16:32 +0800455
456 Data certData;
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800457 certData.wireDecode(Block(reinterpret_cast<const uint8_t*>(sqlite3_column_text(stmt, 1)), sqlite3_column_bytes (stmt, 1)));
Yingdi Yu64206112013-12-24 11:16:32 +0800458 EndorseCertificate endorseCertificate(certData);
459
Yingdi Yu813d4e92013-11-03 16:22:05 -0800460 int isIntroducer = sqlite3_column_int (stmt, 2);
Yingdi Yuede8eaf2013-10-14 14:07:03 -0700461
Yingdi Yu64206112013-12-24 11:16:32 +0800462 contacts.push_back(make_shared<ContactItem>(endorseCertificate, isIntroducer, alias));
Yingdi Yuede8eaf2013-10-14 14:07:03 -0700463 }
Yingdi Yu813d4e92013-11-03 16:22:05 -0800464 sqlite3_finalize (stmt);
465
Yingdi Yu64206112013-12-24 11:16:32 +0800466 vector<shared_ptr<ContactItem> >::iterator it = contacts.begin();
Yingdi Yu813d4e92013-11-03 16:22:05 -0800467 for(; it != contacts.end(); it++)
468 {
469 if((*it)->isIntroducer())
470 {
471 sqlite3_prepare_v2 (m_db, "SELECT trust_scope FROM TrustScope WHERE contact_namespace=?", -1, &stmt, 0);
472 sqlite3_bind_text(stmt, 1, (*it)->getNameSpace().toUri().c_str(), (*it)->getNameSpace().toUri().size(), SQLITE_TRANSIENT);
473
474 while( sqlite3_step (stmt) == SQLITE_ROW)
475 {
476 Name scope(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0)));
477 (*it)->addTrustScope(scope);
478 }
479 sqlite3_finalize (stmt);
480 }
481 }
Yingdi Yuede8eaf2013-10-14 14:07:03 -0700482}
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700483
Yingdi Yu64206112013-12-24 11:16:32 +0800484shared_ptr<ContactItem>
Yingdi Yu813d4e92013-11-03 16:22:05 -0800485ContactStorage::getContact(const Name& name)
Yingdi Yud40226b2013-10-23 14:05:12 -0700486{
487 sqlite3_stmt *stmt;
Yingdi Yu813d4e92013-11-03 16:22:05 -0800488 sqlite3_prepare_v2 (m_db, "SELECT contact_alias, self_certificate, is_introducer FROM Contact where contact_namespace=?", -1, &stmt, 0);
Yingdi Yud40226b2013-10-23 14:05:12 -0700489 sqlite3_bind_text (stmt, 1, name.toUri().c_str(), name.toUri().size(), SQLITE_TRANSIENT);
490
491 if( sqlite3_step (stmt) == SQLITE_ROW)
492 {
493 string alias(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0));
Yingdi Yu64206112013-12-24 11:16:32 +0800494
495 Data certData;
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800496 certData.wireDecode(Block(reinterpret_cast<const uint8_t*>(sqlite3_column_text(stmt, 1)), sqlite3_column_bytes (stmt, 1)));
Yingdi Yu64206112013-12-24 11:16:32 +0800497 EndorseCertificate endorseCertificate(certData);
498
Yingdi Yu813d4e92013-11-03 16:22:05 -0800499 int isIntroducer = sqlite3_column_int (stmt, 2);
Yingdi Yud40226b2013-10-23 14:05:12 -0700500
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800501 sqlite3_finalize (stmt);
502
Yingdi Yu64206112013-12-24 11:16:32 +0800503 shared_ptr<ContactItem> contact = make_shared<ContactItem>(endorseCertificate, isIntroducer, alias);
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800504
505 if(contact->isIntroducer())
506 {
507 sqlite3_prepare_v2 (m_db, "SELECT trust_scope FROM TrustScope WHERE contact_namespace=?", -1, &stmt, 0);
508 sqlite3_bind_text(stmt, 1, name.toUri().c_str(), name.toUri().size(), SQLITE_TRANSIENT);
509
510 while( sqlite3_step (stmt) == SQLITE_ROW)
511 {
512 Name scope(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0)));
513 contact->addTrustScope(scope);
514 }
515 sqlite3_finalize (stmt);
516 }
517
518 return contact;
Yingdi Yud40226b2013-10-23 14:05:12 -0700519 }
Yingdi Yueaa84e22014-01-16 10:30:26 -0800520 return shared_ptr<ContactItem>();
Yingdi Yud40226b2013-10-23 14:05:12 -0700521}
522
Yingdi Yu64206112013-12-24 11:16:32 +0800523shared_ptr<Profile>
Yingdi Yu49eea9f2013-10-17 15:07:17 -0700524ContactStorage::getSelfProfile(const Name& identity) const
525{
Yingdi Yu64206112013-12-24 11:16:32 +0800526 shared_ptr<Profile> profile = make_shared<Profile>(identity);
Yingdi Yu49eea9f2013-10-17 15:07:17 -0700527 sqlite3_stmt *stmt;
528 sqlite3_prepare_v2 (m_db, "SELECT profile_type, profile_value FROM SelfProfile WHERE profile_identity=?", -1, &stmt, 0);
529 sqlite3_bind_text (stmt, 1, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT);
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700530
531 while( sqlite3_step (stmt) == SQLITE_ROW)
532 {
533 string profileType(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0));
Yingdi Yu64206112013-12-24 11:16:32 +0800534 string profileValue(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 1)), sqlite3_column_bytes (stmt, 1));
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700535 profile->setProfileEntry(profileType, profileValue);
536 }
537
Yingdi Yu6a5b9f62013-11-06 23:00:21 -0800538 sqlite3_finalize(stmt);
539
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700540 return profile;
541}
542
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800543Block
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700544ContactStorage::getSelfEndorseCertificate(const Name& identity)
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700545{
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700546 sqlite3_stmt *stmt;
Yingdi Yu590fa5d2013-10-18 18:35:09 -0700547 sqlite3_prepare_v2 (m_db, "SELECT endorse_data FROM SelfEndorse where identity=?", -1, &stmt, 0);
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700548 sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT);
Yingdi Yu49eea9f2013-10-17 15:07:17 -0700549
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700550 if(sqlite3_step (stmt) == SQLITE_ROW)
Yingdi Yu64206112013-12-24 11:16:32 +0800551 {
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800552 Block result(reinterpret_cast<const uint8_t*>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0));
Yingdi Yu64206112013-12-24 11:16:32 +0800553 sqlite3_finalize (stmt);
554 return result;
555 }
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700556
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700557 sqlite3_finalize (stmt);
Yingdi Yu68aced92013-10-17 21:13:03 -0700558
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800559 throw Error("ContactStorage: No self-endorse certificate found!");
560
561 return Block();
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700562}
563
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700564void
Yingdi Yu64206112013-12-24 11:16:32 +0800565ContactStorage::updateSelfEndorseCertificate(const EndorseCertificate& newEndorseCertificate, const Name& identity)
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700566{
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800567 const Block& newEndorseCertificateBlock = newEndorseCertificate.wireEncode();
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700568
569 sqlite3_stmt *stmt;
Yingdi Yu590fa5d2013-10-18 18:35:09 -0700570 sqlite3_prepare_v2 (m_db, "UPDATE SelfEndorse SET endorse_data=? WHERE identity=?", -1, &stmt, 0);
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800571 sqlite3_bind_text(stmt, 1, reinterpret_cast<const char*>(newEndorseCertificateBlock.wire()), newEndorseCertificateBlock.size(), SQLITE_TRANSIENT);
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700572 sqlite3_bind_text(stmt, 2, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT);
573 sqlite3_step(stmt);
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800574
575 sqlite3_finalize (stmt);
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700576}
577
578void
Yingdi Yu64206112013-12-24 11:16:32 +0800579ContactStorage::addSelfEndorseCertificate(const EndorseCertificate& newEndorseCertificate, const Name& identity)
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700580{
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800581 const Block& newEndorseCertificateBlock = newEndorseCertificate.wireEncode();
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700582
583 sqlite3_stmt *stmt;
Yingdi Yu590fa5d2013-10-18 18:35:09 -0700584 sqlite3_prepare_v2 (m_db, "INSERT INTO SelfEndorse (identity, endorse_data) values (?, ?)", -1, &stmt, 0);
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700585 sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT);
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800586 sqlite3_bind_text(stmt, 2, reinterpret_cast<const char*>(newEndorseCertificateBlock.wire()), newEndorseCertificateBlock.size(), SQLITE_TRANSIENT);
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700587 sqlite3_step(stmt);
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800588
589 sqlite3_finalize (stmt);
590}
591
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800592Block
Yingdi Yu64206112013-12-24 11:16:32 +0800593ContactStorage::getEndorseCertificate(const Name& identity)
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800594{
595 sqlite3_stmt *stmt;
596 sqlite3_prepare_v2 (m_db, "SELECT endorse_data FROM ProfileEndorse where identity=?", -1, &stmt, 0);
597 sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT);
598
Yingdi Yu64206112013-12-24 11:16:32 +0800599
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800600 if(sqlite3_step (stmt) == SQLITE_ROW)
Yingdi Yu64206112013-12-24 11:16:32 +0800601 {
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800602 Block result(reinterpret_cast<const uint8_t*>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0));
Yingdi Yu64206112013-12-24 11:16:32 +0800603 sqlite3_finalize (stmt);
604 return result;
605 }
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800606
607 sqlite3_finalize (stmt);
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800608
609 throw Error("ContactStorage: No endorse certificate found!");
Yingdi Yu64206112013-12-24 11:16:32 +0800610
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800611 return Block();
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800612}
613
614void
Yingdi Yu64206112013-12-24 11:16:32 +0800615ContactStorage::updateEndorseCertificate(const EndorseCertificate& endorseCertificate, const Name& identity)
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800616{
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800617 const Block& newEndorseCertificateBlock = endorseCertificate.wireEncode();
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800618
619 sqlite3_stmt *stmt;
620 sqlite3_prepare_v2 (m_db, "UPDATE ProfileEndorse SET endorse_data=? WHERE identity=?", -1, &stmt, 0);
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800621 sqlite3_bind_text(stmt, 1, reinterpret_cast<const char*>(newEndorseCertificateBlock.wire()), newEndorseCertificateBlock.size(), SQLITE_TRANSIENT);
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800622 sqlite3_bind_text(stmt, 2, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT);
623 sqlite3_step(stmt);
624
625 sqlite3_finalize (stmt);
626}
627
628void
Yingdi Yu64206112013-12-24 11:16:32 +0800629ContactStorage::addEndorseCertificate(const EndorseCertificate& endorseCertificate, const Name& identity)
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800630{
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800631 const Block& newEndorseCertificateBlock = endorseCertificate.wireEncode();
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800632
633 sqlite3_stmt *stmt;
634 sqlite3_prepare_v2 (m_db, "INSERT INTO ProfileEndorse (identity, endorse_data) values (?, ?)", -1, &stmt, 0);
635 sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT);
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800636 sqlite3_bind_text(stmt, 2, reinterpret_cast<const char*>(newEndorseCertificateBlock.value()), newEndorseCertificateBlock.size(), SQLITE_TRANSIENT);
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800637 sqlite3_step(stmt);
638
639 sqlite3_finalize (stmt);
640}
641
Yingdi Yu64206112013-12-24 11:16:32 +0800642void
643ContactStorage::getEndorseList(const Name& identity, vector<string>& endorseList)
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800644{
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800645 sqlite3_stmt *stmt;
646 sqlite3_prepare_v2 (m_db, "SELECT profile_type FROM ContactProfile WHERE profile_identity=? AND endorse=1 ORDER BY profile_type", -1, &stmt, 0);
647 sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT);
648
649 while( sqlite3_step (stmt) == SQLITE_ROW)
650 {
651 string profileType(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0));
652 endorseList.push_back(profileType);
653 }
654 sqlite3_finalize (stmt);
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800655}
656
657void
658ContactStorage::updateCollectEndorse(const EndorseCertificate& endorseCertificate)
659{
660 Name endorserName = endorseCertificate.getSigner();
661 Name keyName = endorseCertificate.getPublicKeyName();
662 Name endorseeName = keyName.getPrefix(keyName.size()-1);
663 Name getCertName = endorseCertificate.getName();
664 Name oldCertName;
665 bool insert = true;
666 bool update = true;
667
668 sqlite3_stmt *stmt;
669 sqlite3_prepare_v2 (m_db, "SELECT endorse_name FROM CollectEndorse WHERE endorser=? AND endorsee=?", -1, &stmt, 0);
670 sqlite3_bind_text(stmt, 1, endorserName.toUri().c_str(), endorserName.toUri().size(), SQLITE_TRANSIENT);
671 sqlite3_bind_text(stmt, 2, endorseeName.toUri().c_str(), endorseeName.toUri().size(), SQLITE_TRANSIENT);
672
673 if(sqlite3_step (stmt) == SQLITE_ROW)
674 {
675 insert = false;
676 oldCertName = Name(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0)));
677 if(getCertName == oldCertName)
678 update = false;
679 }
680 sqlite3_finalize (stmt);
681
682 if(insert)
683 {
684 sqlite3_prepare_v2 (m_db, "INSERT INTO CollectEndorse (endorser, endorsee, endorse_name, endorse_data) VALUES (?, ?, ?, ?)", -1, &stmt, 0);
685 sqlite3_bind_text(stmt, 1, endorserName.toUri().c_str(), endorserName.toUri().size(), SQLITE_TRANSIENT);
686 sqlite3_bind_text(stmt, 2, endorseeName.toUri().c_str(), endorseeName.toUri().size(), SQLITE_TRANSIENT);
687 sqlite3_bind_text(stmt, 3, getCertName.toUri().c_str(), getCertName.toUri().size(), SQLITE_TRANSIENT);
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800688 const Block &block = endorseCertificate.wireEncode();
689 sqlite3_bind_text(stmt, 4, reinterpret_cast<const char*>(block.wire()), block.size(), SQLITE_TRANSIENT);
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800690 int res = sqlite3_step (stmt);
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800691 sqlite3_finalize (stmt);
692 return;
693 }
694 if(update)
695 {
696 sqlite3_prepare_v2 (m_db, "UPDATE CollectEndorse SET endorse_name=?, endorse_data=? WHERE endorser=? AND endorsee=?", -1, &stmt, 0);
697 sqlite3_bind_text(stmt, 1, getCertName.toUri().c_str(), getCertName.toUri().size(), SQLITE_TRANSIENT);
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800698 const Block &block = endorseCertificate.wireEncode();
699 sqlite3_bind_text(stmt, 2, reinterpret_cast<const char*>(block.value()), block.size(), SQLITE_TRANSIENT);
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800700 sqlite3_bind_text(stmt, 3, endorserName.toUri().c_str(), endorserName.toUri().size(), SQLITE_TRANSIENT);
701 sqlite3_bind_text(stmt, 4, endorseeName.toUri().c_str(), endorseeName.toUri().size(), SQLITE_TRANSIENT);
702 int res = sqlite3_step (stmt);
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800703 sqlite3_finalize (stmt);
704 return;
705 }
706}
707
Yingdi Yu64206112013-12-24 11:16:32 +0800708void
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800709ContactStorage::getCollectEndorseList(const Name& name, vector<Buffer>& endorseList)
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800710{
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800711 sqlite3_stmt *stmt;
712 sqlite3_prepare_v2 (m_db, "SELECT endorse_data FROM CollectEndorse WHERE endorsee=?", -1, &stmt, 0);
713 sqlite3_bind_text(stmt, 1, name.toUri().c_str(), name.toUri().size(), SQLITE_TRANSIENT);
714
715 while(sqlite3_step (stmt) == SQLITE_ROW)
716 {
Yingdi Yuc9ffa9f2014-01-13 11:19:47 -0800717 Buffer blob(reinterpret_cast<const uint8_t*>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0));
Yingdi Yu64206112013-12-24 11:16:32 +0800718 endorseList.push_back(blob);
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800719 }
720
Yingdi Yu6a5b9f62013-11-06 23:00:21 -0800721 sqlite3_finalize (stmt);
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700722}