Skip to content
This repository has been archived by the owner on May 11, 2021. It is now read-only.

[refactor] introduce debug classes under new structure #213

Merged
merged 7 commits into from
Jun 19, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions docs/advanced.rst
Original file line number Diff line number Diff line change
Expand Up @@ -98,19 +98,19 @@ a proxy connection object that will collect the necessary statistics and
"echo" them out.

If you would like to do something else with these statistics, you can set
a callback. For Dumper::
a callback. For Stopwatch::

$c->callback = function($expression, $time, $fail = false) {
...
}

and for Counter::
and for Profiler::

$c->callback = function($queries, $selects, $rows, $expressions, $fail = false) {
...
}

If you have used "dumper:counter:", then use this::
If you have used "stopwatch:profile:", then use this::

$c->callback = function($expression, $time, $fail = false) {
...
Expand Down
14 changes: 7 additions & 7 deletions docs/extensions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ MSSQL Untested mssql: native, PDO
Other Interesting Drivers
-------------------------

===================== ========= ======== ============
Class Support PDO Dependency
===================== ========= ======== ============
Connection_Dumper Full dumper: native, Proxy
Connection_Counter Full counter: native, Proxy
===================== ========= ======== ============
=========================== ========= ========== ============
Class Support PDO Dependency
=========================== ========= ========== ============
Debug\Stopwatch\Driver Full stopwatch: native, Proxy
Debug\Profiler\Driver Full profile: native, Proxy
=========================== ========= ========== ============


3rd party vendor support
-------------------------
------------------------

===================== ========= ========= ============================
Class Support PDO Dependency
Expand Down
10 changes: 6 additions & 4 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,14 +195,16 @@ public static function connect($dsn, $user = null, $password = null, $args = [])
], $args));

