Spruced up profiler, need to finish up tests
This commit is contained in:
parent
ee8bd63a08
commit
9e65b2cfc2
3 changed files with 31 additions and 43 deletions
|
@ -302,7 +302,7 @@ class Database extends Object
|
|||
* @param array $input_parameters optional key/values to be bound
|
||||
* @return integer ID of the last inserted row or sequence number
|
||||
*/
|
||||
public function execute($sql, $input_parameters = null)
|
||||
public function execute($sql, $input_parameters = null, $explain = false)
|
||||
{
|
||||
$this->open();
|
||||
|
||||
|
@ -314,20 +314,20 @@ class Database extends Object
|
|||
// Establishes if we're working on an EXPLAIN
|
||||
if ($this->config['pickles']['profiler'])
|
||||
{
|
||||
$explain = preg_match('/^SELECT /i', $sql);
|
||||
$explain_results = preg_match('/^SELECT /i', $sql);
|
||||
}
|
||||
else
|
||||
{
|
||||
$explain = false;
|
||||
$explain_results = false;
|
||||
}
|
||||
|
||||
// Executes a standard query
|
||||
if ($input_parameters === null)
|
||||
{
|
||||
// Explains the query
|
||||
if ($explain)
|
||||
if ($explain_results)
|
||||
{
|
||||
$explain = $this->fetch('EXPLAIN ' . $sql);
|
||||
$explain_results = $this->fetch('EXPLAIN ' . $sql, null, true);
|
||||
}
|
||||
|
||||
$start_time = microtime(true);
|
||||
|
@ -337,9 +337,9 @@ class Database extends Object
|
|||
else
|
||||
{
|
||||
// Explains the query
|
||||
if ($explain)
|
||||
if ($explain_results)
|
||||
{
|
||||
$explain = $this->fetch('EXPLAIN ' . $sql, $input_parameters);
|
||||
$explain_results = $this->fetch('EXPLAIN ' . $sql, $input_parameters, true);
|
||||
}
|
||||
|
||||
$start_time = microtime(true);
|
||||
|
@ -351,9 +351,15 @@ class Database extends Object
|
|||
$duration = $end_time - $start_time;
|
||||
|
||||
// Logs the information to the profiler
|
||||
if ($this->config['pickles']['profiler'])
|
||||
if ($this->config['pickles']['profiler'] && !$explain)
|
||||
{
|
||||
Profiler::logQuery($sql, $input_parameters, $explain, $duration);
|
||||
Profiler::query(
|
||||
$sql,
|
||||
$input_parameters,
|
||||
$this->results->fetchAll(\PDO::FETCH_ASSOC),
|
||||
$duration,
|
||||
$explain_results
|
||||
);
|
||||
}
|
||||
}
|
||||
else
|
||||
|
@ -372,13 +378,13 @@ class Database extends Object
|
|||
* @param string $return_type optional type of return set
|
||||
* @return mixed based on return type
|
||||
*/
|
||||
public function fetch($sql = null, $input_parameters = null)
|
||||
public function fetch($sql = null, $input_parameters = null, $explain = false)
|
||||
{
|
||||
$this->open();
|
||||
|
||||
if ($sql !== null)
|
||||
{
|
||||
$this->execute($sql, $input_parameters);
|
||||
$this->execute($sql, $input_parameters, $explain);
|
||||
}
|
||||
|
||||
// Pulls the results based on the type
|
||||
|
|
|
@ -73,10 +73,6 @@ class Profiler
|
|||
// Tidys the data by type
|
||||
switch ($data_type)
|
||||
{
|
||||
case 'array':
|
||||
$details = print_r($data, true);
|
||||
break;
|
||||
|
||||
case 'object':
|
||||
$details['class'] = get_class($data);
|
||||
|
||||
|
@ -93,7 +89,6 @@ class Profiler
|
|||
$data_type = $data_type;
|
||||
break;
|
||||
|
||||
case 'string':
|
||||
default:
|
||||
if ($type != false)
|
||||
{
|
||||
|
@ -114,17 +109,18 @@ class Profiler
|
|||
}
|
||||
|
||||
/**
|
||||
* Log Query
|
||||
* Query
|
||||
*
|
||||
* Serves as a wrapper to get query data to the log function
|
||||
*
|
||||
* @static
|
||||
* @param string $query the query being executed
|
||||
* @param array $input_parameters optional prepared statement data
|
||||
* @param array $explain EXPLAIN data for the query
|
||||
* @param array $results optional results of the query
|
||||
* @param float $duration the speed of the query
|
||||
* @param array $explain EXPLAIN data for the query
|
||||
*/
|
||||
public static function logQuery($query, $input_parameters = false, $explain = false, $duration = false)
|
||||
public static function query($query, $input_parameters = false, $results = false, $duration = false, $explain = false)
|
||||
{
|
||||
$log = [];
|
||||
|
||||
|
@ -156,7 +152,13 @@ class Profiler
|
|||
$log .= 'query_time: ' . $duration;
|
||||
*/
|
||||
|
||||
$log = [$query, $input_parameters, $explain, $duration];
|
||||
$log = [
|
||||
'query' => $query,
|
||||
'parameters' => $input_parameters,
|
||||
'results' => $results,
|
||||
'execution_time' => $duration,
|
||||
'explain' => $explain,
|
||||
];
|
||||
|
||||
self::log($log, false, 'database');
|
||||
}
|
||||
|
|
|
@ -9,26 +9,6 @@ class ProfilerTest extends PHPUnit_Framework_TestCase
|
|||
Pickles\Profiler::report();
|
||||
}
|
||||
|
||||
public function testDisabledType()
|
||||
{
|
||||
$config = Pickles\Config::getInstance();
|
||||
$config->data['pickles']['profiler'] = false;
|
||||
|
||||
$this->assertFalse(Pickles\Profiler::enabled('timers'));
|
||||
}
|
||||
|
||||
public function testTimerDisabled()
|
||||
{
|
||||
$this->assertFalse(Pickles\Profiler::timer('disabled'));
|
||||
}
|
||||
|
||||
public function testReportNothing()
|
||||
{
|
||||
$this->expectOutputRegex('/There is nothing to profile/');
|
||||
|
||||
Pickles\Profiler::report();
|
||||
}
|
||||
|
||||
public function testEnabled()
|
||||
{
|
||||
$config = Pickles\Config::getInstance();
|
||||
|
@ -67,9 +47,9 @@ class ProfilerTest extends PHPUnit_Framework_TestCase
|
|||
],
|
||||
];
|
||||
|
||||
Pickles\Profiler::logQuery('SELECT * FROM table;');
|
||||
Pickles\Profiler::logQuery('SELECT * FROM table WHERE column = ?;', ['foo']);
|
||||
Pickles\Profiler::logQuery('SELECT * FROM table;', false, $explain);
|
||||
Pickles\Profiler::query('SELECT * FROM table;');
|
||||
Pickles\Profiler::query('SELECT * FROM table WHERE column = ?;', ['foo']);
|
||||
Pickles\Profiler::query('SELECT * FROM table;', false, $explain);
|
||||
}
|
||||
|
||||
public function testTimer()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue