Yingdi Yu | ede8eaf | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 1 | /* -*- 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 Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame^] | 12 | #include "null-ptrs.h" |
Yingdi Yu | ede8eaf | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 13 | #include "exception.h" |
| 14 | |
Yingdi Yu | ede8eaf | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 15 | #include <boost/filesystem.hpp> |
Yingdi Yu | 0a6b6c5 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 16 | #include "logging.h" |
Yingdi Yu | ede8eaf | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 17 | |
| 18 | using namespace std; |
| 19 | using namespace ndn; |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame^] | 20 | using namespace ndn::ptr_lib; |
Yingdi Yu | ede8eaf | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 21 | namespace fs = boost::filesystem; |
| 22 | |
Yingdi Yu | 0a6b6c5 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 23 | INIT_LOGGER ("ContactStorage"); |
| 24 | |
| 25 | const string INIT_SP_TABLE = "\ |
| 26 | CREATE TABLE IF NOT EXISTS \n \ |
| 27 | SelfProfile( \n \ |
Yingdi Yu | 49eea9f | 2013-10-17 15:07:17 -0700 | [diff] [blame] | 28 | profile_identity BLOB NOT NULL, \n \ |
Yingdi Yu | 0a6b6c5 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 29 | profile_type BLOB NOT NULL, \n \ |
Yingdi Yu | 49eea9f | 2013-10-17 15:07:17 -0700 | [diff] [blame] | 30 | profile_value BLOB NOT NULL, \n \ |
| 31 | \ |
| 32 | PRIMARY KEY (profile_identity, profile_type) \n \ |
Yingdi Yu | 0a6b6c5 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 33 | ); \n \ |
| 34 | \ |
Yingdi Yu | 49eea9f | 2013-10-17 15:07:17 -0700 | [diff] [blame] | 35 | CREATE INDEX sp_index ON SelfProfile(profile_identity,profile_type); \n \ |
Yingdi Yu | 0a6b6c5 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 36 | "; |
| 37 | |
Yingdi Yu | 590fa5d | 2013-10-18 18:35:09 -0700 | [diff] [blame] | 38 | const string INIT_SE_TABLE = "\ |
Yingdi Yu | 0a6b6c5 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 39 | CREATE TABLE IF NOT EXISTS \n \ |
Yingdi Yu | 590fa5d | 2013-10-18 18:35:09 -0700 | [diff] [blame] | 40 | SelfEndorse( \n \ |
Yingdi Yu | 0a6b6c5 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 41 | identity BLOB NOT NULL UNIQUE, \n \ |
Yingdi Yu | 590fa5d | 2013-10-18 18:35:09 -0700 | [diff] [blame] | 42 | endorse_data BLOB NOT NULL, \n \ |
Yingdi Yu | 0a6b6c5 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 43 | \ |
| 44 | PRIMARY KEY (identity) \n \ |
| 45 | ); \n \ |
Yingdi Yu | 590fa5d | 2013-10-18 18:35:09 -0700 | [diff] [blame] | 46 | CREATE INDEX se_index ON SelfEndorse(identity); \n \ |
Yingdi Yu | 0a6b6c5 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 47 | "; |
Yingdi Yu | ede8eaf | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 48 | |
Yingdi Yu | 813d4e9 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 49 | const string INIT_CONTACT_TABLE = "\ |
Yingdi Yu | ede8eaf | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 50 | CREATE TABLE IF NOT EXISTS \n \ |
Yingdi Yu | 813d4e9 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 51 | Contact( \n \ |
Yingdi Yu | ede8eaf | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 52 | contact_namespace BLOB NOT NULL, \n \ |
| 53 | contact_alias BLOB NOT NULL, \n \ |
| 54 | self_certificate BLOB NOT NULL, \n \ |
Yingdi Yu | 813d4e9 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 55 | is_introducer INTEGER DEFAULT 0, \n \ |
Yingdi Yu | ede8eaf | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 56 | \ |
| 57 | PRIMARY KEY (contact_namespace) \n \ |
| 58 | ); \n \ |
| 59 | \ |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 60 | CREATE INDEX contact_index ON Contact(contact_namespace); \n \ |
Yingdi Yu | ede8eaf | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 61 | "; |
| 62 | |
Yingdi Yu | 813d4e9 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 63 | const string INIT_TS_TABLE = "\ |
Yingdi Yu | ede8eaf | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 64 | CREATE TABLE IF NOT EXISTS \n \ |
Yingdi Yu | 813d4e9 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 65 | TrustScope( \n \ |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 66 | id INTEGER PRIMARY KEY AUTOINCREMENT, \n \ |
Yingdi Yu | ede8eaf | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 67 | contact_namespace BLOB NOT NULL, \n \ |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 68 | trust_scope BLOB NOT NULL \n \ |
Yingdi Yu | ede8eaf | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 69 | ); \n \ |
| 70 | \ |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 71 | CREATE INDEX ts_index ON TrustScope(contact_namespace); \n \ |
| 72 | "; |
| 73 | |
| 74 | const string INIT_CP_TABLE = "\ |
| 75 | CREATE 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 | \ |
| 85 | CREATE INDEX cp_index ON ContactProfile(profile_identity); \n \ |
| 86 | "; |
| 87 | |
| 88 | const string INIT_PE_TABLE = "\ |
| 89 | CREATE 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 | \ |
| 97 | CREATE INDEX pe_index ON ProfileEndorse(identity); \n \ |
| 98 | "; |
| 99 | |
| 100 | const string INIT_CE_TABLE = "\ |
| 101 | CREATE 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 | \ |
| 111 | CREATE INDEX ce_index ON CollectEndorse(endorsee); \n \ |
Yingdi Yu | ede8eaf | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 112 | "; |
| 113 | |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 114 | ContactStorage::ContactStorage() |
Yingdi Yu | ede8eaf | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 115 | { |
| 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 Yu | 0a6b6c5 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 123 | // Check if SelfProfile table exists |
Yingdi Yu | ede8eaf | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 124 | sqlite3_stmt *stmt; |
Yingdi Yu | 0a6b6c5 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 125 | 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 Yu | 590fa5d | 2013-10-18 18:35:09 -0700 | [diff] [blame] | 141 | // 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 Yu | 0a6b6c5 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 143 | res = sqlite3_step (stmt); |
| 144 | |
Yingdi Yu | 590fa5d | 2013-10-18 18:35:09 -0700 | [diff] [blame] | 145 | bool seTableExist = false; |
Yingdi Yu | 0a6b6c5 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 146 | if (res == SQLITE_ROW) |
Yingdi Yu | 590fa5d | 2013-10-18 18:35:09 -0700 | [diff] [blame] | 147 | seTableExist = true; |
Yingdi Yu | 0a6b6c5 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 148 | sqlite3_finalize (stmt); |
| 149 | |
Yingdi Yu | 590fa5d | 2013-10-18 18:35:09 -0700 | [diff] [blame] | 150 | if(!seTableExist) |
Yingdi Yu | 0a6b6c5 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 151 | { |
| 152 | char *errmsg = 0; |
Yingdi Yu | 590fa5d | 2013-10-18 18:35:09 -0700 | [diff] [blame] | 153 | res = sqlite3_exec (m_db, INIT_SE_TABLE.c_str (), NULL, NULL, &errmsg); |
Yingdi Yu | 0a6b6c5 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 154 | if (res != SQLITE_OK && errmsg != 0) |
Yingdi Yu | 590fa5d | 2013-10-18 18:35:09 -0700 | [diff] [blame] | 155 | throw LnException("Init \"error\" in SelfEndorse"); |
Yingdi Yu | 0a6b6c5 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 159 | // Check if Contact table exists |
Yingdi Yu | 813d4e9 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 160 | sqlite3_prepare_v2 (m_db, "SELECT name FROM sqlite_master WHERE type='table' And name='Contact'", -1, &stmt, 0); |
Yingdi Yu | ede8eaf | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 161 | res = sqlite3_step (stmt); |
| 162 | |
Yingdi Yu | 813d4e9 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 163 | bool contactTableExist = false; |
Yingdi Yu | ede8eaf | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 164 | if (res == SQLITE_ROW) |
Yingdi Yu | 813d4e9 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 165 | contactTableExist = true; |
Yingdi Yu | ede8eaf | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 166 | sqlite3_finalize (stmt); |
| 167 | |
Yingdi Yu | 813d4e9 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 168 | if(!contactTableExist) |
Yingdi Yu | ede8eaf | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 169 | { |
| 170 | char *errmsg = 0; |
Yingdi Yu | 813d4e9 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 171 | res = sqlite3_exec (m_db, INIT_CONTACT_TABLE.c_str (), NULL, NULL, &errmsg); |
Yingdi Yu | ede8eaf | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 172 | if (res != SQLITE_OK && errmsg != 0) |
Yingdi Yu | 813d4e9 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 173 | throw LnException("Init \"error\" in Contact"); |
Yingdi Yu | ede8eaf | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 174 | } |
Yingdi Yu | 813d4e9 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 175 | |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 176 | // Check if TrustScope table exists |
Yingdi Yu | 813d4e9 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 177 | sqlite3_prepare_v2 (m_db, "SELECT name FROM sqlite_master WHERE type='table' And name='TrustScope'", -1, &stmt, 0); |
Yingdi Yu | ede8eaf | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 178 | res = sqlite3_step (stmt); |
| 179 | |
Yingdi Yu | 813d4e9 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 180 | bool tsTableExist = false; |
Yingdi Yu | ede8eaf | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 181 | if (res == SQLITE_ROW) |
Yingdi Yu | 813d4e9 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 182 | tsTableExist = true; |
Yingdi Yu | ede8eaf | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 183 | sqlite3_finalize (stmt); |
| 184 | |
Yingdi Yu | 813d4e9 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 185 | if(!tsTableExist) |
Yingdi Yu | ede8eaf | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 186 | { |
| 187 | char *errmsg = 0; |
Yingdi Yu | 813d4e9 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 188 | res = sqlite3_exec (m_db, INIT_TS_TABLE.c_str (), NULL, NULL, &errmsg); |
Yingdi Yu | ede8eaf | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 189 | if (res != SQLITE_OK && errmsg != 0) |
Yingdi Yu | 813d4e9 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 190 | throw LnException("Init \"error\" in TrustScope"); |
Yingdi Yu | ede8eaf | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 191 | } |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 192 | |
| 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 Yu | ede8eaf | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 243 | } |
| 244 | |
Yingdi Yu | 0a6b6c5 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 245 | bool |
Yingdi Yu | 49eea9f | 2013-10-17 15:07:17 -0700 | [diff] [blame] | 246 | ContactStorage::doesSelfEntryExist(const Name& identity, const string& profileType) |
Yingdi Yu | 0a6b6c5 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 247 | { |
| 248 | bool result = false; |
| 249 | |
| 250 | sqlite3_stmt *stmt; |
Yingdi Yu | 49eea9f | 2013-10-17 15:07:17 -0700 | [diff] [blame] | 251 | sqlite3_prepare_v2 (m_db, "SELECT count(*) FROM SelfProfile WHERE profile_type=? and profile_identity=?", -1, &stmt, 0); |
Yingdi Yu | 0a6b6c5 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 252 | sqlite3_bind_text(stmt, 1, profileType.c_str(), profileType.size(), SQLITE_TRANSIENT); |
Yingdi Yu | 49eea9f | 2013-10-17 15:07:17 -0700 | [diff] [blame] | 253 | sqlite3_bind_text(stmt, 2, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT); |
Yingdi Yu | 0a6b6c5 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 254 | |
| 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 Yu | 0a6b6c5 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 267 | void |
Yingdi Yu | 49eea9f | 2013-10-17 15:07:17 -0700 | [diff] [blame] | 268 | ContactStorage::setSelfProfileEntry(const Name& identity, const string& profileType, const Blob& profileValue) |
Yingdi Yu | 0a6b6c5 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 269 | { |
Yingdi Yu | 0a6b6c5 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 270 | sqlite3_stmt *stmt; |
Yingdi Yu | 49eea9f | 2013-10-17 15:07:17 -0700 | [diff] [blame] | 271 | if(doesSelfEntryExist(identity, profileType)) |
Yingdi Yu | 0a6b6c5 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 272 | { |
Yingdi Yu | 49eea9f | 2013-10-17 15:07:17 -0700 | [diff] [blame] | 273 | sqlite3_prepare_v2 (m_db, "UPDATE SelfProfile SET profile_value=? WHERE profile_type=? and profile_identity=?", -1, &stmt, 0); |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame^] | 274 | sqlite3_bind_text(stmt, 1, (const char*)profileValue.buf(), profileValue.size(), SQLITE_TRANSIENT); |
Yingdi Yu | 0a6b6c5 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 275 | sqlite3_bind_text(stmt, 2, profileType.c_str(), profileType.size(), SQLITE_TRANSIENT); |
Yingdi Yu | 49eea9f | 2013-10-17 15:07:17 -0700 | [diff] [blame] | 276 | sqlite3_bind_text(stmt, 3, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT); |
Yingdi Yu | 0a6b6c5 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 277 | } |
| 278 | else |
| 279 | { |
Yingdi Yu | 49eea9f | 2013-10-17 15:07:17 -0700 | [diff] [blame] | 280 | 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 Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame^] | 283 | sqlite3_bind_text(stmt, 3, (const char*)profileValue.buf(), profileValue.size(), SQLITE_TRANSIENT); |
Yingdi Yu | 0a6b6c5 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 284 | } |
| 285 | sqlite3_step (stmt); |
| 286 | sqlite3_finalize (stmt); |
| 287 | } |
| 288 | |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame^] | 289 | shared_ptr<Profile> |
Yingdi Yu | 49eea9f | 2013-10-17 15:07:17 -0700 | [diff] [blame] | 290 | ContactStorage::getSelfProfile(const Name& identity) |
Yingdi Yu | 0a6b6c5 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 291 | { |
Yingdi Yu | 0a6b6c5 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 292 | sqlite3_stmt *stmt; |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame^] | 293 | shared_ptr<Profile> profile = make_shared<Profile>(identity); |
Yingdi Yu | 0a6b6c5 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 294 | |
Yingdi Yu | 79c25a2 | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 295 | sqlite3_prepare_v2(m_db, "SELECT profile_type, profile_value FROM SelfProfile WHERE profile_identity=?", -1, &stmt, 0); |
Yingdi Yu | 49eea9f | 2013-10-17 15:07:17 -0700 | [diff] [blame] | 296 | sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT); |
Yingdi Yu | 79c25a2 | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 297 | |
| 298 | while(sqlite3_step (stmt) == SQLITE_ROW) |
Yingdi Yu | 0a6b6c5 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 299 | { |
| 300 | string profileType(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0)); |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame^] | 301 | string profileValue(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 1)), sqlite3_column_bytes (stmt, 1)); |
Yingdi Yu | 0a6b6c5 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 302 | |
| 303 | profile->setProfileEntry(profileType, profileValue ); |
| 304 | } |
| 305 | |
Yingdi Yu | 6a5b9f6 | 2013-11-06 23:00:21 -0800 | [diff] [blame] | 306 | sqlite3_finalize(stmt); |
| 307 | |
Yingdi Yu | 0a6b6c5 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 308 | return profile; |
| 309 | } |
Yingdi Yu | 908f841 | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 310 | |
| 311 | void |
| 312 | ContactStorage::removeContact(const Name& contactNameSpace) |
| 313 | { |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame^] | 314 | shared_ptr<ContactItem> contact = getContact(contactNameSpace); |
Yingdi Yu | 908f841 | 2013-11-09 00:03:26 -0800 | [diff] [blame] | 315 | 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 Yu | ede8eaf | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 339 | |
| 340 | void |
Yingdi Yu | 813d4e9 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 341 | ContactStorage::addContact(const ContactItem& contact) |
Yingdi Yu | ede8eaf | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 342 | { |
Yingdi Yu | 813d4e9 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 343 | if(doesContactExist(contact.getNameSpace())) |
Yingdi Yu | ede8eaf | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 344 | throw LnException("Normal Contact has already existed"); |
| 345 | |
Yingdi Yu | 813d4e9 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 346 | bool isIntroducer = contact.isIntroducer(); |
| 347 | |
Yingdi Yu | ede8eaf | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 348 | sqlite3_stmt *stmt; |
| 349 | sqlite3_prepare_v2 (m_db, |
Yingdi Yu | 813d4e9 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 350 | "INSERT INTO Contact (contact_namespace, contact_alias, self_certificate, is_introducer) values (?, ?, ?, ?)", |
Yingdi Yu | ede8eaf | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 351 | -1, |
| 352 | &stmt, |
| 353 | 0); |
| 354 | |
Yingdi Yu | 813d4e9 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 355 | 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 Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame^] | 357 | Blob selfCertificateBlob = contact.getSelfEndorseCertificate().wireEncode(); |
| 358 | sqlite3_bind_text(stmt, 3, (const char*)selfCertificateBlob.buf(), selfCertificateBlob.size(), SQLITE_TRANSIENT); |
Yingdi Yu | 813d4e9 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 359 | sqlite3_bind_int(stmt, 4, (isIntroducer ? 1 : 0)); |
Yingdi Yu | ede8eaf | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 360 | |
| 361 | int res = sqlite3_step (stmt); |
| 362 | sqlite3_finalize (stmt); |
Yingdi Yu | 813d4e9 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 363 | |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame^] | 364 | const Profile& profile = contact.getSelfEndorseCertificate().getProfileData().getProfile(); |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 365 | 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 Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame^] | 376 | sqlite3_bind_text(stmt, 3, it->second.c_str(), it->second.size(), SQLITE_TRANSIENT); |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 377 | res = sqlite3_step (stmt); |
| 378 | sqlite3_finalize (stmt); |
| 379 | } |
| 380 | |
Yingdi Yu | 813d4e9 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 381 | 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 Yu | ede8eaf | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 401 | } |
| 402 | |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 403 | void |
| 404 | ContactStorage::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 Yu | 79c25a2 | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 415 | void |
| 416 | ContactStorage::updateAlias(const ndn::Name& identity, std::string alias) |
| 417 | { |
Yingdi Yu | 813d4e9 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 418 | 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 Yu | 79c25a2 | 2013-10-21 13:38:38 -0700 | [diff] [blame] | 425 | } |
| 426 | |
Yingdi Yu | ede8eaf | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 427 | bool |
Yingdi Yu | 813d4e9 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 428 | ContactStorage::doesContactExist(const Name& name) |
Yingdi Yu | ede8eaf | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 429 | { |
| 430 | bool result = false; |
| 431 | |
| 432 | sqlite3_stmt *stmt; |
Yingdi Yu | 813d4e9 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 433 | sqlite3_prepare_v2 (m_db, "SELECT count(*) FROM Contact WHERE contact_namespace=?", -1, &stmt, 0); |
Yingdi Yu | ede8eaf | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 434 | 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 Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame^] | 448 | void |
| 449 | ContactStorage::getAllContacts(vector<shared_ptr<ContactItem> >& contacts) const |
Yingdi Yu | ede8eaf | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 450 | { |
Yingdi Yu | ede8eaf | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 451 | sqlite3_stmt *stmt; |
Yingdi Yu | 813d4e9 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 452 | sqlite3_prepare_v2 (m_db, "SELECT contact_alias, self_certificate, is_introducer FROM Contact", -1, &stmt, 0); |
Yingdi Yu | ede8eaf | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 453 | |
| 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 Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame^] | 457 | |
| 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 Yu | 813d4e9 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 462 | int isIntroducer = sqlite3_column_int (stmt, 2); |
Yingdi Yu | ede8eaf | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 463 | |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame^] | 464 | contacts.push_back(make_shared<ContactItem>(endorseCertificate, isIntroducer, alias)); |
Yingdi Yu | ede8eaf | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 465 | } |
Yingdi Yu | 813d4e9 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 466 | sqlite3_finalize (stmt); |
| 467 | |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame^] | 468 | vector<shared_ptr<ContactItem> >::iterator it = contacts.begin(); |
Yingdi Yu | 813d4e9 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 469 | 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 Yu | ede8eaf | 2013-10-14 14:07:03 -0700 | [diff] [blame] | 484 | } |
Yingdi Yu | 0a6b6c5 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 485 | |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame^] | 486 | shared_ptr<ContactItem> |
Yingdi Yu | 813d4e9 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 487 | ContactStorage::getContact(const Name& name) |
Yingdi Yu | d40226b | 2013-10-23 14:05:12 -0700 | [diff] [blame] | 488 | { |
| 489 | sqlite3_stmt *stmt; |
Yingdi Yu | 813d4e9 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 490 | sqlite3_prepare_v2 (m_db, "SELECT contact_alias, self_certificate, is_introducer FROM Contact where contact_namespace=?", -1, &stmt, 0); |
Yingdi Yu | d40226b | 2013-10-23 14:05:12 -0700 | [diff] [blame] | 491 | 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 Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame^] | 496 | |
| 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 Yu | 813d4e9 | 2013-11-03 16:22:05 -0800 | [diff] [blame] | 501 | int isIntroducer = sqlite3_column_int (stmt, 2); |
Yingdi Yu | d40226b | 2013-10-23 14:05:12 -0700 | [diff] [blame] | 502 | |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 503 | sqlite3_finalize (stmt); |
| 504 | |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame^] | 505 | shared_ptr<ContactItem> contact = make_shared<ContactItem>(endorseCertificate, isIntroducer, alias); |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 506 | |
| 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 Yu | d40226b | 2013-10-23 14:05:12 -0700 | [diff] [blame] | 521 | } |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame^] | 522 | return CHRONOCHAT_NULL_CONTACTITEM_PTR; |
Yingdi Yu | d40226b | 2013-10-23 14:05:12 -0700 | [diff] [blame] | 523 | } |
| 524 | |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame^] | 525 | shared_ptr<Profile> |
Yingdi Yu | 49eea9f | 2013-10-17 15:07:17 -0700 | [diff] [blame] | 526 | ContactStorage::getSelfProfile(const Name& identity) const |
| 527 | { |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame^] | 528 | shared_ptr<Profile> profile = make_shared<Profile>(identity); |
Yingdi Yu | 49eea9f | 2013-10-17 15:07:17 -0700 | [diff] [blame] | 529 | 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 Yu | 0a6b6c5 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 532 | |
| 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 Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame^] | 536 | string profileValue(reinterpret_cast<const char *>(sqlite3_column_text(stmt, 1)), sqlite3_column_bytes (stmt, 1)); |
Yingdi Yu | 0a6b6c5 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 537 | profile->setProfileEntry(profileType, profileValue); |
| 538 | } |
| 539 | |
Yingdi Yu | 6a5b9f6 | 2013-11-06 23:00:21 -0800 | [diff] [blame] | 540 | sqlite3_finalize(stmt); |
| 541 | |
Yingdi Yu | 0a6b6c5 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 542 | return profile; |
| 543 | } |
| 544 | |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame^] | 545 | Blob |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 546 | ContactStorage::getSelfEndorseCertificate(const Name& identity) |
Yingdi Yu | 0a6b6c5 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 547 | { |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 548 | sqlite3_stmt *stmt; |
Yingdi Yu | 590fa5d | 2013-10-18 18:35:09 -0700 | [diff] [blame] | 549 | sqlite3_prepare_v2 (m_db, "SELECT endorse_data FROM SelfEndorse where identity=?", -1, &stmt, 0); |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 550 | sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT); |
Yingdi Yu | 49eea9f | 2013-10-17 15:07:17 -0700 | [diff] [blame] | 551 | |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 552 | if(sqlite3_step (stmt) == SQLITE_ROW) |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame^] | 553 | { |
| 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 Yu | 0a6b6c5 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 558 | |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 559 | sqlite3_finalize (stmt); |
Yingdi Yu | 68aced9 | 2013-10-17 21:13:03 -0700 | [diff] [blame] | 560 | |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame^] | 561 | return CHRONOCHAT_NULL_BLOB; |
Yingdi Yu | 0a6b6c5 | 2013-10-15 17:54:00 -0700 | [diff] [blame] | 562 | } |
| 563 | |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 564 | void |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame^] | 565 | ContactStorage::updateSelfEndorseCertificate(const EndorseCertificate& newEndorseCertificate, const Name& identity) |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 566 | { |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame^] | 567 | Blob newEndorseCertificateBlob = newEndorseCertificate.wireEncode(); |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 568 | |
| 569 | sqlite3_stmt *stmt; |
Yingdi Yu | 590fa5d | 2013-10-18 18:35:09 -0700 | [diff] [blame] | 570 | sqlite3_prepare_v2 (m_db, "UPDATE SelfEndorse SET endorse_data=? WHERE identity=?", -1, &stmt, 0); |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame^] | 571 | sqlite3_bind_text(stmt, 1, (const char*)newEndorseCertificateBlob.buf(), newEndorseCertificateBlob.size(), SQLITE_TRANSIENT); |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 572 | sqlite3_bind_text(stmt, 2, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT); |
| 573 | sqlite3_step(stmt); |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 574 | |
| 575 | sqlite3_finalize (stmt); |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | void |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame^] | 579 | ContactStorage::addSelfEndorseCertificate(const EndorseCertificate& newEndorseCertificate, const Name& identity) |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 580 | { |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame^] | 581 | Blob newEndorseCertificateBlob = newEndorseCertificate.wireEncode(); |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 582 | |
| 583 | sqlite3_stmt *stmt; |
Yingdi Yu | 590fa5d | 2013-10-18 18:35:09 -0700 | [diff] [blame] | 584 | sqlite3_prepare_v2 (m_db, "INSERT INTO SelfEndorse (identity, endorse_data) values (?, ?)", -1, &stmt, 0); |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 585 | sqlite3_bind_text(stmt, 1, identity.toUri().c_str(), identity.toUri().size(), SQLITE_TRANSIENT); |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame^] | 586 | sqlite3_bind_text(stmt, 2, (const char*)newEndorseCertificateBlob.buf(), newEndorseCertificateBlob.size(), SQLITE_TRANSIENT); |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 587 | sqlite3_step(stmt); |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 588 | |
| 589 | sqlite3_finalize (stmt); |
| 590 | } |
| 591 | |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame^] | 592 | Blob |
| 593 | ContactStorage::getEndorseCertificate(const Name& identity) |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 594 | { |
| 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 Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame^] | 599 | |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 600 | if(sqlite3_step (stmt) == SQLITE_ROW) |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame^] | 601 | { |
| 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 Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 606 | |
| 607 | sqlite3_finalize (stmt); |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame^] | 608 | |
| 609 | return CHRONOCHAT_NULL_BLOB; |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 610 | } |
| 611 | |
| 612 | void |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame^] | 613 | ContactStorage::updateEndorseCertificate(const EndorseCertificate& endorseCertificate, const Name& identity) |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 614 | { |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame^] | 615 | Blob newEndorseCertificateBlob = endorseCertificate.wireEncode(); |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 616 | |
| 617 | sqlite3_stmt *stmt; |
| 618 | sqlite3_prepare_v2 (m_db, "UPDATE ProfileEndorse SET endorse_data=? WHERE identity=?", -1, &stmt, 0); |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame^] | 619 | sqlite3_bind_text(stmt, 1, (const char*)newEndorseCertificateBlob.buf(), newEndorseCertificateBlob.size(), SQLITE_TRANSIENT); |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 620 | 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 | |
| 626 | void |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame^] | 627 | ContactStorage::addEndorseCertificate(const EndorseCertificate& endorseCertificate, const Name& identity) |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 628 | { |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame^] | 629 | Blob newEndorseCertificateBlob = endorseCertificate.wireEncode(); |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 630 | |
| 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 Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame^] | 634 | sqlite3_bind_text(stmt, 2, (const char*)newEndorseCertificateBlob.buf(), newEndorseCertificateBlob.size(), SQLITE_TRANSIENT); |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 635 | sqlite3_step(stmt); |
| 636 | |
| 637 | sqlite3_finalize (stmt); |
| 638 | } |
| 639 | |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame^] | 640 | void |
| 641 | ContactStorage::getEndorseList(const Name& identity, vector<string>& endorseList) |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 642 | { |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 643 | 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 Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 653 | } |
| 654 | |
| 655 | void |
| 656 | ContactStorage::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 Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame^] | 686 | Blob blob = endorseCertificate.wireEncode(); |
| 687 | sqlite3_bind_text(stmt, 4, (const char*)blob.buf(), blob.size(), SQLITE_TRANSIENT); |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 688 | int res = sqlite3_step (stmt); |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 689 | 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 Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame^] | 696 | Blob blob = endorseCertificate.wireEncode(); |
| 697 | sqlite3_bind_text(stmt, 2, (const char*)blob.buf(), blob.size(), SQLITE_TRANSIENT); |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 698 | 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 Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 701 | sqlite3_finalize (stmt); |
| 702 | return; |
| 703 | } |
| 704 | } |
| 705 | |
Yingdi Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame^] | 706 | void |
| 707 | ContactStorage::getCollectEndorseList(const Name& name, vector<Blob>& endorseList) |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 708 | { |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 709 | 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 Yu | 6420611 | 2013-12-24 11:16:32 +0800 | [diff] [blame^] | 715 | Blob blob(reinterpret_cast<const uint8_t*>(sqlite3_column_text(stmt, 0)), sqlite3_column_bytes (stmt, 0)); |
| 716 | endorseList.push_back(blob); |
Yingdi Yu | 8dacdf2 | 2013-11-05 23:06:43 -0800 | [diff] [blame] | 717 | } |
| 718 | |
Yingdi Yu | 6a5b9f6 | 2013-11-06 23:00:21 -0800 | [diff] [blame] | 719 | sqlite3_finalize (stmt); |
Yingdi Yu | aa8d769 | 2013-10-18 17:05:02 -0700 | [diff] [blame] | 720 | } |