/[Frey]/trunk/lib/Frey/DBIC/Browser.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /trunk/lib/Frey/DBIC/Browser.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1020 - (show annotations)
Mon Jan 26 14:51:11 2009 UTC (15 years, 3 months ago) by dpavlin
File size: 2049 byte(s)
more dbic fun with resultsets
1 package Frey::DBIC::Browser;
2 use Moose;
3
4 extends 'Frey';
5 with 'Frey::Web';
6 with 'Frey::Config';
7 #with 'Frey::Storage';
8
9 has dbic_class => (
10 is => 'rw',
11 isa => 'Str',
12 required => 1,
13 default => 'Reblog::Schema',
14 );
15
16 has dsn => (
17 is => 'rw',
18 isa => 'Str',
19 required => 1,
20 default => 'DBI:mysql:database=reblog;host=127.0.0.1;port=13306',
21 );
22
23 has result_set => (
24 is => 'rw',
25 isa => 'Str',
26 required => 1,
27 default => 'Items',
28 );
29
30 has order_by => (
31 is => 'rw',
32 isa => 'Str',
33 required => 1,
34 default => 'timestamp desc',
35 );
36
37 has page => (
38 is => 'rw',
39 isa => 'Int',
40 required => 1,
41 default => 1,
42 );
43
44 sub as_sponge {
45 my ($self) = @_;
46
47 my $dbic_class = $self->dbic_class;
48 my $dsn = $self->dsn;
49 my $schema;
50
51 my $code = qq{
52 use $dbic_class ;
53 \$schema = $dbic_class->connect("$dsn", '', '');
54 };
55
56 eval $code;
57 die $@ if $@;
58
59 $schema->storage->debug(1); # XXX dump storage generated SQL
60
61 my $attrs;
62
63 $attrs->{ $_ } = $self->$_ foreach ( grep { $self->$_ } ( qw/page order_by/ ) );
64 warn "# attrs ", $self->dump( $attrs );
65
66 my $rs = $schema->resultset( $self->result_set )
67 ->published
68 ->search( undef, $attrs )
69 ;
70 =for published
71 ->search({
72 'userdata.label' => 'published',
73 'userdata.value_numeric' => 1,
74 }, {
75 join => [ 'userdata' ],
76 %$attrs,
77 })
78 ;
79 =cut
80
81 my @rows;
82 my @name;
83 my $name_pos;
84
85 while ( my $feed = $rs->next ) {
86 my %row = $feed->get_columns;
87
88 my @row;
89
90 foreach my $name ( $feed->columns ) {
91 if ( ! defined $name_pos->{$name} ) {
92 push @name, $name;
93 $name_pos->{$name} = $#name;
94 warn "## name_pos: ", $self->dump( $name_pos ) if $self->debug;
95 }
96
97 my $pos = $name_pos->{$name};
98 $row[$pos] = $row{$name};
99 }
100
101 push @rows, [ @row ];
102 }
103
104 return {
105 rows => \@rows,
106 NAME => \@name,
107 total_entries => $rs->pager->total_entries,
108 }
109 }
110
111 =head1 SEE ALSO
112
113 DBIx::Master Class
114
115 L<http://www.shadowcat.co.uk/catalyst/-talks/yapc-na-2008/dbix-masterclass.xul> presentation
116
117 L<http://www.shadowcat.co.uk/archive/conference-video/yapc-eu-2008/dbic-masterclass/> video
118
119 =cut
120
121 1;

  ViewVC Help
Powered by ViewVC 1.1.26