This repository has been archived by the owner on May 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
[refactor] introduce debug classes under new structure #213
Merged
Merged
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ca0f370
[refactor] introduce debug classes under new structure
georgehristov 7d05954
[fix] run CS Fixer
georgehristov 9f7efba
[update] rename Driver to Connection for naming consistency
georgehristov 640dc0d
[update] tests
georgehristov 34c5409
[update] docs
georgehristov a7b392b
[update] remove future needed method
georgehristov b9ebadd
[fix] error dump notation
georgehristov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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)