mirror of
https://github.com/dg/dibi.git
synced 2025-08-06 14:16:39 +02:00
Result::dump() moved to Helpers
This commit is contained in:
@@ -24,8 +24,47 @@ class Helpers
|
|||||||
public static function dump($sql = NULL, $return = FALSE)
|
public static function dump($sql = NULL, $return = FALSE)
|
||||||
{
|
{
|
||||||
ob_start();
|
ob_start();
|
||||||
if ($sql instanceof Result) {
|
if ($sql instanceof Result && PHP_SAPI === 'cli') {
|
||||||
$sql->dump();
|
$hasColors = (substr(getenv('TERM'), 0, 5) === 'xterm');
|
||||||
|
$maxLen = 0;
|
||||||
|
foreach ($sql as $i => $row) {
|
||||||
|
if ($i === 0) {
|
||||||
|
foreach ($row as $col => $foo) {
|
||||||
|
$len = mb_strlen($col);
|
||||||
|
$maxLen = max($len, $maxLen);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
echo $hasColors ? "\033[1;37m#row: $i\033[0m\n" : "#row: $i\n";
|
||||||
|
foreach ($row as $col => $val) {
|
||||||
|
$spaces = $maxLen - mb_strlen($col) + 2;
|
||||||
|
echo "$col" . str_repeat(' ', $spaces) . "$val\n";
|
||||||
|
}
|
||||||
|
echo "\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
echo empty($row) ? "empty result set\n\n" : "\n";
|
||||||
|
|
||||||
|
} elseif ($sql instanceof Result) {
|
||||||
|
foreach ($sql as $i => $row) {
|
||||||
|
if ($i === 0) {
|
||||||
|
echo "\n<table class=\"dump\">\n<thead>\n\t<tr>\n\t\t<th>#row</th>\n";
|
||||||
|
foreach ($row as $col => $foo) {
|
||||||
|
echo "\t\t<th>" . htmlSpecialChars($col) . "</th>\n";
|
||||||
|
}
|
||||||
|
echo "\t</tr>\n</thead>\n<tbody>\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
echo "\t<tr>\n\t\t<th>", $i, "</th>\n";
|
||||||
|
foreach ($row as $col) {
|
||||||
|
echo "\t\t<td>", htmlSpecialChars($col), "</td>\n";
|
||||||
|
}
|
||||||
|
echo "\t</tr>\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
echo empty($row)
|
||||||
|
? '<p><em>empty result set</em></p>'
|
||||||
|
: "</tbody>\n</table>\n";
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
if ($sql === NULL) {
|
if ($sql === NULL) {
|
||||||
|
@@ -614,66 +614,7 @@ class Result implements IDataSource
|
|||||||
*/
|
*/
|
||||||
final public function dump()
|
final public function dump()
|
||||||
{
|
{
|
||||||
$i = 0;
|
echo Helpers::dump($this);
|
||||||
$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);
|
|
||||||
$maxLen = max($len, $maxLen);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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()) {
|
|
||||||
if ($i === 0) {
|
|
||||||
echo "\n<table class=\"dump\">\n<thead>\n\t<tr>\n\t\t<th>#row</th>\n";
|
|
||||||
|
|
||||||
foreach ($row as $col => $foo) {
|
|
||||||
echo "\t\t<th>" . htmlSpecialChars($col) . "</th>\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
echo "\t</tr>\n</thead>\n<tbody>\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
echo "\t<tr>\n\t\t<th>", $i, "</th>\n";
|
|
||||||
foreach ($row as $col) {
|
|
||||||
//if (is_object($col)) $col = $col->__toString();
|
|
||||||
echo "\t\t<td>", htmlSpecialChars($col), "</td>\n";
|
|
||||||
}
|
|
||||||
echo "\t</tr>\n";
|
|
||||||
$i++;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($i === 0) {
|
|
||||||
echo '<p><em>empty result set</em></p>';
|
|
||||||
} else {
|
|
||||||
echo "</tbody>\n</table>\n";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user