You are here

class ApdqcFakeDatabaseStatement in Asynchronous Prefetch Database Query Cache 7

Fake database statement class for devel logging.

Hierarchy

Expanded class hierarchy of ApdqcFakeDatabaseStatement

File

./apdqc.log.inc, line 10
Creates a fake db statement for devel query logging.

View source
class ApdqcFakeDatabaseStatement extends DatabaseStatementEmpty {
  public $dbh;
  public $queryString;

  /**
   * Constructs a DrupalDatabaseCache object.
   *
   * @param string $query_string
   *   The query string.
   * @param string $extra_data
   *   Extra data about the query; cache hit/miss, etc.
   */
  public function __construct($query_string, $extra_data = '') {
    $dbh = new ApdqcFakeDbh();
    $this->dbh = $dbh;
    $timestamp = '';
    if (!empty($_SERVER['REQUEST_TIME_FLOAT'])) {
      $timestamp = ' at ' . round(microtime(TRUE) - $_SERVER['REQUEST_TIME_FLOAT'], 5) * 1000 . 'ms';
    }
    $query_string = '/* ' . $extra_data . $this
      ->findCallerfunction() . $timestamp . ' */ ' . $query_string;
    $this->queryString = $query_string;
  }

  /**
   * Gets the query string of this statement.
   *
   * @return string
   *   The query string, in its form with placeholders.
   */
  public function getQueryString() {
    return $this->queryString;
  }

  /**
   * Determine the routine that called this query.
   *
   * We define "the routine that called this query" as the first entry in
   * the call stack that is not inside includes/database and does have a file
   * (which excludes call_user_func_array(), anonymous functions and similar).
   * That makes the climbing logic very simple, and handles the variable stack
   * depth caused by the query builders. Also makes sure this is not from the
   * cache api.
   *
   * @link http://www.php.net/debug_backtrace
   *
   * @return string
   *   This method returns a stack trace entry similar to that generated by
   *   debug_backtrace(). However, it flattens the trace entry and the trace
   *   entry before it so that we get the function and args of the function that
   *   called into the database system, not the function and args of the
   *   database call itself.
   */
  public function findCallerfunction() {
    $stack = debug_backtrace();
    $stack_count = count($stack);
    for ($i = 0; $i < $stack_count; ++$i) {
      if (!empty($stack[$i]['file']) && strpos($stack[$i]['file'], 'includes' . DIRECTORY_SEPARATOR . 'database') === FALSE && strpos($stack[$i]['file'], 'includes' . DIRECTORY_SEPARATOR . 'cache') === FALSE && strpos($stack[$i]['file'], 'apdqc.cache.inc') === FALSE && strpos($stack[$i]['function'], '__construct') === FALSE && strpos($stack[$i + 1]['function'], '__construct') === FALSE && strpos($stack[$i]['function'], __FUNCTION__) === FALSE && strpos($stack[$i + 1]['function'], 'apdqc_get_db_object') === FALSE) {
        $stack[$i] += array(
          'args' => array(),
        );
        $full_stack = array();
        if (variable_get('apdqc_verbose_devel_output', APDQC_VERBOSE_DEVEL_OUTPUT)) {
          foreach ($stack as $trace) {
            if (!isset($trace['args'])) {
              $trace['args'] = 1;
            }
            if (isset($trace['class'])) {
              $full_stack[] = $trace['class'] . '::' . $trace['function'] . '(' . count($trace['args']) . ')';
            }
            else {
              $full_stack[] = $trace['function'] . '(' . count($trace['args']) . ')';
            }
          }
          $full_stack = array_slice($full_stack, 2, 30);
        }
        return 'Call from ' . $stack[$i + 1]['function'] . '()' . '.  ' . implode(' -> ', array_reverse($full_stack));
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ApdqcFakeDatabaseStatement::$dbh public property
ApdqcFakeDatabaseStatement::$queryString public property
ApdqcFakeDatabaseStatement::findCallerfunction public function Determine the routine that called this query.
ApdqcFakeDatabaseStatement::getQueryString public function Gets the query string of this statement. Overrides DatabaseStatementEmpty::getQueryString
ApdqcFakeDatabaseStatement::__construct public function Constructs a DrupalDatabaseCache object.
DatabaseStatementEmpty::current public function
DatabaseStatementEmpty::execute public function Executes a prepared statement Overrides DatabaseStatementInterface::execute
DatabaseStatementEmpty::fetch public function
DatabaseStatementEmpty::fetchAll function
DatabaseStatementEmpty::fetchAllAssoc public function Returns the result set as an associative array keyed by the given field. Overrides DatabaseStatementInterface::fetchAllAssoc
DatabaseStatementEmpty::fetchAllKeyed public function Returns the entire result set as a single associative array. Overrides DatabaseStatementInterface::fetchAllKeyed
DatabaseStatementEmpty::fetchAssoc public function Fetches the next row and returns it as an associative array. Overrides DatabaseStatementInterface::fetchAssoc
DatabaseStatementEmpty::fetchCol public function Returns an entire single column of a result set as an indexed array. Overrides DatabaseStatementInterface::fetchCol
DatabaseStatementEmpty::fetchField public function Returns a single field from the next record of a result set. Overrides DatabaseStatementInterface::fetchField
DatabaseStatementEmpty::fetchObject public function
DatabaseStatementEmpty::key public function
DatabaseStatementEmpty::next public function
DatabaseStatementEmpty::rewind public function
DatabaseStatementEmpty::rowCount public function Returns the number of rows affected by the last SQL statement. Overrides DatabaseStatementInterface::rowCount
DatabaseStatementEmpty::setFetchMode public function
DatabaseStatementEmpty::valid public function