| ... | ... |
@@ -1,3 +1,5 @@ |
| 1 |
+0.1692 |
|
| 2 |
+ - separate DBIx::Custom::Result type_rule from filter again |
|
| 1 | 3 |
0.1692 |
| 2 | 4 |
- removed EXPERIMENTAL DBIx::Model result_filter |
| 3 | 5 |
- DBIx::Custom::Result filter override type_rule |
| ... | ... |
@@ -1,44 +1,36 @@ |
| 1 | 1 |
package DBIx::Custom::Result; |
| 2 |
- |
|
| 3 | 2 |
use Object::Simple -base; |
| 4 | 3 |
|
| 5 | 4 |
use Carp 'croak'; |
| 6 | 5 |
use DBIx::Custom::Util qw/_array_to_hash _subname/; |
| 7 | 6 |
|
| 8 |
-has [qw/filters filter_off sth type_rule_off/], |
|
| 9 |
- stash => sub { {} };
|
|
| 7 |
+has [qw/filters filter_off sth type_rule_off/]; |
|
| 8 |
+has stash => sub { {} };
|
|
| 10 | 9 |
|
| 11 | 10 |
*all = \&fetch_hash_all; |
| 12 | 11 |
|
| 13 | 12 |
sub filter {
|
| 14 | 13 |
my $self = shift; |
| 15 | 14 |
|
| 15 |
+ # Set |
|
| 16 | 16 |
if (@_) {
|
| 17 |
- my $filter = {};
|
|
| 18 | 17 |
|
| 19 |
- if (ref $_[0] eq 'HASH') {
|
|
| 20 |
- $filter = $_[0]; |
|
| 21 |
- } |
|
| 22 |
- else {
|
|
| 23 |
- $filter = _array_to_hash( |
|
| 24 |
- @_ > 1 ? [@_] : $_[0] |
|
| 25 |
- ); |
|
| 26 |
- } |
|
| 27 |
- |
|
| 18 |
+ # Convert filter name to subroutine |
|
| 19 |
+ my $filter = @_ == 1 ? $_[0] : [@_]; |
|
| 20 |
+ $filter = _array_to_hash($filter); |
|
| 28 | 21 |
foreach my $column (keys %$filter) {
|
| 29 | 22 |
my $fname = $filter->{$column};
|
| 30 |
- |
|
| 31 | 23 |
if (exists $filter->{$column}
|
| 32 | 24 |
&& defined $fname |
| 33 | 25 |
&& ref $fname ne 'CODE') |
| 34 | 26 |
{
|
| 35 | 27 |
croak qq{Filter "$fname" is not registered" } . _subname
|
| 36 | 28 |
unless exists $self->filters->{$fname};
|
| 37 |
- |
|
| 38 | 29 |
$filter->{$column} = $self->filters->{$fname};
|
| 39 | 30 |
} |
| 40 | 31 |
} |
| 41 | 32 |
|
| 33 |
+ # Merge |
|
| 42 | 34 |
$self->{filter} = {%{$self->filter}, %$filter};
|
| 43 | 35 |
|
| 44 | 36 |
return $self; |
| ... | ... |
@@ -86,9 +78,8 @@ sub fetch_all {
|
| 86 | 78 |
|
| 87 | 79 |
# Fetch all rows |
| 88 | 80 |
my $rows = []; |
| 89 |
- while(my $row = $self->fetch) {
|
|
| 90 |
- push @$rows, $row; |
|
| 91 |
- } |
|
| 81 |
+ while(my $row = $self->fetch) { push @$rows, $row}
|
|
| 82 |
+ |
|
| 92 | 83 |
return $rows; |
| 93 | 84 |
} |
| 94 | 85 |
|
| ... | ... |
@@ -97,8 +88,6 @@ sub fetch_first {
|
| 97 | 88 |
|
| 98 | 89 |
# Fetch |
| 99 | 90 |
my $row = $self->fetch; |
| 100 |
- |
|
| 101 |
- # No row |
|
| 102 | 91 |
return unless $row; |
| 103 | 92 |
|
| 104 | 93 |
# Finish statement handle |
| ... | ... |
@@ -149,9 +138,7 @@ sub fetch_hash_all {
|
| 149 | 138 |
|
| 150 | 139 |
# Fetch all rows as hash |
| 151 | 140 |
my $rows = []; |
| 152 |
- while(my $row = $self->fetch_hash) {
|
|
| 153 |
- push @$rows, $row; |
|
| 154 |
- } |
|
| 141 |
+ while(my $row = $self->fetch_hash) { push @$rows, $row }
|
|
| 155 | 142 |
|
| 156 | 143 |
return $rows; |
| 157 | 144 |
} |
| ... | ... |
@@ -161,8 +148,6 @@ sub fetch_hash_first {
|
| 161 | 148 |
|
| 162 | 149 |
# Fetch hash |
| 163 | 150 |
my $row = $self->fetch_hash; |
| 164 |
- |
|
| 165 |
- # No row |
|
| 166 | 151 |
return unless $row; |
| 167 | 152 |
|
| 168 | 153 |
# Finish statement handle |
| ... | ... |
@@ -174,11 +159,9 @@ sub fetch_hash_first {
|
| 174 | 159 |
sub fetch_hash_multi {
|
| 175 | 160 |
my ($self, $count) = @_; |
| 176 | 161 |
|
| 177 |
- # Row count not specified |
|
| 162 |
+ # Fetch multiple rows |
|
| 178 | 163 |
croak 'Row count must be specified ' . _subname |
| 179 | 164 |
unless $count; |
| 180 |
- |
|
| 181 |
- # Fetch multi rows |
|
| 182 | 165 |
my $rows = []; |
| 183 | 166 |
for (my $i = 0; $i < $count; $i++) {
|
| 184 | 167 |
my $row = $self->fetch_hash; |
| ... | ... |
@@ -229,84 +212,64 @@ sub type_rule {
|
| 229 | 212 |
$type_rule->{$data_type} = $self->filters->{$fname};
|
| 230 | 213 |
} |
| 231 | 214 |
} |
| 232 |
- $self->{type_rule} = {%{$self->type_rule}, %$type_rule};
|
|
| 215 |
+ |
|
| 216 |
+ # Replace |
|
| 217 |
+ if (@_ == 1) { $self->{type_rule} = $type_rule }
|
|
| 218 |
+ # Merge |
|
| 219 |
+ else { $self->{type_rule} = {%{$self->type_rule}, %$type_rule} }
|
|
| 233 | 220 |
} |
| 234 | 221 |
|
| 235 | 222 |
return $self->{type_rule} ||= {};
|
| 236 | 223 |
} |
| 237 | 224 |
|
| 238 |
-sub clear_type_rule {
|
|
| 239 |
- my $self = shift; |
|
| 240 |
- $self->{type_rule} = {};
|
|
| 241 |
- return $self; |
|
| 242 |
-} |
|
| 243 |
- |
|
| 244 | 225 |
# DEPRECATED! |
| 245 | 226 |
sub end_filter {
|
| 246 | 227 |
my $self = shift; |
| 247 |
- |
|
| 248 | 228 |
if (@_) {
|
| 249 | 229 |
my $end_filter = {};
|
| 250 |
- |
|
| 251 |
- if (ref $_[0] eq 'HASH') {
|
|
| 252 |
- $end_filter = $_[0]; |
|
| 253 |
- } |
|
| 254 |
- else {
|
|
| 230 |
+ if (ref $_[0] eq 'HASH') { $end_filter = $_[0] }
|
|
| 231 |
+ else {
|
|
| 255 | 232 |
$end_filter = _array_to_hash( |
| 256 | 233 |
@_ > 1 ? [@_] : $_[0] |
| 257 | 234 |
); |
| 258 | 235 |
} |
| 259 |
- |
|
| 260 | 236 |
foreach my $column (keys %$end_filter) {
|
| 261 | 237 |
my $fname = $end_filter->{$column};
|
| 262 |
- |
|
| 263 | 238 |
if (exists $end_filter->{$column}
|
| 264 | 239 |
&& defined $fname |
| 265 | 240 |
&& ref $fname ne 'CODE') |
| 266 | 241 |
{
|
| 267 | 242 |
croak qq{Filter "$fname" is not registered" } . _subname
|
| 268 | 243 |
unless exists $self->filters->{$fname};
|
| 269 |
- |
|
| 270 | 244 |
$end_filter->{$column} = $self->filters->{$fname};
|
| 271 | 245 |
} |
| 272 | 246 |
} |
| 273 |
- |
|
| 274 | 247 |
$self->{end_filter} = {%{$self->end_filter}, %$end_filter};
|
| 275 |
- |
|
| 276 | 248 |
return $self; |
| 277 | 249 |
} |
| 278 |
- |
|
| 279 | 250 |
return $self->{end_filter} ||= {};
|
| 280 | 251 |
} |
| 281 | 252 |
|
| 282 | 253 |
# DEPRECATED! |
| 283 | 254 |
sub remove_end_filter {
|
| 284 | 255 |
my $self = shift; |
| 285 |
- |
|
| 286 | 256 |
warn "remove_end_filter is DEPRECATED! use filter_off attribute instead"; |
| 287 |
- |
|
| 288 | 257 |
$self->{end_filter} = {};
|
| 289 |
- |
|
| 290 | 258 |
return $self; |
| 291 | 259 |
} |
| 292 | 260 |
|
| 293 | 261 |
# DEPRECATED! |
| 294 | 262 |
sub remove_filter {
|
| 295 | 263 |
my $self = shift; |
| 296 |
- |
|
| 297 | 264 |
warn "remove_filter is DEPRECATED! use filter_off attribute instead"; |
| 298 |
- |
|
| 299 | 265 |
$self->{filter} = {};
|
| 300 |
- |
|
| 301 | 266 |
return $self; |
| 302 | 267 |
} |
| 303 | 268 |
|
| 304 | 269 |
# DEPRECATED! |
| 305 | 270 |
sub default_filter {
|
| 306 | 271 |
my $self = shift; |
| 307 |
- |
|
| 308 | 272 |
warn "default_filter is DEPRECATED!"; |
| 309 |
- |
|
| 310 | 273 |
if (@_) {
|
| 311 | 274 |
my $fname = $_[0]; |
| 312 | 275 |
if (@_ && !$fname) {
|
| ... | ... |
@@ -315,13 +278,10 @@ sub default_filter {
|
| 315 | 278 |
else {
|
| 316 | 279 |
croak qq{Filter "$fname" is not registered}
|
| 317 | 280 |
unless exists $self->filters->{$fname};
|
| 318 |
- |
|
| 319 | 281 |
$self->{default_filter} = $self->filters->{$fname};
|
| 320 | 282 |
} |
| 321 |
- |
|
| 322 | 283 |
return $self; |
| 323 | 284 |
} |
| 324 |
- |
|
| 325 | 285 |
return $self->{default_filter};
|
| 326 | 286 |
} |
| 327 | 287 |
|
| ... | ... |
@@ -336,76 +296,50 @@ DBIx::Custom::Result - Result of select statement |
| 336 | 296 |
|
| 337 | 297 |
=head1 SYNOPSIS |
| 338 | 298 |
|
| 339 |
-Get the result of select statement. |
|
| 340 |
- |
|
| 341 | 299 |
# Result |
| 342 |
- my $result = $dbi->select(table => 'books'); |
|
| 300 |
+ my $result = $dbi->select(table => 'book'); |
|
| 343 | 301 |
|
| 344 |
-Fetch row into array. |
|
| 345 |
- |
|
| 346 |
- # Fetch a row into array |
|
| 302 |
+ # Fetch a row and put it into array reference |
|
| 347 | 303 |
while (my $row = $result->fetch) {
|
| 348 | 304 |
my $author = $row->[0]; |
| 349 | 305 |
my $title = $row->[1]; |
| 350 |
- |
|
| 351 | 306 |
} |
| 352 | 307 |
|
| 353 |
- # Fetch only a first row into array |
|
| 308 |
+ # Fetch only a first row and put it into array reference |
|
| 354 | 309 |
my $row = $result->fetch_first; |
| 355 | 310 |
|
| 356 |
- # Fetch multiple rows into array of array |
|
| 357 |
- while (my $rows = $result->fetch_multi(5)) {
|
|
| 358 |
- my $first_author = $rows->[0][0]; |
|
| 359 |
- my $first_title = $rows->[0][1]; |
|
| 360 |
- my $second_author = $rows->[1][0]; |
|
| 361 |
- my $second_value = $rows->[1][1]; |
|
| 362 |
- |
|
| 363 |
- } |
|
| 364 |
- |
|
| 365 |
- # Fetch all rows into array of array |
|
| 311 |
+ # Fetch all rows and put them into array of array reference |
|
| 366 | 312 |
my $rows = $result->fetch_all; |
| 367 | 313 |
|
| 368 |
-Fetch row into hash. |
|
| 369 |
- |
|
| 370 |
- # Fetch a row into hash |
|
| 314 |
+ # Fetch a row and put it into hash reference |
|
| 371 | 315 |
while (my $row = $result->fetch_hash) {
|
| 372 | 316 |
my $title = $row->{title};
|
| 373 | 317 |
my $author = $row->{author};
|
| 374 |
- |
|
| 375 | 318 |
} |
| 376 | 319 |
|
| 377 |
- # Fetch only a first row into hash |
|
| 320 |
+ # Fetch only a first row and put it into hash reference |
|
| 378 | 321 |
my $row = $result->fetch_hash_first; |
| 322 |
+ my $row = $result->one; # Same as fetch_hash_first |
|
| 379 | 323 |
|
| 380 |
- # Fetch multiple rows into array of hash |
|
| 381 |
- while (my $rows = $result->fetch_hash_multi(5)) {
|
|
| 382 |
- my $first_title = $rows->[0]{title};
|
|
| 383 |
- my $first_author = $rows->[0]{author};
|
|
| 384 |
- my $second_title = $rows->[1]{title};
|
|
| 385 |
- my $second_author = $rows->[1]{author};
|
|
| 386 |
- } |
|
| 387 |
- |
|
| 388 |
- # Fetch all rows into array of hash |
|
| 324 |
+ # Fetch all rows and put them into array of hash reference |
|
| 389 | 325 |
my $rows = $result->fetch_hash_all; |
| 326 |
+ my $rows = $result->all; # Same as fetch_hash_all |
|
| 390 | 327 |
|
| 391 | 328 |
=head1 ATTRIBUTES |
| 392 | 329 |
|
| 393 |
-Filters when a row is fetched. |
|
| 394 |
-This overwrites C<default_filter>. |
|
| 395 |
- |
|
| 396 | 330 |
=head2 C<filter_off> EXPERIMENTAL |
| 397 | 331 |
|
| 398 | 332 |
my $filter_off = $resutl->filter_off; |
| 399 | 333 |
$result = $result->filter_off(1); |
| 400 | 334 |
|
| 401 |
-Turn filter off. |
|
| 335 |
+Filtering by C<filter> method is turned off. |
|
| 402 | 336 |
|
| 403 | 337 |
=head2 C<filters> |
| 404 | 338 |
|
| 405 | 339 |
my $filters = $result->filters; |
| 406 |
- $result = $result->filters(\%filters); |
|
| 340 |
+ $result = $result->filters(\%filters); |
|
| 407 | 341 |
|
| 408 |
-Resistered filters. |
|
| 342 |
+Filters. |
|
| 409 | 343 |
|
| 410 | 344 |
=head2 C<sth> |
| 411 | 345 |
|
| ... | ... |
@@ -419,7 +353,7 @@ Statement handle of L<DBI>. |
| 419 | 353 |
my $type_rule_off = $result->type_rule_off; |
| 420 | 354 |
$result = $result->type_rule_off(1); |
| 421 | 355 |
|
| 422 |
-Turn type rule off. |
|
| 356 |
+Filtering by C<type_rule> is turned off. |
|
| 423 | 357 |
|
| 424 | 358 |
=head1 METHODS |
| 425 | 359 |
|
| ... | ... |
@@ -430,80 +364,71 @@ and implements the following new ones. |
| 430 | 364 |
|
| 431 | 365 |
my $rows = $result->all; |
| 432 | 366 |
|
| 433 |
-This is alias for C<fetch_hash_all>. |
|
| 367 |
+Same as C<fetch_hash_all>. |
|
| 434 | 368 |
|
| 435 | 369 |
=head2 C<fetch> |
| 436 | 370 |
|
| 437 | 371 |
my $row = $result->fetch; |
| 438 | 372 |
|
| 439 |
-Fetch a row into array. |
|
| 373 |
+Fetch a row and put it into array reference. |
|
| 440 | 374 |
|
| 441 | 375 |
=head2 C<fetch_all> |
| 442 | 376 |
|
| 443 | 377 |
my $rows = $result->fetch_all; |
| 444 | 378 |
|
| 445 |
-Fetch all rows into array of array. |
|
| 379 |
+Fetch all rows and put them into array of array reference. |
|
| 446 | 380 |
|
| 447 | 381 |
=head2 C<fetch_first> |
| 448 | 382 |
|
| 449 | 383 |
my $row = $result->fetch_first; |
| 450 | 384 |
|
| 451 |
-Fetch only a first row into array and finish statment handle. |
|
| 385 |
+Fetch only a first row and put it into array reference, |
|
| 386 |
+and finish statment handle. |
|
| 452 | 387 |
|
| 453 | 388 |
=head2 C<fetch_hash> |
| 454 | 389 |
|
| 455 | 390 |
my $row = $result->fetch_hash; |
| 456 | 391 |
|
| 457 |
-Fetch a row into hash |
|
| 392 |
+Fetch a row and put it into hash reference. |
|
| 458 | 393 |
|
| 459 | 394 |
=head2 C<fetch_hash_all> |
| 460 | 395 |
|
| 461 | 396 |
my $rows = $result->fetch_hash_all; |
| 462 | 397 |
|
| 463 |
-Fetch all rows into array of hash. |
|
| 398 |
+Fetch all rows and put them into array of hash reference. |
|
| 464 | 399 |
|
| 465 | 400 |
=head2 C<fetch_hash_first> |
| 466 | 401 |
|
| 467 | 402 |
my $row = $result->fetch_hash_first; |
| 468 | 403 |
|
| 469 |
-Fetch only first row into hash and finish statment handle. |
|
| 404 |
+Fetch only a first row and put it into hash reference, |
|
| 405 |
+and finish statment handle. |
|
| 470 | 406 |
|
| 471 | 407 |
=head2 C<fetch_hash_multi> |
| 472 | 408 |
|
| 473 | 409 |
my $rows = $result->fetch_hash_multi(5); |
| 474 | 410 |
|
| 475 |
-Fetch multiple rows into array of hash |
|
| 476 |
-Row count must be specified. |
|
| 411 |
+Fetch multiple rows and put them into array of hash reference. |
|
| 477 | 412 |
|
| 478 | 413 |
=head2 C<fetch_multi> |
| 479 | 414 |
|
| 480 | 415 |
my $rows = $result->fetch_multi(5); |
| 481 | 416 |
|
| 482 |
-Fetch multiple rows into array of array. |
|
| 483 |
-Row count must be specified. |
|
| 417 |
+Fetch multiple rows and put them into array of array reference. |
|
| 484 | 418 |
|
| 485 | 419 |
=head2 C<filter> |
| 486 | 420 |
|
| 487 |
- $result = $result->filter(title => 'to_something', |
|
| 488 |
- author => 'to_something'); |
|
| 421 |
+ $result->filter(title => sub { uc $_[0] }, author => 'to_upper');
|
|
| 422 |
+ $result->filter([qw/title author/] => 'to_upper'); |
|
| 489 | 423 |
|
| 490 |
- $result = $result->filter([qw/title author/] => 'to_something'); |
|
| 491 |
- |
|
| 492 |
-Filters. |
|
| 493 |
-These each filters override the filters applied by C<apply_filter> of |
|
| 494 |
-L<DBIx::Custom>. |
|
| 424 |
+Set filter for column. |
|
| 425 |
+You can use subroutine or filter name as filter. |
|
| 495 | 426 |
|
| 496 | 427 |
=head2 C<one> |
| 497 | 428 |
|
| 498 | 429 |
my $row = $result->one; |
| 499 | 430 |
|
| 500 |
-This is alias for C<fetch_hash_first>. |
|
| 501 |
- |
|
| 502 |
-=head2 C<remove_filter> |
|
| 503 |
- |
|
| 504 |
- $result->remove_filter; |
|
| 505 |
- |
|
| 506 |
-Remove filter. End filter is not removed. |
|
| 431 |
+Same as C<fetch_hash_first>. |
|
| 507 | 432 |
|
| 508 | 433 |
=head2 C<stash> |
| 509 | 434 |
|
| ... | ... |
@@ -511,10 +436,11 @@ Remove filter. End filter is not removed. |
| 511 | 436 |
my $foo = $result->stash->{foo};
|
| 512 | 437 |
$result->stash->{foo} = $foo;
|
| 513 | 438 |
|
| 514 |
-Stash is hash reference to save your data. |
|
| 439 |
+Stash is hash reference for data. |
|
| 515 | 440 |
|
| 516 | 441 |
=head2 C<type_rule> EXPERIMENTAL |
| 517 |
- |
|
| 442 |
+ |
|
| 443 |
+ # Merge type rule |
|
| 518 | 444 |
$result->type_rule( |
| 519 | 445 |
# DATE |
| 520 | 446 |
9 => sub { ... },
|
| ... | ... |
@@ -522,23 +448,14 @@ Stash is hash reference to save your data. |
| 522 | 448 |
11 => sub { ... }
|
| 523 | 449 |
); |
| 524 | 450 |
|
| 525 |
-This override L<DBIx::Custom>'s C<type_rule> C<from> section. |
|
| 526 |
- |
|
| 527 |
-=head2 C<remove_end_filter> DEPRECATED! |
|
| 528 |
- |
|
| 529 |
- $result->remove_end_filter; |
|
| 530 |
- |
|
| 531 |
-Remove end filter. |
|
| 532 |
- |
|
| 533 |
-=head2 C<end_filter> DEPRECATED! |
|
| 534 |
- |
|
| 535 |
- $result = $result->end_filter(title => 'to_something', |
|
| 536 |
- author => 'to_something'); |
|
| 537 |
- |
|
| 538 |
- $result = $result->end_filter([qw/title author/] => 'to_something'); |
|
| 451 |
+ # Replace type rule(by reference) |
|
| 452 |
+ $result->type_rule([ |
|
| 453 |
+ # DATE |
|
| 454 |
+ 9 => sub { ... },
|
|
| 455 |
+ # DATETIME or TIMESTAMP |
|
| 456 |
+ 11 => sub { ... }
|
|
| 457 |
+ ]); |
|
| 539 | 458 |
|
| 540 |
-End filters. |
|
| 541 |
-These each filters is executed after the filters applied by C<apply_filter> of |
|
| 542 |
-L<DBIx::Custom> or C<filter> method. |
|
| 459 |
+This is same as L<DBIx::Custom>'s C<type_rule>'s <from>. |
|
| 543 | 460 |
|
| 544 | 461 |
=cut |
| ... | ... |
@@ -2833,15 +2833,20 @@ $row = $result->one; |
| 2833 | 2833 |
is($row->{key1}, 6);
|
| 2834 | 2834 |
is($row->{key2}, 8);
|
| 2835 | 2835 |
$result = $dbi->select(table => 'table1'); |
| 2836 |
-$result->type_rule({date => sub { $_[0] * 3 }});
|
|
| 2836 |
+$result->type_rule(date => sub { $_[0] * 3 });
|
|
| 2837 | 2837 |
$row = $result->one; |
| 2838 | 2838 |
is($row->{key1}, 6);
|
| 2839 | 2839 |
is($row->{key2}, 8);
|
| 2840 | 2840 |
$result = $dbi->select(table => 'table1'); |
| 2841 |
+$result->type_rule({date => sub { $_[0] * 3 }});
|
|
| 2842 |
+$row = $result->one; |
|
| 2843 |
+is($row->{key1}, 6);
|
|
| 2844 |
+is($row->{key2}, 2);
|
|
| 2845 |
+$result = $dbi->select(table => 'table1'); |
|
| 2841 | 2846 |
$result->type_rule([date => sub { $_[0] * 3 }]);
|
| 2842 | 2847 |
$row = $result->one; |
| 2843 | 2848 |
is($row->{key1}, 6);
|
| 2844 |
-is($row->{key2}, 8);
|
|
| 2849 |
+is($row->{key2}, 2);
|
|
| 2845 | 2850 |
$dbi->register_filter(fivetimes => sub { $_[0] * 5});
|
| 2846 | 2851 |
$result = $dbi->select(table => 'table1'); |
| 2847 | 2852 |
$result->type_rule(date => 'fivetimes'); |