/** * {@inheritdoc} */ public function log($level, $message, array $context = array()) { // Remove any backtraces since they may contain an unserializable variable. unset($context['backtrace']); // Convert PSR3-style messages to SafeMarkup::format() style, so they can be // translated too in runtime. $message_placeholders = $this->parser->parseMessagePlaceholders($message, $context); try { $this->connection->insert('watchdog')->fields(array('uid' => $context['uid'], 'type' => Unicode::substr($context['channel'], 0, 64), 'message' => $message, 'variables' => serialize($message_placeholders), 'severity' => $level, 'link' => $context['link'], 'location' => $context['request_uri'], 'referer' => $context['referer'], 'hostname' => Unicode::substr($context['ip'], 0, 128), 'timestamp' => $context['timestamp']))->execute(); } catch (\Exception $e) { // When running Drupal on MySQL or MariaDB you can run into several errors // that corrupt the database connection. Some examples for these kind of // errors on the database layer are "1100 - Table 'xyz' was not locked // with LOCK TABLES" and "1153 - Got a packet bigger than // 'max_allowed_packet' bytes". If such an error happens, the MySQL server // invalidates the connection and answers all further requests in this // connection with "2006 - MySQL server had gone away". In that case the // insert statement above results in a database exception. To ensure that // the causal error is written to the log we try once to open a dedicated // connection and write again. if (($e instanceof DatabaseException || $e instanceof \PDOException) && $this->connection->getTarget() != self::DEDICATED_DBLOG_CONNECTION_TARGET) { // Open a dedicated connection for logging. $key = $this->connection->getKey(); $info = Database::getConnectionInfo($key); Database::addConnectionInfo($key, self::DEDICATED_DBLOG_CONNECTION_TARGET, $info['default']); $this->connection = Database::getConnection(self::DEDICATED_DBLOG_CONNECTION_TARGET, $key); // Now try once to log the error again. $this->log($level, $message, $context); } else { throw $e; } } }
/** * Constructs a Query object. * * @param \Drupal\Core\Database\Connection $connection * Database connection object. * @param array $options * Array of query options. */ public function __construct(Connection $connection, $options) { $this->uniqueIdentifier = uniqid('', TRUE); $this->connection = $connection; $this->connectionKey = $this->connection->getKey(); $this->connectionTarget = $this->connection->getTarget(); $this->queryOptions = $options; }
/** * Loads a database fixture into the source database connection. * * @param string $path * Path to the dump file. */ protected function loadFixture($path) { $default_db = Database::getConnection()->getKey(); Database::setActiveConnection($this->sourceDatabase->getKey()); if (substr($path, -3) == '.gz') { $path = 'compress.zlib://' . $path; } require $path; Database::setActiveConnection($default_db); }
/** * Run the database script. * * @param \Drupal\Core\Database\Connection $connection * Connection used by the script when included. * @param string $script * Path to dump script. */ protected function runScript(Connection $connection, $script) { $old_key = Database::setActiveConnection($connection->getKey()); if (substr($script, -3) == '.gz') { $script = "compress.zlib://{$script}"; } try { require $script; } catch (SchemaObjectExistsException $e) { throw new \RuntimeException('An existing Drupal installation exists at this location. Try removing all tables or changing the database prefix in your settings.php file.'); } Database::setActiveConnection($old_key); }