Unverified Commit c377d261 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3089326 by mfb, ankithashetty, ayushmishra206, ravi.shankar, agrochal,...

Issue #3089326 by mfb, ankithashetty, ayushmishra206, ravi.shankar, agrochal, krystalcode, daffie, alexpott: Query start time in database log
parent cc41d94d
Loading
Loading
Loading
Loading
+12 −7
Original line number Diff line number Diff line
@@ -22,8 +22,8 @@ class Log {
   *
   * array(
   *   $logging_key = array(
   *     array('query' => '', 'args' => array(), 'caller' => '', 'target' => '', 'time' => 0),
   *     array('query' => '', 'args' => array(), 'caller' => '', 'target' => '', 'time' => 0),
   *     array('query' => '', 'args' => array(), 'caller' => '', 'target' => '', 'time' => 0, 'start' => 0),
   *     array('query' => '', 'args' => array(), 'caller' => '', 'target' => '', 'time' => 0, 'start' => 0),
   *   ),
   * );
   *
@@ -103,14 +103,18 @@ public function end($logging_key) {
  /**
   * Log a query to all active logging keys.
   *
   * @param $statement
   * @param \Drupal\Core\Database\StatementInterface $statement
   *   The prepared statement object to log.
   * @param $args
   * @param array $args
   *   The arguments passed to the statement object.
   * @param $time
   *   The time in milliseconds the query took to execute.
   * @param float $time
   *   The time the query took to execute as a float (in seconds with
   *   microsecond precision).
   * @param float $start
   *   The time the query started as a float (in seconds since the Unix epoch
   *   with microsecond precision).
   */
  public function log(StatementInterface $statement, $args, $time) {
  public function log(StatementInterface $statement, $args, $time, float $start = NULL) {
    foreach (array_keys($this->queryLog) as $key) {
      $this->queryLog[$key][] = [
        'query' => $statement->getQueryString(),
@@ -118,6 +122,7 @@ public function log(StatementInterface $statement, $args, $time) {
        'target' => $statement->dbh->getTarget(),
        'caller' => $this->findCaller(),
        'time' => $time,
        'start' => $start,
      ];
    }
  }
+1 −1
Original line number Diff line number Diff line
@@ -68,7 +68,7 @@ public function execute($args = [], $options = []) {

    if (!empty($logger)) {
      $query_end = microtime(TRUE);
      $logger->log($this, $args, $query_end - $query_start);
      $logger->log($this, $args, $query_end - $query_start, $query_start);
    }

    return $return;
+1 −1
Original line number Diff line number Diff line
@@ -186,7 +186,7 @@ public function execute($args = [], $options = []) {

    if (!empty($logger)) {
      $query_end = microtime(TRUE);
      $logger->log($this, $args, $query_end - $query_start);
      $logger->log($this, $args, $query_end - $query_start, $query_start);
    }

    // Initialize the first row in $this->currentRow.
+1 −1
Original line number Diff line number Diff line
@@ -117,7 +117,7 @@ public function execute($args = [], $options = []) {

    if (!empty($logger)) {
      $query_end = microtime(TRUE);
      $logger->log($this, $args, $query_end - $query_start);
      $logger->log($this, $args, $query_end - $query_start, $query_start);
    }

    return $return;
+3 −0
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ class LoggingTest extends DatabaseTestBase {
  public function testEnableLogging() {
    Database::startLog('testing');

    $start = microtime(TRUE);
    $this->connection->query('SELECT [name] FROM {test} WHERE [age] > :age', [':age' => 25])->fetchCol();
    $this->connection->query('SELECT [age] FROM {test} WHERE [name] = :name', [':name' => 'Ringo'])->fetchCol();

@@ -32,6 +33,8 @@ public function testEnableLogging() {

    foreach ($queries as $query) {
      $this->assertEqual(__FUNCTION__, $query['caller']['function'], 'Correct function in query log.');
      $this->assertIsFloat($query['start']);
      $this->assertGreaterThanOrEqual($start, $query['start']);
    }
  }