/** * @param Contract $streamContract * @param Identifier $streamId * @param $expectedStreamRevision * @throws \EventCentric\Persistence\OptimisticConcurrencyFailed */ protected function controlOptimisticConcurrency(Contract $streamContract, Identifier $streamId, $expectedStreamRevision) { $result = $this->connection->fetchArray(MaxStreamRevision::from(self::TABLE_NAME), ['streamContract' => $streamContract, 'streamId' => $streamId]); $actualStreamRevision = (int) $result[0]; if ($actualStreamRevision != $expectedStreamRevision) { throw OptimisticConcurrencyFailed::revisionDoesNotMatch($expectedStreamRevision, $actualStreamRevision); } }
/** * Makes sure database is created and ready to use. * * @param Fiddle $fiddle * @return string */ protected function prepareDatabase(Fiddle $fiddle) { $directory = $this->getDirectory($fiddle); $fiddleEscaped = $this->db->quote($fiddle->getId(), \PDO::PARAM_STR); $fiddleId = $this->db->quoteIdentifier($fiddle->getId()); if (!$this->debug && file_exists($directory . '/home/sandbox/propel.yml')) { return false; } $dbName = 'fiddle_' . $fiddle->getId(); $userName = $fiddle->getId(); $password = substr(md5(microtime() * 10000 + mt_rand()), 0, 14); $dbNameIdentifier = $this->db->quoteIdentifier($dbName); //create new db credentials $row = $this->db->fetchArray("SHOW DATABASES LIKE ?", [$dbName]); if (false === $row) { // database does not exist yet $this->db->executeQuery(sprintf("CREATE DATABASE %s", $dbNameIdentifier)); } $user = $this->db->fetchArray("SELECT User FROM mysql.user WHERE User = ?", [$fiddle->getId()]); // we don't save the password in our database so we have to remove the user first if ($user) { $this->db->executeQuery(sprintf("DROP USER %s@'localhost'", $fiddleEscaped)); } $this->db->executeQuery(sprintf("CREATE USER %s@'localhost' IDENTIFIED BY %s", $fiddleEscaped, $this->db->quote($password, \PDO::PARAM_STR))); $this->needConfigConvertCall = true; $this->db->executeQuery(sprintf("GRANT USAGE, ALTER, CREATE, DELETE, DROP, INDEX, INSERT, SELECT, UPDATE ON %s.* TO %s@'localhost'", $dbNameIdentifier, $fiddleEscaped)); $propelConfig = <<<EOF propel: database: connections: default: adapter: mysql classname: Propel\\Runtime\\Connection\\DebugPDO dsn: mysql:host=127.0.0.1;dbname={$dbName} user: {$userName} password: {$password} attributes: runtime: log: defaultLogger: type: stream path: ./propel_log.txt level: 100 defaultConnection: default connections: - default generator: defaultConnection: default connections: - default EOF; file_put_contents($directory . '/home/sandbox/propel.yml', $propelConfig); return $propelConfig; }
/** * All my reviews in common with my followers' reviews of the last week * @param $userId * * @return int */ public function getLastWeekCommonReviewsCount($userId) { $followerIds = $this->userRepository->getFollowersIds($userId); $sql = 'SELECT COUNT(*) as counter FROM ( SELECT reviews.id, reviews.restaurant_id FROM reviews WHERE reviews.user_id = ?) my_reviews INNER JOIN ( SELECT reviews.id, reviews.restaurant_id FROM reviews WHERE reviews.user_id IN (?) AND reviews.created > DATE_SUB(NOW(), INTERVAL 1 WEEK)) follower_reviews ON my_reviews.restaurant_id = follower_reviews.restaurant_id'; $result = $this->connection->fetchArray($sql, array($userId, $followerIds), array(\PDO::PARAM_INT, Connection::PARAM_INT_ARRAY)); return (int) $result[0]; }
public function contains($md5) { $query = 'SELECT * FROM blender WHERE md5 = "' . $md5 . '"'; $rs = $this->conn->fetchArray($query); return $rs ? count($rs) > 0 : $rs; }
/** * @param \Doctrine\DBAL\Connection $connection * @return string[] */ private function getAllMyIsamTables($connection) { $dbName = \OC::$server->getConfig()->getSystemValue("dbname"); $result = $connection->fetchArray("SELECT table_name FROM information_schema.tables WHERE table_schema = ? AND engine = 'MyISAM' AND TABLE_NAME LIKE \"*PREFIX*%\"", array($dbName)); return $result; }