bool SQLiteManager::connect(const std::string& fileName) { bool success = true; if(mIsConnected) { LOGWARNING("SQLite is already connected."); success = false; } else { int status = sqlite3_open(fileName.c_str(), &mDatabase); success = status == SQLITE_OK; if (success) { mIsConnected = true; //synchronous = OFF permet de dire à SQL lite de ne pas atteindre que l'opération d'écriture soit terminé dans le système de fichier //journal_mode = OFF On ne peut pas faire de rollback ou autre. Je ne pense pas que ce soit nécessaire pour l'instant de toute façon mIsConnected = executeStatement("PRAGMA synchronous = OFF") && executeStatement("PRAGMA journal_mode = OFF"); } else { sqlite3_close(mDatabase); LOGERROR("Could not connect to database " << fileName); } } return success && mIsConnected; }
CassError CassandraClient::createSchema() { CassError rc = CASS_OK; logger.information("Creating Schema"); // Writing Schema logging // rc = executeStatement("SELECT zmfLogging FROM system.schema_keyspaces"); rc = executeStatement( "CREATE KEYSPACE IF NOT EXISTS logging WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };"); // Create Table messages rc = executeStatement( "CREATE TABLE IF NOT EXISTS logging.messages (id uuid PRIMARY KEY, messageType blob, senderType varint, senderId varint, timeStamp timestamp);"); return rc; }
void ApplicationCacheStorage::storeNewestCache(ApplicationCacheGroup* group) { openDatabase(true); SQLiteTransaction storeCacheTransaction(m_database); storeCacheTransaction.begin(); if (!group->storageID()) { // Store the group if (!store(group)) return; } ASSERT(group->newestCache()); ASSERT(!group->newestCache()->storageID()); // Store the newest cache if (!store(group->newestCache())) return; // Update the newest cache in the group. SQLiteStatement statement(m_database, "UPDATE CacheGroups SET newestCache=? WHERE id=?"); if (statement.prepare() != SQLResultOk) return; statement.bindInt64(1, group->newestCache()->storageID()); statement.bindInt64(2, group->storageID()); if (!executeStatement(statement)) return; storeCacheTransaction.commit(); }
bool ApplicationCacheStorage::store(ApplicationCache* cache) { ASSERT(cache->storageID() == 0); ASSERT(cache->group()->storageID() != 0); SQLiteStatement statement(m_database, "INSERT INTO Caches (cacheGroup) VALUES (?)"); if (statement.prepare() != SQLResultOk) return false; statement.bindInt64(1, cache->group()->storageID()); if (!executeStatement(statement)) return false; unsigned cacheStorageID = (unsigned)m_database.lastInsertRowID(); // Store all resources { ApplicationCache::ResourceMap::const_iterator end = cache->end(); for (ApplicationCache::ResourceMap::const_iterator it = cache->begin(); it != end; ++it) { if (!store(it->second.get(), cacheStorageID)) return false; } } // Store the online whitelist const HashSet<String>& onlineWhitelist = cache->onlineWhitelist(); { HashSet<String>::const_iterator end = onlineWhitelist.end(); for (HashSet<String>::const_iterator it = onlineWhitelist.begin(); it != end; ++it) { SQLiteStatement statement(m_database, "INSERT INTO CacheWhitelistURLs (url, cache) VALUES (?, ?)"); statement.prepare(); statement.bindText(1, *it); statement.bindInt64(2, cacheStorageID); if (!executeStatement(statement)) return false; } } cache->setStorageID(cacheStorageID); return true; }
bool AsyncExecuteStatements::executeAndProcessStatement(sqlite3_stmt *aStatement, bool aLastStatement) { mMutex.AssertNotCurrentThreadOwns(); // Execute our statement bool hasResults; do { hasResults = executeStatement(aStatement); // If we had an error, bail. if (mState == ERROR) return false; // If we have been canceled, there is no point in going on... { MutexAutoLock lockedScope(mMutex); if (mCancelRequested) { mState = CANCELED; return false; } } // Build our result set and notify if we got anything back and have a // callback to notify. if (mCallback && hasResults && NS_FAILED(buildAndNotifyResults(aStatement))) { // We had an error notifying, so we notify on error and stop processing. mState = ERROR; // Notify, and stop processing statements. (void)notifyError(mozIStorageError::ERROR, "An error occurred while notifying about results"); return false; } } while (hasResults); #ifdef DEBUG // Check to make sure that this statement was smart about what it did. checkAndLogStatementPerformance(aStatement); #endif // If we are done, we need to set our state accordingly while we still hold // our mutex. We would have already returned if we were canceled or had // an error at this point. if (aLastStatement) mState = COMPLETED; return true; }
void ApplicationCacheStorage::remove(ApplicationCache* cache) { if (!cache->storageID()) return; openDatabase(false); if (!m_database.isOpen()) return; SQLiteStatement statement(m_database, "DELETE FROM Caches WHERE id=?"); if (statement.prepare() != SQLResultOk) return; statement.bindInt64(1, cache->storageID()); executeStatement(statement); }
bool ApplicationCacheStorage::store(ApplicationCacheGroup* group) { ASSERT(group->storageID() == 0); SQLiteStatement statement(m_database, "INSERT INTO CacheGroups (manifestHostHash, manifestURL) VALUES (?, ?)"); if (statement.prepare() != SQLResultOk) return false; statement.bindInt64(1, urlHostHash(group->manifestURL())); statement.bindText(2, group->manifestURL()); if (!executeStatement(statement)) return false; group->setStorageID((unsigned)m_database.lastInsertRowID()); return true; }
void ApplicationCacheStorage::verifySchemaVersion() { if (m_database.tableExists("SchemaVersion")) { int version = SQLiteStatement(m_database, "SELECT version from SchemaVersion").getColumnInt(0); if (version == SchemaVersion) return; } m_database.clearAllTables(); SQLiteTransaction createSchemaVersionTable(m_database); createSchemaVersionTable.begin(); executeSQLCommand("CREATE TABLE SchemaVersion (version INTEGER NOT NULL)"); SQLiteStatement statement(m_database, "INSERT INTO SchemaVersion (version) VALUES (?)"); if (statement.prepare() != SQLResultOk) return; statement.bindInt64(1, SchemaVersion); executeStatement(statement); createSchemaVersionTable.commit(); }
CassError CassandraClient::insertLogMsg(const zmf::data::ZmfMessage& message, const zmf::data::ModuleUniqueId& sender) { CassError rc = CASS_OK; std::stringstream stream; for (int i = 0; i < message.getType().getMatchLength(); i++) { stream << std::setfill('0') << std::setw(2) << std::hex << unsigned(message.getType().getMatchRaw()[i]); } std::string asString = stream.str(); logger.trace("New Log Message"); std::string statement = std::string( "INSERT INTO logging.messages (id, messageType, senderType, senderId, timestamp) VALUES (uuid(), 0x") + asString + ", " + std::to_string(sender.TypeId) + ", " + std::to_string(sender.InstanceId) + ", dateOf(now()));"; rc = executeStatement(statement); return rc; }
bool Transaction::executeStatement(MYSQL* connection, std::shared_ptr<IQueryData> ptr) { TransactionData* data = (TransactionData*)ptr.get(); data->setStatus(QUERY_RUNNING); //This temporarily disables reconnect, since a reconnect //would rollback (and cancel) a transaction //Which could lead to parts of the transaction being executed outside of a transaction //If they are being executed after the reconnect my_bool oldReconnectStatus = m_database->getAutoReconnect(); m_database->setAutoReconnect((my_bool)0); auto resetReconnectStatus = finally([&] { m_database->setAutoReconnect(oldReconnectStatus); }); try { this->mysqlAutocommit(connection, false); { for (auto& query : data->m_queries) { try { //Errors are cleared in case this is retrying after losing connection query.second->setResultStatus(QUERY_NONE); query.second->setError(""); query.first->executeQuery(connection, query.second); } catch (const MySQLException& error) { query.second->setError(error.what()); query.second->setResultStatus(QUERY_ERROR); throw error; } } } mysql_commit(connection); data->setResultStatus(QUERY_SUCCESS); this->mysqlAutocommit(connection, true); } catch (const MySQLException& error) { //This check makes sure that setting mysqlAutocommit back to true doesn't cause the transaction to fail //Even though the transaction was executed successfully if (data->getResultStatus() != QUERY_SUCCESS) { int errorCode = error.getErrorCode(); if (oldReconnectStatus && !data->retried && (errorCode == CR_SERVER_LOST || errorCode == CR_SERVER_GONE_ERROR)) { //Because autoreconnect is disabled we want to try and explicitly execute the transaction once more //if we can get the client to reconnect (reconnect is caused by mysql_ping) //If this fails we just go ahead and error m_database->setAutoReconnect((my_bool)1); if (mysql_ping(connection) == 0) { data->retried = true; return executeStatement(connection, ptr); } } //If this call fails it means that the connection was (probably) lost //In that case the mysql server rolls back any transaction anyways so it doesn't //matter if it fails mysql_rollback(connection); data->setResultStatus(QUERY_ERROR); } //If this fails it probably means that the connection was lost //In that case autocommit is turned back on anyways (once the connection is reestablished) //See: https://dev.mysql.com/doc/refman/5.7/en/auto-reconnect.html mysql_autocommit(connection, true); data->setError(error.what()); } for (auto& pair : data->m_queries) { pair.second->setResultStatus(data->getResultStatus()); pair.second->setStatus(QUERY_COMPLETE); } data->setStatus(QUERY_COMPLETE); return true; }
bool ApplicationCacheStorage::store(ApplicationCacheResource* resource, unsigned cacheStorageID) { ASSERT(cacheStorageID); ASSERT(!resource->storageID()); openDatabase(true); // First, insert the data SQLiteStatement dataStatement(m_database, "INSERT INTO CacheResourceData (data) VALUES (?)"); if (dataStatement.prepare() != SQLResultOk) return false; if (resource->data()->size()) dataStatement.bindBlob(1, resource->data()->data(), resource->data()->size()); if (!dataStatement.executeCommand()) return false; unsigned dataId = (unsigned)m_database.lastInsertRowID(); // Then, insert the resource // Serialize the headers Vector<UChar> stringBuilder; HTTPHeaderMap::const_iterator end = resource->response().httpHeaderFields().end(); for (HTTPHeaderMap::const_iterator it = resource->response().httpHeaderFields().begin(); it!= end; ++it) { stringBuilder.append(it->first.characters(), it->first.length()); stringBuilder.append((UChar)':'); stringBuilder.append(it->second.characters(), it->second.length()); stringBuilder.append((UChar)'\n'); } String headers = String::adopt(stringBuilder); SQLiteStatement resourceStatement(m_database, "INSERT INTO CacheResources (url, statusCode, responseURL, headers, data, mimeType, textEncodingName) VALUES (?, ?, ?, ?, ?, ?, ?)"); if (resourceStatement.prepare() != SQLResultOk) return false; resourceStatement.bindText(1, resource->url()); resourceStatement.bindInt64(2, resource->response().httpStatusCode()); resourceStatement.bindText(3, resource->response().url()); resourceStatement.bindText(4, headers); resourceStatement.bindInt64(5, dataId); resourceStatement.bindText(6, resource->response().mimeType()); resourceStatement.bindText(7, resource->response().textEncodingName()); if (!executeStatement(resourceStatement)) return false; unsigned resourceId = (unsigned)m_database.lastInsertRowID(); // Finally, insert the cache entry SQLiteStatement entryStatement(m_database, "INSERT INTO CacheEntries (cache, type, resource) VALUES (?, ?, ?)"); if (entryStatement.prepare() != SQLResultOk) return false; entryStatement.bindInt64(1, cacheStorageID); entryStatement.bindInt64(2, resource->type()); entryStatement.bindInt64(3, resourceId); if (!executeStatement(entryStatement)) return false; return true; }
bool SQLiteManager::endTransaction() { const std::string statement = "end transaction"; return executeStatement(statement); }