/** * Collect all the executed queries by now. */ public function collect() { // Get all SQL output $queries = []; $output = $this->logger->grep(' '); $queries = array(); foreach ($output as $key => $value) { // Clean all "resuldsets" outputs if (substr($value, 0, 9) == 'resultset') { unset($output[$key]); } else { if (!self::$showKeepCache) { $value = str_replace('-- keep-cache', '', $value); } $queries[] = array('sql' => strip_tags(preg_replace('!\\s+!', ' ', $value))); } } return array('nb_statements' => count($queries), 'statements' => $queries); }
/** * This method runs the actual SQL query and binds a list of parameters to the query. * slots. The result of the query will be stored in the protected property * $rs (always array). The number of rows affected (result of rowcount, if supported by database) * is stored in protected property $affectedRows. If the debug flag is set * this function will send debugging output to screen buffer. * * @param string $sql the SQL string to be send to database server * @param array $bindings the values that need to get bound to the query slots * * @return void */ protected function runQuery($sql, $bindings, $options = array()) { $this->connect(); if ($this->loggingEnabled && $this->logger) { $this->logger->log($sql, $bindings); } try { if (strpos('pgsql', $this->dsn) === 0) { if (defined('\\PDO::PGSQL_ATTR_DISABLE_NATIVE_PREPARED_STATEMENT')) { $statement = $this->pdo->prepare($sql, array(\PDO::PGSQL_ATTR_DISABLE_NATIVE_PREPARED_STATEMENT => TRUE)); } else { $statement = $this->pdo->prepare($sql); } } else { $statement = $this->pdo->prepare($sql); } $this->bindParams($statement, $bindings); $statement->execute(); $this->queryCounter++; $this->affectedRows = $statement->rowCount(); if ($statement->columnCount()) { $fetchStyle = isset($options['fetchStyle']) ? $options['fetchStyle'] : NULL; if (isset($options['noFetch']) && $options['noFetch']) { $this->resultArray = array(); return $statement; } $this->resultArray = $statement->fetchAll($fetchStyle); if ($this->loggingEnabled && $this->logger) { $this->logger->log('resultset: ' . count($this->resultArray) . ' rows'); } } else { $this->resultArray = array(); } } catch (\PDOException $e) { //Unfortunately the code field is supposed to be int by default (php) //So we need a property to convey the SQL State code. $err = $e->getMessage(); if ($this->loggingEnabled && $this->logger) { $this->logger->log('An error occurred: ' . $err); } $exception = new SQL($err, 0); $exception->setSQLState($e->getCode()); throw $exception; } }