mirror of
https://github.com/dg/dibi.git
synced 2025-08-04 13:17:58 +02:00
dibi::dump() moved to class Helpers
This commit is contained in:
@@ -65,7 +65,7 @@ class DibiNettePanel extends DibiObject implements Nette\Diagnostics\IBarPanel
|
|||||||
if ($e instanceof DibiException && $e->getSql()) {
|
if ($e instanceof DibiException && $e->getSql()) {
|
||||||
return [
|
return [
|
||||||
'tab' => 'SQL',
|
'tab' => 'SQL',
|
||||||
'panel' => dibi::dump($e->getSql(), TRUE),
|
'panel' => DibiHelpers::dump($e->getSql(), TRUE),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -104,7 +104,7 @@ class DibiNettePanel extends DibiObject implements Nette\Diagnostics\IBarPanel
|
|||||||
$backup = [$event->connection->onEvent, dibi::$numOfQueries, dibi::$totalTime];
|
$backup = [$event->connection->onEvent, dibi::$numOfQueries, dibi::$totalTime];
|
||||||
$event->connection->onEvent = NULL;
|
$event->connection->onEvent = NULL;
|
||||||
$cmd = is_string($this->explain) ? $this->explain : ($event->connection->getConfig('driver') === 'oracle' ? 'EXPLAIN PLAN FOR' : 'EXPLAIN');
|
$cmd = is_string($this->explain) ? $this->explain : ($event->connection->getConfig('driver') === 'oracle' ? 'EXPLAIN PLAN FOR' : 'EXPLAIN');
|
||||||
$explain = dibi::dump($event->connection->nativeQuery("$cmd $event->sql"), TRUE);
|
$explain = DibiHelpers::dump($event->connection->nativeQuery("$cmd $event->sql"), TRUE);
|
||||||
} catch (DibiException $e) {
|
} catch (DibiException $e) {
|
||||||
}
|
}
|
||||||
list($event->connection->onEvent, dibi::$numOfQueries, dibi::$totalTime) = $backup;
|
list($event->connection->onEvent, dibi::$numOfQueries, dibi::$totalTime) = $backup;
|
||||||
@@ -117,7 +117,7 @@ class DibiNettePanel extends DibiObject implements Nette\Diagnostics\IBarPanel
|
|||||||
$s .= "<br /><a href='#nette-debug-DibiProfiler-row-$counter' class='nette-toggler nette-toggle-collapsed' rel='#nette-debug-DibiProfiler-row-$counter'>explain</a>";
|
$s .= "<br /><a href='#nette-debug-DibiProfiler-row-$counter' class='nette-toggler nette-toggle-collapsed' rel='#nette-debug-DibiProfiler-row-$counter'>explain</a>";
|
||||||
}
|
}
|
||||||
|
|
||||||
$s .= '</td><td class="nette-DibiProfiler-sql">' . dibi::dump(strlen($event->sql) > self::$maxLength ? substr($event->sql, 0, self::$maxLength) . '...' : $event->sql, TRUE);
|
$s .= '</td><td class="nette-DibiProfiler-sql">' . DibiHelpers::dump(strlen($event->sql) > self::$maxLength ? substr($event->sql, 0, self::$maxLength) . '...' : $event->sql, TRUE);
|
||||||
if ($explain) {
|
if ($explain) {
|
||||||
$s .= "<div id='nette-debug-DibiProfiler-row-$counter' class='nette-collapsed'>{$explain}</div>";
|
$s .= "<div id='nette-debug-DibiProfiler-row-$counter' class='nette-collapsed'>{$explain}</div>";
|
||||||
}
|
}
|
||||||
|
@@ -9,6 +9,7 @@ namespace Dibi\Bridges\Tracy;
|
|||||||
|
|
||||||
use dibi;
|
use dibi;
|
||||||
use Tracy;
|
use Tracy;
|
||||||
|
use DibiHelpers;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -67,7 +68,7 @@ class Panel extends \DibiObject implements Tracy\IBarPanel
|
|||||||
if ($e instanceof \DibiException && $e->getSql()) {
|
if ($e instanceof \DibiException && $e->getSql()) {
|
||||||
return [
|
return [
|
||||||
'tab' => 'SQL',
|
'tab' => 'SQL',
|
||||||
'panel' => dibi::dump($e->getSql(), TRUE),
|
'panel' => DibiHelpers::dump($e->getSql(), TRUE),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -107,7 +108,7 @@ class Panel extends \DibiObject implements Tracy\IBarPanel
|
|||||||
$backup = [$event->connection->onEvent, dibi::$numOfQueries, dibi::$totalTime];
|
$backup = [$event->connection->onEvent, dibi::$numOfQueries, dibi::$totalTime];
|
||||||
$event->connection->onEvent = NULL;
|
$event->connection->onEvent = NULL;
|
||||||
$cmd = is_string($this->explain) ? $this->explain : ($event->connection->getConfig('driver') === 'oracle' ? 'EXPLAIN PLAN FOR' : 'EXPLAIN');
|
$cmd = is_string($this->explain) ? $this->explain : ($event->connection->getConfig('driver') === 'oracle' ? 'EXPLAIN PLAN FOR' : 'EXPLAIN');
|
||||||
$explain = dibi::dump($event->connection->nativeQuery("$cmd $event->sql"), TRUE);
|
$explain = DibiHelpers::dump($event->connection->nativeQuery("$cmd $event->sql"), TRUE);
|
||||||
} catch (\DibiException $e) {
|
} catch (\DibiException $e) {
|
||||||
}
|
}
|
||||||
list($event->connection->onEvent, dibi::$numOfQueries, dibi::$totalTime) = $backup;
|
list($event->connection->onEvent, dibi::$numOfQueries, dibi::$totalTime) = $backup;
|
||||||
@@ -120,7 +121,7 @@ class Panel extends \DibiObject implements Tracy\IBarPanel
|
|||||||
$s .= "<br /><a href='#tracy-debug-DibiProfiler-row-$counter' class='tracy-toggle tracy-collapsed' rel='#tracy-debug-DibiProfiler-row-$counter'>explain</a>";
|
$s .= "<br /><a href='#tracy-debug-DibiProfiler-row-$counter' class='tracy-toggle tracy-collapsed' rel='#tracy-debug-DibiProfiler-row-$counter'>explain</a>";
|
||||||
}
|
}
|
||||||
|
|
||||||
$s .= '</td><td class="tracy-DibiProfiler-sql">' . dibi::dump(strlen($event->sql) > self::$maxLength ? substr($event->sql, 0, self::$maxLength) . '...' : $event->sql, TRUE);
|
$s .= '</td><td class="tracy-DibiProfiler-sql">' . DibiHelpers::dump(strlen($event->sql) > self::$maxLength ? substr($event->sql, 0, self::$maxLength) . '...' : $event->sql, TRUE);
|
||||||
if ($explain) {
|
if ($explain) {
|
||||||
$s .= "<div id='tracy-debug-DibiProfiler-row-$counter' class='tracy-collapsed'>{$explain}</div>";
|
$s .= "<div id='tracy-debug-DibiProfiler-row-$counter' class='tracy-collapsed'>{$explain}</div>";
|
||||||
}
|
}
|
||||||
|
@@ -260,12 +260,12 @@ class DibiConnection extends DibiObject
|
|||||||
{
|
{
|
||||||
$args = func_get_args();
|
$args = func_get_args();
|
||||||
try {
|
try {
|
||||||
dibi::dump($this->translateArgs($args));
|
DibiHelpers::dump($this->translateArgs($args));
|
||||||
return TRUE;
|
return TRUE;
|
||||||
|
|
||||||
} catch (DibiException $e) {
|
} catch (DibiException $e) {
|
||||||
if ($e->getSql()) {
|
if ($e->getSql()) {
|
||||||
dibi::dump($e->getSql());
|
DibiHelpers::dump($e->getSql());
|
||||||
} else {
|
} else {
|
||||||
echo get_class($e) . ': ' . $e->getMessage() . (PHP_SAPI === 'cli' ? "\n" : '<br>');
|
echo get_class($e) . ': ' . $e->getMessage() . (PHP_SAPI === 'cli' ? "\n" : '<br>');
|
||||||
}
|
}
|
||||||
|
@@ -12,6 +12,85 @@
|
|||||||
class DibiHelpers
|
class DibiHelpers
|
||||||
{
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Prints out a syntax highlighted version of the SQL command or DibiResult.
|
||||||
|
* @param string|DibiResult
|
||||||
|
* @param bool return output instead of printing it?
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public static function dump($sql = NULL, $return = FALSE)
|
||||||
|
{
|
||||||
|
ob_start();
|
||||||
|
if ($sql instanceof DibiResult) {
|
||||||
|
$sql->dump();
|
||||||
|
|
||||||
|
} else {
|
||||||
|
if ($sql === NULL) {
|
||||||
|
$sql = dibi::$sql;
|
||||||
|
}
|
||||||
|
|
||||||
|
static $keywords1 = 'SELECT|(?:ON\s+DUPLICATE\s+KEY)?UPDATE|INSERT(?:\s+INTO)?|REPLACE(?:\s+INTO)?|DELETE|CALL|UNION|FROM|WHERE|HAVING|GROUP\s+BY|ORDER\s+BY|LIMIT|OFFSET|FETCH\s+NEXT|SET|VALUES|LEFT\s+JOIN|INNER\s+JOIN|TRUNCATE|START\s+TRANSACTION|BEGIN|COMMIT|ROLLBACK(?:\s+TO\s+SAVEPOINT)?|(?:RELEASE\s+)?SAVEPOINT';
|
||||||
|
static $keywords2 = 'ALL|DISTINCT|DISTINCTROW|IGNORE|AS|USING|ON|AND|OR|IN|IS|NOT|NULL|LIKE|RLIKE|REGEXP|TRUE|FALSE';
|
||||||
|
|
||||||
|
// insert new lines
|
||||||
|
$sql = " $sql ";
|
||||||
|
$sql = preg_replace("#(?<=[\\s,(])($keywords1)(?=[\\s,)])#i", "\n\$1", $sql);
|
||||||
|
|
||||||
|
// reduce spaces
|
||||||
|
$sql = preg_replace('#[ \t]{2,}#', ' ', $sql);
|
||||||
|
|
||||||
|
$sql = wordwrap($sql, 100);
|
||||||
|
$sql = preg_replace("#([ \t]*\r?\n){2,}#", "\n", $sql);
|
||||||
|
|
||||||
|
// syntax highlight
|
||||||
|
$highlighter = "#(/\\*.+?\\*/)|(\\*\\*.+?\\*\\*)|(?<=[\\s,(])($keywords1)(?=[\\s,)])|(?<=[\\s,(=])($keywords2)(?=[\\s,)=])#is";
|
||||||
|
if (PHP_SAPI === 'cli') {
|
||||||
|
if (substr(getenv('TERM'), 0, 5) === 'xterm') {
|
||||||
|
$sql = preg_replace_callback($highlighter, function ($m) {
|
||||||
|
if (!empty($m[1])) { // comment
|
||||||
|
return "\033[1;30m" . $m[1] . "\033[0m";
|
||||||
|
|
||||||
|
} elseif (!empty($m[2])) { // error
|
||||||
|
return "\033[1;31m" . $m[2] . "\033[0m";
|
||||||
|
|
||||||
|
} elseif (!empty($m[3])) { // most important keywords
|
||||||
|
return "\033[1;34m" . $m[3] . "\033[0m";
|
||||||
|
|
||||||
|
} elseif (!empty($m[4])) { // other keywords
|
||||||
|
return "\033[1;32m" . $m[4] . "\033[0m";
|
||||||
|
}
|
||||||
|
}, $sql);
|
||||||
|
}
|
||||||
|
echo trim($sql) . "\n\n";
|
||||||
|
|
||||||
|
} else {
|
||||||
|
$sql = htmlSpecialChars($sql);
|
||||||
|
$sql = preg_replace_callback($highlighter, function ($m) {
|
||||||
|
if (!empty($m[1])) { // comment
|
||||||
|
return '<em style="color:gray">' . $m[1] . '</em>';
|
||||||
|
|
||||||
|
} elseif (!empty($m[2])) { // error
|
||||||
|
return '<strong style="color:red">' . $m[2] . '</strong>';
|
||||||
|
|
||||||
|
} elseif (!empty($m[3])) { // most important keywords
|
||||||
|
return '<strong style="color:blue">' . $m[3] . '</strong>';
|
||||||
|
|
||||||
|
} elseif (!empty($m[4])) { // other keywords
|
||||||
|
return '<strong style="color:green">' . $m[4] . '</strong>';
|
||||||
|
}
|
||||||
|
}, $sql);
|
||||||
|
echo '<pre class="dump">', trim($sql), "</pre>\n\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($return) {
|
||||||
|
return ob_get_clean();
|
||||||
|
} else {
|
||||||
|
ob_end_flush();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/** @internal */
|
/** @internal */
|
||||||
public static function escape($driver, $value, $type)
|
public static function escape($driver, $value, $type)
|
||||||
{
|
{
|
||||||
|
@@ -51,9 +51,6 @@ class dibi
|
|||||||
/** @var DibiConnection Current connection */
|
/** @var DibiConnection Current connection */
|
||||||
private static $connection;
|
private static $connection;
|
||||||
|
|
||||||
/** @var array @see addHandler */
|
|
||||||
private static $handlers = [];
|
|
||||||
|
|
||||||
/** @var string Last SQL command @see dibi::query() */
|
/** @var string Last SQL command @see dibi::query() */
|
||||||
public static $sql;
|
public static $sql;
|
||||||
|
|
||||||
@@ -459,74 +456,7 @@ class dibi
|
|||||||
*/
|
*/
|
||||||
public static function dump($sql = NULL, $return = FALSE)
|
public static function dump($sql = NULL, $return = FALSE)
|
||||||
{
|
{
|
||||||
ob_start();
|
return DibiHelpers::dump($sql, $return);
|
||||||
if ($sql instanceof DibiResult) {
|
|
||||||
$sql->dump();
|
|
||||||
|
|
||||||
} else {
|
|
||||||
if ($sql === NULL) {
|
|
||||||
$sql = self::$sql;
|
|
||||||
}
|
|
||||||
|
|
||||||
static $keywords1 = 'SELECT|(?:ON\s+DUPLICATE\s+KEY)?UPDATE|INSERT(?:\s+INTO)?|REPLACE(?:\s+INTO)?|DELETE|CALL|UNION|FROM|WHERE|HAVING|GROUP\s+BY|ORDER\s+BY|LIMIT|OFFSET|FETCH\s+NEXT|SET|VALUES|LEFT\s+JOIN|INNER\s+JOIN|TRUNCATE|START\s+TRANSACTION|BEGIN|COMMIT|ROLLBACK(?:\s+TO\s+SAVEPOINT)?|(?:RELEASE\s+)?SAVEPOINT';
|
|
||||||
static $keywords2 = 'ALL|DISTINCT|DISTINCTROW|IGNORE|AS|USING|ON|AND|OR|IN|IS|NOT|NULL|LIKE|RLIKE|REGEXP|TRUE|FALSE';
|
|
||||||
|
|
||||||
// insert new lines
|
|
||||||
$sql = " $sql ";
|
|
||||||
$sql = preg_replace("#(?<=[\\s,(])($keywords1)(?=[\\s,)])#i", "\n\$1", $sql);
|
|
||||||
|
|
||||||
// reduce spaces
|
|
||||||
$sql = preg_replace('#[ \t]{2,}#', ' ', $sql);
|
|
||||||
|
|
||||||
$sql = wordwrap($sql, 100);
|
|
||||||
$sql = preg_replace("#([ \t]*\r?\n){2,}#", "\n", $sql);
|
|
||||||
|
|
||||||
// syntax highlight
|
|
||||||
$highlighter = "#(/\\*.+?\\*/)|(\\*\\*.+?\\*\\*)|(?<=[\\s,(])($keywords1)(?=[\\s,)])|(?<=[\\s,(=])($keywords2)(?=[\\s,)=])#is";
|
|
||||||
if (PHP_SAPI === 'cli') {
|
|
||||||
if (substr(getenv('TERM'), 0, 5) === 'xterm') {
|
|
||||||
$sql = preg_replace_callback($highlighter, function ($m) {
|
|
||||||
if (!empty($m[1])) { // comment
|
|
||||||
return "\033[1;30m" . $m[1] . "\033[0m";
|
|
||||||
|
|
||||||
} elseif (!empty($m[2])) { // error
|
|
||||||
return "\033[1;31m" . $m[2] . "\033[0m";
|
|
||||||
|
|
||||||
} elseif (!empty($m[3])) { // most important keywords
|
|
||||||
return "\033[1;34m" . $m[3] . "\033[0m";
|
|
||||||
|
|
||||||
} elseif (!empty($m[4])) { // other keywords
|
|
||||||
return "\033[1;32m" . $m[4] . "\033[0m";
|
|
||||||
}
|
|
||||||
}, $sql);
|
|
||||||
}
|
|
||||||
echo trim($sql) . "\n\n";
|
|
||||||
|
|
||||||
} else {
|
|
||||||
$sql = htmlSpecialChars($sql);
|
|
||||||
$sql = preg_replace_callback($highlighter, function ($m) {
|
|
||||||
if (!empty($m[1])) { // comment
|
|
||||||
return '<em style="color:gray">' . $m[1] . '</em>';
|
|
||||||
|
|
||||||
} elseif (!empty($m[2])) { // error
|
|
||||||
return '<strong style="color:red">' . $m[2] . '</strong>';
|
|
||||||
|
|
||||||
} elseif (!empty($m[3])) { // most important keywords
|
|
||||||
return '<strong style="color:blue">' . $m[3] . '</strong>';
|
|
||||||
|
|
||||||
} elseif (!empty($m[4])) { // other keywords
|
|
||||||
return '<strong style="color:green">' . $m[4] . '</strong>';
|
|
||||||
}
|
|
||||||
}, $sql);
|
|
||||||
echo '<pre class="dump">', trim($sql), "</pre>\n\n";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($return) {
|
|
||||||
return ob_get_clean();
|
|
||||||
} else {
|
|
||||||
ob_end_flush();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user