6

DBIx :: Class::ResultSetの結果を次のようにきれいに印刷したいと思います。

my $schema = MyDatabase::Main->connect('dbi:SQLite:db/example.db');
my $rs = $schema->resultset('Track')->all()
# then print $rs with all of those feilds

DBIx :: SQLCrosstab :: Formatクラスを見つけましたが、独自のクエリでのみ機能するようです。

4

2 に答える 2

5

DBICプリティプリントモジュールについては知りませんが、CPANの無数のテキスト、html、またはその他のタイプの表形式出力モジュールから簡単に実装できます。

以下は、使用した私の簡単な作業例ですText::Table

use 5.012;
use warnings;
use List::MoreUtils 'zip';
use Text::Table;

# my database with Album schema from DBIx::Class::Manual::Intro
use MySchema;
my $db     = MySchema->connect( "DBI:SQLite:myschema_db" );
my $album  = $db->resultset( 'Album' );

# get column names for the Album table
my @cols   = $album->result_source->columns;

# create header with these column names
my $table  = Text::Table->new( header( @cols ) );

# add each Album row to table output
while (my $cd = $album->next) {
    $table->add( map { $cd->get_column( $_ ) } @cols );
}

print $table;    # => tabular text output

# adds | separator between header labels
sub header {
    my @sep = (\' | ') x @_;
    zip @_, @sep;
}

これにより、テストデータで次のように出力されます。

albumid | artist      | title          | rank | 
1       | Lou Reed    | Transformer    |      | 
2       | Lou Reed    | Berlin         |      | 
3       | David Bowie | Ziggy Stardust |      | 
4       | Japan       | Tin Drum       |      |

/I3az/

于 2010-11-01T20:50:42.630 に答える
2

本当にきれいな出力を探している場合は、上記の draegtun の例を使用してください。ただし、Data::Dumper を使用して、すべてのソース データなしで DBIx::Class::Row オブジェクトを吐き出したいだけの場合は、このフックを結果クラスに追加することができます (または、ベースの結果に追加することをお勧めします)。すべてのスキーマ結果のクラス)

sub _dumper_hook {
  $_[0] = bless { %{ $_[0] }, _source_handle=>undef }, ref($_[0]); 
}
$Data::Dumper::Freezer = '_dumper_hook';
于 2010-12-07T18:21:14.613 に答える