From c4c2f760ece99afd4738c28ce17e635a4f24d102 Mon Sep 17 00:00:00 2001 From: Moritz Bunkus Date: Fri, 1 Jun 2007 09:13:14 +0000 Subject: [PATCH] =?utf8?q?Eine=20Funktion=20zum=20tabellarischen=20Ausgebe?= =?utf8?q?n=20von=20SQL-Ergebnissen=20(erwartet=20eine=20Referenz=20auf=20?= =?utf8?q?ein=20Array=20mit=20Hashreferenzen)=20hinzugef=C3=BCgt.?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- SL/LXDebug.pm | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/SL/LXDebug.pm b/SL/LXDebug.pm index 16ecf9af5..87bfd7f7c 100644 --- a/SL/LXDebug.pm +++ b/SL/LXDebug.pm @@ -143,6 +143,41 @@ sub dump_yaml { $self->message($level, "dumping ${name}:\n" . YAML::Dump($variable)); } +sub dump_sql_result { + my ($self, $level, $prefix, $results) = @_; + + if (!$results || !scalar @{ $results }) { + $self->message($level, "Empty result set"); + return; + } + + my %column_lengths; + my $first_row = 1; + + foreach my $row (@{ $results }) { + if ($first_row) { + map { $column_lengths{$_} = length $_ } keys %{ $row }; + $first_row = 0; + } + + map { $column_lengths{$_} = length $row->{$_} if (length $row->{$_} > $column_lengths{$_}) } keys %{ $row }; + } + + my @sorted_names = sort keys %column_lengths; + my $format = join '|', map { '%' . $column_lengths{$_} . 's' } @sorted_names; + + $prefix =~ s|\s*$||; + $prefix .= ' ' if $prefix; + + $self->message($level, $prefix . sprintf($format, @sorted_names)); + $self->message($level, $prefix . join('+', map { '-' x $column_lengths{$_} } @sorted_names)); + + foreach my $row (@{ $results }) { + $self->message($level, $prefix . sprintf($format, map { $row->{$_} } @sorted_names)); + } + $self->message($level, $prefix . sprintf('(%d row%s)', scalar @{ $results }, scalar @{ $results } > 1 ? 's' : '')); +} + sub enable_sub_tracing { my ($self) = @_; $self->{level} | TRACE; -- 2.20.1