1
0
mirror of https://github.com/dg/dibi.git synced 2025-08-06 06:07:39 +02:00

DibiResult: added simple dump in CLI mode

This commit is contained in:
Jakub Krčma
2012-10-03 22:21:38 +02:00
parent f355b8ede7
commit 6ced2d3af2

View File

@@ -636,13 +636,43 @@ class DibiResult extends DibiObject implements IDataSource
/** /**
* Displays complete result set as HTML table for debug purposes. * Displays complete result set as HTML or text table for debug purposes.
* @return void * @return void
*/ */
final public function dump() final public function dump()
{ {
$i = 0; $i = 0;
$this->seek(0); $this->seek(0);
if (PHP_SAPI === 'cli') {
$hasColors = (substr(getenv('TERM'), 0, 5) === 'xterm');
$maxLen = 0;
while ($row = $this->fetch()) {
if ($i === 0) {
foreach ($row as $col => $foo) {
$len = mb_strlen($col);
if ($len > $maxLen) $maxLen = $len;
}
}
if ($hasColors) {
echo "\033[1;37m#row: $i\033[0m\n";
} else {
echo "#row: $i\n";
}
foreach ($row as $col => $val) {
$spaces = $maxLen - mb_strlen($col) + 2;
echo "$col" . str_repeat(" ", $spaces) . "$val\n";
}
echo "\n";
$i++;
}
if ($i === 0) echo "empty result set\n";
echo "\n";
} else {
while ($row = $this->fetch()) { while ($row = $this->fetch()) {
if ($i === 0) { if ($i === 0) {
echo "\n<table class=\"dump\">\n<thead>\n\t<tr>\n\t\t<th>#row</th>\n"; echo "\n<table class=\"dump\">\n<thead>\n\t<tr>\n\t\t<th>#row</th>\n";
@@ -669,5 +699,6 @@ class DibiResult extends DibiObject implements IDataSource
echo "</tbody>\n</table>\n"; echo "</tbody>\n</table>\n";
} }
} }
}
} }