/usr/share/perl5/DBIx/Class/Manual/Features.pod is in libdbix-class-perl 0.082840-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 | =head1 NAME
DBIx::Class::Manual::Features - A boatload of DBIx::Class features with links to respective documentation
=head1 META
=head2 Large Community
There are L<hundres of DBIC contributors|DBIx::Class/AUTHORS> listed in
F<AUTHORS>. That ranges from documentation help, to test help, to added
features, to entire database support.
=head2 Active Community
Currently (June 9, 2010) 6 active branches (committed to
in the last two weeks) in git. Last release (0.08122)
had 14 new features, and 16 bug fixes. Of course that
L<ebbs and flows|http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=dbsrgits/DBIx-Class.git;a=blob;f=Changes>.)
=head2 Responsive Community
=over 1
=item I needed MSSQL order-by support; the community helped me add support
=item generally very welcoming of people willing to help
=back
=head1 General ORM
These are things that are in most other ORMs, but are still reasons to use
DBIC over raw SQL.
=head2 Cross DB
The vast majority of code should run on all databases without needing tweaking
=head2 Basic CRUD
=over 1
=item C - Create
=item R - Retrieve
=item U - Update
=item D - Delete
=back
=head2 SQL: Create
my $sth = $dbh->prepare('
INSERT INTO books
(title, author_id)
values (?,?)
');
$sth->execute( 'A book title', $author_id );
=head2 DBIC: Create
my $book = $book_rs->create({
title => 'A book title',
author_id => $author_id,
});
See L<DBIx::Class::ResultSet/create>
=over 1
=item No need to pair placeholders and values
=item Automatically gets autoincremented id for you
=item Transparently uses INSERT ... RETURNING for databases that support it
=back
=head2 SQL: Read
my $sth = $dbh->prepare('
SELECT title,
authors.name as author_name
FROM books, authors
WHERE books.author = authors.id
');
while ( my $book = $sth->fetchrow_hashref ) {
say "Author of $book->{title} is $book->{author_name}";
}
=head2 DBIC: Read
my $book = $book_rs->find($book_id);
or
my $book = $book_rs->search({ title => 'A book title' }, { rows => 1 })->next;
or
my @books = $book_rs->search({ author => $author_id })->all;
or
while( my $book = $books_rs->next ) {
printf "Author of %s is %s\n", $book->title, $book->author->name;
}
See L<DBIx::Class::ResultSet/find>, L<DBIx::Class::ResultSet/search>, L<DBIx::Class::ResultSet/next>, and L<DBIx::Class::ResultSet/all>
B<TMTOWTDI!>
=head2 SQL: Update
my $update = $dbh->prepare('
UPDATE books
SET title = ?
WHERE id = ?
');
$update->execute( 'New title', $book_id );
=head2 DBIC: Update
$book->update({ title => 'New title' });
See L<DBIx::Class::Row/update>
Will not update unless value changes
=head2 SQL: Delete
my $delete = $dbh->prepare('DELETE FROM books WHERE id = ?');
$delete->execute($book_id);
=head2 DBIC: Delete
$book->delete
See L<DBIx::Class::Row/delete>
=head2 SQL: Search
my $sth = $dbh->prepare('
SELECT title,
authors.name as author_name
FROM books
WHERE books.name LIKE "%monte cristo%" AND
books.topic = "jailbreak"
');
=head2 DBIC: Search
my $book = $book_rs->search({
'me.name' => { -like => '%monte cristo%' },
'me.topic' => 'jailbreak',
})->next;
=over 1
=item See L<SQL::Abstract>, L<DBIx::Class::ResultSet/next>, and L<DBIx::Class::ResultSet/search>
=item (kinda) introspectible
=item Prettier than SQL
=back
=head2 OO Overridability
=over 1
=item Override new if you want to do validation
=item Override delete if you want to disable deletion
=item and on and on
=back
=head2 Convenience Methods
=over 1
=item L<DBIx::Class::ResultSet/find_or_create>
=item L<DBIx::Class::ResultSet/update_or_create>
=back
=head2 Non-column methods
Need a method to get a user's gravatar URL? Add a C<gravatar_url> method to the
Result class
=head2 RELATIONSHIPS
=over 1
=item L<DBIx::Class::Relationship/belongs_to>
=item L<DBIx::Class::Relationship/has_many>
=item L<DBIx::Class::Relationship/might_have>
=item L<DBIx::Class::Relationship/has_one>
=item L<DBIx::Class::Relationship/many_to_many>
=item SET AND FORGET
=back
=head1 DBIx::Class Specific Features
These things may be in other ORM's, but they are very specific, so doubtful
=head2 ->deploy
Create a database from your DBIx::Class schema.
my $schema = Frew::Schema->connect( $dsn, $user, $pass );
$schema->deploy
See L<DBIx::Class::Schema/deploy>.
See also: L<DBIx::Class::DeploymentHandler>
=head2 Schema::Loader
Create a DBIx::Class schema from your database.
package Frew::Schema;
use strict;
use warnings;
use base 'DBIx::Class::Schema::Loader';
__PACKAGE__->loader_options({
naming => 'v7',
debug => $ENV{DBIC_TRACE},
});
1;
# elsewhere...
my $schema = Frew::Schema->connect( $dsn, $user, $pass );
See L<DBIx::Class::Schema::Loader> and L<DBIx::Class::Schema::Loader::Base/CONSTRUCTOR OPTIONS>.
=head2 Populate
Made for inserting lots of rows very quickly into database
$schema->populate([ Users =>
[qw( username password )],
[qw( frew >=4char$ )],
[qw( ... )],
[qw( ... )],
);
See L<DBIx::Class::Schema/populate>
I use populate L<here|http://blog.afoolishmanifesto.com/archives/1255> to export our whole
(200M~) db to SQLite
=head2 Multicreate
Create an object and its related objects all at once
$schema->resultset('Author')->create({
name => 'Stephen King',
books => [{ title => 'The Dark Tower' }],
address => {
street => '123 Turtle Back Lane',
state => { abbreviation => 'ME' },
city => { name => 'Lowell' },
},
});
See L<DBIx::Class::ResultSet/create>
=over 1
=item books is a has_many
=item address is a belongs_to which in turn belongs to state and city each
=item for this to work right state and city must mark abbreviation and name as unique
=back
=head2 Extensible
DBIx::Class helped pioneer fast MI in Perl 5 with Class::C3, so it is made to
allow extensions to nearly every part of it.
=head2 Extensibility example: DBIx::Class::Helpers
=over 1
=item L<DBIx::Class::Helper::ResultSet::IgnoreWantarray>
=item L<DBIx::Class::Helper::ResultSet::Random>
=item L<DBIx::Class::Helper::ResultSet::SetOperations>
=item L<DBIx::Class::Helper::Row::JoinTable>
=item L<DBIx::Class::Helper::Row::NumifyGet>
=item L<DBIx::Class::Helper::Row::SubClass>
=item L<DBIx::Class::Helper::Row::ToJSON>
=item L<DBIx::Class::Helper::Row::StorageValues>
=item L<DBIx::Class::Helper::Row::OnColumnChange>
=back
=head2 Extensibility example: DBIx::Class::TimeStamp
=over 1
=item See L<DBIx::Class::TimeStamp>
=item Cross DB
=item set_on_create
=item set_on_update
=back
=head2 Extensibility example: Kioku
=over 1
=item See L<DBIx::Class::Schema::KiokuDB>
=item Kioku is the new hotness
=item Mix RDBMS with Object DB
=back
=head2 Result vs ResultSet
=over 1
=item Result == Row
=item ResultSet == Query Plan
=over 1
=item Internal Join Optimizer for all DB's (!!!)
=back
=item (less important but...)
=item ResultSource == Queryable collection of rows (Table, View, etc)
=item Storage == Database
=item Schema == associates a set of ResultSources with a Storage
=back
=head2 ResultSet methods
package MyApp::Schema::ResultSet::Book;
use strict;
use warnings;
use base 'DBIx::Class::ResultSet';
sub good {
my $self = shift;
$self->search({
$self->current_source_alias . '.rating' => { '>=' => 4 }
})
};
sub cheap {
my $self = shift;
$self->search({
$self->current_source_alias . '.price' => { '<=' => 5}
})
};
# ...
1;
See L<DBIx::Class::Manual::Cookbook/Predefined searches>
=over 1
=item All searches should be ResultSet methods
=item Name has obvious meaning
=item L<DBIx::Class::ResultSet/current_source_alias> helps things to work no matter what
=back
=head2 ResultSet method in Action
$schema->resultset('Book')->good
=head2 ResultSet Chaining
$schema->resultset('Book')
->good
->cheap
->recent
=head2 search_related
my $score = $schema->resultset('User')
->search({'me.userid' => 'frew'})
->related_resultset('access')
->related_resultset('mgmt')
->related_resultset('orders')
->telephone
->search_related( shops => {
'shops.datecompleted' => {
-between => ['2009-10-01','2009-10-08']
}
})->completed
->related_resultset('rpt_score')
->search(undef, { rows => 1})
->get_column('raw_scores')
->next;
The SQL that this produces (with placeholders filled in for clarity's sake)
on our system (Microsoft SQL) is:
SELECT raw_scores
FROM (
SELECT raw_scores, ROW_NUMBER() OVER (
ORDER BY (
SELECT (1)
)
) AS rno__row__index
FROM (
SELECT rpt_score.raw_scores
FROM users me
JOIN access access
ON access.userid = me.userid
JOIN mgmt mgmt
ON mgmt.mgmtid = access.mgmtid
JOIN [order] orders
ON orders.mgmtid = mgmt.mgmtid
JOIN shop shops
ON shops.orderno = orders.orderno
JOIN rpt_scores rpt_score
ON rpt_score.shopno = shops.shopno
WHERE (
datecompleted IS NOT NULL AND
(
(shops.datecompleted BETWEEN '2009-10-01' AND '2009-10-08') AND
(type = '1' AND me.userid = 'frew')
)
)
) rpt_score
) rpt_score
WHERE rno__row__index BETWEEN 1 AND 1
See: L<DBIx::Class::ResultSet/related_resultset>,
L<DBIx::Class::ResultSet/search_related>, and
L<DBIx::Class::ResultSet/get_column>.
=head2 bonus rel methods
my $book = $author->create_related(
books => {
title => 'Another Discworld book',
}
);
my $book2 = $pratchett->add_to_books({
title => 'MOAR Discworld book',
});
See L<DBIx::Class::Relationship::Base/create_related> and L<DBIx::Class::Relationship::Base/add_to_$rel>
Note that it automatically fills in foreign key for you
=head2 Excellent Transaction Support
$schema->txn_do(sub {
...
});
$schema->txn_begin; # <-- low level
# ...
$schema->txn_commit;
See L<DBIx::Class::Schema/txn_do>, L<DBIx::Class::Schema/txn_begin>,
and L<DBIx::Class::Schema/txn_commit>.
=head2 InflateColumn
package Frew::Schema::Result::Book;
use strict;
use warnings;
use base 'DBIx::Class::Core';
use DateTime::Format::MySQL;
# Result code here
__PACKAGE__->load_components('InflateColumn');
__PACKAGE__->inflate_column(
date_published => {
inflate => sub { DateTime::Format::MySQL->parse_date( shift ) },
deflate => sub { shift->ymd },
},
);
See L<DBIx::Class::InflateColumn>, L<DBIx::Class::InflateColumn/inflate_column>, and
L<DBIx::Class::InflateColumn::DateTime>.
=head2 InflateColumn: deflation
$book->date_published(DateTime->now);
$book->update;
=head2 InflateColumn: inflation
say $book->date_published->month_abbr; # Nov
=head2 FilterColumn
package Frew::Schema::Result::Book;
use strict;
use warnings;
use base 'DBIx::Class::Core';
# Result code here
__PACKAGE__->load_components('FilterColumn');
__PACKAGE__->filter_column(
length => {
to_storage => 'to_metric',
from_storage => 'to_imperial',
},
);
sub to_metric { $_[1] * .305 }
sub to_imperial { $_[1] * 3.28 }
See L<DBIx::Class::FilterColumn> and L<DBIx::Class::FilterColumn/filter_column>
=head2 ResultSetColumn
my $rsc = $schema->resultset('Book')->get_column('price');
$rsc->first;
$rsc->all;
$rsc->min;
$rsc->max;
$rsc->sum;
See L<DBIx::Class::ResultSetColumn>
=head2 Aggregates
my @res = $rs->search(undef, {
select => [
'price',
'genre',
{ max => price },
{ avg => price },
],
as => [
qw(price genre max_price avg_price)
],
group_by => [qw(price genre)],
});
for (@res) {
say $_->price . ' ' . $_->genre;
say $_->get_column('max_price');
say $_->get_column('avg_price');
}
See L<DBIx::Class::ResultSet/select>, L<DBIx::Class::ResultSet/as>, and
L<DBIx::Class::ResultSet/group_by>
=over 1
=item Careful, get_column can basically mean B<three> things
=item private in which case you should use an accessor
=item public for what there is no accessor for
=item public for get resultset column (prev example)
=back
=head2 HRI
$rs->search(undef, {
result_class => 'DBIx::Class::ResultClass::HashRefInflator',
});
See L<DBIx::Class::ResultSet/result_class> and L<DBIx::Class::ResultClass::HashRefInflator>.
=over 1
=item Easy on memory
=item Mega fast
=item Great for quick debugging
=item Great for performance tuning (we went from 2m to < 3s)
=back
=head2 Subquery Support
my $inner_query = $schema->resultset('Artist')
->search({
name => [ 'Billy Joel', 'Brittany Spears' ],
})->get_column('id')->as_query;
my $rs = $schema->resultset('CD')->search({
artist_id => { -in => $inner_query },
});
See L<DBIx::Class::Manual::Cookbook/Subqueries>
=head2 Bare SQL w/ Placeholders
$rs->update({
# !!! SQL INJECTION VECTOR
price => \"price + $inc", # DON'T DO THIS
});
Better:
$rs->update({
price => \['price + ?', [inc => $inc]],
});
See L<SQL::Abstract/Literal SQL with placeholders and bind values (subqueries)>
=head1 FURTHER QUESTIONS?
Check the list of L<additional DBIC resources|DBIx::Class/GETTING HELP/SUPPORT>.
=head1 COPYRIGHT AND LICENSE
This module is free software L<copyright|DBIx::Class/COPYRIGHT AND LICENSE>
by the L<DBIx::Class (DBIC) authors|DBIx::Class/AUTHORS>. You can
redistribute it and/or modify it under the same terms as the
L<DBIx::Class library|DBIx::Class/COPYRIGHT AND LICENSE>.
|