break;
case 'dumper':
$c = new Connection_Dumper(array_merge([
case 'dumper': // deprecated - will be removed in v2.3
case 'stopwatch':
$c = new Debug\Stopwatch\Driver(array_merge([
'connection' => static::connect($dsn['rest'], $dsn['user'], $dsn['pass']),
], $args));

break;
case 'counter':
$c = new Connection_Counter(array_merge([
case 'counter': // deprecated - will be removed in v2.3
case 'profile':
$c = new Debug\Profiler\Driver(array_merge([
'connection' => static::connect($dsn['rest'], $dsn['user'], $dsn['pass']),
], $args));

Expand Down
100 changes: 4 additions & 96 deletions src/Connection_Counter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,101 +4,9 @@

namespace atk4\dsql;

class Connection_Counter extends Connection_Proxy
/**
* @deprecated will be removed in v2.3
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I propose to write here: "will be removed in 2020-dec"

It is much easier for users to know when it will be removed and when to remove it in the code.

(update in everywhere)

*/
class Connection_Counter extends Debug\Profiler\Driver
{
/**
* Callable to call for outputting.
*
* Will receive parameters:
* - int Count of executed queries
* - int Count of executed selects
* - int Count of rows iterated
* - int Count of executed expressions
* - boolean True if we had exception while executing expression
*
* @var callable
*/
public $callback;

/** @var int Count of executed selects */
protected $selects = 0;

/** @var int Count of executed queries */
protected $queries = 0;

/** @var int Count of executed expressions */
protected $expressions = 0;

/** @var int Count of rows iterated */
protected $rows = 0;

/**
* Iterate (yield) array.
*
* @param array $ret
*
* @return mixed
*/
public function iterate($ret)
{
foreach ($ret as $key => $row) {
++$this->rows;
yield $key => $row;
}
}

/**
* Execute expression.
*
* @return mixed
*/
public function execute(Expression $expr)
{
if ($expr instanceof Query) {
++$this->queries;
if ($expr->mode === 'select' || $expr->mode === null) {
++$this->selects;
}
} else {
++$this->expressions;
}

try {
$ret = parent::execute($expr);
} catch (\Exception $e) {
if ($this->callback && is_callable($this->callback)) {
call_user_func($this->callback, $this->queries, $this->selects, $this->rows, $this->expressions, true);
} else {
printf(
"[ERROR] Queries: %3d, Selects: %3d, Rows fetched: %4d, Expressions %3d\n",
$this->queries,
$this->selects,
$this->rows,
$this->expressions
);
}

throw $e;
}

return $this->iterate($ret);
}

/**
* Log results when destructing.
*/
public function __destruct()
{
if ($this->callback && is_callable($this->callback)) {
call_user_func($this->callback, $this->queries, $this->selects, $this->rows, $this->expressions, false);
} else {
printf(
"Queries: %3d, Selects: %3d, Rows fetched: %4d, Expressions %3d\n",
$this->queries,
$this->selects,
$this->rows,
$this->expressions
);
}
}
}
54 changes: 4 additions & 50 deletions src/Connection_Dumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,55 +4,9 @@

namespace atk4\dsql;

class Connection_Dumper extends Connection_Proxy
/**
* @deprecated will be removed in v2.3
*/
class Connection_Dumper extends Debug\Stopwatch\Driver
{
/**
* Callable to call for outputting.
*
* Will receive parameters:
* - Expression Expression object
* - float How long it took to execute expression
* - boolean True if we had exception while executing expression
*
* @var callable
*/
public $callback;

/**
* @var float
*/
protected $start_time;

/**
* Execute expression.
*
* @return \PDOStatement
*/
public function execute(Expression $expr)
{
$this->start_time = microtime(true);

try {
$ret = parent::execute($expr);
$took = microtime(true) - $this->start_time;

if ($this->callback && is_callable($this->callback)) {
call_user_func($this->callback, $expr, $took, false);
} else {
printf("[%02.6f] %s\n", $took, $expr->getDebugQuery());
}
} catch (\Exception $e) {
$took = microtime(true) - $this->start_time;

if ($this->callback && is_callable($this->callback)) {
call_user_func($this->callback, $expr, $took, true);
} else {
printf("[ERROR %02.6f] %s\n", $took, $expr->getDebugQuery());
}

throw $e;
}

return $ret;
}
}
116 changes: 116 additions & 0 deletions src/Debug/Profiler/Driver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php

declare(strict_types=1);

namespace atk4\dsql\Debug\Profiler;

use atk4\dsql\Connection_Proxy;
use atk4\dsql\Expression;
use atk4\dsql\Query;

class Driver extends Connection_Proxy
{
public $driverType = 'profile';

/**
* Callable to call for outputting.
*
* Will receive parameters:
* - int Count of executed queries
* - int Count of executed selects
* - int Count of rows iterated
* - int Count of executed expressions
* - boolean True if we had exception while executing expression
*
* @var callable
*/
public $callback;

/** @var int Count of executed selects */
protected $selects = 0;

/** @var int Count of executed queries */
protected $queries = 0;

/** @var int Count of executed expressions */
protected $expressions = 0;

/** @var int Count of rows iterated */
protected $rows = 0;

public static function createDriver(array $dsn)
{
return static::create($dsn['rest'], $dsn['user'], $dsn['pass']);
}

/**
* Iterate (yield) array.
*
* @param array $ret
*
* @return mixed
*/
public function iterate($ret)
{
foreach ($ret as $key => $row) {
++$this->rows;
yield $key => $row;
}
}

/**
* Execute expression.
*
* @return mixed
*/
public function execute(Expression $expr)
{
if ($expr instanceof Query) {
++$this->queries;
if ($expr->mode === 'select' || $expr->mode === null) {
++$this->selects;
}
} else {
++$this->expressions;
}

try {
$ret = parent::execute($expr);
} catch (\Exception $e) {
if ($this->callback && is_callable($this->callback)) {
call_user_func($this->callback, $this->queries, $this->selects, $this->rows, $this->expressions, true);
} else {
$this->dump(true);
}

throw $e;
}

return $this->iterate($ret);
}

/**
* Log results when destructing.
*/
public function __destruct()
{
if ($this->callback && is_callable($this->callback)) {
call_user_func($this->callback, $this->queries, $this->selects, $this->rows, $this->expressions, false);
} else {
$this->dump();
}
}

protected function dump($error = false)
{
$error = $error ? 'ERROR ' : '';

printf(
"{$error}Queries: %3d, Selects: %3d, Rows fetched: %4d, Expressions %3d\n",
$this->queries,
$this->selects,
$this->rows,
$this->expressions
);
}
}
Loading