blob: 61ed746a88464b93d7cf4de6fed02a7f624681ab [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 Yu64206112013-12-24 11:16:32 +080012#include "null-ptrs.h"
Yingdi Yuede8eaf2013-10-14 14:07:03 -070013#include "exception.h"
14
Yingdi Yuede8eaf2013-10-14 14:07:03 -070015#include <boost/filesystem.hpp>
Yingdi Yu0a6b6c52013-10-15 17:54:00 -070016#include "logging.h"
Yingdi Yuede8eaf2013-10-14 14:07:03 -070017
18using namespace std;
19using namespace ndn;
Yingdi Yu64206112013-12-24 11:16:32 +080020using namespace ndn::ptr_lib;
Yingdi Yuede8eaf2013-10-14 14:07:03 -070021namespace fs = boost::filesystem;
22
Yingdi Yu0a6b6c52013-10-15 17:54:00 -070023INIT_LOGGER ("ContactStorage");
24
25const string INIT_SP_TABLE = "\
26CREATE TABLE IF NOT EXISTS \n \
27 SelfProfile( \n \
Yingdi Yu49eea9f2013-10-17 15:07:17 -070028 profile_identity BLOB NOT NULL, \n \
Yingdi Yu0a6b6c52013-10-15 17:54:00 -070029 profile_type BLOB NOT NULL, \n \
Yingdi Yu49eea9f2013-10-17 15:07:17 -070030 profile_value BLOB NOT NULL, \n \
31 \
32 PRIMARY KEY (profile_identity, profile_type) \n \
Yingdi Yu0a6b6c52013-10-15 17:54:00 -070033 ); \n \
34 \
Yingdi Yu49eea9f2013-10-17 15:07:17 -070035CREATE INDEX sp_index ON SelfProfile(profile_identity,profile_type); \n \
Yingdi Yu0a6b6c52013-10-15 17:54:00 -070036";
37
Yingdi Yu590fa5d2013-10-18 18:35:09 -070038const string INIT_SE_TABLE = "\
Yingdi Yu0a6b6c52013-10-15 17:54:00 -070039CREATE TABLE IF NOT EXISTS \n \
Yingdi Yu590fa5d2013-10-18 18:35:09 -070040 SelfEndorse( \n \
Yingdi Yu0a6b6c52013-10-15 17:54:00 -070041 identity BLOB NOT NULL UNIQUE, \n \
Yingdi Yu590fa5d2013-10-18 18:35:09 -070042 endorse_data BLOB NOT NULL, \n \
Yingdi Yu0a6b6c52013-10-15 17:54:00 -070043 \
44 PRIMARY KEY (identity) \n \
45 ); \n \
Yingdi Yu590fa5d2013-10-18 18:35:09 -070046CREATE INDEX se_index ON SelfEndorse(identity); \n \
Yingdi Yu0a6b6c52013-10-15 17:54:00 -070047";
Yingdi Yuede8eaf2013-10-14 14:07:03 -070048
Yingdi Yu813d4e92013-11-03 16:22:05 -080049const string INIT_CONTACT_TABLE = "\
Yingdi Yuede8eaf2013-10-14 14:07:03 -070050CREATE TABLE IF NOT EXISTS \n \
Yingdi Yu813d4e92013-11-03 16:22:05 -080051 Contact( \n \
Yingdi Yuede8eaf2013-10-14 14:07:03 -070052 contact_namespace BLOB NOT NULL, \n \
53 contact_alias BLOB NOT NULL, \n \
54 self_certificate BLOB NOT NULL, \n \
Yingdi Yu813d4e92013-11-03 16:22:05 -080055 is_introducer INTEGER DEFAULT 0, \n \
Yingdi Yuede8eaf2013-10-14 14:07:03 -070056 \
57 PRIMARY KEY (contact_namespace) \n \
58 ); \n \
59 \
Yingdi Yu8dacdf22013-11-05 23:06:43 -080060CREATE INDEX contact_index ON Contact(contact_namespace); \n \
Yingdi Yuede8eaf2013-10-14 14:07:03 -070061";
62
Yingdi Yu813d4e92013-11-03 16:22:05 -080063const string INIT_TS_TABLE = "\
Yingdi Yuede8eaf2013-10-14 14:07:03 -070064CREATE TABLE IF NOT EXISTS \n \
Yingdi Yu813d4e92013-11-03 16:22:05 -080065 TrustScope( \n \
Yingdi Yu8dacdf22013-11-05 23:06:43 -080066 id INTEGER PRIMARY KEY AUTOINCREMENT, \n \
Yingdi Yuede8eaf2013-10-14 14:07:03 -070067 contact_namespace BLOB NOT NULL, \n \
Yingdi Yu8dacdf22013-11-05 23:06:43 -080068 trust_scope BLOB NOT NULL \n \
Yingdi Yuede8eaf2013-10-14 14:07:03 -070069 ); \n \
70 \
Yingdi Yu8dacdf22013-11-05 23:06:43 -080071CREATE INDEX ts_index ON TrustScope(contact_namespace); \n \
72";
73
74const string INIT_CP_TABLE = "\
75CREATE TABLE IF NOT EXISTS \n \
76 ContactProfile( \n \
77 profile_identity BLOB NOT NULL, \n \
78 profile_type BLOB NOT NULL, \n \
79 profile_value BLOB NOT NULL, \n \
80 endorse INTEGER NOT NULL, \n \
81 \
82 PRIMARY KEY (profile_identity, profile_type) \n \
83 ); \n \
84 \
85CREATE INDEX cp_index ON ContactProfile(profile_identity); \n \
86";
87
88const string INIT_PE_TABLE = "\
89CREATE TABLE IF NOT EXISTS \n \
90 ProfileEndorse( \n \
91 identity BLOB NOT NULL UNIQUE, \n \
92 endorse_data BLOB NOT NULL, \n \
93 \
94 PRIMARY KEY (identity) \n \
95 ); \n \
96 \
97CREATE INDEX pe_index ON ProfileEndorse(identity); \n \
98";
99
100const string INIT_CE_TABLE = "\
101CREATE TABLE IF NOT EXISTS \n \
102 CollectEndorse( \n \
103 endorsee BLOB NOT NULL, \n \
104 endorser BLOB NOT NULL, \n \
105 endorse_name BLOB NOT NULL, \n \
106 endorse_data BLOB NOT NULL, \n \
107 \
108 PRIMARY KEY (endorsee, endorser) \n \
109 ); \n \
110 \
111CREATE INDEX ce_index ON CollectEndorse(endorsee); \n \
Yingdi Yuede8eaf2013-10-14 14:07:03 -0700112";
113
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700114ContactStorage::ContactStorage()
Yingdi Yuede8eaf2013-10-14 14:07:03 -0700115{
116 fs::path chronosDir = fs::path(getenv("HOME")) / ".chronos";
117 fs::create_directories (chronosDir);
118
119 int res = sqlite3_open((chronosDir / "chronos.db").c_str (), &m_db);
120 if (res != SQLITE_OK)
121 throw LnException("Chronos DB cannot be open/created");
122
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700123 // Check if SelfProfile table exists
Yingdi Yuede8eaf2013-10-14 14:07:03 -0700124 sqlite3_stmt *stmt;
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700125 sqlite3_prepare_v2 (m_db, "SELECT name FROM sqlite_master WHERE type='table' And name='SelfProfile'", -1, &stmt, 0);
126 res = sqlite3_step (stmt);
127
128 bool spTableExist = false;
129 if (res == SQLITE_ROW)
130 spTableExist = true;
131 sqlite3_finalize (stmt);
132
133 if(!spTableExist)
134 {
135 char *errmsg = 0;
136 res = sqlite3_exec (m_db, INIT_SP_TABLE.c_str (), NULL, NULL, &errmsg);
137 if (res != SQLITE_OK && errmsg != 0)
138 throw LnException("Init \"error\" in SelfProfile");
139 }
140
Yingdi Yu590fa5d2013-10-18 18:35:09 -0700141 // Check if SelfEndorse table exists
142 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 -0700143 res = sqlite3_step (stmt);
144
Yingdi Yu590fa5d2013-10-18 18:35:09 -0700145 bool seTableExist = false;
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700146 if (res == SQLITE_ROW)
Yingdi Yu590fa5d2013-10-18 18:35:09 -0700147 seTableExist = true;
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700148 sqlite3_finalize (stmt);
149
Yingdi Yu590fa5d2013-10-18 18:35:09 -0700150 if(!seTableExist)
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700151 {
152 char *errmsg = 0;
Yingdi Yu590fa5d2013-10-18 18:35:09 -0700153 res = sqlite3_exec (m_db, INIT_SE_TABLE.c_str (), NULL, NULL, &errmsg);
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700154 if (res != SQLITE_OK && errmsg != 0)
Yingdi Yu590fa5d2013-10-18 18:35:09 -0700155 throw LnException("Init \"error\" in SelfEndorse");
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700156 }
157
158
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800159 // Check if Contact table exists
Yingdi Yu813d4e92013-11-03 16:22:05 -0800160 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 -0700161 res = sqlite3_step (stmt);
162
Yingdi Yu813d4e92013-11-03 16:22:05 -0800163 bool contactTableExist = false;
Yingdi Yuede8eaf2013-10-14 14:07:03 -0700164 if (res == SQLITE_ROW)
Yingdi Yu813d4e92013-11-03 16:22:05 -0800165 contactTableExist = true;
Yingdi Yuede8eaf2013-10-14 14:07:03 -0700166 sqlite3_finalize (stmt);
167
Yingdi Yu813d4e92013-11-03 16:22:05 -0800168 if(!contactTableExist)
Yingdi Yuede8eaf2013-10-14 14:07:03 -0700169 {
170 char *errmsg = 0;
Yingdi Yu813d4e92013-11-03 16:22:05 -0800171 res = sqlite3_exec (m_db, INIT_CONTACT_TABLE.c_str (), NULL, NULL, &errmsg);
Yingdi Yuede8eaf2013-10-14 14:07:03 -0700172 if (res != SQLITE_OK && errmsg != 0)
Yingdi Yu813d4e92013-11-03 16:22:05 -0800173 throw LnException("Init \"error\" in Contact");
Yingdi Yuede8eaf2013-10-14 14:07:03 -0700174 }
Yingdi Yu813d4e92013-11-03 16:22:05 -0800175
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800176 // Check if TrustScope table exists
Yingdi Yu813d4e92013-11-03 16:22:05 -0800177 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 -0700178 res = sqlite3_step (stmt);
179
Yingdi Yu813d4e92013-11-03 16:22:05 -0800180 bool tsTableExist = false;
Yingdi Yuede8eaf2013-10-14 14:07:03 -0700181 if (res == SQLITE_ROW)
Yingdi Yu813d4e92013-11-03 16:22:05 -0800182 tsTableExist = true;
Yingdi Yuede8eaf2013-10-14 14:07:03 -0700183 sqlite3_finalize (stmt);
184
Yingdi Yu813d4e92013-11-03 16:22:05 -0800185 if(!tsTableExist)
Yingdi Yuede8eaf2013-10-14 14:07:03 -0700186 {
187 char *errmsg = 0;
Yingdi Yu813d4e92013-11-03 16:22:05 -0800188 res = sqlite3_exec (m_db, INIT_TS_TABLE.c_str (), NULL, NULL, &errmsg);
Yingdi Yuede8eaf2013-10-14 14:07:03 -0700189 if (res != SQLITE_OK && errmsg != 0)
Yingdi Yu813d4e92013-11-03 16:22:05 -0800190 throw LnException("Init \"error\" in TrustScope");
Yingdi Yuede8eaf2013-10-14 14:07:03 -0700191 }
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800192
193 // Check if ContactProfile table exists
194 sqlite3_prepare_v2 (m_db, "SELECT name FROM sqlite_master WHERE type='table' And name='ContactProfile'", -1, &stmt, 0);
195 res = sqlite3_step (stmt);
196
197 bool cpTableExist = false;
198 if (res == SQLITE_ROW)
199 cpTableExist = true;
200 sqlite3_finalize (stmt);
201
202 if(!cpTableExist)
203 {
204 char *errmsg = 0;
205 res = sqlite3_exec (m_db, INIT_CP_TABLE.c_str (), NULL, NULL, &errmsg);
206 if (res != SQLITE_OK && errmsg != 0)
207 throw LnException("Init \"error\" in ContactProfile");
208 }
209
210 // Check if ProfileEndorse table exists
211 sqlite3_prepare_v2 (m_db, "SELECT name FROM sqlite_master WHERE type='table' And name='ProfileEndorse'", -1, &stmt, 0);
212 res = sqlite3_step (stmt);
213
214 bool peTableExist = false;
215 if (res == SQLITE_ROW)
216 peTableExist = true;
217 sqlite3_finalize (stmt);
218
219 if(!peTableExist)
220 {
221 char *errmsg = 0;
222 res = sqlite3_exec (m_db, INIT_PE_TABLE.c_str (), NULL, NULL, &errmsg);
223 if (res != SQLITE_OK && errmsg != 0)
224 throw LnException("Init \"error\" in ProfileEndorse");
225 }
226
227 // Check if CollectEndorse table exists
228 sqlite3_prepare_v2 (m_db, "SELECT name FROM sqlite_master WHERE type='table' And name='CollectEndorse'", -1, &stmt, 0);
229 res = sqlite3_step (stmt);
230
231 bool ceTableExist = false;
232 if (res == SQLITE_ROW)
233 ceTableExist = true;
234 sqlite3_finalize (stmt);
235
236 if(!ceTableExist)
237 {
238 char *errmsg = 0;
239 res = sqlite3_exec (m_db, INIT_CE_TABLE.c_str (), NULL, NULL, &errmsg);
240 if (res != SQLITE_OK && errmsg != 0)
241 throw LnException("Init \"error\" in CollectEndorse");
242 }
Yingdi Yuede8eaf2013-10-14 14:07:03 -0700243}
244
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700245bool
Yingdi Yu49eea9f2013-10-17 15:07:17 -0700246ContactStorage::doesSelfEntryExist(const Name& identity, const string& profileType)
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700247{
248 bool result = false;
249
250 sqlite3_stmt *stmt;
Yingdi Yu49eea9f2013-10-17 15:07:17 -0700251 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 -0700252 sqlite3_bind_text(stmt, 1, profileType.c_str(), profileType.size(), SQLITE_TRANSIENT);
Yingdi Yu49eea9f2013-10-17 15:07:17 -0700253 sqlite3_bind_text(stmt, 2, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT);
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700254
255 int res = sqlite3_step (stmt);
256
257 if (res == SQLITE_ROW)
258 {
259 int countAll = sqlite3_column_int (stmt, 0);
260 if (countAll > 0)
261 result = true;
262 }
263 sqlite3_finalize (stmt);
264 return result;
265}
266
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700267void
Yingdi Yu49eea9f2013-10-17 15:07:17 -0700268ContactStorage::setSelfProfileEntry(const Name& identity, const string& profileType, const Blob& profileValue)
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700269{
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700270 sqlite3_stmt *stmt;
Yingdi Yu49eea9f2013-10-17 15:07:17 -0700271 if(doesSelfEntryExist(identity, profileType))
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700272 {
Yingdi Yu49eea9f2013-10-17 15:07:17 -0700273 sqlite3_prepare_v2 (m_db, "UPDATE SelfProfile SET profile_value=? WHERE profile_type=? and profile_identity=?", -1, &stmt, 0);
Yingdi Yu64206112013-12-24 11:16:32 +0800274 sqlite3_bind_text(stmt, 1, (const char*)profileValue.buf(), profileValue.size(), SQLITE_TRANSIENT);
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700275 sqlite3_bind_text(stmt, 2, profileType.c_str(), profileType.size(), SQLITE_TRANSIENT);
Yingdi Yu49eea9f2013-10-17 15:07:17 -0700276 sqlite3_bind_text(stmt, 3, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT);
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700277 }
278 else
279 {
Yingdi Yu49eea9f2013-10-17 15:07:17 -0700280 sqlite3_prepare_v2 (m_db, "INSERT INTO SelfProfile (profile_identity, profile_type, profile_value) values (?, ?, ?)", -1, &stmt, 0);
281 sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT);
282 sqlite3_bind_text(stmt, 2, profileType.c_str(), profileType.size(), SQLITE_TRANSIENT);
Yingdi Yu64206112013-12-24 11:16:32 +0800283 sqlite3_bind_text(stmt, 3, (const char*)profileValue.buf(), profileValue.size(), SQLITE_TRANSIENT);
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700284 }
285 sqlite3_step (stmt);
286 sqlite3_finalize (stmt);
287}
288
Yingdi Yu64206112013-12-24 11:16:32 +0800289shared_ptr<Profile>
Yingdi Yu49eea9f2013-10-17 15:07:17 -0700290ContactStorage::getSelfProfile(const Name& identity)
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700291{
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700292 sqlite3_stmt *stmt;
Yingdi Yu64206112013-12-24 11:16:32 +0800293 shared_ptr<Profile> profile = make_shared<Profile>(identity);
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700294
Yingdi Yu79c25a22013-10-21 13:38:38 -0700295 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 -0700296 sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT);
Yingdi Yu79c25a22013-10-21 13:38:38 -0700297
298 while(sqlite3_step (stmt) == SQLITE_ROW)
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700299 {
300 string profileType(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0));
Yingdi Yu64206112013-12-24 11:16:32 +0800301 string profileValue(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 1)), sqlite3_column_bytes (stmt, 1));
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700302
303 profile->setProfileEntry(profileType, profileValue );
304 }
305
Yingdi Yu6a5b9f62013-11-06 23:00:21 -0800306 sqlite3_finalize(stmt);
307
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700308 return profile;
309}
Yingdi Yu908f8412013-11-09 00:03:26 -0800310
311void
312ContactStorage::removeContact(const Name& contactNameSpace)
313{
Yingdi Yu64206112013-12-24 11:16:32 +0800314 shared_ptr<ContactItem> contact = getContact(contactNameSpace);
Yingdi Yu908f8412013-11-09 00:03:26 -0800315 string identity = contactNameSpace.toUri();
316
317 if(contact == NULL)
318 return;
319
320 sqlite3_stmt *stmt;
321 sqlite3_prepare_v2 (m_db, "DELETE FROM Contact WHERE contact_namespace=?", -1, &stmt, 0);
322 sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size (), SQLITE_TRANSIENT);
323 int res = sqlite3_step (stmt);
324 sqlite3_finalize (stmt);
325
326 sqlite3_prepare_v2 (m_db, "DELETE FROM ContactProfile WHERE profile_identity=?", -1, &stmt, 0);
327 sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size (), SQLITE_TRANSIENT);
328 res = sqlite3_step (stmt);
329 sqlite3_finalize (stmt);
330
331 if(contact->isIntroducer())
332 {
333 sqlite3_prepare_v2 (m_db, "DELETE FROM TrustScope WHERE contact_namespace=?", -1, &stmt, 0);
334 sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size (), SQLITE_TRANSIENT);
335 res = sqlite3_step (stmt);
336 sqlite3_finalize (stmt);
337 }
338}
Yingdi Yuede8eaf2013-10-14 14:07:03 -0700339
340void
Yingdi Yu813d4e92013-11-03 16:22:05 -0800341ContactStorage::addContact(const ContactItem& contact)
Yingdi Yuede8eaf2013-10-14 14:07:03 -0700342{
Yingdi Yu813d4e92013-11-03 16:22:05 -0800343 if(doesContactExist(contact.getNameSpace()))
Yingdi Yuede8eaf2013-10-14 14:07:03 -0700344 throw LnException("Normal Contact has already existed");
345
Yingdi Yu813d4e92013-11-03 16:22:05 -0800346 bool isIntroducer = contact.isIntroducer();
347
Yingdi Yuede8eaf2013-10-14 14:07:03 -0700348 sqlite3_stmt *stmt;
349 sqlite3_prepare_v2 (m_db,
Yingdi Yu813d4e92013-11-03 16:22:05 -0800350 "INSERT INTO Contact (contact_namespace, contact_alias, self_certificate, is_introducer) values (?, ?, ?, ?)",
Yingdi Yuede8eaf2013-10-14 14:07:03 -0700351 -1,
352 &stmt,
353 0);
354
Yingdi Yu813d4e92013-11-03 16:22:05 -0800355 sqlite3_bind_text(stmt, 1, contact.getNameSpace().toUri().c_str(), contact.getNameSpace().toUri().size (), SQLITE_TRANSIENT);
356 sqlite3_bind_text(stmt, 2, contact.getAlias().c_str(), contact.getAlias().size(), SQLITE_TRANSIENT);
Yingdi Yu64206112013-12-24 11:16:32 +0800357 Blob selfCertificateBlob = contact.getSelfEndorseCertificate().wireEncode();
358 sqlite3_bind_text(stmt, 3, (const char*)selfCertificateBlob.buf(), selfCertificateBlob.size(), SQLITE_TRANSIENT);
Yingdi Yu813d4e92013-11-03 16:22:05 -0800359 sqlite3_bind_int(stmt, 4, (isIntroducer ? 1 : 0));
Yingdi Yuede8eaf2013-10-14 14:07:03 -0700360
361 int res = sqlite3_step (stmt);
362 sqlite3_finalize (stmt);
Yingdi Yu813d4e92013-11-03 16:22:05 -0800363
Yingdi Yu64206112013-12-24 11:16:32 +0800364 const Profile& profile = contact.getSelfEndorseCertificate().getProfileData().getProfile();
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800365 Profile::const_iterator it = profile.begin();
366 string identity = contact.getNameSpace().toUri();
367 for(; it != profile.end(); it++)
368 {
369 sqlite3_prepare_v2 (m_db,
370 "INSERT INTO ContactProfile (profile_identity, profile_type, profile_value, endorse) values (?, ?, ?, 0)",
371 -1,
372 &stmt,
373 0);
374 sqlite3_bind_text(stmt, 1, identity.c_str(), identity.size (), SQLITE_TRANSIENT);
375 sqlite3_bind_text(stmt, 2, it->first.c_str(), it->first.size(), SQLITE_TRANSIENT);
Yingdi Yu64206112013-12-24 11:16:32 +0800376 sqlite3_bind_text(stmt, 3, it->second.c_str(), it->second.size(), SQLITE_TRANSIENT);
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800377 res = sqlite3_step (stmt);
378 sqlite3_finalize (stmt);
379 }
380
Yingdi Yu813d4e92013-11-03 16:22:05 -0800381 if(isIntroducer)
382 {
383 const vector<Name>& scopeList = contact.getTrustScopeList();
384 vector<Name>::const_iterator it = scopeList.begin();
385 string nameSpace = contact.getNameSpace().toUri();
386
387 while(it != scopeList.end())
388 {
389 sqlite3_prepare_v2 (m_db,
390 "INSERT INTO TrustScope (contact_namespace, trust_scope) values (?, ?)",
391 -1,
392 &stmt,
393 0);
394 sqlite3_bind_text(stmt, 1, nameSpace.c_str(), nameSpace.size (), SQLITE_TRANSIENT);
395 sqlite3_bind_text(stmt, 2, it->toUri().c_str(), it->toUri().size(), SQLITE_TRANSIENT);
396 res = sqlite3_step (stmt);
397 sqlite3_finalize (stmt);
398 it++;
399 }
400 }
Yingdi Yuede8eaf2013-10-14 14:07:03 -0700401}
402
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800403void
404ContactStorage::updateIsIntroducer(const ndn::Name& identity, bool isIntroducer)
405{
406 sqlite3_stmt *stmt;
407 sqlite3_prepare_v2 (m_db, "UPDATE Contact SET is_introducer=? WHERE contact_namespace=?", -1, &stmt, 0);
408 sqlite3_bind_int(stmt, 1, (isIntroducer ? 1 : 0));
409 sqlite3_bind_text(stmt, 2, identity.toUri().c_str(), identity.toUri().size (), SQLITE_TRANSIENT);
410 int res = sqlite3_step (stmt);
411 sqlite3_finalize (stmt);
412 return;
413}
414
Yingdi Yu79c25a22013-10-21 13:38:38 -0700415void
416ContactStorage::updateAlias(const ndn::Name& identity, std::string alias)
417{
Yingdi Yu813d4e92013-11-03 16:22:05 -0800418 sqlite3_stmt *stmt;
419 sqlite3_prepare_v2 (m_db, "UPDATE Contact SET contact_alias=? WHERE contact_namespace=?", -1, &stmt, 0);
420 sqlite3_bind_text(stmt, 1, alias.c_str(), alias.size(), SQLITE_TRANSIENT);
421 sqlite3_bind_text(stmt, 2, identity.toUri().c_str(), identity.toUri().size (), SQLITE_TRANSIENT);
422 int res = sqlite3_step (stmt);
423 sqlite3_finalize (stmt);
424 return;
Yingdi Yu79c25a22013-10-21 13:38:38 -0700425}
426
Yingdi Yuede8eaf2013-10-14 14:07:03 -0700427bool
Yingdi Yu813d4e92013-11-03 16:22:05 -0800428ContactStorage::doesContactExist(const Name& name)
Yingdi Yuede8eaf2013-10-14 14:07:03 -0700429{
430 bool result = false;
431
432 sqlite3_stmt *stmt;
Yingdi Yu813d4e92013-11-03 16:22:05 -0800433 sqlite3_prepare_v2 (m_db, "SELECT count(*) FROM Contact WHERE contact_namespace=?", -1, &stmt, 0);
Yingdi Yuede8eaf2013-10-14 14:07:03 -0700434 sqlite3_bind_text(stmt, 1, name.toUri().c_str(), name.toUri().size(), SQLITE_TRANSIENT);
435
436 int res = sqlite3_step (stmt);
437
438 if (res == SQLITE_ROW)
439 {
440 int countAll = sqlite3_column_int (stmt, 0);
441 if (countAll > 0)
442 result = true;
443 }
444 sqlite3_finalize (stmt);
445 return result;
446}
447
Yingdi Yu64206112013-12-24 11:16:32 +0800448void
449ContactStorage::getAllContacts(vector<shared_ptr<ContactItem> >& contacts) const
Yingdi Yuede8eaf2013-10-14 14:07:03 -0700450{
Yingdi Yuede8eaf2013-10-14 14:07:03 -0700451 sqlite3_stmt *stmt;
Yingdi Yu813d4e92013-11-03 16:22:05 -0800452 sqlite3_prepare_v2 (m_db, "SELECT contact_alias, self_certificate, is_introducer FROM Contact", -1, &stmt, 0);
Yingdi Yuede8eaf2013-10-14 14:07:03 -0700453
454 while( sqlite3_step (stmt) == SQLITE_ROW)
455 {
456 string alias(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0));
Yingdi Yu64206112013-12-24 11:16:32 +0800457
458 Data certData;
459 certData.wireDecode(reinterpret_cast<const uint8_t*>(sqlite3_column_text(stmt, 1)), sqlite3_column_bytes (stmt, 1));
460 EndorseCertificate endorseCertificate(certData);
461
Yingdi Yu813d4e92013-11-03 16:22:05 -0800462 int isIntroducer = sqlite3_column_int (stmt, 2);
Yingdi Yuede8eaf2013-10-14 14:07:03 -0700463
Yingdi Yu64206112013-12-24 11:16:32 +0800464 contacts.push_back(make_shared<ContactItem>(endorseCertificate, isIntroducer, alias));
Yingdi Yuede8eaf2013-10-14 14:07:03 -0700465 }
Yingdi Yu813d4e92013-11-03 16:22:05 -0800466 sqlite3_finalize (stmt);
467
Yingdi Yu64206112013-12-24 11:16:32 +0800468 vector<shared_ptr<ContactItem> >::iterator it = contacts.begin();
Yingdi Yu813d4e92013-11-03 16:22:05 -0800469 for(; it != contacts.end(); it++)
470 {
471 if((*it)->isIntroducer())
472 {
473 sqlite3_prepare_v2 (m_db, "SELECT trust_scope FROM TrustScope WHERE contact_namespace=?", -1, &stmt, 0);
474 sqlite3_bind_text(stmt, 1, (*it)->getNameSpace().toUri().c_str(), (*it)->getNameSpace().toUri().size(), SQLITE_TRANSIENT);
475
476 while( sqlite3_step (stmt) == SQLITE_ROW)
477 {
478 Name scope(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0)));
479 (*it)->addTrustScope(scope);
480 }
481 sqlite3_finalize (stmt);
482 }
483 }
Yingdi Yuede8eaf2013-10-14 14:07:03 -0700484}
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700485
Yingdi Yu64206112013-12-24 11:16:32 +0800486shared_ptr<ContactItem>
Yingdi Yu813d4e92013-11-03 16:22:05 -0800487ContactStorage::getContact(const Name& name)
Yingdi Yud40226b2013-10-23 14:05:12 -0700488{
489 sqlite3_stmt *stmt;
Yingdi Yu813d4e92013-11-03 16:22:05 -0800490 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 -0700491 sqlite3_bind_text (stmt, 1, name.toUri().c_str(), name.toUri().size(), SQLITE_TRANSIENT);
492
493 if( sqlite3_step (stmt) == SQLITE_ROW)
494 {
495 string alias(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0));
Yingdi Yu64206112013-12-24 11:16:32 +0800496
497 Data certData;
498 certData.wireDecode(reinterpret_cast<const uint8_t*>(sqlite3_column_text(stmt, 1)), sqlite3_column_bytes (stmt, 1));
499 EndorseCertificate endorseCertificate(certData);
500
Yingdi Yu813d4e92013-11-03 16:22:05 -0800501 int isIntroducer = sqlite3_column_int (stmt, 2);
Yingdi Yud40226b2013-10-23 14:05:12 -0700502
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800503 sqlite3_finalize (stmt);
504
Yingdi Yu64206112013-12-24 11:16:32 +0800505 shared_ptr<ContactItem> contact = make_shared<ContactItem>(endorseCertificate, isIntroducer, alias);
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800506
507 if(contact->isIntroducer())
508 {
509 sqlite3_prepare_v2 (m_db, "SELECT trust_scope FROM TrustScope WHERE contact_namespace=?", -1, &stmt, 0);
510 sqlite3_bind_text(stmt, 1, name.toUri().c_str(), name.toUri().size(), SQLITE_TRANSIENT);
511
512 while( sqlite3_step (stmt) == SQLITE_ROW)
513 {
514 Name scope(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0)));
515 contact->addTrustScope(scope);
516 }
517 sqlite3_finalize (stmt);
518 }
519
520 return contact;
Yingdi Yud40226b2013-10-23 14:05:12 -0700521 }
Yingdi Yu64206112013-12-24 11:16:32 +0800522 return CHRONOCHAT_NULL_CONTACTITEM_PTR;
Yingdi Yud40226b2013-10-23 14:05:12 -0700523}
524
Yingdi Yu64206112013-12-24 11:16:32 +0800525shared_ptr<Profile>
Yingdi Yu49eea9f2013-10-17 15:07:17 -0700526ContactStorage::getSelfProfile(const Name& identity) const
527{
Yingdi Yu64206112013-12-24 11:16:32 +0800528 shared_ptr<Profile> profile = make_shared<Profile>(identity);
Yingdi Yu49eea9f2013-10-17 15:07:17 -0700529 sqlite3_stmt *stmt;
530 sqlite3_prepare_v2 (m_db, "SELECT profile_type, profile_value FROM SelfProfile WHERE profile_identity=?", -1, &stmt, 0);
531 sqlite3_bind_text (stmt, 1, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT);
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700532
533 while( sqlite3_step (stmt) == SQLITE_ROW)
534 {
535 string profileType(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0));
Yingdi Yu64206112013-12-24 11:16:32 +0800536 string profileValue(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 1)), sqlite3_column_bytes (stmt, 1));
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700537 profile->setProfileEntry(profileType, profileValue);
538 }
539
Yingdi Yu6a5b9f62013-11-06 23:00:21 -0800540 sqlite3_finalize(stmt);
541
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700542 return profile;
543}
544
Yingdi Yu64206112013-12-24 11:16:32 +0800545Blob
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700546ContactStorage::getSelfEndorseCertificate(const Name& identity)
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700547{
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700548 sqlite3_stmt *stmt;
Yingdi Yu590fa5d2013-10-18 18:35:09 -0700549 sqlite3_prepare_v2 (m_db, "SELECT endorse_data FROM SelfEndorse where identity=?", -1, &stmt, 0);
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700550 sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT);
Yingdi Yu49eea9f2013-10-17 15:07:17 -0700551
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700552 if(sqlite3_step (stmt) == SQLITE_ROW)
Yingdi Yu64206112013-12-24 11:16:32 +0800553 {
554 Blob result(reinterpret_cast<const uint8_t*>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0));
555 sqlite3_finalize (stmt);
556 return result;
557 }
Yingdi Yu0a6b6c52013-10-15 17:54:00 -0700558
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700559 sqlite3_finalize (stmt);
Yingdi Yu68aced92013-10-17 21:13:03 -0700560
Yingdi Yu64206112013-12-24 11:16:32 +0800561 return CHRONOCHAT_NULL_BLOB;
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 Yu64206112013-12-24 11:16:32 +0800567 Blob newEndorseCertificateBlob = 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 Yu64206112013-12-24 11:16:32 +0800571 sqlite3_bind_text(stmt, 1, (const char*)newEndorseCertificateBlob.buf(), newEndorseCertificateBlob.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 Yu64206112013-12-24 11:16:32 +0800581 Blob newEndorseCertificateBlob = 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 Yu64206112013-12-24 11:16:32 +0800586 sqlite3_bind_text(stmt, 2, (const char*)newEndorseCertificateBlob.buf(), newEndorseCertificateBlob.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 Yu64206112013-12-24 11:16:32 +0800592Blob
593ContactStorage::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 {
602 Blob result(reinterpret_cast<const uint8_t*>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0));
603 sqlite3_finalize (stmt);
604 return result;
605 }
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800606
607 sqlite3_finalize (stmt);
Yingdi Yu64206112013-12-24 11:16:32 +0800608
609 return CHRONOCHAT_NULL_BLOB;
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800610}
611
612void
Yingdi Yu64206112013-12-24 11:16:32 +0800613ContactStorage::updateEndorseCertificate(const EndorseCertificate& endorseCertificate, const Name& identity)
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800614{
Yingdi Yu64206112013-12-24 11:16:32 +0800615 Blob newEndorseCertificateBlob = endorseCertificate.wireEncode();
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800616
617 sqlite3_stmt *stmt;
618 sqlite3_prepare_v2 (m_db, "UPDATE ProfileEndorse SET endorse_data=? WHERE identity=?", -1, &stmt, 0);
Yingdi Yu64206112013-12-24 11:16:32 +0800619 sqlite3_bind_text(stmt, 1, (const char*)newEndorseCertificateBlob.buf(), newEndorseCertificateBlob.size(), SQLITE_TRANSIENT);
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800620 sqlite3_bind_text(stmt, 2, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT);
621 sqlite3_step(stmt);
622
623 sqlite3_finalize (stmt);
624}
625
626void
Yingdi Yu64206112013-12-24 11:16:32 +0800627ContactStorage::addEndorseCertificate(const EndorseCertificate& endorseCertificate, const Name& identity)
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800628{
Yingdi Yu64206112013-12-24 11:16:32 +0800629 Blob newEndorseCertificateBlob = endorseCertificate.wireEncode();
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800630
631 sqlite3_stmt *stmt;
632 sqlite3_prepare_v2 (m_db, "INSERT INTO ProfileEndorse (identity, endorse_data) values (?, ?)", -1, &stmt, 0);
633 sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT);
Yingdi Yu64206112013-12-24 11:16:32 +0800634 sqlite3_bind_text(stmt, 2, (const char*)newEndorseCertificateBlob.buf(), newEndorseCertificateBlob.size(), SQLITE_TRANSIENT);
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800635 sqlite3_step(stmt);
636
637 sqlite3_finalize (stmt);
638}
639
Yingdi Yu64206112013-12-24 11:16:32 +0800640void
641ContactStorage::getEndorseList(const Name& identity, vector<string>& endorseList)
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800642{
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800643 sqlite3_stmt *stmt;
644 sqlite3_prepare_v2 (m_db, "SELECT profile_type FROM ContactProfile WHERE profile_identity=? AND endorse=1 ORDER BY profile_type", -1, &stmt, 0);
645 sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT);
646
647 while( sqlite3_step (stmt) == SQLITE_ROW)
648 {
649 string profileType(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0));
650 endorseList.push_back(profileType);
651 }
652 sqlite3_finalize (stmt);
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800653}
654
655void
656ContactStorage::updateCollectEndorse(const EndorseCertificate& endorseCertificate)
657{
658 Name endorserName = endorseCertificate.getSigner();
659 Name keyName = endorseCertificate.getPublicKeyName();
660 Name endorseeName = keyName.getPrefix(keyName.size()-1);
661 Name getCertName = endorseCertificate.getName();
662 Name oldCertName;
663 bool insert = true;
664 bool update = true;
665
666 sqlite3_stmt *stmt;
667 sqlite3_prepare_v2 (m_db, "SELECT endorse_name FROM CollectEndorse WHERE endorser=? AND endorsee=?", -1, &stmt, 0);
668 sqlite3_bind_text(stmt, 1, endorserName.toUri().c_str(), endorserName.toUri().size(), SQLITE_TRANSIENT);
669 sqlite3_bind_text(stmt, 2, endorseeName.toUri().c_str(), endorseeName.toUri().size(), SQLITE_TRANSIENT);
670
671 if(sqlite3_step (stmt) == SQLITE_ROW)
672 {
673 insert = false;
674 oldCertName = Name(string(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0)));
675 if(getCertName == oldCertName)
676 update = false;
677 }
678 sqlite3_finalize (stmt);
679
680 if(insert)
681 {
682 sqlite3_prepare_v2 (m_db, "INSERT INTO CollectEndorse (endorser, endorsee, endorse_name, endorse_data) VALUES (?, ?, ?, ?)", -1, &stmt, 0);
683 sqlite3_bind_text(stmt, 1, endorserName.toUri().c_str(), endorserName.toUri().size(), SQLITE_TRANSIENT);
684 sqlite3_bind_text(stmt, 2, endorseeName.toUri().c_str(), endorseeName.toUri().size(), SQLITE_TRANSIENT);
685 sqlite3_bind_text(stmt, 3, getCertName.toUri().c_str(), getCertName.toUri().size(), SQLITE_TRANSIENT);
Yingdi Yu64206112013-12-24 11:16:32 +0800686 Blob blob = endorseCertificate.wireEncode();
687 sqlite3_bind_text(stmt, 4, (const char*)blob.buf(), blob.size(), SQLITE_TRANSIENT);
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800688 int res = sqlite3_step (stmt);
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800689 sqlite3_finalize (stmt);
690 return;
691 }
692 if(update)
693 {
694 sqlite3_prepare_v2 (m_db, "UPDATE CollectEndorse SET endorse_name=?, endorse_data=? WHERE endorser=? AND endorsee=?", -1, &stmt, 0);
695 sqlite3_bind_text(stmt, 1, getCertName.toUri().c_str(), getCertName.toUri().size(), SQLITE_TRANSIENT);
Yingdi Yu64206112013-12-24 11:16:32 +0800696 Blob blob = endorseCertificate.wireEncode();
697 sqlite3_bind_text(stmt, 2, (const char*)blob.buf(), blob.size(), SQLITE_TRANSIENT);
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800698 sqlite3_bind_text(stmt, 3, endorserName.toUri().c_str(), endorserName.toUri().size(), SQLITE_TRANSIENT);
699 sqlite3_bind_text(stmt, 4, endorseeName.toUri().c_str(), endorseeName.toUri().size(), SQLITE_TRANSIENT);
700 int res = sqlite3_step (stmt);
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800701 sqlite3_finalize (stmt);
702 return;
703 }
704}
705
Yingdi Yu64206112013-12-24 11:16:32 +0800706void
707ContactStorage::getCollectEndorseList(const Name& name, vector<Blob>& endorseList)
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800708{
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800709 sqlite3_stmt *stmt;
710 sqlite3_prepare_v2 (m_db, "SELECT endorse_data FROM CollectEndorse WHERE endorsee=?", -1, &stmt, 0);
711 sqlite3_bind_text(stmt, 1, name.toUri().c_str(), name.toUri().size(), SQLITE_TRANSIENT);
712
713 while(sqlite3_step (stmt) == SQLITE_ROW)
714 {
Yingdi Yu64206112013-12-24 11:16:32 +0800715 Blob blob(reinterpret_cast<const uint8_t*>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0));
716 endorseList.push_back(blob);
Yingdi Yu8dacdf22013-11-05 23:06:43 -0800717 }
718
Yingdi Yu6a5b9f62013-11-06 23:00:21 -0800719 sqlite3_finalize (stmt);
Yingdi Yuaa8d7692013-10-18 17:05:02 -0700720}