src/utilities/bcl/LocalBCL.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /*********************************************************************************************************************** | ||
| 2 | * OpenStudio(R), Copyright (c) Alliance for Energy Innovation, LLC. | ||
| 3 | * See also https://openstudio.net/license | ||
| 4 | ***********************************************************************************************************************/ | ||
| 5 | |||
| 6 | #include "LocalBCL.hpp" | ||
| 7 | #include "RemoteBCL.hpp" | ||
| 8 | #include "../core/Assert.hpp" | ||
| 9 | #include "../time/DateTime.hpp" | ||
| 10 | #include "../core/PathHelpers.hpp" | ||
| 11 | #include "../core/FilesystemHelpers.hpp" | ||
| 12 | |||
| 13 | #include <sqlite3.h> | ||
| 14 | |||
| 15 | #include <boost/algorithm/string/replace.hpp> | ||
| 16 | |||
| 17 | #include <iomanip> | ||
| 18 | |||
| 19 | namespace openstudio { | ||
| 20 | |||
| 21 | 25 | LocalBCL::LocalBCL(const path& libraryPath) | |
| 22 |
3/3✓ Branch 2 taken 25 times.
✓ Branch 5 taken 25 times.
✓ Branch 8 taken 25 times.
|
100 | : m_libraryPath(libraryPath.lexically_normal()), m_dbName("components.sql"), m_dbVersion("1.3"), m_connectionOpen(false) { |
| 23 | //Check for BCL directory | ||
| 24 |
3/8✓ Branch 1 taken 25 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 25 times.
✗ Branch 6 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✓ Branch 10 taken 25 times.
✗ Branch 11 not taken.
|
25 | if (!openstudio::filesystem::is_directory(m_libraryPath) || !openstudio::filesystem::exists(m_libraryPath)) { |
| 25 |
1/1✓ Branch 1 taken 25 times.
|
25 | openstudio::filesystem::create_directory(m_libraryPath); |
| 26 | } | ||
| 27 | // Now we know the library path exists for sure (needed for canonical call), | ||
| 28 | // we convert to an absolute path with no symlink, or dot, or dot-dot elements | ||
| 29 |
2/2✓ Branch 1 taken 25 times.
✓ Branch 4 taken 25 times.
|
25 | m_libraryPath = openstudio::filesystem::canonical(m_libraryPath); |
| 30 | |||
| 31 |
1/1✓ Branch 1 taken 25 times.
|
50 | m_sqliteFilePath = m_libraryPath / m_dbName; |
| 32 |
1/1✓ Branch 2 taken 25 times.
|
50 | m_sqliteFilename = toString(m_sqliteFilePath.make_preferred().native()); |
| 33 | |||
| 34 | //Check for local database | ||
| 35 |
1/1✓ Branch 1 taken 25 times.
|
25 | bool initschema = !openstudio::filesystem::exists(m_sqliteFilePath); |
| 36 | |||
| 37 | // Open the database | ||
| 38 |
1/1✓ Branch 2 taken 25 times.
|
25 | int success = sqlite3_open_v2(m_sqliteFilename.c_str(), &m_db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_EXCLUSIVE, nullptr); |
| 39 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
|
25 | if (success != SQLITE_OK) { |
| 40 | ✗ | LOG_AND_THROW("Unable to open connection for LocalBCL at '" << m_sqliteFilename << "'."); | |
| 41 | } else { | ||
| 42 | 25 | m_connectionOpen = true; | |
| 43 | } | ||
| 44 | |||
| 45 | // If need to create it, and it didn't go well, throw | ||
| 46 |
4/7✓ Branch 0 taken 25 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 25 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 25 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 25 times.
|
25 | if (initschema && !initializeLocalDb()) { |
| 47 | ✗ | LOG_AND_THROW("Unable to initialize Local Database"); | |
| 48 | } | ||
| 49 | |||
| 50 | //Check for out-of-date database | ||
| 51 |
2/3✓ Branch 1 taken 25 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 25 times.
|
25 | if (!updateLocalDb()) { |
| 52 | ✗ | LOG_AND_THROW("Unable to update Local Database"); | |
| 53 | } | ||
| 54 | |||
| 55 | // Retrieve oauthConsumerKeys from database | ||
| 56 | { | ||
| 57 |
1/1✓ Branch 1 taken 25 times.
|
25 | std::string statement = "SELECT data FROM Settings WHERE name='prodAuthKey'"; |
| 58 | sqlite3_stmt* sqlStmtPtr; | ||
| 59 |
2/3✓ Branch 2 taken 25 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 25 times.
|
25 | if (sqlite3_prepare_v2(m_db, statement.c_str(), -1, &sqlStmtPtr, nullptr) != SQLITE_OK) { |
| 60 | ✗ | sqlite3_finalize(sqlStmtPtr); // No-op | |
| 61 | ✗ | LOG_AND_THROW("Unable to prepare m_prodAuthKey Statement"); | |
| 62 | } | ||
| 63 |
2/3✓ Branch 1 taken 25 times.
✓ Branch 3 taken 25 times.
✗ Branch 4 not taken.
|
25 | if (sqlite3_step(sqlStmtPtr) == SQLITE_ROW) { |
| 64 |
2/2✓ Branch 1 taken 25 times.
✓ Branch 4 taken 25 times.
|
25 | m_prodAuthKey = columnText(sqlite3_column_text(sqlStmtPtr, 0)); |
| 65 | } | ||
| 66 | // must finalize to prevent memory leaks | ||
| 67 |
1/1✓ Branch 1 taken 25 times.
|
25 | sqlite3_finalize(sqlStmtPtr); |
| 68 | 25 | } | |
| 69 | |||
| 70 | { | ||
| 71 |
1/1✓ Branch 1 taken 25 times.
|
25 | std::string statement = "SELECT data FROM Settings WHERE name='devAuthKey'"; |
| 72 | sqlite3_stmt* sqlStmtPtr; | ||
| 73 |
2/3✓ Branch 2 taken 25 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 25 times.
|
25 | if (sqlite3_prepare_v2(m_db, statement.c_str(), -1, &sqlStmtPtr, nullptr) != SQLITE_OK) { |
| 74 | ✗ | sqlite3_finalize(sqlStmtPtr); // No-op | |
| 75 | ✗ | LOG_AND_THROW("Unable to prepare m_prodAuthKey Statement"); | |
| 76 | } | ||
| 77 |
2/3✓ Branch 1 taken 25 times.
✓ Branch 3 taken 25 times.
✗ Branch 4 not taken.
|
25 | if (sqlite3_step(sqlStmtPtr) == SQLITE_ROW) { |
| 78 |
2/2✓ Branch 1 taken 25 times.
✓ Branch 4 taken 25 times.
|
25 | m_devAuthKey = columnText(sqlite3_column_text(sqlStmtPtr, 0)); |
| 79 | } | ||
| 80 | // must finalize to prevent memory leaks | ||
| 81 |
1/1✓ Branch 1 taken 25 times.
|
25 | sqlite3_finalize(sqlStmtPtr); |
| 82 | 25 | } | |
| 83 | 25 | } | |
| 84 | |||
| 85 | 5 | bool LocalBCL::beginTransaction() { | |
| 86 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
|
5 | if (!m_db) { |
| 87 | ✗ | LOG(Warn, "Cannot begin transaction without an open database"); | |
| 88 | ✗ | return false; | |
| 89 | } | ||
| 90 | |||
| 91 | 5 | char* err = nullptr; | |
| 92 |
2/3✓ Branch 1 taken 5 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 5 times.
|
5 | if (sqlite3_exec(m_db, "BEGIN", nullptr, nullptr, &err) != SQLITE_OK) { |
| 93 | ✗ | std::string errstr; | |
| 94 | |||
| 95 | ✗ | if (err) { | |
| 96 | ✗ | errstr = err; | |
| 97 | ✗ | sqlite3_free(err); | |
| 98 | } | ||
| 99 | |||
| 100 | ✗ | LOG(Error, "Cannot begin transaction: " << errstr); | |
| 101 | |||
| 102 | ✗ | return false; | |
| 103 | ✗ | } | |
| 104 | |||
| 105 | 5 | return true; | |
| 106 | } | ||
| 107 | |||
| 108 | 5 | bool LocalBCL::commitTransaction() { | |
| 109 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
|
5 | if (!m_db) { |
| 110 | ✗ | LOG(Warn, "Cannot commit transaction without an open database"); | |
| 111 | ✗ | return false; | |
| 112 | } | ||
| 113 | |||
| 114 | 5 | char* err = nullptr; | |
| 115 |
2/3✓ Branch 1 taken 5 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 5 times.
|
5 | if (sqlite3_exec(m_db, "COMMIT", nullptr, nullptr, &err) != SQLITE_OK) { |
| 116 | ✗ | std::string errstr; | |
| 117 | |||
| 118 | ✗ | if (err) { | |
| 119 | ✗ | errstr = err; | |
| 120 | ✗ | sqlite3_free(err); | |
| 121 | } | ||
| 122 | |||
| 123 | ✗ | LOG(Error, "Cannot commit transaction: " << errstr); | |
| 124 | |||
| 125 | ✗ | if (rollbackTransaction()) { | |
| 126 | ✗ | LOG(Warn, "Rolled back transaction."); | |
| 127 | } | ||
| 128 | |||
| 129 | ✗ | return false; | |
| 130 | ✗ | } | |
| 131 | |||
| 132 | 5 | return true; | |
| 133 | } | ||
| 134 | |||
| 135 | ✗ | bool LocalBCL::rollbackTransaction() { | |
| 136 | ✗ | if (!m_db) { | |
| 137 | ✗ | LOG(Warn, "Cannot rollback transaction without an open database"); | |
| 138 | ✗ | return false; | |
| 139 | } | ||
| 140 | |||
| 141 | ✗ | char* err = nullptr; | |
| 142 | ✗ | if (sqlite3_exec(m_db, "ROLLBACK", nullptr, nullptr, &err) != SQLITE_OK) { | |
| 143 | ✗ | std::string errstr; | |
| 144 | |||
| 145 | ✗ | if (err) { | |
| 146 | ✗ | errstr = err; | |
| 147 | ✗ | sqlite3_free(err); | |
| 148 | } | ||
| 149 | |||
| 150 | // TODO: LOG AND THROW? | ||
| 151 | // LOG(Error, "Cannot Rollback transaction! " << errstr); | ||
| 152 | ✗ | LOG_AND_THROW("Cannot Rollback Transaction!" << errstr); | |
| 153 | return false; | ||
| 154 | ✗ | } | |
| 155 | |||
| 156 | ✗ | return true; | |
| 157 | } | ||
| 158 | |||
| 159 | ✗ | openstudio::path LocalBCL::dbPath() const { | |
| 160 | ✗ | return m_libraryPath / m_dbName; | |
| 161 | } | ||
| 162 | |||
| 163 | 83 | std::string LocalBCL::columnText(const unsigned char* column) { | |
| 164 |
1/1✓ Branch 1 taken 83 times.
|
249 | return {reinterpret_cast<const char*>(column)}; |
| 165 | } | ||
| 166 | |||
| 167 | // http://sqlite.org/faq.html#q14 | ||
| 168 | 209 | std::string LocalBCL::escape(const std::string& s) { | |
| 169 | 209 | return boost::replace_all_copy(s, "'", "''"); | |
| 170 | } | ||
| 171 | |||
| 172 | 30 | LocalBCL& LocalBCL::instance() { | |
| 173 | 30 | std::shared_ptr<LocalBCL>& ptr = instanceInternal(); | |
| 174 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 30 times.
|
30 | if (!ptr) { |
| 175 | // DLM: might want to put this somewhere a little more hidden | ||
| 176 | ✗ | ptr = std::shared_ptr<LocalBCL>(new LocalBCL(openstudio::filesystem::home_path() / toPath("BCL"))); | |
| 177 | } | ||
| 178 | 30 | return *ptr; | |
| 179 | } | ||
| 180 | |||
| 181 | 25 | LocalBCL& LocalBCL::instance(const path& libraryPath) { | |
| 182 | 25 | std::shared_ptr<LocalBCL>& ptr = instanceInternal(); | |
| 183 |
1/2✓ Branch 1 taken 25 times.
✗ Branch 2 not taken.
|
25 | if (ptr == nullptr) { |
| 184 | // Doesn't exist yet: no problem, just go ahead and create it | ||
| 185 |
3/5✓ Branch 1 taken 25 times.
✓ Branch 4 taken 25 times.
✓ Branch 7 taken 25 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
|
25 | ptr = std::shared_ptr<LocalBCL>(new LocalBCL(libraryPath)); |
| 186 | } else { | ||
| 187 | // Try to avoid doing something if the canonical path asked matches the one we have in store | ||
| 188 | ✗ | openstudio::path canLibraryPath(libraryPath); | |
| 189 | // If libraryPath is an existing dir, take the canonical path, so that it matches what we stored in the Ctor | ||
| 190 | ✗ | if (openstudio::filesystem::is_directory(canLibraryPath) && openstudio::filesystem::exists(canLibraryPath)) { | |
| 191 | ✗ | canLibraryPath = openstudio::filesystem::canonical(canLibraryPath); | |
| 192 | } | ||
| 193 | ✗ | if (ptr->libraryPath() != canLibraryPath) { | |
| 194 | // TODO: same comment as in LocalBCL::close, can we be sure that ptr.reset() actually ends up calling the dtor? | ||
| 195 | ✗ | ptr->closeConnection(); | |
| 196 | ✗ | ptr.reset(); | |
| 197 | ✗ | ptr = std::shared_ptr<LocalBCL>(new LocalBCL(libraryPath)); | |
| 198 | } | ||
| 199 | ✗ | } | |
| 200 | 25 | return *ptr; | |
| 201 | } | ||
| 202 | |||
| 203 | 105 | std::shared_ptr<LocalBCL>& LocalBCL::instanceInternal() { | |
| 204 |
3/4✓ Branch 0 taken 25 times.
✓ Branch 1 taken 80 times.
✓ Branch 3 taken 25 times.
✗ Branch 4 not taken.
|
105 | static std::shared_ptr<LocalBCL> instance; |
| 205 | 105 | return instance; | |
| 206 | } | ||
| 207 | |||
| 208 | 25 | void LocalBCL::close() { | |
| 209 | // Close the connection to the database if needed. | ||
| 210 | // TODO: something is bugging me here. why are we using a std::shared_ptr and not unique_ptr? | ||
| 211 | // shared_ptr -> reset() should destroy the object (which calls closeConnection) if it's only reference left, which it should... | ||
| 212 | // TODO: probabably not needed | ||
| 213 | 25 | instanceInternal()->closeConnection(); | |
| 214 | // reset pointer | ||
| 215 | 25 | instanceInternal().reset(); | |
| 216 | 25 | } | |
| 217 | |||
| 218 | 50 | bool LocalBCL::closeConnection() { | |
| 219 | // Close the connection to the database if needed | ||
| 220 |
2/2✓ Branch 0 taken 25 times.
✓ Branch 1 taken 25 times.
|
50 | if (m_connectionOpen) { |
| 221 | 25 | sqlite3_close(m_db); | |
| 222 | 25 | m_connectionOpen = false; | |
| 223 | } | ||
| 224 | 50 | return true; | |
| 225 | } | ||
| 226 | |||
| 227 | 100 | LocalBCL::~LocalBCL() { | |
| 228 | // Close the connection to the database if needed | ||
| 229 | 25 | closeConnection(); | |
| 230 | 50 | } | |
| 231 | |||
| 232 | 25 | bool LocalBCL::initializeLocalDb() { | |
| 233 | std::string create_statements( | ||
| 234 | "CREATE TABLE Settings (name VARCHAR, data VARCHAR);" | ||
| 235 | "CREATE TABLE Components (uid VARCHAR, version_id VARCHAR, name VARCHAR, description VARCHAR, date_added DATETIME, date_modified DATETIME);" | ||
| 236 | "CREATE TABLE Files (uid VARCHAR, version_id VARCHAR, filename VARCHAR, filetype VARCHAR, usage_type VARCHAR, checksum VARCHAR);" | ||
| 237 | "CREATE TABLE Attributes (uid VARCHAR, version_id VARCHAR, name VARCHAR, value VARCHAR, units VARCHAR, type VARCHAR);" | ||
| 238 | "CREATE TABLE Measures (uid VARCHAR, version_id VARCHAR, name VARCHAR, description VARCHAR, modeler_description VARCHAR, date_added DATETIME, " | ||
| 239 |
1/1✓ Branch 1 taken 25 times.
|
25 | "date_modified DATETIME);"); |
| 240 | |||
| 241 | 25 | char* err = nullptr; | |
| 242 |
2/3✓ Branch 2 taken 25 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 25 times.
|
25 | if (sqlite3_exec(m_db, create_statements.c_str(), nullptr, nullptr, &err) != SQLITE_OK) { |
| 243 | ✗ | std::string errstr; | |
| 244 | |||
| 245 | ✗ | if (err) { | |
| 246 | ✗ | errstr = err; | |
| 247 | ✗ | sqlite3_free(err); | |
| 248 | } | ||
| 249 | |||
| 250 | ✗ | LOG(Error, "Erorr in initializeLocalDb when creating tables: " << errstr); | |
| 251 | ✗ | return false; | |
| 252 | ✗ | } | |
| 253 | |||
| 254 | 25 | sqlite3_stmt* sqlStmtPtr = nullptr; | |
| 255 | // 1=name, 2=value | ||
| 256 |
1/1✓ Branch 1 taken 25 times.
|
25 | std::string insert_statement = "INSERT INTO Settings VALUES (?, ?)"; |
| 257 |
2/3✓ Branch 3 taken 25 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 25 times.
|
25 | if (sqlite3_prepare_v2(m_db, insert_statement.c_str(), insert_statement.size(), &sqlStmtPtr, nullptr) != SQLITE_OK) { |
| 258 | ✗ | LOG(Error, "Error preparing insert statement"); | |
| 259 | // Note JM 2020-03-30: There's no need to call `sqlite3_finalize(sqlStmtPtr);` | ||
| 260 | // if prepare_v2 has failed since you wouldn't have a valid handle, but it's a harmless no-op so for consistency/forcing | ||
| 261 | // us to remember to do it, I do anyways | ||
| 262 | ✗ | sqlite3_finalize(sqlStmtPtr); // No-op | |
| 263 | ✗ | return false; | |
| 264 | } | ||
| 265 | |||
| 266 | // BCL auth keys are deprecated, so pre-populating it with 32-chars will prevent prompts for it | ||
| 267 | std::vector<std::pair<std::string, std::string>> vals = { | ||
| 268 | 25 | {"dbVersion", m_dbVersion}, | |
| 269 | {"prodAuthKey", "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"}, | ||
| 270 | {"devAuthKey", "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"}, | ||
| 271 |
3/5✓ Branch 1 taken 25 times.
✓ Branch 3 taken 75 times.
✓ Branch 4 taken 25 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
|
125 | }; |
| 272 | |||
| 273 | 25 | bool errorsFound = false; | |
| 274 |
2/2✓ Branch 5 taken 75 times.
✓ Branch 6 taken 25 times.
|
100 | for (const auto& p : vals) { |
| 275 |
2/3✓ Branch 3 taken 75 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 75 times.
|
75 | if (sqlite3_bind_text(sqlStmtPtr, 1, p.first.c_str(), p.first.size(), SQLITE_TRANSIENT) != SQLITE_OK) { |
| 276 | ✗ | LOG(Error, "Error binding to the 1st parameter (initializeLocalDb): " << p.first); | |
| 277 | ✗ | errorsFound = true; | |
| 278 |
2/3✓ Branch 3 taken 75 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 75 times.
|
75 | } else if (sqlite3_bind_text(sqlStmtPtr, 2, p.second.c_str(), p.second.size(), SQLITE_TRANSIENT) != SQLITE_OK) { |
| 279 | ✗ | LOG(Error, "Error binding to the 2nd parameter (initializeLocalDb): " << p.second); | |
| 280 | ✗ | errorsFound = true; | |
| 281 |
2/3✓ Branch 1 taken 75 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 75 times.
|
75 | } else if (sqlite3_step(sqlStmtPtr) != SQLITE_DONE) { |
| 282 | ✗ | LOG(Error, "Error executing prepared statement"); | |
| 283 | ✗ | errorsFound = true; | |
| 284 | } | ||
| 285 | |||
| 286 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 75 times.
|
75 | if (errorsFound) { |
| 287 | // must finalize to prevent memory leaks (which resets too) | ||
| 288 | ✗ | sqlite3_finalize(sqlStmtPtr); | |
| 289 | ✗ | return false; | |
| 290 | } | ||
| 291 | |||
| 292 | // Reset the statement to allow binding variables on the next iteration | ||
| 293 |
1/1✓ Branch 1 taken 75 times.
|
75 | sqlite3_reset(sqlStmtPtr); |
| 294 | } | ||
| 295 | |||
| 296 |
1/1✓ Branch 1 taken 25 times.
|
25 | sqlite3_finalize(sqlStmtPtr); |
| 297 | |||
| 298 | 25 | return true; | |
| 299 | 50 | } | |
| 300 | |||
| 301 | 25 | bool LocalBCL::updateLocalDb() { | |
| 302 | |||
| 303 | 25 | std::string localDbVersion; | |
| 304 | |||
| 305 | { | ||
| 306 | // Get current version from the sqlite file in question | ||
| 307 |
1/1✓ Branch 1 taken 25 times.
|
25 | std::string statement = "SELECT data FROM Settings WHERE name='dbVersion'"; |
| 308 | sqlite3_stmt* sqlStmtPtr; | ||
| 309 |
2/3✓ Branch 2 taken 25 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 25 times.
|
25 | if (sqlite3_prepare_v2(m_db, statement.c_str(), -1, &sqlStmtPtr, nullptr) != SQLITE_OK) { |
| 310 | ✗ | LOG(Error, "Unable to prepare dbVersion Statement"); | |
| 311 | ✗ | sqlite3_finalize(sqlStmtPtr); // No-op | |
| 312 | ✗ | return false; | |
| 313 | } | ||
| 314 | // If this doesn't return, it may be normal (old version), so don't return / log an error | ||
| 315 |
2/3✓ Branch 1 taken 25 times.
✓ Branch 3 taken 25 times.
✗ Branch 4 not taken.
|
25 | if (sqlite3_step(sqlStmtPtr) == SQLITE_ROW) { |
| 316 |
2/2✓ Branch 1 taken 25 times.
✓ Branch 4 taken 25 times.
|
25 | localDbVersion = columnText(sqlite3_column_text(sqlStmtPtr, 0)); |
| 317 | // If already latest version, do nothing | ||
| 318 |
1/2✓ Branch 1 taken 25 times.
✗ Branch 2 not taken.
|
25 | if (localDbVersion == m_dbVersion) { |
| 319 |
1/1✓ Branch 1 taken 25 times.
|
25 | sqlite3_finalize(sqlStmtPtr); |
| 320 | 25 | return true; | |
| 321 | } | ||
| 322 | } | ||
| 323 | ✗ | sqlite3_finalize(sqlStmtPtr); | |
| 324 | 25 | } | |
| 325 | |||
| 326 | // 1.0, 1.1 -> 1.2 | ||
| 327 | { | ||
| 328 | // Get current version from the sqlite file in question | ||
| 329 | ✗ | std::string statement = "SELECT oauthConsumerKey, dbVersion FROM Settings"; | |
| 330 | sqlite3_stmt* sqlStmtPtr; | ||
| 331 | ✗ | if (sqlite3_prepare_v2(m_db, statement.c_str(), -1, &sqlStmtPtr, nullptr) != SQLITE_OK) { | |
| 332 | ✗ | LOG(Error, "Unable to prepare dbVersion 1.0/1.1 Statement"); | |
| 333 | ✗ | sqlite3_finalize(sqlStmtPtr); // No-op | |
| 334 | ✗ | return false; | |
| 335 | } | ||
| 336 | // If this doesn't return, it may be normal (not the right version), so don't return / log an error | ||
| 337 | // Otherwise, we expect one row, with the two values we care about about | ||
| 338 | ✗ | if (sqlite3_step(sqlStmtPtr) == SQLITE_ROW) { | |
| 339 | |||
| 340 | ✗ | std::string oauthConsumerKey = columnText(sqlite3_column_text(sqlStmtPtr, 0)); | |
| 341 | ✗ | localDbVersion = columnText(sqlite3_column_text(sqlStmtPtr, 1)); | |
| 342 | ✗ | sqlite3_finalize(sqlStmtPtr); | |
| 343 | |||
| 344 | // If already latest version, do nothing | ||
| 345 | ✗ | if (localDbVersion == "1.0" || localDbVersion == "1.1") { | |
| 346 | |||
| 347 | // Start a transaction, so we can handle failures without messing up the database | ||
| 348 | ✗ | if (!beginTransaction()) { | |
| 349 | ✗ | return false; | |
| 350 | } | ||
| 351 | |||
| 352 | std::string update_statements("ALTER TABLE Components ADD date_added DATETIME;" | ||
| 353 | "ALTER TABLE Components ADD date_modified DATETIME;" | ||
| 354 | "DROP TABLE Settings;" | ||
| 355 | ✗ | "CREATE TABLE Settings (name VARCHAR, data VARCHAR);"); | |
| 356 | |||
| 357 | ✗ | char* err = nullptr; | |
| 358 | ✗ | if (sqlite3_exec(m_db, update_statements.c_str(), nullptr, nullptr, &err) != SQLITE_OK) { | |
| 359 | ✗ | std::string errstr; | |
| 360 | |||
| 361 | ✗ | if (err) { | |
| 362 | ✗ | errstr = err; | |
| 363 | ✗ | sqlite3_free(err); | |
| 364 | } | ||
| 365 | |||
| 366 | ✗ | LOG(Error, "Error in UpdateLocalDb when altering tables from 1.0/1.1 to " << m_dbVersion << ": " << errstr); | |
| 367 | // Rollback changes | ||
| 368 | ✗ | rollbackTransaction(); | |
| 369 | ✗ | return false; | |
| 370 | ✗ | } | |
| 371 | |||
| 372 | // Insertion block | ||
| 373 | { | ||
| 374 | ✗ | sqlite3_stmt* sqlInsertStmtPtr = nullptr; | |
| 375 | // 1=name, 2=value | ||
| 376 | ✗ | std::string insert_statement = "INSERT INTO Settings VALUES (?, ?)"; | |
| 377 | ✗ | if (sqlite3_prepare_v2(m_db, insert_statement.c_str(), insert_statement.size(), &sqlInsertStmtPtr, nullptr) != SQLITE_OK) { | |
| 378 | ✗ | LOG(Error, "Error preparing insert statement"); | |
| 379 | ✗ | sqlite3_finalize(sqlInsertStmtPtr); // No-op | |
| 380 | ✗ | rollbackTransaction(); | |
| 381 | ✗ | return false; | |
| 382 | } | ||
| 383 | |||
| 384 | std::vector<std::pair<std::string, std::string>> vals = { | ||
| 385 | ✗ | {"dbVersion", m_dbVersion}, | |
| 386 | // old oauthConsumerKey becomes prodAuthKey | ||
| 387 | {"prodAuthKey", oauthConsumerKey}, | ||
| 388 | {"devAuthKey", ""}, | ||
| 389 | ✗ | }; | |
| 390 | |||
| 391 | ✗ | bool errorsFound = false; | |
| 392 | ✗ | for (const auto& p : vals) { | |
| 393 | ✗ | if (sqlite3_bind_text(sqlInsertStmtPtr, 1, p.first.c_str(), p.first.size(), SQLITE_TRANSIENT) != SQLITE_OK) { | |
| 394 | ✗ | LOG(Error, "Error binding to the 1st parameter: " << p.first); | |
| 395 | ✗ | errorsFound = true; | |
| 396 | ✗ | } else if (sqlite3_bind_text(sqlInsertStmtPtr, 2, p.second.c_str(), p.second.size(), SQLITE_TRANSIENT) != SQLITE_OK) { | |
| 397 | ✗ | LOG(Error, "Error binding to the 2nd parameter: " << p.second); | |
| 398 | ✗ | errorsFound = true; | |
| 399 | ✗ | } else if (sqlite3_step(sqlInsertStmtPtr) != SQLITE_DONE) { | |
| 400 | ✗ | LOG(Error, "Error executing prepared statement"); | |
| 401 | ✗ | errorsFound = true; | |
| 402 | } | ||
| 403 | |||
| 404 | ✗ | if (errorsFound) { | |
| 405 | // Rollback changes | ||
| 406 | ✗ | sqlite3_finalize(sqlInsertStmtPtr); // No-op | |
| 407 | ✗ | rollbackTransaction(); | |
| 408 | ✗ | return false; | |
| 409 | } | ||
| 410 | |||
| 411 | // Reset the statement to allow binding variables on the next iteration | ||
| 412 | ✗ | sqlite3_reset(sqlInsertStmtPtr); | |
| 413 | } | ||
| 414 | |||
| 415 | ✗ | sqlite3_finalize(sqlStmtPtr); | |
| 416 | ✗ | } | |
| 417 | |||
| 418 | // Commit changes now that everything went well | ||
| 419 | ✗ | return commitTransaction(); | |
| 420 | ✗ | } | |
| 421 | ✗ | } | |
| 422 | ✗ | } // End version 1.0/1.1 | |
| 423 | |||
| 424 | // 1.2 -> 1.3 | ||
| 425 | ✗ | if (localDbVersion == "1.2") { | |
| 426 | |||
| 427 | // Start a transaction, so we can handle failures without messing up the database | ||
| 428 | ✗ | if (!beginTransaction()) { | |
| 429 | ✗ | return false; | |
| 430 | } | ||
| 431 | |||
| 432 | // First update | ||
| 433 | { | ||
| 434 | std::string update_statements("ALTER TABLE Attributes ADD version_id VARCHAR;" | ||
| 435 | "ALTER TABLE Files ADD version_id VARCHAR;" | ||
| 436 | "UPDATE Attributes SET type = 'string' WHERE type = 'varchar';" | ||
| 437 | "ALTER TABLE Files ADD usage_type VARCHAR;" | ||
| 438 | "ALTER TABLE Files ADD checksum VARCHAR;" | ||
| 439 | "CREATE TABLE Measures (uid VARCHAR, version_id VARCHAR, name VARCHAR, description VARCHAR, modeler_description " | ||
| 440 | "VARCHAR, date_added DATETIME, date_modified DATETIME);" | ||
| 441 | "UPDATE Settings SET data = " | ||
| 442 | ✗ | + m_dbVersion + " WHERE name = 'dbVersion';"); | |
| 443 | |||
| 444 | ✗ | char* err = nullptr; | |
| 445 | ✗ | if (sqlite3_exec(m_db, update_statements.c_str(), nullptr, nullptr, &err) != SQLITE_OK) { | |
| 446 | ✗ | std::string errstr; | |
| 447 | |||
| 448 | ✗ | if (err) { | |
| 449 | ✗ | errstr = err; | |
| 450 | ✗ | sqlite3_free(err); | |
| 451 | } | ||
| 452 | |||
| 453 | ✗ | LOG(Error, "Error in UpdateLocalDb when altering tables from 1.2 to " << m_dbVersion << ": " << errstr); | |
| 454 | // Rollback changes | ||
| 455 | ✗ | rollbackTransaction(); | |
| 456 | ✗ | return false; | |
| 457 | ✗ | } | |
| 458 | ✗ | } | |
| 459 | |||
| 460 | // Now, Loop on the components table to update version_ID | ||
| 461 | { | ||
| 462 | |||
| 463 | // Update statement | ||
| 464 | ✗ | std::string update_attribute_record = "UPDATE Attributes SET version_ID=? WHERE uid=?"; | |
| 465 | sqlite3_stmt* updateAtrStmtPtr; | ||
| 466 | ✗ | if (sqlite3_prepare_v2(m_db, update_attribute_record.c_str(), update_attribute_record.size(), &updateAtrStmtPtr, nullptr) != SQLITE_OK) { | |
| 467 | ✗ | LOG(Error, "Error preparing update_attribute_record statement"); | |
| 468 | ✗ | sqlite3_finalize(updateAtrStmtPtr); // No-op | |
| 469 | ✗ | rollbackTransaction(); | |
| 470 | ✗ | return false; | |
| 471 | } | ||
| 472 | |||
| 473 | ✗ | std::string update_file_record = "UPDATE Files SET version_ID=? WHERE uid=?"; | |
| 474 | sqlite3_stmt* updateFileStmtPtr; | ||
| 475 | ✗ | if (sqlite3_prepare_v2(m_db, update_file_record.c_str(), update_file_record.size(), &updateFileStmtPtr, nullptr) != SQLITE_OK) { | |
| 476 | ✗ | LOG(Error, "Error preparing update_file_record statement"); | |
| 477 | // Rollback changes | ||
| 478 | ✗ | sqlite3_finalize(updateFileStmtPtr); // No-op | |
| 479 | ✗ | sqlite3_finalize(updateAtrStmtPtr); // Needed | |
| 480 | ✗ | rollbackTransaction(); | |
| 481 | ✗ | return false; | |
| 482 | } | ||
| 483 | |||
| 484 | // Get current version from the sqlite file in question | ||
| 485 | ✗ | std::string statement = "SELECT uid, version_id FROM Components WHERE uid IN (SELECT DISTINCT uid FROM Components)"; | |
| 486 | sqlite3_stmt* sqlStmtPtr; | ||
| 487 | ✗ | int code = sqlite3_prepare_v2(m_db, statement.c_str(), -1, &sqlStmtPtr, nullptr); | |
| 488 | ✗ | if (code != SQLITE_OK) { | |
| 489 | ✗ | LOG(Error, "Unable to prepare current version Statement: " << statement); | |
| 490 | ✗ | sqlite3_finalize(sqlStmtPtr); // No-op | |
| 491 | ✗ | return false; | |
| 492 | } | ||
| 493 | |||
| 494 | ✗ | bool errorsFound = false; | |
| 495 | |||
| 496 | // Loop until done (or failed) | ||
| 497 | ✗ | while ((!errorsFound) && (code != SQLITE_DONE) && (code != SQLITE_BUSY) && (code != SQLITE_ERROR) && (code != SQLITE_MISUSE)) { | |
| 498 | ✗ | code = sqlite3_step(sqlStmtPtr); | |
| 499 | ✗ | if (code == SQLITE_ROW) { | |
| 500 | // Get values from SELECT | ||
| 501 | ✗ | std::string uid = columnText(sqlite3_column_text(sqlStmtPtr, 0)); | |
| 502 | ✗ | std::string version_id = columnText(sqlite3_column_text(sqlStmtPtr, 1)); | |
| 503 | |||
| 504 | // Update record | ||
| 505 | ✗ | if (sqlite3_bind_text(updateAtrStmtPtr, 1, uid.c_str(), uid.size(), SQLITE_TRANSIENT) != SQLITE_OK) { | |
| 506 | ✗ | LOG(Error, "updateAtrStmtPtr: Error binding to the 1st parameter uid=" << uid); | |
| 507 | ✗ | errorsFound = true; | |
| 508 | ✗ | } else if (sqlite3_bind_text(updateAtrStmtPtr, 2, version_id.c_str(), version_id.size(), SQLITE_TRANSIENT) != SQLITE_OK) { | |
| 509 | ✗ | LOG(Error, "updateAtrStmtPtr: Error binding to the 2nd parameter version_id=" << version_id); | |
| 510 | ✗ | errorsFound = true; | |
| 511 | ✗ | } else if (sqlite3_step(updateAtrStmtPtr) != SQLITE_DONE) { | |
| 512 | ✗ | LOG(Error, "Error executing prepared updateAtrStmtPtr"); | |
| 513 | ✗ | errorsFound = true; | |
| 514 | } else { | ||
| 515 | // Reset the statement to allow binding variables on the next iteration | ||
| 516 | ✗ | sqlite3_reset(updateAtrStmtPtr); | |
| 517 | } | ||
| 518 | |||
| 519 | // Update record | ||
| 520 | ✗ | if (sqlite3_bind_text(updateFileStmtPtr, 1, uid.c_str(), uid.size(), SQLITE_TRANSIENT) != SQLITE_OK) { | |
| 521 | ✗ | LOG(Error, "updateFileStmtPtr: Error binding to the 1st parameter uid=" << uid); | |
| 522 | ✗ | errorsFound = true; | |
| 523 | ✗ | } else if (sqlite3_bind_text(updateFileStmtPtr, 2, version_id.c_str(), version_id.size(), SQLITE_TRANSIENT) != SQLITE_OK) { | |
| 524 | ✗ | LOG(Error, "updateFileStmtPtr: Error binding to the 2nd parameter version_id=" << version_id); | |
| 525 | ✗ | errorsFound = true; | |
| 526 | ✗ | } else if (sqlite3_step(updateFileStmtPtr) != SQLITE_DONE) { | |
| 527 | ✗ | LOG(Error, "Error executing prepared updateFileStmtPtr"); | |
| 528 | ✗ | errorsFound = true; | |
| 529 | } else { | ||
| 530 | // Reset the statement to allow binding variables on the next iteration | ||
| 531 | ✗ | sqlite3_reset(updateFileStmtPtr); | |
| 532 | } | ||
| 533 | |||
| 534 | ✗ | } else // i didn't get a row. something is wrong so set the exit condition. | |
| 535 | { // should never get here since i test for all documented return states above | ||
| 536 | ✗ | code = SQLITE_DONE; | |
| 537 | } | ||
| 538 | } // End loop on each uid, version_id | ||
| 539 | |||
| 540 | // Finalize all statements whether it suceeded or not | ||
| 541 | ✗ | sqlite3_finalize(sqlStmtPtr); | |
| 542 | ✗ | sqlite3_finalize(updateAtrStmtPtr); | |
| 543 | ✗ | sqlite3_finalize(updateFileStmtPtr); | |
| 544 | |||
| 545 | ✗ | if (errorsFound || code != SQLITE_DONE) { | |
| 546 | // Rollback changes | ||
| 547 | ✗ | rollbackTransaction(); | |
| 548 | ✗ | return false; | |
| 549 | } | ||
| 550 | ✗ | } // End of update of the version_id | |
| 551 | |||
| 552 | // Change paths | ||
| 553 | { | ||
| 554 | // Get current version from the sqlite file in question | ||
| 555 | ✗ | std::string statement = "SELECT uid, version_id, directory FROM Components"; | |
| 556 | sqlite3_stmt* sqlStmtPtr; | ||
| 557 | ✗ | int code = sqlite3_prepare_v2(m_db, statement.c_str(), -1, &sqlStmtPtr, nullptr); | |
| 558 | ✗ | if (code != SQLITE_OK) { | |
| 559 | ✗ | LOG(Error, "Unable to prepare current version Statement: " << statement); | |
| 560 | ✗ | sqlite3_finalize(sqlStmtPtr); // No-op | |
| 561 | ✗ | return false; | |
| 562 | } | ||
| 563 | |||
| 564 | // Loop until done (or failed) | ||
| 565 | ✗ | while ((code != SQLITE_DONE) && (code != SQLITE_BUSY) && (code != SQLITE_ERROR) && (code != SQLITE_MISUSE)) //loop until SQLITE_DONE | |
| 566 | { | ||
| 567 | ✗ | code = sqlite3_step(sqlStmtPtr); | |
| 568 | ✗ | if (code == SQLITE_ROW) { | |
| 569 | // Get values from SELECT | ||
| 570 | ✗ | std::string uid = columnText(sqlite3_column_text(sqlStmtPtr, 0)); | |
| 571 | ✗ | std::string version_id = columnText(sqlite3_column_text(sqlStmtPtr, 1)); | |
| 572 | ✗ | std::string directory = columnText(sqlite3_column_text(sqlStmtPtr, 2)); | |
| 573 | |||
| 574 | // Remove trailing separator (`/` or `\`) | ||
| 575 | ✗ | openstudio::path p_directory(directory); | |
| 576 | ✗ | openstudio::path src = m_libraryPath / uid / version_id / p_directory.remove_trailing_separator().filename(); | |
| 577 | ✗ | openstudio::path dest = src.parent_path(); | |
| 578 | |||
| 579 | ✗ | openstudio::filesystem::remove(dest / toPath("DISCLAIMER.txt")); | |
| 580 | ✗ | openstudio::filesystem::remove(dest / toPath("README.txt")); | |
| 581 | ✗ | openstudio::filesystem::remove(dest / toPath("output.xml")); | |
| 582 | ✗ | copyDirectory(src, dest); | |
| 583 | ✗ | removeDirectory(src); | |
| 584 | |||
| 585 | ✗ | } else // i didn't get a row. something is wrong so set the exit condition. | |
| 586 | { // should never get here since i test for all documented return states above | ||
| 587 | ✗ | code = SQLITE_DONE; | |
| 588 | } | ||
| 589 | } // End loop on each uid, version_id, directory | ||
| 590 | |||
| 591 | // Finalize statement whether it suceeded or not | ||
| 592 | ✗ | sqlite3_finalize(sqlStmtPtr); | |
| 593 | |||
| 594 | // TODO: this still deleted the files already... | ||
| 595 | ✗ | if (code != SQLITE_DONE) { | |
| 596 | // Rollback changes | ||
| 597 | ✗ | rollbackTransaction(); | |
| 598 | ✗ | return false; | |
| 599 | } | ||
| 600 | |||
| 601 | ✗ | } // End update paths | |
| 602 | |||
| 603 | // Final update | ||
| 604 | { | ||
| 605 | |||
| 606 | std::string update_statements( | ||
| 607 | "ALTER TABLE Components RENAME TO ComponentsTmp;" | ||
| 608 | "CREATE TABLE Components(uid VARCHAR, version_id VARCHAR, name VARCHAR, description VARCHAR, date_added DATETIME, date_modified DATETIME);" | ||
| 609 | "INSERT INTO Components SELECT uid, version_id, name, type, date_added, date_modified FROM ComponentsTmp;" | ||
| 610 | ✗ | "DROP TABLE ComponentsTmp;"); | |
| 611 | |||
| 612 | ✗ | char* err = nullptr; | |
| 613 | ✗ | if (sqlite3_exec(m_db, update_statements.c_str(), nullptr, nullptr, &err) != SQLITE_OK) { | |
| 614 | ✗ | std::string errstr; | |
| 615 | |||
| 616 | ✗ | if (err) { | |
| 617 | ✗ | errstr = err; | |
| 618 | ✗ | sqlite3_free(err); | |
| 619 | } | ||
| 620 | |||
| 621 | ✗ | LOG(Error, "Error in UpdateLocalDb when altering final tables from 1.2 to " << m_dbVersion << ": " << errstr); | |
| 622 | // Rollback changes | ||
| 623 | ✗ | rollbackTransaction(); | |
| 624 | ✗ | return false; | |
| 625 | ✗ | } | |
| 626 | ✗ | } | |
| 627 | |||
| 628 | // Commit changes now that everything went well | ||
| 629 | ✗ | return commitTransaction(); | |
| 630 | |||
| 631 | } // End 1.2 -> 1.3 | ||
| 632 | |||
| 633 | ✗ | return false; | |
| 634 | 25 | } | |
| 635 | |||
| 636 | /// Inherited members | ||
| 637 | |||
| 638 | 7 | boost::optional<BCLComponent> LocalBCL::getComponent(const std::string& uid, const std::string& versionId) const { | |
| 639 | 7 | boost::optional<BCLComponent> result; | |
| 640 | |||
| 641 |
1/2✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
|
7 | if (m_db) { |
| 642 | |||
| 643 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
|
7 | if (versionId.empty()) { |
| 644 | |||
| 645 | ✗ | std::string statement = "SELECT version_id FROM Components WHERE uid='" + escape(uid) + "'"; | |
| 646 | sqlite3_stmt* sqlStmtPtr; | ||
| 647 | ✗ | if (sqlite3_prepare_v2(m_db, statement.c_str(), -1, &sqlStmtPtr, nullptr) != SQLITE_OK) { | |
| 648 | ✗ | LOG(Error, "Unable to prepare version_id Statement"); | |
| 649 | ✗ | sqlite3_finalize(sqlStmtPtr); // No-op | |
| 650 | ✗ | return boost::none; | |
| 651 | } | ||
| 652 | |||
| 653 | ✗ | if (sqlite3_step(sqlStmtPtr) == SQLITE_ROW) { | |
| 654 | ✗ | std::string version_id = columnText(sqlite3_column_text(sqlStmtPtr, 0)); | |
| 655 | ✗ | result = boost::optional<BCLComponent>(m_libraryPath / uid / version_id); | |
| 656 | ✗ | } | |
| 657 | // Finalize statement to prevent memory leak | ||
| 658 | ✗ | sqlite3_finalize(sqlStmtPtr); | |
| 659 | ✗ | } else { | |
| 660 |
6/6✓ Branch 1 taken 7 times.
✓ Branch 4 taken 7 times.
✓ Branch 7 taken 7 times.
✓ Branch 10 taken 7 times.
✓ Branch 13 taken 7 times.
✓ Branch 16 taken 7 times.
|
7 | std::string statement = "SELECT version_id FROM Components WHERE uid='" + escape(uid) + "' AND version_id='" + escape(versionId) + "'"; |
| 661 | sqlite3_stmt* sqlStmtPtr; | ||
| 662 |
2/3✓ Branch 2 taken 7 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 7 times.
|
7 | if (sqlite3_prepare_v2(m_db, statement.c_str(), -1, &sqlStmtPtr, nullptr) != SQLITE_OK) { |
| 663 | ✗ | LOG(Error, "Unable to prepare version_id Statement from uid and versionId."); | |
| 664 | ✗ | sqlite3_finalize(sqlStmtPtr); // No-op | |
| 665 | ✗ | return boost::none; | |
| 666 | } | ||
| 667 | |||
| 668 |
3/3✓ Branch 1 taken 7 times.
✓ Branch 3 taken 4 times.
✓ Branch 4 taken 3 times.
|
7 | if (sqlite3_step(sqlStmtPtr) == SQLITE_ROW) { |
| 669 |
2/2✓ Branch 1 taken 4 times.
✓ Branch 4 taken 4 times.
|
4 | std::string version_id = columnText(sqlite3_column_text(sqlStmtPtr, 0)); |
| 670 |
3/3✓ Branch 1 taken 4 times.
✓ Branch 4 taken 4 times.
✓ Branch 7 taken 4 times.
|
8 | result = boost::optional<BCLComponent>(m_libraryPath / uid / version_id); |
| 671 | 4 | } | |
| 672 | // Finalize statement to prevent memory leak | ||
| 673 |
1/1✓ Branch 1 taken 7 times.
|
7 | sqlite3_finalize(sqlStmtPtr); |
| 674 | 7 | } | |
| 675 | } | ||
| 676 | |||
| 677 |
1/1✓ Branch 1 taken 7 times.
|
7 | return result; |
| 678 | 7 | } | |
| 679 | |||
| 680 | ✗ | boost::optional<BCLMeasure> LocalBCL::getMeasure(const std::string& uid, const std::string& versionId) const { | |
| 681 | ✗ | boost::optional<BCLMeasure> result; | |
| 682 | |||
| 683 | ✗ | if (m_db) { | |
| 684 | |||
| 685 | ✗ | if (versionId.empty()) { | |
| 686 | |||
| 687 | ✗ | std::string statement = "SELECT version_id FROM Measures WHERE uid='" + escape(uid) + "'"; | |
| 688 | sqlite3_stmt* sqlStmtPtr; | ||
| 689 | ✗ | int code = sqlite3_prepare_v2(m_db, statement.c_str(), -1, &sqlStmtPtr, nullptr); | |
| 690 | ✗ | if (code != SQLITE_OK) { | |
| 691 | ✗ | LOG(Error, "Unable to prepare version_id Statement: " << statement); | |
| 692 | ✗ | sqlite3_finalize(sqlStmtPtr); // No-op | |
| 693 | ✗ | return boost::none; | |
| 694 | } | ||
| 695 | |||
| 696 | // We seek the most recent modified one in case we find several, so store that | ||
| 697 | ✗ | boost::optional<DateTime> mostRecentModified; | |
| 698 | |||
| 699 | // Loop until done (or failed) | ||
| 700 | ✗ | while ((code != SQLITE_DONE) && (code != SQLITE_BUSY) && (code != SQLITE_ERROR) && (code != SQLITE_MISUSE)) //loop until SQLITE_DONE | |
| 701 | { | ||
| 702 | ✗ | code = sqlite3_step(sqlStmtPtr); | |
| 703 | ✗ | if (code == SQLITE_ROW) { | |
| 704 | // Get values from SELECT | ||
| 705 | ✗ | std::string version_id = columnText(sqlite3_column_text(sqlStmtPtr, 0)); | |
| 706 | |||
| 707 | try { | ||
| 708 | ✗ | BCLMeasure measure(m_libraryPath / uid / version_id); | |
| 709 | |||
| 710 | ✗ | boost::optional<DateTime> versionModified = measure.versionModified(); | |
| 711 | ✗ | if (mostRecentModified) { | |
| 712 | ✗ | if (versionModified && versionModified.get() > mostRecentModified.get()) { | |
| 713 | ✗ | mostRecentModified = versionModified; | |
| 714 | ✗ | result = measure; | |
| 715 | } | ||
| 716 | } else { | ||
| 717 | ✗ | mostRecentModified = versionModified; | |
| 718 | ✗ | result = measure; | |
| 719 | } | ||
| 720 | ✗ | } catch (const std::exception& e) { | |
| 721 | ✗ | LOG(Error, "Cannot find BCL measure at '" << m_libraryPath / uid / version_id << "': " << e.what()); | |
| 722 | ✗ | } | |
| 723 | |||
| 724 | ✗ | } else // i didn't get a row. something is wrong so set the exit condition. | |
| 725 | { // should never get here since i test for all documented return states above | ||
| 726 | ✗ | code = SQLITE_DONE; | |
| 727 | } | ||
| 728 | } // End loop on each match | ||
| 729 | |||
| 730 | // Finalize statement whether it suceeded or not | ||
| 731 | ✗ | sqlite3_finalize(sqlStmtPtr); | |
| 732 | |||
| 733 | ✗ | } else { | |
| 734 | |||
| 735 | ✗ | std::string statement = "SELECT version_id FROM Measures WHERE uid='" + escape(uid) + "' AND version_id='" + escape(versionId) + "'"; | |
| 736 | sqlite3_stmt* sqlStmtPtr; | ||
| 737 | ✗ | if (sqlite3_prepare_v2(m_db, statement.c_str(), -1, &sqlStmtPtr, nullptr) != SQLITE_OK) { | |
| 738 | ✗ | sqlite3_finalize(sqlStmtPtr); // No-op | |
| 739 | ✗ | LOG(Error, "Unable to prepare version_id Statement"); | |
| 740 | } | ||
| 741 | |||
| 742 | ✗ | if (sqlite3_step(sqlStmtPtr) == SQLITE_ROW) { | |
| 743 | ✗ | std::string version_id = columnText(sqlite3_column_text(sqlStmtPtr, 0)); | |
| 744 | try { | ||
| 745 | ✗ | result = boost::optional<BCLMeasure>(m_libraryPath / uid / version_id); | |
| 746 | ✗ | } catch (const std::exception& e) { | |
| 747 | ✗ | LOG(Error, "Cannot find BCL measure at '" << m_libraryPath / uid / version_id << "': " << e.what()); | |
| 748 | ✗ | } | |
| 749 | ✗ | } | |
| 750 | // Finalize statement to prevent memory leak | ||
| 751 | ✗ | sqlite3_finalize(sqlStmtPtr); | |
| 752 | ✗ | } | |
| 753 | } | ||
| 754 | |||
| 755 | ✗ | return result; | |
| 756 | ✗ | } | |
| 757 | |||
| 758 | ✗ | std::vector<BCLComponent> LocalBCL::components() const { | |
| 759 | ✗ | std::vector<BCLComponent> allComponents; | |
| 760 | |||
| 761 | ✗ | if (m_db) { | |
| 762 | ✗ | std::string statement = "SELECT uid, version_id FROM Components"; | |
| 763 | sqlite3_stmt* sqlStmtPtr; | ||
| 764 | ✗ | if (sqlite3_prepare_v2(m_db, statement.c_str(), -1, &sqlStmtPtr, nullptr) != SQLITE_OK) { | |
| 765 | ✗ | LOG(Error, "Unable to prepare components Statement"); | |
| 766 | ✗ | sqlite3_finalize(sqlStmtPtr); // No-op | |
| 767 | ✗ | return allComponents; | |
| 768 | } | ||
| 769 | |||
| 770 | ✗ | int code = sqlite3_prepare_v2(m_db, statement.c_str(), -1, &sqlStmtPtr, nullptr); | |
| 771 | ✗ | if (code != SQLITE_OK) { | |
| 772 | ✗ | LOG(Error, "Unable to prepare components Statement: " << statement); | |
| 773 | ✗ | sqlite3_finalize(sqlStmtPtr); // No-op | |
| 774 | ✗ | return allComponents; | |
| 775 | } | ||
| 776 | |||
| 777 | // Loop until done (or failed) | ||
| 778 | ✗ | while ((code != SQLITE_DONE) && (code != SQLITE_BUSY) && (code != SQLITE_ERROR) && (code != SQLITE_MISUSE)) //loop until SQLITE_DONE | |
| 779 | { | ||
| 780 | ✗ | code = sqlite3_step(sqlStmtPtr); | |
| 781 | ✗ | if (code == SQLITE_ROW) { | |
| 782 | // Get values from SELECT | ||
| 783 | ✗ | std::string uid = columnText(sqlite3_column_text(sqlStmtPtr, 0)); | |
| 784 | ✗ | std::string version_id = columnText(sqlite3_column_text(sqlStmtPtr, 1)); | |
| 785 | |||
| 786 | ✗ | boost::optional<BCLComponent> current(m_libraryPath / uid / version_id); | |
| 787 | ✗ | if (current) { | |
| 788 | ✗ | allComponents.push_back(current.get()); | |
| 789 | } | ||
| 790 | |||
| 791 | ✗ | } else // i didn't get a row. something is wrong so set the exit condition. | |
| 792 | { // should never get here since i test for all documented return states above | ||
| 793 | ✗ | code = SQLITE_DONE; | |
| 794 | } | ||
| 795 | } // End loop on each match | ||
| 796 | |||
| 797 | // Finalize statement to prevent memory leak | ||
| 798 | ✗ | sqlite3_finalize(sqlStmtPtr); | |
| 799 | ✗ | } | |
| 800 | ✗ | return allComponents; | |
| 801 | ✗ | } | |
| 802 | |||
| 803 | ✗ | std::vector<BCLMeasure> LocalBCL::measures() const { | |
| 804 | ✗ | std::vector<BCLMeasure> allMeasures; | |
| 805 | |||
| 806 | ✗ | if (m_db) { | |
| 807 | ✗ | std::string statement = "SELECT uid, version_id FROM Measures"; | |
| 808 | sqlite3_stmt* sqlStmtPtr; | ||
| 809 | ✗ | if (sqlite3_prepare_v2(m_db, statement.c_str(), -1, &sqlStmtPtr, nullptr) != SQLITE_OK) { | |
| 810 | ✗ | LOG(Error, "Unable to prepare measures Statement"); | |
| 811 | ✗ | sqlite3_finalize(sqlStmtPtr); // No-op | |
| 812 | ✗ | return allMeasures; | |
| 813 | } | ||
| 814 | |||
| 815 | ✗ | int code = sqlite3_prepare_v2(m_db, statement.c_str(), -1, &sqlStmtPtr, nullptr); | |
| 816 | ✗ | if (code != SQLITE_OK) { | |
| 817 | ✗ | LOG(Error, "Unable to prepare measures Statement: " << statement); | |
| 818 | ✗ | sqlite3_finalize(sqlStmtPtr); // No-op | |
| 819 | ✗ | return allMeasures; | |
| 820 | } | ||
| 821 | |||
| 822 | // Loop until done (or failed) | ||
| 823 | ✗ | while ((code != SQLITE_DONE) && (code != SQLITE_BUSY) && (code != SQLITE_ERROR) && (code != SQLITE_MISUSE)) //loop until SQLITE_DONE | |
| 824 | { | ||
| 825 | ✗ | code = sqlite3_step(sqlStmtPtr); | |
| 826 | ✗ | if (code == SQLITE_ROW) { | |
| 827 | // Get values from SELECT | ||
| 828 | ✗ | std::string uid = columnText(sqlite3_column_text(sqlStmtPtr, 0)); | |
| 829 | ✗ | std::string version_id = columnText(sqlite3_column_text(sqlStmtPtr, 1)); | |
| 830 | |||
| 831 | ✗ | boost::optional<BCLMeasure> current = BCLMeasure::load(m_libraryPath / uid / version_id); | |
| 832 | ✗ | if (current) { | |
| 833 | ✗ | allMeasures.push_back(current.get()); | |
| 834 | } | ||
| 835 | |||
| 836 | ✗ | } else // i didn't get a row. something is wrong so set the exit condition. | |
| 837 | { // should never get here since i test for all documented return states above | ||
| 838 | ✗ | code = SQLITE_DONE; | |
| 839 | } | ||
| 840 | } // End loop on each match | ||
| 841 | |||
| 842 | // Finalize statement to prevent memory leak | ||
| 843 | ✗ | sqlite3_finalize(sqlStmtPtr); | |
| 844 | ✗ | } | |
| 845 | |||
| 846 | ✗ | return allMeasures; | |
| 847 | ✗ | } | |
| 848 | |||
| 849 | ✗ | std::vector<std::string> LocalBCL::measureUids() const { | |
| 850 | ✗ | std::vector<std::string> uids; | |
| 851 | |||
| 852 | ✗ | if (m_db) { | |
| 853 | ✗ | std::string statement = "SELECT DISTINCT uid FROM Measures"; | |
| 854 | sqlite3_stmt* sqlStmtPtr; | ||
| 855 | ✗ | if (sqlite3_prepare_v2(m_db, statement.c_str(), -1, &sqlStmtPtr, nullptr) != SQLITE_OK) { | |
| 856 | ✗ | LOG(Error, "Unable to prepare measureUids Statement"); | |
| 857 | ✗ | sqlite3_finalize(sqlStmtPtr); // No-op | |
| 858 | ✗ | return uids; | |
| 859 | } | ||
| 860 | |||
| 861 | ✗ | int code = sqlite3_prepare_v2(m_db, statement.c_str(), -1, &sqlStmtPtr, nullptr); | |
| 862 | ✗ | if (code != SQLITE_OK) { | |
| 863 | ✗ | LOG(Error, "Unable to prepare measureUids Statement: " << statement); | |
| 864 | ✗ | sqlite3_finalize(sqlStmtPtr); // No-op | |
| 865 | ✗ | return uids; | |
| 866 | } | ||
| 867 | |||
| 868 | // Loop until done (or failed) | ||
| 869 | ✗ | while ((code != SQLITE_DONE) && (code != SQLITE_BUSY) && (code != SQLITE_ERROR) && (code != SQLITE_MISUSE)) //loop until SQLITE_DONE | |
| 870 | { | ||
| 871 | ✗ | code = sqlite3_step(sqlStmtPtr); | |
| 872 | ✗ | if (code == SQLITE_ROW) { | |
| 873 | // Get values from SELECT | ||
| 874 | ✗ | uids.push_back(columnText(sqlite3_column_text(sqlStmtPtr, 0))); | |
| 875 | } else // i didn't get a row. something is wrong so set the exit condition. | ||
| 876 | { // should never get here since i test for all documented return states above | ||
| 877 | ✗ | code = SQLITE_DONE; | |
| 878 | } | ||
| 879 | } // End loop on each match | ||
| 880 | |||
| 881 | // Finalize statement to prevent memory leak | ||
| 882 | ✗ | sqlite3_finalize(sqlStmtPtr); | |
| 883 | ✗ | } | |
| 884 | |||
| 885 | ✗ | return uids; | |
| 886 | ✗ | } | |
| 887 | |||
| 888 | ✗ | std::vector<BCLComponent> LocalBCL::searchComponents(const std::string& searchTerm, const std::string& /*componentType*/) const { | |
| 889 | ✗ | std::vector<BCLComponent> results; | |
| 890 | |||
| 891 | ✗ | if (m_db) { | |
| 892 | std::string statement("SELECT uid, version_id FROM Components where name" | ||
| 893 | " LIKE \"%" | ||
| 894 | ✗ | + searchTerm + "%\" OR description LIKE \"%" + searchTerm + "%\""); | |
| 895 | sqlite3_stmt* sqlStmtPtr; | ||
| 896 | ✗ | if (sqlite3_prepare_v2(m_db, statement.c_str(), -1, &sqlStmtPtr, nullptr) != SQLITE_OK) { | |
| 897 | ✗ | LOG(Error, "Unable to prepare searchComponents Statement"); | |
| 898 | ✗ | sqlite3_finalize(sqlStmtPtr); // No-op | |
| 899 | ✗ | return results; | |
| 900 | } | ||
| 901 | |||
| 902 | ✗ | int code = sqlite3_prepare_v2(m_db, statement.c_str(), -1, &sqlStmtPtr, nullptr); | |
| 903 | ✗ | if (code != SQLITE_OK) { | |
| 904 | ✗ | LOG(Error, "Unable to prepare searchComponents Statement: " << statement); | |
| 905 | ✗ | sqlite3_finalize(sqlStmtPtr); // No-op | |
| 906 | ✗ | return results; | |
| 907 | } | ||
| 908 | |||
| 909 | // Loop until done (or failed) | ||
| 910 | ✗ | while ((code != SQLITE_DONE) && (code != SQLITE_BUSY) && (code != SQLITE_ERROR) && (code != SQLITE_MISUSE)) //loop until SQLITE_DONE | |
| 911 | { | ||
| 912 | ✗ | code = sqlite3_step(sqlStmtPtr); | |
| 913 | ✗ | if (code == SQLITE_ROW) { | |
| 914 | // Get values from SELECT | ||
| 915 | ✗ | std::string uid = columnText(sqlite3_column_text(sqlStmtPtr, 0)); | |
| 916 | ✗ | std::string version_id = columnText(sqlite3_column_text(sqlStmtPtr, 1)); | |
| 917 | |||
| 918 | ✗ | boost::optional<BCLComponent> current(m_libraryPath / uid / version_id); | |
| 919 | ✗ | if (current) { | |
| 920 | ✗ | results.push_back(current.get()); | |
| 921 | } | ||
| 922 | ✗ | } else // i didn't get a row. something is wrong so set the exit condition. | |
| 923 | { // should never get here since i test for all documented return states above | ||
| 924 | ✗ | code = SQLITE_DONE; | |
| 925 | } | ||
| 926 | } // End loop on each match | ||
| 927 | |||
| 928 | // Finalize statement to prevent memory leak | ||
| 929 | ✗ | sqlite3_finalize(sqlStmtPtr); | |
| 930 | ✗ | } | |
| 931 | |||
| 932 | ✗ | return results; | |
| 933 | ✗ | } | |
| 934 | |||
| 935 | ✗ | std::vector<BCLComponent> LocalBCL::searchComponents(const std::string& searchTerm, const unsigned /*componentTypeTID*/) const { | |
| 936 | ✗ | return searchComponents(searchTerm, ""); | |
| 937 | } | ||
| 938 | |||
| 939 | ✗ | std::vector<BCLMeasure> LocalBCL::searchMeasures(const std::string& searchTerm, const std::string& /*componentType*/) const { | |
| 940 | |||
| 941 | ✗ | std::vector<BCLMeasure> results; | |
| 942 | |||
| 943 | ✗ | if (m_db) { | |
| 944 | ✗ | std::string statement("SELECT uid, version_id FROM Measures where name LIKE \"%" + searchTerm | |
| 945 | ✗ | + "%\"" | |
| 946 | " OR description LIKE \"%" | ||
| 947 | ✗ | + searchTerm + "%\" OR modeler_description LIKE \"%" + searchTerm + "%\""); | |
| 948 | sqlite3_stmt* sqlStmtPtr; | ||
| 949 | ✗ | if (sqlite3_prepare_v2(m_db, statement.c_str(), -1, &sqlStmtPtr, nullptr) != SQLITE_OK) { | |
| 950 | ✗ | LOG(Error, "Unable to prepare searchMeasures Statement"); | |
| 951 | ✗ | sqlite3_finalize(sqlStmtPtr); // No-op | |
| 952 | ✗ | return results; | |
| 953 | } | ||
| 954 | |||
| 955 | ✗ | int code = sqlite3_prepare_v2(m_db, statement.c_str(), -1, &sqlStmtPtr, nullptr); | |
| 956 | ✗ | if (code != SQLITE_OK) { | |
| 957 | ✗ | LOG(Error, "Unable to prepare searchMeasures Statement: " << statement); | |
| 958 | ✗ | sqlite3_finalize(sqlStmtPtr); // No-op | |
| 959 | ✗ | return results; | |
| 960 | } | ||
| 961 | |||
| 962 | // Loop until done (or failed) | ||
| 963 | ✗ | while ((code != SQLITE_DONE) && (code != SQLITE_BUSY) && (code != SQLITE_ERROR) && (code != SQLITE_MISUSE)) //loop until SQLITE_DONE | |
| 964 | { | ||
| 965 | ✗ | code = sqlite3_step(sqlStmtPtr); | |
| 966 | ✗ | if (code == SQLITE_ROW) { | |
| 967 | // Get values from SELECT | ||
| 968 | ✗ | std::string uid = columnText(sqlite3_column_text(sqlStmtPtr, 0)); | |
| 969 | ✗ | std::string version_id = columnText(sqlite3_column_text(sqlStmtPtr, 1)); | |
| 970 | |||
| 971 | ✗ | boost::optional<BCLMeasure> current = BCLMeasure::load(m_libraryPath / uid / version_id); | |
| 972 | ✗ | if (current) { | |
| 973 | ✗ | results.push_back(current.get()); | |
| 974 | } | ||
| 975 | ✗ | } else // i didn't get a row. something is wrong so set the exit condition. | |
| 976 | { // should never get here since i test for all documented return states above | ||
| 977 | ✗ | code = SQLITE_DONE; | |
| 978 | } | ||
| 979 | } // End loop on each match | ||
| 980 | |||
| 981 | // Finalize statement to prevent memory leak | ||
| 982 | ✗ | sqlite3_finalize(sqlStmtPtr); | |
| 983 | ✗ | } | |
| 984 | |||
| 985 | ✗ | return results; | |
| 986 | ✗ | } | |
| 987 | |||
| 988 | ✗ | std::vector<BCLMeasure> LocalBCL::searchMeasures(const std::string& searchTerm, const unsigned /*componentTypeTID*/) const { | |
| 989 | ✗ | return searchMeasures(searchTerm, ""); | |
| 990 | } | ||
| 991 | |||
| 992 | /// Class members | ||
| 993 | |||
| 994 | // cppcheck-suppress constParameterReference | ||
| 995 | 3 | bool LocalBCL::addComponent(BCLComponent& component) { | |
| 996 | //Check for uid | ||
| 997 |
8/18✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 3 times.
✓ Branch 6 taken 3 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 3 times.
✓ Branch 12 taken 3 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 3 times.
✗ Branch 15 not taken.
✓ Branch 17 taken 3 times.
✗ Branch 18 not taken.
✓ Branch 20 taken 3 times.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
✗ Branch 25 not taken.
✗ Branch 26 not taken.
|
3 | if ((m_db != nullptr) && !component.uid().empty() && !component.versionId().empty()) { |
| 998 | |||
| 999 | // Start a transaction, so we can handle failures without messing up the database | ||
| 1000 |
2/3✓ Branch 1 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
|
3 | if (!beginTransaction()) { |
| 1001 | ✗ | return false; | |
| 1002 | } | ||
| 1003 | |||
| 1004 |
1/1✓ Branch 1 taken 3 times.
|
3 | std::string uid = component.uid(); |
| 1005 |
1/1✓ Branch 1 taken 3 times.
|
3 | std::string versionId = component.versionId(); |
| 1006 | |||
| 1007 |
6/6✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
✓ Branch 7 taken 3 times.
✓ Branch 10 taken 3 times.
✓ Branch 13 taken 3 times.
✓ Branch 16 taken 3 times.
|
3 | std::string statement = "DELETE FROM Components WHERE uid='" + escape(uid) + "' AND version_id='" + escape(versionId) + "'"; |
| 1008 |
2/3✓ Branch 2 taken 3 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 3 times.
|
3 | if (sqlite3_exec(m_db, statement.c_str(), nullptr, nullptr, nullptr) != SQLITE_OK) { |
| 1009 | // Rollback changes | ||
| 1010 | ✗ | LOG(Error, "addComponent: statement failed, rolling back: " << statement); | |
| 1011 | ✗ | rollbackTransaction(); | |
| 1012 | ✗ | return false; | |
| 1013 | } | ||
| 1014 | |||
| 1015 | { | ||
| 1016 |
1/1✓ Branch 1 taken 3 times.
|
3 | std::stringstream ss; |
| 1017 | ✗ | ss << "INSERT INTO Components (uid, version_id, name, description, date_added, date_modified) " << "VALUES('" << escape(uid) << "', '" | |
| 1018 |
2/2✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
|
9 | << escape(versionId) << "', '" << escape(component.name()) << "', '" << escape(component.description()) |
| 1019 |
14/14✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
✓ Branch 7 taken 3 times.
✓ Branch 10 taken 3 times.
✓ Branch 13 taken 3 times.
✓ Branch 16 taken 3 times.
✓ Branch 19 taken 3 times.
✓ Branch 22 taken 3 times.
✓ Branch 25 taken 3 times.
✓ Branch 28 taken 3 times.
✓ Branch 31 taken 3 times.
✓ Branch 34 taken 3 times.
✓ Branch 37 taken 3 times.
✓ Branch 40 taken 3 times.
|
9 | << "', datetime('now','localtime'), datetime('now','localtime'));"; |
| 1020 | |||
| 1021 |
1/1✓ Branch 1 taken 3 times.
|
3 | statement = ss.str(); |
| 1022 | |||
| 1023 |
2/3✓ Branch 2 taken 3 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 3 times.
|
3 | if (sqlite3_exec(m_db, statement.c_str(), nullptr, nullptr, nullptr) != SQLITE_OK) { |
| 1024 | // Rollback changes | ||
| 1025 | ✗ | LOG(Error, "addComponent: statement failed, rolling back: " << statement); | |
| 1026 | ✗ | rollbackTransaction(); | |
| 1027 | ✗ | return false; | |
| 1028 | } | ||
| 1029 | 3 | } | |
| 1030 | |||
| 1031 | // Insert files | ||
| 1032 |
6/6✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
✓ Branch 7 taken 3 times.
✓ Branch 10 taken 3 times.
✓ Branch 13 taken 3 times.
✓ Branch 16 taken 3 times.
|
3 | statement = "DELETE FROM Files WHERE uid='" + escape(uid) + "' AND version_id='" + escape(versionId) + "'"; |
| 1033 |
2/3✓ Branch 2 taken 3 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 3 times.
|
3 | if (sqlite3_exec(m_db, statement.c_str(), nullptr, nullptr, nullptr) != SQLITE_OK) { |
| 1034 | // Rollback changes | ||
| 1035 | ✗ | LOG(Error, "addComponent: statement failed, rolling back: " << statement); | |
| 1036 | ✗ | rollbackTransaction(); | |
| 1037 | ✗ | return false; | |
| 1038 | } | ||
| 1039 | |||
| 1040 |
3/3✓ Branch 1 taken 3 times.
✓ Branch 5 taken 2 times.
✓ Branch 6 taken 1 time.
|
3 | if (!component.files().empty()) { |
| 1041 |
3/3✓ Branch 1 taken 8 times.
✓ Branch 5 taken 6 times.
✓ Branch 6 taken 2 times.
|
8 | for (int i = 0; i < (int)component.files().size(); i++) { |
| 1042 | |||
| 1043 |
1/1✓ Branch 1 taken 6 times.
|
6 | std::stringstream ss; |
| 1044 | 6 | ss << "INSERT INTO Files (uid, version_id, filename, filetype) " << "VALUES('" << escape(uid) << "', '" << escape(versionId) << "', '" | |
| 1045 |
16/16✓ Branch 1 taken 6 times.
✓ Branch 4 taken 6 times.
✓ Branch 7 taken 6 times.
✓ Branch 10 taken 6 times.
✓ Branch 13 taken 6 times.
✓ Branch 16 taken 6 times.
✓ Branch 19 taken 6 times.
✓ Branch 22 taken 6 times.
✓ Branch 25 taken 6 times.
✓ Branch 29 taken 6 times.
✓ Branch 32 taken 6 times.
✓ Branch 35 taken 6 times.
✓ Branch 38 taken 6 times.
✓ Branch 42 taken 6 times.
✓ Branch 45 taken 6 times.
✓ Branch 48 taken 6 times.
|
6 | << escape(component.files()[i]) << "', '" << escape(component.filetypes()[i]) << "');"; |
| 1046 | |||
| 1047 |
1/1✓ Branch 1 taken 6 times.
|
6 | statement = ss.str(); |
| 1048 | |||
| 1049 |
2/3✓ Branch 2 taken 6 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 6 times.
|
6 | if (sqlite3_exec(m_db, statement.c_str(), nullptr, nullptr, nullptr) != SQLITE_OK) { |
| 1050 | // Rollback changes | ||
| 1051 | ✗ | LOG(Error, "addComponent: statement failed, rolling back: " << statement); | |
| 1052 | ✗ | rollbackTransaction(); | |
| 1053 | ✗ | return false; | |
| 1054 | } | ||
| 1055 | 6 | } | |
| 1056 | } // End insert all component's files | ||
| 1057 | |||
| 1058 | // Insert Attributes | ||
| 1059 |
6/6✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
✓ Branch 7 taken 3 times.
✓ Branch 10 taken 3 times.
✓ Branch 13 taken 3 times.
✓ Branch 16 taken 3 times.
|
3 | statement = "DELETE FROM Attributes WHERE uid='" + escape(uid) + "' AND version_id='" + escape(versionId) + "'"; |
| 1060 |
2/3✓ Branch 2 taken 3 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 3 times.
|
3 | if (sqlite3_exec(m_db, statement.c_str(), nullptr, nullptr, nullptr) != SQLITE_OK) { |
| 1061 | // Rollback changes | ||
| 1062 | ✗ | LOG(Error, "addComponent: statement failed, rolling back: " << statement); | |
| 1063 | ✗ | rollbackTransaction(); | |
| 1064 | ✗ | return false; | |
| 1065 | } | ||
| 1066 | |||
| 1067 |
3/3✓ Branch 1 taken 3 times.
✓ Branch 8 taken 22 times.
✓ Branch 9 taken 3 times.
|
25 | for (const Attribute& attribute : component.attributes()) { |
| 1068 | 22 | std::string dataValue; | |
| 1069 | 22 | std::string dataType; | |
| 1070 |
4/4✓ Branch 1 taken 22 times.
✓ Branch 4 taken 22 times.
✓ Branch 6 taken 1 time.
✓ Branch 7 taken 21 times.
|
22 | if (attribute.valueType().value() == AttributeValueType::Boolean) { |
| 1071 |
2/2✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
|
1 | dataValue = boost::lexical_cast<std::string>(attribute.valueAsBoolean()); |
| 1072 |
1/1✓ Branch 1 taken 1 time.
|
1 | dataType = "boolean"; |
| 1073 |
3/4✓ Branch 1 taken 21 times.
✓ Branch 4 taken 21 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 21 times.
|
21 | } else if (attribute.valueType().value() == AttributeValueType::Integer) { |
| 1074 | ✗ | dataValue = boost::lexical_cast<std::string>(attribute.valueAsInteger()); | |
| 1075 | ✗ | dataType = "int"; | |
| 1076 |
4/4✓ Branch 1 taken 21 times.
✓ Branch 4 taken 21 times.
✓ Branch 6 taken 12 times.
✓ Branch 7 taken 9 times.
|
21 | } else if (attribute.valueType().value() == AttributeValueType::Double) { |
| 1077 |
2/2✓ Branch 1 taken 12 times.
✓ Branch 4 taken 12 times.
|
12 | dataValue = formatString(attribute.valueAsDouble()); |
| 1078 |
1/1✓ Branch 1 taken 12 times.
|
12 | dataType = "float"; |
| 1079 | } else { | ||
| 1080 |
1/1✓ Branch 1 taken 9 times.
|
9 | dataValue = attribute.valueAsString(); |
| 1081 |
1/1✓ Branch 1 taken 9 times.
|
9 | dataType = "string"; |
| 1082 | } | ||
| 1083 | |||
| 1084 |
1/1✓ Branch 1 taken 22 times.
|
22 | std::stringstream ss; |
| 1085 | 22 | ss << "INSERT INTO Attributes (uid, version_id, name, value, units, type) " << "VALUES('" << escape(uid) << "', '" << escape(versionId) | |
| 1086 |
6/16✓ Branch 1 taken 22 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 22 times.
✗ Branch 6 not taken.
✗ Branch 9 not taken.
✗ Branch 12 not taken.
✓ Branch 15 taken 22 times.
✓ Branch 18 taken 22 times.
✓ Branch 21 taken 22 times.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
✓ Branch 24 taken 22 times.
✗ Branch 33 not taken.
✗ Branch 34 not taken.
✗ Branch 35 not taken.
✗ Branch 36 not taken.
|
110 | << "', '" << escape(attribute.name()) << "', '" << escape(dataValue) << "', '" << escape(attribute.units() ? attribute.units().get() : "") |
| 1087 |
20/20✓ Branch 1 taken 22 times.
✓ Branch 4 taken 22 times.
✓ Branch 7 taken 22 times.
✓ Branch 10 taken 22 times.
✓ Branch 13 taken 22 times.
✓ Branch 16 taken 22 times.
✓ Branch 19 taken 22 times.
✓ Branch 22 taken 22 times.
✓ Branch 25 taken 22 times.
✓ Branch 28 taken 22 times.
✓ Branch 31 taken 22 times.
✓ Branch 34 taken 22 times.
✓ Branch 37 taken 22 times.
✓ Branch 40 taken 22 times.
✓ Branch 43 taken 22 times.
✓ Branch 46 taken 22 times.
✓ Branch 49 taken 22 times.
✓ Branch 52 taken 22 times.
✓ Branch 55 taken 22 times.
✓ Branch 58 taken 22 times.
|
66 | << "', '" << escape(dataType) << "');"; |
| 1088 | |||
| 1089 |
1/1✓ Branch 1 taken 22 times.
|
22 | statement = ss.str(); |
| 1090 | |||
| 1091 |
2/3✓ Branch 2 taken 22 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 22 times.
|
22 | if (sqlite3_exec(m_db, statement.c_str(), nullptr, nullptr, nullptr) != SQLITE_OK) { |
| 1092 | // Rollback changes | ||
| 1093 | ✗ | LOG(Error, "addComponent: statement failed, rolling back: " << statement); | |
| 1094 | ✗ | rollbackTransaction(); | |
| 1095 | ✗ | return false; | |
| 1096 | } | ||
| 1097 | 25 | } // End insert each attribute | |
| 1098 | |||
| 1099 | // Commit changes now that everything went well | ||
| 1100 |
1/1✓ Branch 1 taken 3 times.
|
3 | return commitTransaction(); |
| 1101 | |||
| 1102 | 3 | } // End check uid | |
| 1103 | |||
| 1104 | ✗ | return false; | |
| 1105 | } | ||
| 1106 | |||
| 1107 | // cppcheck-suppress constParameterReference | ||
| 1108 | 1 | bool LocalBCL::removeComponent(BCLComponent& component) { | |
| 1109 | // if uid is empty or not found in database return false | ||
| 1110 |
8/18✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 3 taken 1 time.
✓ Branch 6 taken 1 time.
✗ Branch 7 not taken.
✓ Branch 9 taken 1 time.
✗ Branch 12 not taken.
✓ Branch 13 taken 1 time.
✓ Branch 14 taken 1 time.
✗ Branch 15 not taken.
✓ Branch 17 taken 1 time.
✗ Branch 18 not taken.
✗ Branch 20 not taken.
✓ Branch 21 taken 1 time.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
✗ Branch 25 not taken.
✗ Branch 26 not taken.
|
1 | if ((m_db == nullptr) || component.uid().empty() || component.versionId().empty()) { |
| 1111 | ✗ | return false; | |
| 1112 | } | ||
| 1113 | |||
| 1114 | // Start by deleting stuff from the SQL file | ||
| 1115 | // We don't expect any of these to fail really, if it isn't present, then it still returns OK | ||
| 1116 | // But let's be prudent and still start a transaction | ||
| 1117 | |||
| 1118 | // Start a transaction, so we can handle failures without messing up the database | ||
| 1119 |
2/3✓ Branch 1 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 time.
|
1 | if (!beginTransaction()) { |
| 1120 | ✗ | return false; | |
| 1121 | } | ||
| 1122 | |||
| 1123 |
1/1✓ Branch 1 taken 1 time.
|
1 | std::string uid = component.uid(); |
| 1124 |
1/1✓ Branch 1 taken 1 time.
|
1 | std::string versionId = component.versionId(); |
| 1125 | |||
| 1126 |
5/5✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
✓ Branch 7 taken 1 time.
✓ Branch 10 taken 1 time.
✓ Branch 13 taken 1 time.
|
2 | std::string statement("DELETE FROM Components WHERE uid='" + escape(uid) + "' AND version_id='" + escape(versionId) |
| 1127 |
1/1✓ Branch 1 taken 1 time.
|
2 | + "';" |
| 1128 | "DELETE FROM Files WHERE uid='" | ||
| 1129 |
5/5✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
✓ Branch 7 taken 1 time.
✓ Branch 10 taken 1 time.
✓ Branch 13 taken 1 time.
|
4 | + escape(uid) + "' AND version_id='" + escape(versionId) |
| 1130 |
1/1✓ Branch 1 taken 1 time.
|
2 | + "';" |
| 1131 | "DELETE FROM Attributes WHERE uid='" | ||
| 1132 |
6/6✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
✓ Branch 7 taken 1 time.
✓ Branch 10 taken 1 time.
✓ Branch 13 taken 1 time.
✓ Branch 16 taken 1 time.
|
3 | + escape(uid) + "' AND version_id='" + escape(versionId) + "';"); |
| 1133 |
2/3✓ Branch 2 taken 1 time.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 time.
|
1 | if (sqlite3_exec(m_db, statement.c_str(), nullptr, nullptr, nullptr) != SQLITE_OK) { |
| 1134 | ✗ | LOG(Error, "Couldn't delete component from SQL file: " << statement); | |
| 1135 | // Rollback changes | ||
| 1136 | ✗ | rollbackTransaction(); | |
| 1137 | ✗ | return false; | |
| 1138 | } | ||
| 1139 | |||
| 1140 | // Commit changes now that everything went well | ||
| 1141 |
2/3✓ Branch 1 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 time.
|
1 | if (!commitTransaction()) { |
| 1142 | ✗ | LOG(Error, "Transaction commit failed, will not remove Component from disk."); | |
| 1143 | ✗ | return false; | |
| 1144 | } | ||
| 1145 | |||
| 1146 | // proceed deleting component on disk | ||
| 1147 |
1/1✓ Branch 1 taken 1 time.
|
2 | openstudio::path pathToRemove = m_libraryPath / uid / versionId; |
| 1148 | |||
| 1149 | // We look at the parent path, and if we find only one folder = (one versionId), we delete the parent instead | ||
| 1150 | // so we don't end up with an empty folder | ||
| 1151 | 1 | unsigned int n_dirs = 0; | |
| 1152 |
5/5✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
✓ Branch 9 taken 2 times.
✓ Branch 12 taken 1 time.
✓ Branch 13 taken 1 time.
|
2 | for (auto dir_itr = openstudio::filesystem::directory_iterator(pathToRemove.parent_path()); dir_itr != openstudio::filesystem::directory_iterator(); |
| 1153 |
1/1✓ Branch 1 taken 1 time.
|
1 | ++dir_itr) { |
| 1154 |
2/2✓ Branch 1 taken 1 time.
✓ Branch 5 taken 1 time.
|
1 | const auto p = dir_itr->path(); |
| 1155 |
5/8✓ Branch 1 taken 1 time.
✓ Branch 3 taken 1 time.
✗ Branch 4 not taken.
✓ Branch 6 taken 1 time.
✓ Branch 8 taken 1 time.
✗ Branch 9 not taken.
✓ Branch 10 taken 1 time.
✗ Branch 11 not taken.
|
1 | if (openstudio::filesystem::is_directory(p) && openstudio::filesystem::exists(p)) { |
| 1156 | 1 | ++n_dirs; | |
| 1157 | } | ||
| 1158 | 2 | } | |
| 1159 | |||
| 1160 | // Lambda version, doesn't really help clarity here | ||
| 1161 | /* | ||
| 1162 | *int n_dirs = std::count_if(openstudio::filesystem::directory_iterator(pathToRemove.parent_path()), | ||
| 1163 | * openstudio::filesystem::directory_iterator(), | ||
| 1164 | * [](const openstudio::filesystem::directory_entry& d) { | ||
| 1165 | * const auto p = d.path(); | ||
| 1166 | * return openstudio::filesystem::is_directory(p) && openstudio::filesystem::exists(p); | ||
| 1167 | * } | ||
| 1168 | * ); | ||
| 1169 | */ | ||
| 1170 | |||
| 1171 |
1/2✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 | if (n_dirs == 1) { |
| 1172 |
1/1✓ Branch 1 taken 1 time.
|
1 | pathToRemove = pathToRemove.parent_path(); |
| 1173 | } | ||
| 1174 |
1/1✓ Branch 1 taken 1 time.
|
1 | removeDirectory(pathToRemove); |
| 1175 | |||
| 1176 | 1 | return true; | |
| 1177 | 1 | } | |
| 1178 | |||
| 1179 | // cppcheck-suppress constParameterReference | ||
| 1180 | ✗ | bool LocalBCL::addMeasure(BCLMeasure& measure) { | |
| 1181 | |||
| 1182 | // if uid is empty or not found in database return false | ||
| 1183 | ✗ | if ((m_db == nullptr) || measure.uid().empty() || measure.versionId().empty()) { | |
| 1184 | ✗ | return false; | |
| 1185 | } | ||
| 1186 | |||
| 1187 | // Start a transaction, so we can handle failures without messing up the database | ||
| 1188 | ✗ | if (!beginTransaction()) { | |
| 1189 | ✗ | return false; | |
| 1190 | } | ||
| 1191 | |||
| 1192 | ✗ | std::string uid = measure.uid(); | |
| 1193 | ✗ | std::string versionId = measure.versionId(); | |
| 1194 | |||
| 1195 | ✗ | std::string statement = "DELETE FROM Measures WHERE uid='" + escape(uid) + "' AND version_id='" + escape(versionId) + "'"; | |
| 1196 | ✗ | if (sqlite3_exec(m_db, statement.c_str(), nullptr, nullptr, nullptr) != SQLITE_OK) { | |
| 1197 | // Rollback changes | ||
| 1198 | ✗ | LOG(Error, "addMeasure: statement failed, rolling back: " << statement); | |
| 1199 | ✗ | rollbackTransaction(); | |
| 1200 | ✗ | return false; | |
| 1201 | } | ||
| 1202 | |||
| 1203 | { | ||
| 1204 | ✗ | std::stringstream ss; | |
| 1205 | ✗ | ss << "INSERT INTO Measures (uid, version_id, name, description, modeler_description, date_added, date_modified) " << "VALUES('" << escape(uid) | |
| 1206 | ✗ | << "', '" << escape(versionId) << "', '" << escape(measure.name()) << "', '" << escape(measure.description()) << "', '" | |
| 1207 | ✗ | << escape(measure.description()) << "'" << ", datetime('now','localtime'), datetime('now','localtime'));"; | |
| 1208 | |||
| 1209 | ✗ | statement = ss.str(); | |
| 1210 | |||
| 1211 | ✗ | if (sqlite3_exec(m_db, statement.c_str(), nullptr, nullptr, nullptr) != SQLITE_OK) { | |
| 1212 | // Rollback changes | ||
| 1213 | ✗ | LOG(Error, "addMeasure: statement failed, rolling back: " << statement); | |
| 1214 | ✗ | rollbackTransaction(); | |
| 1215 | ✗ | return false; | |
| 1216 | } | ||
| 1217 | ✗ | } | |
| 1218 | // Insert files | ||
| 1219 | ✗ | statement = "DELETE FROM Files WHERE uid='" + escape(uid) + "' AND version_id='" + escape(versionId) + "'"; | |
| 1220 | ✗ | if (sqlite3_exec(m_db, statement.c_str(), nullptr, nullptr, nullptr) != SQLITE_OK) { | |
| 1221 | // Rollback changes | ||
| 1222 | ✗ | LOG(Error, "addMeasure: statement failed, rolling back: " << statement); | |
| 1223 | ✗ | rollbackTransaction(); | |
| 1224 | ✗ | return false; | |
| 1225 | } | ||
| 1226 | |||
| 1227 | ✗ | for (const BCLFileReference& file : measure.files()) { | |
| 1228 | |||
| 1229 | ✗ | std::stringstream ss; | |
| 1230 | ✗ | ss << "INSERT INTO Files (uid, version_id, filename, filetype, usage_type, checksum) " << "VALUES('" << escape(uid) << "', '" << escape(versionId) | |
| 1231 | ✗ | << "', '" << escape(file.fileName()) << "', '" << escape(file.fileType()) << "', '" << escape(file.usageType()) << "', '" | |
| 1232 | ✗ | << escape(file.checksum()) << "');"; | |
| 1233 | |||
| 1234 | ✗ | statement = ss.str(); | |
| 1235 | |||
| 1236 | ✗ | if (sqlite3_exec(m_db, statement.c_str(), nullptr, nullptr, nullptr) != SQLITE_OK) { | |
| 1237 | // Rollback changes | ||
| 1238 | ✗ | LOG(Error, "addMeasure: statement failed, rolling back: " << statement); | |
| 1239 | ✗ | rollbackTransaction(); | |
| 1240 | ✗ | return false; | |
| 1241 | } | ||
| 1242 | ✗ | } // End insert all measure's files | |
| 1243 | |||
| 1244 | // Insert Attributes | ||
| 1245 | ✗ | statement = "DELETE FROM Attributes WHERE uid='" + escape(uid) + "' AND version_id='" + escape(versionId) + "'"; | |
| 1246 | ✗ | if (sqlite3_exec(m_db, statement.c_str(), nullptr, nullptr, nullptr) != SQLITE_OK) { | |
| 1247 | // Rollback changes | ||
| 1248 | ✗ | LOG(Error, "addMeasure: statement failed, rolling back: " << statement); | |
| 1249 | ✗ | rollbackTransaction(); | |
| 1250 | ✗ | return false; | |
| 1251 | } | ||
| 1252 | |||
| 1253 | ✗ | for (const Attribute& attribute : measure.attributes()) { | |
| 1254 | ✗ | std::string dataValue; | |
| 1255 | ✗ | std::string dataType; | |
| 1256 | ✗ | if (attribute.valueType().value() == AttributeValueType::Boolean) { | |
| 1257 | ✗ | dataValue = boost::lexical_cast<std::string>(attribute.valueAsBoolean()); | |
| 1258 | ✗ | dataType = "boolean"; | |
| 1259 | ✗ | } else if (attribute.valueType().value() == AttributeValueType::Integer) { | |
| 1260 | ✗ | dataValue = boost::lexical_cast<std::string>(attribute.valueAsInteger()); | |
| 1261 | ✗ | dataType = "int"; | |
| 1262 | ✗ | } else if (attribute.valueType().value() == AttributeValueType::Double) { | |
| 1263 | ✗ | dataValue = formatString(attribute.valueAsDouble()); | |
| 1264 | ✗ | dataType = "float"; | |
| 1265 | } else { | ||
| 1266 | ✗ | dataValue = attribute.valueAsString(); | |
| 1267 | ✗ | dataType = "string"; | |
| 1268 | } | ||
| 1269 | |||
| 1270 | ✗ | std::stringstream ss; | |
| 1271 | ✗ | ss << "INSERT INTO Attributes (uid, version_id, name, value, units, type) " << "VALUES('" << escape(uid) << "', '" << escape(versionId) << "', '" | |
| 1272 | ✗ | << escape(attribute.name()) << "', '" << escape(dataValue) << "', '" << escape(attribute.units() ? attribute.units().get() : "") << "', '" | |
| 1273 | ✗ | << escape(dataType) << "');"; | |
| 1274 | |||
| 1275 | ✗ | statement = ss.str(); | |
| 1276 | |||
| 1277 | ✗ | if (sqlite3_exec(m_db, statement.c_str(), nullptr, nullptr, nullptr) != SQLITE_OK) { | |
| 1278 | // Rollback changes | ||
| 1279 | ✗ | LOG(Error, "addMeasure: statement failed, rolling back: " << statement); | |
| 1280 | ✗ | rollbackTransaction(); | |
| 1281 | ✗ | return false; | |
| 1282 | } | ||
| 1283 | ✗ | } // End insert each attribute | |
| 1284 | |||
| 1285 | // Commit changes now that everything went well | ||
| 1286 | ✗ | return commitTransaction(); | |
| 1287 | ✗ | } | |
| 1288 | |||
| 1289 | // cppcheck-suppress constParameterReference | ||
| 1290 | ✗ | bool LocalBCL::removeMeasure(BCLMeasure& measure) { | |
| 1291 | // if uid is empty | ||
| 1292 | ✗ | if ((m_db == nullptr) || measure.uid().empty() || measure.versionId().empty()) { | |
| 1293 | ✗ | return false; | |
| 1294 | } | ||
| 1295 | |||
| 1296 | // TODO: JM 2018-11-14 Check if Measure is actually part of the SQL database first? | ||
| 1297 | |||
| 1298 | // Start by deleting stuff from the SQL file | ||
| 1299 | // We don't expect any of these to fail really, if it isn't present, then it still returns OK | ||
| 1300 | // But let's be prudent and still start a transaction | ||
| 1301 | |||
| 1302 | // Start a transaction, so we can handle failures without messing up the database | ||
| 1303 | ✗ | if (!beginTransaction()) { | |
| 1304 | ✗ | return false; | |
| 1305 | } | ||
| 1306 | |||
| 1307 | ✗ | std::string uid = measure.uid(); | |
| 1308 | ✗ | std::string versionId = measure.versionId(); | |
| 1309 | |||
| 1310 | ✗ | std::string statement("DELETE FROM Measures WHERE uid='" + escape(uid) + "' AND version_id='" + escape(versionId) | |
| 1311 | ✗ | + "';" | |
| 1312 | "DELETE FROM Files WHERE uid='" | ||
| 1313 | ✗ | + escape(uid) + "' AND version_id='" + escape(versionId) | |
| 1314 | ✗ | + "';" | |
| 1315 | "DELETE FROM Attributes WHERE uid='" | ||
| 1316 | ✗ | + escape(uid) + "' AND version_id='" + escape(versionId) + "';"); | |
| 1317 | ✗ | if (sqlite3_exec(m_db, statement.c_str(), nullptr, nullptr, nullptr) != SQLITE_OK) { | |
| 1318 | ✗ | LOG(Error, "Couldn't delete measure from SQL file: " << statement); | |
| 1319 | // Rollback changes | ||
| 1320 | ✗ | rollbackTransaction(); | |
| 1321 | ✗ | return false; | |
| 1322 | } | ||
| 1323 | |||
| 1324 | // Commit changes now that everything went well | ||
| 1325 | ✗ | if (!commitTransaction()) { | |
| 1326 | ✗ | LOG(Error, "Transaction commit failed, will not remove Measure from disk."); | |
| 1327 | ✗ | return false; | |
| 1328 | } | ||
| 1329 | |||
| 1330 | // proceed deleting component on disk | ||
| 1331 | ✗ | openstudio::path pathToRemove = m_libraryPath / uid / versionId; | |
| 1332 | |||
| 1333 | // We look at the parent path, and if we find only one folder = (one versionId), we delete the parent instead | ||
| 1334 | // so we don't end up with an empty folder | ||
| 1335 | ✗ | unsigned int n_dirs = 0; | |
| 1336 | ✗ | for (auto dir_itr = openstudio::filesystem::directory_iterator(pathToRemove.parent_path()); dir_itr != openstudio::filesystem::directory_iterator(); | |
| 1337 | ✗ | ++dir_itr) { | |
| 1338 | ✗ | const auto p = dir_itr->path(); | |
| 1339 | ✗ | if (openstudio::filesystem::is_directory(p) && openstudio::filesystem::exists(p)) { | |
| 1340 | ✗ | ++n_dirs; | |
| 1341 | } | ||
| 1342 | ✗ | } | |
| 1343 | |||
| 1344 | // Lambda version, doesn't really help clarity here | ||
| 1345 | /* | ||
| 1346 | *int n_dirs = std::count_if(openstudio::filesystem::directory_iterator(pathToRemove.parent_path()), | ||
| 1347 | * openstudio::filesystem::directory_iterator(), | ||
| 1348 | * [](const openstudio::filesystem::directory_entry& d) { | ||
| 1349 | * const auto p = d.path(); | ||
| 1350 | * return openstudio::filesystem::is_directory(p) && openstudio::filesystem::exists(p); | ||
| 1351 | * } | ||
| 1352 | * ); | ||
| 1353 | */ | ||
| 1354 | |||
| 1355 | ✗ | if (n_dirs == 1) { | |
| 1356 | ✗ | pathToRemove = pathToRemove.parent_path(); | |
| 1357 | } | ||
| 1358 | ✗ | removeDirectory(pathToRemove); | |
| 1359 | |||
| 1360 | ✗ | return true; | |
| 1361 | ✗ | } | |
| 1362 | |||
| 1363 | ✗ | size_t LocalBCL::removeOutdatedLocalComponents(const std::string& uid, const std::string& currentVersionId) { | |
| 1364 | ✗ | size_t num_removed = 0; | |
| 1365 | ✗ | for (auto& component : components()) { | |
| 1366 | ✗ | if ((component.uid() == uid) && (component.versionId() != currentVersionId)) { | |
| 1367 | ✗ | ++num_removed; | |
| 1368 | ✗ | removeComponent(component); | |
| 1369 | } | ||
| 1370 | ✗ | } | |
| 1371 | ✗ | return num_removed; | |
| 1372 | } | ||
| 1373 | |||
| 1374 | ✗ | size_t LocalBCL::removeOutdatedLocalMeasures(const std::string& uid, const std::string& currentVersionId) { | |
| 1375 | ✗ | size_t num_removed = 0; | |
| 1376 | ✗ | for (auto& measure : measures()) { | |
| 1377 | ✗ | if ((measure.uid() == uid) && (measure.versionId() != currentVersionId)) { | |
| 1378 | ✗ | ++num_removed; | |
| 1379 | ✗ | removeMeasure(measure); | |
| 1380 | } | ||
| 1381 | ✗ | } | |
| 1382 | ✗ | return num_removed; | |
| 1383 | } | ||
| 1384 | |||
| 1385 | 1 | std::vector<BCLComponent> LocalBCL::componentAttributeSearch(const std::vector<std::pair<std::string, std::string>>& searchTerms) const { | |
| 1386 |
2/2✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
|
1 | auto uids = attributeSearch(searchTerms, "component"); |
| 1387 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
|
1 | if (uids.empty()) { |
| 1388 | ✗ | return {}; | |
| 1389 | } | ||
| 1390 | |||
| 1391 | 1 | std::vector<BCLComponent> result; | |
| 1392 |
2/2✓ Branch 5 taken 1 time.
✓ Branch 6 taken 1 time.
|
2 | for (const auto& uid : uids) { |
| 1393 |
1/1✓ Branch 1 taken 1 time.
|
1 | boost::optional<BCLComponent> component = getComponent(uid.first, uid.second); |
| 1394 |
1/2✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 | if (component) { |
| 1395 |
2/2✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
|
1 | result.push_back(*component); |
| 1396 | } | ||
| 1397 | 1 | } | |
| 1398 | |||
| 1399 | 1 | return result; | |
| 1400 | 1 | } | |
| 1401 | |||
| 1402 | ✗ | std::vector<BCLMeasure> LocalBCL::measureAttributeSearch(const std::vector<std::pair<std::string, std::string>>& searchTerms) const { | |
| 1403 | ✗ | auto uids = this->attributeSearch(searchTerms, "measure"); | |
| 1404 | ✗ | if (uids.empty()) { | |
| 1405 | ✗ | return {}; | |
| 1406 | } | ||
| 1407 | |||
| 1408 | ✗ | std::vector<BCLMeasure> result; | |
| 1409 | ✗ | for (const auto& uid : uids) { | |
| 1410 | ✗ | boost::optional<BCLMeasure> measure = getMeasure(uid.first, uid.second); | |
| 1411 | ✗ | if (measure) { | |
| 1412 | ✗ | result.push_back(*measure); | |
| 1413 | } | ||
| 1414 | ✗ | } | |
| 1415 | |||
| 1416 | ✗ | return result; | |
| 1417 | ✗ | } | |
| 1418 | |||
| 1419 | 1 | std::set<std::pair<std::string, std::string>> LocalBCL::attributeSearch(const std::vector<std::pair<std::string, std::string>>& searchTerms, | |
| 1420 | const std::string& componentType) const { | ||
| 1421 | using UidsVecType = std::vector<std::pair<std::string, std::string>>; | ||
| 1422 | using UidsType = std::set<std::pair<std::string, std::string>>; | ||
| 1423 | |||
| 1424 | 1 | UidsType uids; | |
| 1425 | |||
| 1426 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
|
1 | if (!m_db) { |
| 1427 | ✗ | return uids; | |
| 1428 | } | ||
| 1429 | |||
| 1430 | // Make pairs of (uid, version_id) | ||
| 1431 | { | ||
| 1432 |
3/7✓ Branch 1 taken 1 time.
✓ Branch 3 taken 1 time.
✗ Branch 4 not taken.
✗ Branch 6 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✓ Branch 11 taken 1 time.
|
1 | std::string tableName = (componentType == "component") ? "Components" : ((componentType == "measure") ? "Measures" : ""); |
| 1433 | |||
| 1434 |
1/1✓ Branch 1 taken 1 time.
|
1 | std::string statement = "SELECT DISTINCT uid, version_id FROM " + tableName; |
| 1435 | sqlite3_stmt* sqlStmtPtr; | ||
| 1436 |
1/1✓ Branch 2 taken 1 time.
|
1 | int code = sqlite3_prepare_v2(m_db, statement.c_str(), -1, &sqlStmtPtr, nullptr); |
| 1437 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
|
1 | if (code != SQLITE_OK) { |
| 1438 | ✗ | LOG(Error, "Cannot prepare statement (in searchTerms): " << statement); | |
| 1439 | ✗ | sqlite3_finalize(sqlStmtPtr); // No-op | |
| 1440 | ✗ | return {}; | |
| 1441 | } | ||
| 1442 | |||
| 1443 | // Loop until done (or failed) | ||
| 1444 |
5/8✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✗ Branch 7 not taken.
|
3 | while ((code != SQLITE_DONE) && (code != SQLITE_BUSY) && (code != SQLITE_ERROR) && (code != SQLITE_MISUSE)) //loop until SQLITE_DONE |
| 1445 | { | ||
| 1446 |
1/1✓ Branch 1 taken 2 times.
|
2 | code = sqlite3_step(sqlStmtPtr); |
| 1447 |
2/2✓ Branch 0 taken 1 time.
✓ Branch 1 taken 1 time.
|
2 | if (code == SQLITE_ROW) { |
| 1448 | // Get values from SELECT | ||
| 1449 |
2/2✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
|
1 | std::string uid = columnText(sqlite3_column_text(sqlStmtPtr, 0)); |
| 1450 |
2/2✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
|
1 | std::string version_id = columnText(sqlite3_column_text(sqlStmtPtr, 1)); |
| 1451 | |||
| 1452 |
2/2✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
|
1 | uids.insert(make_pair(uid, version_id)); |
| 1453 | |||
| 1454 | 1 | } else // i didn't get a row. something is wrong so set the exit condition. | |
| 1455 | { // should never get here since i test for all documented return states above | ||
| 1456 | 1 | code = SQLITE_DONE; | |
| 1457 | } | ||
| 1458 | } // End loop on each match | ||
| 1459 | |||
| 1460 | // Finalize statement to prevent memory leak | ||
| 1461 |
1/1✓ Branch 1 taken 1 time.
|
1 | sqlite3_finalize(sqlStmtPtr); |
| 1462 | 1 | } | |
| 1463 | |||
| 1464 | // Loop on search terms | ||
| 1465 | { | ||
| 1466 | |||
| 1467 | // Prepare a statement that we'll bind | ||
| 1468 | // (Note: do not do name='?', it won't think it's a bind parameter) | ||
| 1469 |
1/1✓ Branch 1 taken 1 time.
|
1 | std::string statement = "SELECT uid, version_id FROM Attributes WHERE name=? COLLATE NOCASE AND value=? COLLATE NOCASE"; |
| 1470 | sqlite3_stmt* sqlStmtPtr; | ||
| 1471 |
2/3✓ Branch 2 taken 1 time.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 time.
|
1 | if (sqlite3_prepare_v2(m_db, statement.c_str(), -1, &sqlStmtPtr, nullptr) != SQLITE_OK) { |
| 1472 | ✗ | LOG(Error, "Cannot prepare statement (in searchTerms): " << statement); | |
| 1473 | ✗ | sqlite3_finalize(sqlStmtPtr); // No-op | |
| 1474 | ✗ | return {}; | |
| 1475 | } | ||
| 1476 | |||
| 1477 |
2/2✓ Branch 5 taken 1 time.
✓ Branch 6 taken 1 time.
|
2 | for (const auto& searchTerm : searchTerms) { |
| 1478 | |||
| 1479 | 1 | UidsType theseUids; | |
| 1480 | |||
| 1481 | // Reset the statement to allow binding variables on the next iteration | ||
| 1482 |
1/1✓ Branch 1 taken 1 time.
|
1 | sqlite3_reset(sqlStmtPtr); |
| 1483 | |||
| 1484 | // Bind the values now | ||
| 1485 |
1/1✓ Branch 1 taken 1 time.
|
1 | std::string name = escape(searchTerm.first); |
| 1486 |
1/1✓ Branch 1 taken 1 time.
|
1 | std::string value = escape(searchTerm.second); |
| 1487 | |||
| 1488 |
2/3✓ Branch 3 taken 1 time.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 time.
|
1 | if (sqlite3_bind_text(sqlStmtPtr, 1, name.c_str(), name.size(), SQLITE_TRANSIENT) != SQLITE_OK) { |
| 1489 | ✗ | LOG(Error, "Error binding to the 1st parameter (in searchTerms), name: " << name); | |
| 1490 | ✗ | sqlite3_finalize(sqlStmtPtr); | |
| 1491 | ✗ | return {}; | |
| 1492 |
2/3✓ Branch 3 taken 1 time.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 time.
|
1 | } else if (sqlite3_bind_text(sqlStmtPtr, 2, value.c_str(), value.size(), SQLITE_TRANSIENT) != SQLITE_OK) { |
| 1493 | ✗ | LOG(Error, "Error binding to the 2nd parameter (in searchTerms), value: " << value); | |
| 1494 | ✗ | sqlite3_finalize(sqlStmtPtr); | |
| 1495 | ✗ | return {}; | |
| 1496 | } | ||
| 1497 | |||
| 1498 | 1 | int code = SQLITE_OK; | |
| 1499 | |||
| 1500 | // Loop until done (or failed) | ||
| 1501 |
5/8✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✗ Branch 7 not taken.
|
3 | while ((code != SQLITE_DONE) && (code != SQLITE_BUSY) && (code != SQLITE_ERROR) && (code != SQLITE_MISUSE)) //loop until SQLITE_DONE |
| 1502 | { | ||
| 1503 |
1/1✓ Branch 1 taken 2 times.
|
2 | code = sqlite3_step(sqlStmtPtr); |
| 1504 |
2/2✓ Branch 0 taken 1 time.
✓ Branch 1 taken 1 time.
|
2 | if (code == SQLITE_ROW) { |
| 1505 | // Get values from SELECT | ||
| 1506 |
2/2✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
|
1 | std::string uid = columnText(sqlite3_column_text(sqlStmtPtr, 0)); |
| 1507 |
2/2✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
|
1 | std::string version_id = columnText(sqlite3_column_text(sqlStmtPtr, 1)); |
| 1508 | |||
| 1509 |
2/2✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
|
1 | theseUids.insert(make_pair(uid, version_id)); |
| 1510 | |||
| 1511 | 1 | } else // i didn't get a row. something is wrong so set the exit condition. | |
| 1512 | { // should never get here since i test for all documented return states above | ||
| 1513 | 1 | code = SQLITE_DONE; | |
| 1514 | } | ||
| 1515 | } // End loop on each match | ||
| 1516 | |||
| 1517 |
1/1✓ Branch 4 taken 1 time.
|
1 | UidsVecType newUids(std::max(uids.size(), theseUids.size())); |
| 1518 |
1/1✓ Branch 6 taken 1 time.
|
1 | auto insertEnd = std::set_intersection(uids.begin(), uids.end(), theseUids.begin(), theseUids.end(), newUids.begin()); |
| 1519 | |||
| 1520 | 1 | uids.clear(); | |
| 1521 |
1/1✓ Branch 2 taken 1 time.
|
1 | uids.insert(newUids.begin(), insertEnd); |
| 1522 | |||
| 1523 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
|
1 | if (uids.empty()) { |
| 1524 | ✗ | sqlite3_finalize(sqlStmtPtr); | |
| 1525 | ✗ | return {}; | |
| 1526 | } | ||
| 1527 | |||
| 1528 | 1 | } // End loop on searchTerms | |
| 1529 | |||
| 1530 | // Finalize statement to prevent memory leak | ||
| 1531 |
1/1✓ Branch 1 taken 1 time.
|
1 | sqlite3_finalize(sqlStmtPtr); |
| 1532 | 1 | } // End Loop on search terms | |
| 1533 | |||
| 1534 | 1 | return uids; | |
| 1535 | 1 | } | |
| 1536 | |||
| 1537 | 12 | std::string LocalBCL::formatString(double d, unsigned prec) { | |
| 1538 |
1/1✓ Branch 1 taken 12 times.
|
12 | std::stringstream ss; |
| 1539 |
2/2✓ Branch 3 taken 12 times.
✓ Branch 6 taken 12 times.
|
12 | ss << std::setprecision(prec) << std::showpoint << d; |
| 1540 |
1/1✓ Branch 1 taken 12 times.
|
12 | std::string s = ss.str(); |
| 1541 | |||
| 1542 | // truncate zeros from the end | ||
| 1543 | 12 | int i = s.size() - 1; | |
| 1544 |
5/6✓ Branch 0 taken 157 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 145 times.
✓ Branch 4 taken 12 times.
✓ Branch 5 taken 145 times.
✓ Branch 6 taken 12 times.
|
157 | while (i > 0 && s[i] == '0') { |
| 1545 | 145 | --i; | |
| 1546 | } | ||
| 1547 |
1/2✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
|
12 | if (i > 0) { |
| 1548 |
1/1✓ Branch 1 taken 12 times.
|
12 | s.erase(i + 1); |
| 1549 |
2/2✓ Branch 2 taken 5 times.
✓ Branch 3 taken 7 times.
|
12 | if (*s.rbegin() == '.') { |
| 1550 |
1/1✓ Branch 1 taken 5 times.
|
5 | s.push_back('0'); |
| 1551 | } | ||
| 1552 | } | ||
| 1553 | 24 | return s; | |
| 1554 | 12 | } | |
| 1555 | |||
| 1556 | 58 | std::string LocalBCL::prodAuthKey() const { | |
| 1557 | 58 | return m_prodAuthKey; | |
| 1558 | } | ||
| 1559 | |||
| 1560 | 1 | bool LocalBCL::setProdAuthKey(const std::string& prodAuthKey) { | |
| 1561 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
|
1 | if (!m_db) { |
| 1562 | ✗ | return false; | |
| 1563 | } | ||
| 1564 | |||
| 1565 |
1/1✓ Branch 1 taken 1 time.
|
1 | RemoteBCL remoteBCL; |
| 1566 |
2/3✓ Branch 1 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 time.
|
1 | if (!remoteBCL.setProdAuthKey(prodAuthKey)) { |
| 1567 | ✗ | LOG(Error, "prodAuthKey was rejected by RemoteBCL."); | |
| 1568 | ✗ | return false; | |
| 1569 | } | ||
| 1570 | |||
| 1571 | // Overwrite prodAuthKey in database | ||
| 1572 | |||
| 1573 | // Start a transaction, so we can handle failures without messing up the database | ||
| 1574 |
2/3✓ Branch 1 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 time.
|
1 | if (!beginTransaction()) { |
| 1575 | ✗ | return false; | |
| 1576 | } | ||
| 1577 | |||
| 1578 |
3/3✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
✓ Branch 7 taken 1 time.
|
1 | std::string statement = "UPDATE Settings SET data='" + escape(prodAuthKey) + "' WHERE name='prodAuthKey'"; |
| 1579 |
2/3✓ Branch 2 taken 1 time.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 time.
|
1 | if (sqlite3_exec(m_db, statement.c_str(), nullptr, nullptr, nullptr) != SQLITE_OK) { |
| 1580 | // Rollback changes | ||
| 1581 | ✗ | LOG(Error, "Cannot update prodAuthKey, rolling back."); | |
| 1582 | ✗ | rollbackTransaction(); | |
| 1583 | ✗ | return false; | |
| 1584 | } | ||
| 1585 |
1/1✓ Branch 1 taken 1 time.
|
1 | bool result = commitTransaction(); |
| 1586 |
1/2✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 | if (result) { |
| 1587 | // Store key | ||
| 1588 |
1/1✓ Branch 1 taken 1 time.
|
1 | m_prodAuthKey = prodAuthKey; |
| 1589 | } | ||
| 1590 | 1 | return result; | |
| 1591 | 1 | } | |
| 1592 | |||
| 1593 | 7 | std::string LocalBCL::devAuthKey() const { | |
| 1594 | 7 | return m_devAuthKey; | |
| 1595 | } | ||
| 1596 | |||
| 1597 | ✗ | bool LocalBCL::setDevAuthKey(const std::string& devAuthKey) { | |
| 1598 | |||
| 1599 | ✗ | if (!m_db) { | |
| 1600 | ✗ | return false; | |
| 1601 | } | ||
| 1602 | |||
| 1603 | ✗ | RemoteBCL remoteBCL; | |
| 1604 | |||
| 1605 | ✗ | if (!remoteBCL.setDevAuthKey(devAuthKey)) { | |
| 1606 | ✗ | LOG(Error, "devAuthKey was rejected by RemoteBCL."); | |
| 1607 | ✗ | return false; | |
| 1608 | } | ||
| 1609 | // Store key | ||
| 1610 | ✗ | m_devAuthKey = devAuthKey; | |
| 1611 | |||
| 1612 | //Overwrite devAuthKey in database | ||
| 1613 | // Start a transaction, so we can handle failures without messing up the database | ||
| 1614 | ✗ | if (!beginTransaction()) { | |
| 1615 | ✗ | return false; | |
| 1616 | } | ||
| 1617 | |||
| 1618 | ✗ | std::string statement = "UPDATE Settings SET data='" + escape(devAuthKey) + "' WHERE name='devAuthKey'"; | |
| 1619 | ✗ | if (sqlite3_exec(m_db, statement.c_str(), nullptr, nullptr, nullptr) != SQLITE_OK) { | |
| 1620 | // Rollback changes | ||
| 1621 | ✗ | LOG(Error, "Cannot update devAuthKey, rolling back.") | |
| 1622 | ✗ | rollbackTransaction(); | |
| 1623 | ✗ | return false; | |
| 1624 | } | ||
| 1625 | ✗ | bool result = commitTransaction(); | |
| 1626 | ✗ | if (result) { | |
| 1627 | // Store key | ||
| 1628 | ✗ | m_devAuthKey = devAuthKey; | |
| 1629 | } | ||
| 1630 | ✗ | return result; | |
| 1631 | ✗ | } | |
| 1632 | |||
| 1633 | 3 | openstudio::path LocalBCL::libraryPath() const { | |
| 1634 | 3 | return m_libraryPath; | |
| 1635 | } | ||
| 1636 | |||
| 1637 | } // namespace openstudio | ||
| 1638 |