| 1 | package DBIx::Class::Wrapper::Factory; | ||||||||||||||||||||||||||||
| 2 | 4 | $DBIx::Class::Wrapper::Factory::VERSION = '0.009'; | |||||||||||||||||||||||||||
| 3 | 4 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 4 | extends qw/DBIx::Class::Wrapper::FactoryBase/; | |||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | =head1 NAME | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | DBIx::Class::Wrapper::Factory - A factory class that decorates a L<DBIx::Class::ResultSet>. | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | A model implementing the role DBIx::Class::Wrapper will automatically instantiate | ||||||||||||||||||||||||||||
| 13 | subclasses of this for any underlying DBIx::Class ResultSet. | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | To implement your own factory containing your business code for the underlying | ||||||||||||||||||||||||||||
| 16 | DBIC resulsets, you need to subclass this. | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | See L<DBIx::Class::Wrapper> for a simple synopsis overview. | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | =head1 PROPERTIES | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | =head2 dbic_rs | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | The original L<DBIx::Class::ResultSet>. Mandatory. | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | =head2 bm | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | The business model consuming the role L<DBIx::Class::Wrapper>. Mandatory. | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | See L<DBIx::Class::Wrapper> for more details. | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | =cut | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 4 | has 'dbic_rs' => ( is => 'ro' , isa => 'DBIx::Class::ResultSet', required => 1 , lazy_build => 1); | |||||||||||||||||||||||||||
| 35 | 4 | has 'bm' => ( is => 'ro' , does => 'DBIx::Class::Wrapper' , required => 1 , weak_ref => 1 ); | |||||||||||||||||||||||||||
| 36 | 4 | has 'name' => ( is => 'ro' , isa => 'Str' , required => 1 ); | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 20 | ▶ | sub _build_dbic_rs{ | ||||||||||||||||||||||||||
|
Subroutine
_build_dbic_rs: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 39 | 20 | my ($self) = @_; | |||||||||||||||||||||||||||
| 40 | 20 | return $self->build_dbic_rs(); | |||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | =head2 build_dbic_rs | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | Builds the dbic ResultSet to be wrapped by this factory. | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | Defaults to the DBIx::Class Resultset with the same name | ||||||||||||||||||||||||||||
| 48 | as this factory. | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | You can override this in your business specific factories to build | ||||||||||||||||||||||||||||
| 51 | specific resultsets: | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | package My::Model::Factory::SomeName; | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | use Moose; extends qw/DBIx::Class::Wrapper::Factory/ ; | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | sub build_dbic_rs{ | ||||||||||||||||||||||||||||
| 58 | my ($self) = @_; | ||||||||||||||||||||||||||||
| 59 | return $self->bm->dbic_schema->resultset('SomeOtherName'); | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | # Or with some restriction: | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | return $self->bm->dbic_schema->resultset('SomeOtherName') | ||||||||||||||||||||||||||||
| 64 | ->search({ bla => ... }); | ||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | =cut | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 10 | ▶ | sub build_dbic_rs{ | ||||||||||||||||||||||||||
|
Subroutine
build_dbic_rs: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 71 | 10 | my ($self) = @_; | |||||||||||||||||||||||||||
| 72 | 10 | my $resultset = eval{ return $self->bm->dbic_schema->resultset($self->name); }; | |||||||||||||||||||||||||||
| 73 | 10 | ▶ | if( my $err = $@ ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 2 | confess("Cannot build resultset for $self NAME=".$self->name().' :'.$err); | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | 8 | return $resultset; | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | =head2 new_result | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | Instantiate a new NOT INSERTED IN DB row and wrap it using | ||||||||||||||||||||||||||||
| 83 | the wrap method. | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | See L<DBIx::Class::ResultSet/new_result> | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | =cut | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 1 | ▶ | sub new_result{ | ||||||||||||||||||||||||||
|
Subroutine
new_result: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 90 | 1 | my ($self, $args) = @_; | |||||||||||||||||||||||||||
| 91 | 1 | return $self->wrap($self->dbic_rs->new_result($args)); | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | =head2 create | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | Creates a new object in the DBIC Schema and return it wrapped | ||||||||||||||||||||||||||||
| 97 | using the wrapper method. | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | See L<DBIx::Class::ResultSet/create> | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | =cut | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 2204 | ▶ | sub create{ | ||||||||||||||||||||||||||
|
Subroutine
create: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 104 | 2204 | my ($self , $args) = @_; | |||||||||||||||||||||||||||
| 105 | 2204 | return $self->wrap($self->dbic_rs->create($args)); | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | =head2 find | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | Finds an object in the DBIC schema and returns it wrapped | ||||||||||||||||||||||||||||
| 111 | using the wrapper method. | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | See L<DBIx::Class::ResultSet/find> | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | =cut | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 9 | ▶ | sub find{ | ||||||||||||||||||||||||||
|
Subroutine
find: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 118 | 9 | my ($self , @rest) = @_; | |||||||||||||||||||||||||||
| 119 | 9 | my $original = $self->dbic_rs->find(@rest); | |||||||||||||||||||||||||||
| 120 | 9 | ▶ | return $original ? $self->wrap($original) : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | =head2 first | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | Equivalent to DBIC Resultset 'first' method. | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | See <DBIx::Class::ResultSet/first> | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | =cut | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 2 | ▶ | sub first{ | ||||||||||||||||||||||||||
|
Subroutine
first: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||
| 132 | 2 | my ($self) = @_; | |||||||||||||||||||||||||||
| 133 | 2 | my $original = $self->dbic_rs->first(); | |||||||||||||||||||||||||||
| 134 | 2 | ▶ | return $original ? $self->wrap($original) : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | =head2 single | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | Equivalent to DBIx::Class::ResultSet::single. It's a bit more efficient than C<first()>. | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | =cut | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 1 | ▶ | sub single { | ||||||||||||||||||||||||||
|
Subroutine
single: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||
| 144 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||
| 145 | 1 | my $original = $self->dbic_rs->single(); | |||||||||||||||||||||||||||
| 146 | 1 | ▶ | return $original ? $self->wrap($original) : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | =head2 update_or_create | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | Wraps around the original DBIC update_or_create method. | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | See L<DBIx::Class::ResultSet/update_or_create> | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | =cut | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 2 | ▶ | sub update_or_create { | ||||||||||||||||||||||||||
|
Subroutine
update_or_create: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||
| 158 | 2 | my ($self, $args) = @_; | |||||||||||||||||||||||||||
| 159 | 2 | my $original = $self->dbic_rs->update_or_create($args); | |||||||||||||||||||||||||||
| 160 | 2 | ▶ | return $original ? $self->wrap($original) : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | =head2 find_or_create | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | Wraps around the original DBIC find_or_create method. | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | See L<DBIx::Class::ResultSet/find_or_create> | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | =cut | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 4 | ▶ | sub find_or_create{ | ||||||||||||||||||||||||||
|
Subroutine
find_or_create: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||
| 172 | 4 | my ($self , $args) = @_; | |||||||||||||||||||||||||||
| 173 | 4 | my $original = $self->dbic_rs->find_or_create($args); | |||||||||||||||||||||||||||
| 174 | 4 | ▶ | return $original ? $self->wrap($original) : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | =head2 find_or_new | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | Wraps around the original DBIC find_or_new method. | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | See L<DBIx::Class::ResultSet/find_or_new> | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | =cut | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | 2 | ▶ | sub find_or_new { | ||||||||||||||||||||||||||
|
Subroutine
find_or_new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 186 | 2 | my ($self, @args) = @_; | |||||||||||||||||||||||||||
| 187 | 2 | return $self->wrap( $self->dbic_rs->find_or_new( @args ) ); | |||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | =head2 pager | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | Shortcut to underlying dbic_rs pager. | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | See L<DBIx::Class::ResultSet/pager>. | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | =cut | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | 8 | ▶ | sub pager{ | ||||||||||||||||||||||||||
|
Subroutine
pager: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 200 | 8 | my ($self) = @_; | |||||||||||||||||||||||||||
| 201 | 8 | return $self->dbic_rs->pager(); | |||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | =head2 delete | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | Shortcut to L<DBIx::Class::ResultSet/delete> | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | =cut | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | 2 | ▶ | sub delete{ | ||||||||||||||||||||||||||
|
Subroutine
delete: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 211 | 2 | my ($self , @rest) = @_; | |||||||||||||||||||||||||||
| 212 | 2 | return $self->dbic_rs->delete(@rest); | |||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | =head2 get_column | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | Shortcut to the get_column of the decorated dbic_rs | ||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | See L<DBIx::Class::ResultSet/get_column> | ||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | =cut | ||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | 2 | ▶ | sub get_column{ | ||||||||||||||||||||||||||
|
Subroutine
get_column: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 224 | 2 | my ($self, @rest) = @_; | |||||||||||||||||||||||||||
| 225 | 2 | return $self->dbic_rs->get_column(@rest); | |||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | =head2 search_rs | ||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | Alias for search | ||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | =cut | ||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | 2 | ▶ | sub search_rs{ | ||||||||||||||||||||||||||
|
Subroutine
search_rs: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 235 | 2 | goto &search; | |||||||||||||||||||||||||||
| 236 | } | ||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | =head2 search | ||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | Search objects in the DBIC Schema and returns a new instance | ||||||||||||||||||||||||||||
| 241 | of this factory. | ||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | Note that unlike DBIx::Class::ResultSet, this search method | ||||||||||||||||||||||||||||
| 244 | will not return an Array of all results in an array context. | ||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||
| 246 | =cut | ||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | 91 | ▶ | sub search{ | ||||||||||||||||||||||||||
|
Subroutine
search: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 249 | 91 | my ($self , @rest) = @_; | |||||||||||||||||||||||||||
| 250 | 91 | my $class = ref($self); | |||||||||||||||||||||||||||
| 251 | 91 | return $class->new({ dbic_rs => $self->dbic_rs->search_rs(@rest), | |||||||||||||||||||||||||||
| 252 | bm => $self->bm(), | ||||||||||||||||||||||||||||
| 253 | name => $self->name() | ||||||||||||||||||||||||||||
| 254 | }); | ||||||||||||||||||||||||||||
| 255 | } | ||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||
| 258 | =head2 wrap | ||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||
| 260 | Wraps an L<DBIx::Class::Row> in a business object. By default, it returns the | ||||||||||||||||||||||||||||
| 261 | Row itself. | ||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||
| 263 | Override that in your subclasses of factories if you need to wrap some business code | ||||||||||||||||||||||||||||
| 264 | around the L<DBIx::Class::Row>: | ||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||
| 266 | sub wrap{ | ||||||||||||||||||||||||||||
| 267 | my ($self, $o) = @_; | ||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||
| 269 | return My::Model::O::SomeObject->new({ o => $o , ... }); | ||||||||||||||||||||||||||||
| 270 | } | ||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||
| 272 | =cut | ||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||
| 274 | 10413 | ▶ | sub wrap{ | ||||||||||||||||||||||||||
|
Subroutine
wrap: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 275 | 10413 | my ($self , $o) = @_; | |||||||||||||||||||||||||||
| 276 | 10413 | return $o; | |||||||||||||||||||||||||||
| 277 | } | ||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||
| 280 | =head2 all | ||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||
| 282 | Similar to DBIC Resultset all. | ||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||
| 284 | Usage: | ||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||
| 286 | my @objs = $this->all(); | ||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||
| 288 | =cut | ||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||
| 290 | 2 | ▶ | sub all{ | ||||||||||||||||||||||||||
|
Subroutine
all: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 291 | 2 | my ($self) = @_; | |||||||||||||||||||||||||||
| 292 | 2 | my $search = $self->search(); | |||||||||||||||||||||||||||
| 293 | 2 | my @res = (); | |||||||||||||||||||||||||||
| 294 | 2 | ▶ | while( my $next = $search->next() ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 295 | 4 | push @res , $next; | |||||||||||||||||||||||||||
| 296 | } | ||||||||||||||||||||||||||||
| 297 | 2 | return @res; | |||||||||||||||||||||||||||
| 298 | } | ||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||
| 300 | =head2 loop_through | ||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||
| 302 | Loop through all the elements of this factory | ||||||||||||||||||||||||||||
| 303 | whilst paging and execute the given code | ||||||||||||||||||||||||||||
| 304 | with the current retrieved object. | ||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||
| 306 | WARNINGS: | ||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||
| 308 | Make sure your resultset is ordered as | ||||||||||||||||||||||||||||
| 309 | it wouldn't make much sense to page through an unordered resultset. | ||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||
| 311 | In case other things are concurrently adding to this resultset, it is possible | ||||||||||||||||||||||||||||
| 312 | that the code you give will be called with the same objects twice. | ||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||
| 314 | If it's not the problem and if the rate at which objects are added is | ||||||||||||||||||||||||||||
| 315 | not too fast compared to the processing you are doing in the code, it | ||||||||||||||||||||||||||||
| 316 | should be just fine. | ||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||
| 318 | In other cases, you probably want to wrap this in a transaction to have | ||||||||||||||||||||||||||||
| 319 | a frozen view of the resultset. | ||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||
| 321 | Usage: | ||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||
| 323 | $this->loop_through(sub{ my $o = shift ; do something with o }); | ||||||||||||||||||||||||||||
| 324 | $this->loop_through(sub{...} , { limit => 1000 }); # Do only 1000 calls to sub. | ||||||||||||||||||||||||||||
| 325 | $this->loop_through(sub{...} , { rows => 20 }); # Go by pages of 20 rows | ||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||
| 327 | =cut | ||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||
| 329 | 6 | ▶ | sub loop_through{ | ||||||||||||||||||||||||||
|
Subroutine
loop_through: called CC=9 SCAR=22.0
Pod
documented
| |||||||||||||||||||||||||||||
| 330 | 6 | my ($self, $code , $opts ) = @_; | |||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||
| 332 | 6 | ▶ | unless( defined $opts ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 333 | 2 | $opts = {}; | |||||||||||||||||||||||||||
| 334 | } | ||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||
| 336 | 6 | my $limit = $opts->{limit}; | |||||||||||||||||||||||||||
| 337 | 6 | ▶ | my $rows = defined $opts->{rows} ? $opts->{rows} : 10; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||
| 339 | 6 | ▶ | my $attrs = { %{$self->dbic_rs->{attrs} || {} } }; | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->dbic_rs->{'attrs'} || {}
MC/DC0%0/1
$self->dbic_rs->{'attrs'} || {}
$self->dbic_rs->{'attrs'}
Truth table50%1/2
A
A= $self->dbic_rs->{'attrs'}
| |||||||||||||||||||||||||||||
| 340 | 6 | ▶ | unless( $attrs->{order_by} ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 341 | 0 | warn(q| | |||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||
| 343 | Missing order_by attribute. Order will be undefined in |.__PACKAGE__.q| loop_through. | ||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||
| 345 | |); | ||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||
| 347 | } | ||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||
| 349 | # init | ||||||||||||||||||||||||||||
| 350 | 6 | my $page = 1; | |||||||||||||||||||||||||||
| 351 | 6 | my $search = $self->search(undef , { page => $page , rows => $rows }); | |||||||||||||||||||||||||||
| 352 | 6 | my $last_page = $search->pager->last_page(); | |||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||
| 354 | 6 | my $ncalls = 0; | |||||||||||||||||||||||||||
| 355 | # loop though all pages. | ||||||||||||||||||||||||||||
| 356 | PAGELOOP: | ||||||||||||||||||||||||||||
| 357 | 6 | ▶ | while( $page <= $last_page ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 358 | # Loop through this page | ||||||||||||||||||||||||||||
| 359 | 62 | ▶ | while( my $o = $search->next() ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 360 | 2402 | $code->($o); | |||||||||||||||||||||||||||
| 361 | 2402 | $ncalls++; | |||||||||||||||||||||||||||
| 362 | 2402 | ▶ | if( $limit && ( $ncalls >= $limit ) ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$limit and $ncalls >= $limit
MC/DC100%2/2
$limit and $ncalls >= $limit
$limit
$ncalls >= $limit
Truth table100%3/3
A and B
A= $limitB= $ncalls >= $limit
| |||||||||||||||||||||||||||||
| 363 | 2 | last PAGELOOP; | |||||||||||||||||||||||||||
| 364 | } | ||||||||||||||||||||||||||||
| 365 | } | ||||||||||||||||||||||||||||
| 366 | # Done with this page. | ||||||||||||||||||||||||||||
| 367 | # Go to the next one. | ||||||||||||||||||||||||||||
| 368 | 60 | $page++; | |||||||||||||||||||||||||||
| 369 | 60 | $search = $self->search(undef, { page => $page , rows => $rows }); | |||||||||||||||||||||||||||
| 370 | } | ||||||||||||||||||||||||||||
| 371 | } | ||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||
| 374 | =head2 fast_loop_through | ||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||
| 376 | Loops through all the objects of this factory | ||||||||||||||||||||||||||||
| 377 | in a Seeking fashion. If the primary key of the underlying | ||||||||||||||||||||||||||||
| 378 | resultset is orderable and indexed, this should run | ||||||||||||||||||||||||||||
| 379 | in linear time of the number of rows on the resultset. | ||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||
| 381 | Usage: | ||||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||
| 383 | $this->fast_loop_through(sub{my ($o) = @_; ... } ); | ||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||
| 385 | $this->fast_loop_through(sub{ .. } , { rows => 100 , limit => 1000 }); | ||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||
| 387 | Options: | ||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||
| 389 | rows: Fetch this amount of rows at each query. Default to 100 | ||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||
| 391 | limit: Return after looping through this amount of rows. | ||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||
| 393 | B<Important> | ||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||
| 395 | =over | ||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||
| 397 | You do not need to order the set, as this will order it by ascending primary key. | ||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||
| 399 | This means aggregation functions (such as group_by) will not work. | ||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||
| 401 | Incidentally, it means that if other processes are writing to this resultset, | ||||||||||||||||||||||||||||
| 402 | this method will play catch up on the resultset, so if the writing rate is higher | ||||||||||||||||||||||||||||
| 403 | than the reading rate, this might take a while to return. | ||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||
| 405 | If you want to avoid this, set the option 'order' to 'desc'. | ||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||
| 407 | =back | ||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||
| 409 | Returns the number of rows looped through. | ||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||
| 411 | Prerequisites: | ||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||
| 413 | Must have: | ||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||
| 415 | - The underlying L<DBIx::Class::ResultSource> has a primary key | ||||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||
| 417 | - Each component of the primary key supports the operators '>' and '<' | ||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||
| 419 | - It is possible to order all the rows by this primary key alone. | ||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||
| 421 | Should have: | ||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||
| 423 | - This primary key is indexed and offers fast comparison access. | ||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||
| 425 | Inspired by http://use-the-index-luke.com/sql/partial-results/fetch-next-page | ||||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||
| 427 | =cut | ||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||
| 429 | 7 | ▶ | sub fast_loop_through{ | ||||||||||||||||||||||||||
|
Subroutine
fast_loop_through: called CC=13 SCAR=25.9
Pod
documented
| |||||||||||||||||||||||||||||
| 430 | 7 | my ($self , $code, $opts) = @_; | |||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||
| 432 | 0 | ▶ | unless( defined $code ){ $code = sub{}; } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 433 | 7 | ▶ | unless( defined $opts ){ $opts = {}; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||
| 435 | 7 | ▶ | my $order = $opts->{order} || 'asc'; | ||||||||||||||||||||||||||
|
Condition100%2/2
$opts->{'order'} || 'asc'
MC/DC100%1/1
$opts->{'order'} || 'asc'
$opts->{'order'}
Truth table100%2/2
A
A= $opts->{'order'}
| |||||||||||||||||||||||||||||
| 436 | 7 | ▶ | my $rows = $opts->{rows} || 100; | ||||||||||||||||||||||||||
|
Condition50%1/2
$opts->{'rows'} || 100
MC/DC0%0/1
$opts->{'rows'} || 100
$opts->{'rows'}
Truth table50%1/2
A
A= $opts->{'rows'}
| |||||||||||||||||||||||||||||
| 437 | 7 | my $limit = $opts->{limit}; | |||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||
| 439 | # Gather the required info about the resultset | ||||||||||||||||||||||||||||
| 440 | 7 | my $rs = $self->dbic_rs(); | |||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||
| 442 | # What is this source alias? | ||||||||||||||||||||||||||||
| 443 | 7 | my $me = $rs->current_source_alias(); | |||||||||||||||||||||||||||
| 444 | |||||||||||||||||||||||||||||
| 445 | # The source | ||||||||||||||||||||||||||||
| 446 | 7 | my $source = $rs->result_source(); | |||||||||||||||||||||||||||
| 447 | 7 | my @primary_columns = $source->primary_columns(); | |||||||||||||||||||||||||||
| 448 | 7 | ▶ | unless( @primary_columns ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 449 | 0 | confess("Result Source ".$source->source_name()." does not have a primary key"); | |||||||||||||||||||||||||||
| 450 | } | ||||||||||||||||||||||||||||
| 451 | |||||||||||||||||||||||||||||
| 452 | 7 | my $order_by = [ map{ +{ '-'.$order => $me.'.'.$_ } } @primary_columns ]; | |||||||||||||||||||||||||||
| 453 | |||||||||||||||||||||||||||||
| 454 | 7 | my $n_rows = 0; | |||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||
| 456 | 7 | my $last_row; | |||||||||||||||||||||||||||
| 457 | 7 | ▶ | do{ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 458 | 66 | my $resultset = $self->dbic_rs->search_rs(undef , { order_by => $order_by , rows => $rows }); | |||||||||||||||||||||||||||
| 459 | 66 | ▶ | if( $last_row ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 460 | # We have a last row. | ||||||||||||||||||||||||||||
| 461 | # The idea here is to use the primary key to get the rows above | ||||||||||||||||||||||||||||
| 462 | # this last row. | ||||||||||||||||||||||||||||
| 463 | |||||||||||||||||||||||||||||
| 464 | # The primary key of the first queried row should be greater than | ||||||||||||||||||||||||||||
| 465 | # the last row's one. | ||||||||||||||||||||||||||||
| 466 | # Logically it should be: ( queried PK components ) > ( last row PK components ) | ||||||||||||||||||||||||||||
| 467 | # If the primary key is A , B , C , then the where clause should contain (for order desc): | ||||||||||||||||||||||||||||
| 468 | # A > a || ( A = a && B > b || ( B = b && C > c ) ) | ||||||||||||||||||||||||||||
| 469 | |||||||||||||||||||||||||||||
| 470 | 59 | my $top_or = { -or => [] }; | |||||||||||||||||||||||||||
| 471 | 59 | my $cur_or = $top_or; | |||||||||||||||||||||||||||
| 472 | |||||||||||||||||||||||||||||
| 473 | 59 | ▶ | my $cmp_op = $order eq 'asc' ? '>' : '<'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 474 | |||||||||||||||||||||||||||||
| 475 | 59 | my $key_i = 0; | |||||||||||||||||||||||||||
| 476 | 59 | ▶ | for(; $key_i < @primary_columns - 1 ; $key_i++ ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 477 | |||||||||||||||||||||||||||||
| 478 | 58 | my $column = $primary_columns[$key_i]; | |||||||||||||||||||||||||||
| 479 | |||||||||||||||||||||||||||||
| 480 | 58 | my $nested_or = { -or => [] }; | |||||||||||||||||||||||||||
| 481 | |||||||||||||||||||||||||||||
| 482 | 58 | push @{ $cur_or->{-or} } , { $me.'.'.$column => { $cmp_op => $last_row->get_column($column) }}; | |||||||||||||||||||||||||||
| 483 | 58 | push @{ $cur_or->{-or} } , { -and => [ { $me.'.'.$column => { '=' => $last_row->get_column($column) } }, | |||||||||||||||||||||||||||
| 484 | $nested_or | ||||||||||||||||||||||||||||
| 485 | ] | ||||||||||||||||||||||||||||
| 486 | }; | ||||||||||||||||||||||||||||
| 487 | 58 | $cur_or = $nested_or; | |||||||||||||||||||||||||||
| 488 | } | ||||||||||||||||||||||||||||
| 489 | |||||||||||||||||||||||||||||
| 490 | 59 | my $last_column = $primary_columns[$key_i]; | |||||||||||||||||||||||||||
| 491 | 59 | push @{ $cur_or->{-or} } , { $me.'.'.$last_column => { $cmp_op => $last_row->get_column($last_column) } }; | |||||||||||||||||||||||||||
| 492 | |||||||||||||||||||||||||||||
| 493 | 59 | $resultset = $resultset->search_rs($top_or); | |||||||||||||||||||||||||||
| 494 | } # End of above last row seeking | ||||||||||||||||||||||||||||
| 495 | |||||||||||||||||||||||||||||
| 496 | 66 | $last_row = undef; | |||||||||||||||||||||||||||
| 497 | |||||||||||||||||||||||||||||
| 498 | 66 | ▶ | while( my $o = $resultset->next() ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 499 | 5799 | $last_row = $o; | |||||||||||||||||||||||||||
| 500 | 5799 | my $wrapped = $self->wrap($o); | |||||||||||||||||||||||||||
| 501 | 5799 | &$code($wrapped); | |||||||||||||||||||||||||||
| 502 | 5799 | $n_rows++; | |||||||||||||||||||||||||||
| 503 | |||||||||||||||||||||||||||||
| 504 | 5799 | ▶ | if( $limit && ( $n_rows == $limit ) ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$limit and $n_rows == $limit
MC/DC0%0/2
$limit and $n_rows == $limit
$limit
$n_rows == $limit
Truth table33%1/3
A and B
A= $limitB= $n_rows == $limit
| |||||||||||||||||||||||||||||
| 505 | 0 | return $n_rows; | |||||||||||||||||||||||||||
| 506 | } | ||||||||||||||||||||||||||||
| 507 | } | ||||||||||||||||||||||||||||
| 508 | }while( $last_row ); | ||||||||||||||||||||||||||||
| 509 | |||||||||||||||||||||||||||||
| 510 | 7 | return $n_rows; | |||||||||||||||||||||||||||
| 511 | } | ||||||||||||||||||||||||||||
| 512 | |||||||||||||||||||||||||||||
| 513 | =head2 next | ||||||||||||||||||||||||||||
| 514 | |||||||||||||||||||||||||||||
| 515 | Returns next Business Object from this current DBIx::Resultset. | ||||||||||||||||||||||||||||
| 516 | |||||||||||||||||||||||||||||
| 517 | See L<DBIx::Class::ResultSet/next> | ||||||||||||||||||||||||||||
| 518 | |||||||||||||||||||||||||||||
| 519 | =cut | ||||||||||||||||||||||||||||
| 520 | |||||||||||||||||||||||||||||
| 521 | 2478 | ▶ | sub next{ | ||||||||||||||||||||||||||
|
Subroutine
next: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 522 | 2478 | my ($self) = @_; | |||||||||||||||||||||||||||
| 523 | 2478 | my $next_o = $self->dbic_rs->next(); | |||||||||||||||||||||||||||
| 524 | 2478 | ▶ | return undef unless $next_o; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 525 | 2412 | return $self->wrap($next_o); | |||||||||||||||||||||||||||
| 526 | } | ||||||||||||||||||||||||||||
| 527 | |||||||||||||||||||||||||||||
| 528 | =head2 count | ||||||||||||||||||||||||||||
| 529 | |||||||||||||||||||||||||||||
| 530 | Returns the number of objects in this ResultSet. | ||||||||||||||||||||||||||||
| 531 | |||||||||||||||||||||||||||||
| 532 | See L<DBIx::Class::ResultSet/count> | ||||||||||||||||||||||||||||
| 533 | |||||||||||||||||||||||||||||
| 534 | =cut | ||||||||||||||||||||||||||||
| 535 | |||||||||||||||||||||||||||||
| 536 | 21 | ▶ | sub count{ | ||||||||||||||||||||||||||
|
Subroutine
count: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 537 | 21 | my ($self) = @_; | |||||||||||||||||||||||||||
| 538 | 21 | return $self->dbic_rs->count(); | |||||||||||||||||||||||||||
| 539 | } | ||||||||||||||||||||||||||||
| 540 | |||||||||||||||||||||||||||||
| 541 | |||||||||||||||||||||||||||||
| 542 | 4 | __PACKAGE__->meta->make_immutable(); | |||||||||||||||||||||||||||
| 543 | 4 | 1; | |||||||||||||||||||||||||||