Release v0.08271
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class.pm
CommitLineData
ea2e61bf 1package DBIx::Class;
2
5d283305 3use strict;
4use warnings;
5
f9cc85ce 6our $VERSION;
7# Always remember to do all digits for the version even if they're 0
8# i.e. first release of 0.XX *must* be 0.XX000. This avoids fBSD ports
9# brain damage and presumably various other packaging systems too
10
11# $VERSION declaration must stay up here, ahead of any other package
12# declarations, as to not confuse various modules attempting to determine
13# this ones version, whether that be s.c.o. or Module::Metadata, etc
0d2cde0d 14$VERSION = '0.08271';
f9cc85ce 15
16$VERSION = eval $VERSION if $VERSION =~ /_/; # numify for warning-free dev releases
17
37873f78 18use DBIx::Class::_Util;
d38cd95c 19use mro 'c3';
329d7385 20
2527233b 21use DBIx::Class::Optional::Dependencies;
22
db29433c 23use base qw/DBIx::Class::Componentised DBIx::Class::AccessorGroup/;
11736b4c 24use DBIx::Class::StartupCheck;
f9080e45 25use DBIx::Class::Exception;
3e110410 26
70c28808 27__PACKAGE__->mk_group_accessors(inherited => '_skip_namespace_frames');
9345b14c 28__PACKAGE__->_skip_namespace_frames('^DBIx::Class|^SQL::Abstract|^Try::Tiny|^Class::Accessor::Grouped|^Context::Preserve');
70c28808 29
ade0fe3b 30sub mk_classdata {
77d518d1 31 shift->mk_classaccessor(@_);
32}
33
34sub mk_classaccessor {
35 my $self = shift;
ade0fe3b 36 $self->mk_group_accessors('inherited', $_[0]);
77d518d1 37 $self->set_inherited(@_) if @_ > 1;
3e110410 38}
3c0068c1 39
7411204b 40sub component_base_class { 'DBIx::Class' }
227d4dee 41
f0750722 42sub MODIFY_CODE_ATTRIBUTES {
b5d2c57f 43 my ($class,$code,@attrs) = @_;
44 $class->mk_classdata('__attr_cache' => {})
45 unless $class->can('__attr_cache');
46 $class->__attr_cache->{$code} = [@attrs];
47 return ();
f0750722 48}
49
da95b45f 50sub _attr_cache {
b5d2c57f 51 my $self = shift;
52 my $cache = $self->can('__attr_cache') ? $self->__attr_cache : {};
9780718f 53
54 return {
55 %$cache,
56 %{ $self->maybe::next::method || {} },
20674fcd 57 };
da95b45f 58}
59
ea2e61bf 601;
34d52be2 61
97ad6fb8 62__END__
63
64=encoding UTF-8
65
75d07914 66=head1 NAME
34d52be2 67
7e4b2f59 68DBIx::Class - Extensible and flexible object <-> relational mapper.
34d52be2 69
06752a03 70=head1 WHERE TO START READING
3b1c2bbd 71
06752a03 72See L<DBIx::Class::Manual::DocMap> for an overview of the exhaustive documentation.
73To get the most out of DBIx::Class with the least confusion it is strongly
74recommended to read (at the very least) the
75L<Manuals|DBIx::Class::Manual::DocMap/Manuals> in the order presented there.
76
77=head1 HOW TO GET HELP
78
79Due to the complexity of its problem domain, DBIx::Class is a relatively
80complex framework. After you start using DBIx::Class questions will inevitably
81arise. If you are stuck with a problem or have doubts about a particular
82approach do not hesitate to contact the community with your questions. The
83list below is sorted by "fastest response time":
3b1c2bbd 84
a06e1181 85=over
3b1c2bbd 86
c6fdaf2a 87=item * IRC: irc.perl.org#dbix-class
88
89=for html
e1ddfc8a 90<a href="https://chat.mibbit.com/#dbix-class@irc.perl.org">(click for instant chatroom login)</a>
3b1c2bbd 91
a06e1181 92=item * Mailing list: L<http://lists.scsys.co.uk/mailman/listinfo/dbix-class>
3b1c2bbd 93
e1ddfc8a 94=item * RT Bug Tracker: L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=DBIx-Class>
86a23587 95
e1ddfc8a 96=item * Twitter: L<https://www.twitter.com/dbix_class>
86a23587 97
86a23587 98=item * Web Site: L<http://www.dbix-class.org/>
a06e1181 99
86a23587 100=back
101
34d52be2 102=head1 SYNOPSIS
103
113e8d16 104For the very impatient: L<DBIx::Class::Manual::QuickStart>
105
106This code in the next step can be generated automatically from an existing
107database, see L<dbicdump> from the distribution C<DBIx-Class-Schema-Loader>.
108
5b56d1ac 109=head2 Schema classes preparation
110
53aa53f3 111Create a schema class called F<MyApp/Schema.pm>:
34d52be2 112
03460bef 113 package MyApp::Schema;
a0638a7b 114 use base qw/DBIx::Class::Schema/;
34d52be2 115
f0bb26f3 116 __PACKAGE__->load_namespaces();
daec44b8 117
a0638a7b 118 1;
daec44b8 119
30e1753a 120Create a result class to represent artists, who have many CDs, in
53aa53f3 121F<MyApp/Schema/Result/Artist.pm>:
daec44b8 122
30e1753a 123See L<DBIx::Class::ResultSource> for docs on defining result classes.
124
03460bef 125 package MyApp::Schema::Result::Artist;
d88ecca6 126 use base qw/DBIx::Class::Core/;
daec44b8 127
a0638a7b 128 __PACKAGE__->table('artist');
129 __PACKAGE__->add_columns(qw/ artistid name /);
130 __PACKAGE__->set_primary_key('artistid');
326dacbf 131 __PACKAGE__->has_many(cds => 'MyApp::Schema::Result::CD', 'artistid');
daec44b8 132
a0638a7b 133 1;
daec44b8 134
30e1753a 135A result class to represent a CD, which belongs to an artist, in
53aa53f3 136F<MyApp/Schema/Result/CD.pm>:
39fe0e65 137
03460bef 138 package MyApp::Schema::Result::CD;
d88ecca6 139 use base qw/DBIx::Class::Core/;
39fe0e65 140
d88ecca6 141 __PACKAGE__->load_components(qw/InflateColumn::DateTime/);
a0638a7b 142 __PACKAGE__->table('cd');
bd077b47 143 __PACKAGE__->add_columns(qw/ cdid artistid title year /);
a0638a7b 144 __PACKAGE__->set_primary_key('cdid');
03460bef 145 __PACKAGE__->belongs_to(artist => 'MyApp::Schema::Result::Artist', 'artistid');
39fe0e65 146
a0638a7b 147 1;
39fe0e65 148
5b56d1ac 149=head2 API usage
150
a0638a7b 151Then you can use these classes in your application's code:
39fe0e65 152
a0638a7b 153 # Connect to your database.
03460bef 154 use MyApp::Schema;
155 my $schema = MyApp::Schema->connect($dbi_dsn, $user, $pass, \%dbi_params);
a0638a7b 156
157 # Query for all artists and put them in an array,
158 # or retrieve them as a result set object.
30e1753a 159 # $schema->resultset returns a DBIx::Class::ResultSet
2053ab2a 160 my @all_artists = $schema->resultset('Artist')->all;
161 my $all_artists_rs = $schema->resultset('Artist');
126042ee 162
30e1753a 163 # Output all artists names
4e8ffded 164 # $artist here is a DBIx::Class::Row, which has accessors
16ccb4fe 165 # for all its columns. Rows are also subclasses of your Result class.
85067746 166 foreach $artist (@all_artists) {
30e1753a 167 print $artist->name, "\n";
168 }
169
a0638a7b 170 # Create a result set to search for artists.
86beca1d 171 # This does not query the DB.
2053ab2a 172 my $johns_rs = $schema->resultset('Artist')->search(
6576ef54 173 # Build your WHERE using an SQL::Abstract structure:
2053ab2a 174 { name => { like => 'John%' } }
a0638a7b 175 );
39fe0e65 176
2053ab2a 177 # Execute a joined query to get the cds.
a0638a7b 178 my @all_john_cds = $johns_rs->search_related('cds')->all;
448c8424 179
f0bb26f3 180 # Fetch the next available row.
a0638a7b 181 my $first_john = $johns_rs->next;
448c8424 182
2053ab2a 183 # Specify ORDER BY on the query.
a0638a7b 184 my $first_john_cds_by_title_rs = $first_john->cds(
185 undef,
186 { order_by => 'title' }
187 );
448c8424 188
bd077b47 189 # Create a result set that will fetch the artist data
2053ab2a 190 # at the same time as it fetches CDs, using only one query.
884559b1 191 my $millennium_cds_rs = $schema->resultset('CD')->search(
a0638a7b 192 { year => 2000 },
193 { prefetch => 'artist' }
194 );
448c8424 195
880a1a0c 196 my $cd = $millennium_cds_rs->next; # SELECT ... FROM cds JOIN artists ...
bd077b47 197 my $cd_artist_name = $cd->artist->name; # Already has the data so no 2nd query
076652e8 198
fb13a49f 199 # new() makes a Result object but doesnt insert it into the DB.
264f1571 200 # create() is the same as new() then insert().
884559b1 201 my $new_cd = $schema->resultset('CD')->new({ title => 'Spoon' });
f183eccd 202 $new_cd->artist($cd->artist);
f183eccd 203 $new_cd->insert; # Auto-increment primary key filled in after INSERT
f183eccd 204 $new_cd->title('Fork');
205
884559b1 206 $schema->txn_do(sub { $new_cd->update }); # Runs the update in a transaction
f183eccd 207
bd077b47 208 # change the year of all the millennium CDs at once
209 $millennium_cds_rs->update({ year => 2002 });
f183eccd 210
211=head1 DESCRIPTION
212
213This is an SQL to OO mapper with an object API inspired by L<Class::DBI>
bd077b47 214(with a compatibility layer as a springboard for porting) and a resultset API
f183eccd 215that allows abstract encapsulation of database operations. It aims to make
216representing queries in your code as perl-ish as possible while still
a0638a7b 217providing access to as many of the capabilities of the database as possible,
f183eccd 218including retrieving related records from multiple tables in a single query,
53aa53f3 219C<JOIN>, C<LEFT JOIN>, C<COUNT>, C<DISTINCT>, C<GROUP BY>, C<ORDER BY> and
220C<HAVING> support.
f183eccd 221
222DBIx::Class can handle multi-column primary and foreign keys, complex
223queries and database-level paging, and does its best to only query the
75d07914 224database in order to return something you've directly asked for. If a
225resultset is used as an iterator it only fetches rows off the statement
226handle as requested in order to minimise memory usage. It has auto-increment
2053ab2a 227support for SQLite, MySQL, PostgreSQL, Oracle, SQL Server and DB2 and is
228known to be used in production on at least the first four, and is fork-
ec6415a9 229and thread-safe out of the box (although
9361b05d 230L<your DBD may not be|DBI/Threads and Thread Safety>).
f183eccd 231
dfccde48 232This project is still under rapid development, so large new features may be
53aa53f3 233marked B<experimental> - such APIs are still usable but may have edge bugs.
234Failing test cases are I<always> welcome and point releases are put out rapidly
dfccde48 235as bugs are found and fixed.
236
237We do our best to maintain full backwards compatibility for published
238APIs, since DBIx::Class is used in production in many organisations,
239and even backwards incompatible changes to non-published APIs will be fixed
240if they're reported and doing so doesn't cost the codebase anything.
241
264f1571 242The test suite is quite substantial, and several developer releases
243are generally made to CPAN before the branch for the next release is
244merged back to trunk for a major release.
f183eccd 245
6ed05cfd 246=head1 HOW TO CONTRIBUTE
247
248Contributions are always welcome, in all usable forms (we especially
249welcome documentation improvements). The delivery methods include git-
250or unified-diff formatted patches, GitHub pull requests, or plain bug
251reports either via RT or the Mailing list. Contributors are generally
252granted full access to the official repository after their first patch
253passes successful review.
254
255=for comment
256FIXME: Getty, frew and jnap need to get off their asses and finish the contrib section so we can link it here ;)
257
258This project is maintained in a git repository. The code and related tools are
259accessible at the following locations:
260
261=over
262
263=item * Official repo: L<git://git.shadowcat.co.uk/dbsrgits/DBIx-Class.git>
264
265=item * Official gitweb: L<http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=dbsrgits/DBIx-Class.git>
266
267=item * GitHub mirror: L<https://github.com/dbsrgits/DBIx-Class>
268
269=item * Authorized committers: L<ssh://dbsrgits@git.shadowcat.co.uk/DBIx-Class.git>
270
271=item * Travis-CI log: L<https://travis-ci.org/dbsrgits/dbix-class/builds>
272
6ed05cfd 273=back
274
3942ab4d 275=head1 AUTHOR
34d52be2 276
266bdcc3 277mst: Matt S. Trout <mst@shadowcatsystems.co.uk>
34d52be2 278
dfccde48 279(I mostly consider myself "project founder" these days but the AUTHOR heading
280is traditional :)
281
3942ab4d 282=head1 CONTRIBUTORS
283
907ed671 284abraxxa: Alexander Hartmaier <abraxxa@cpan.org>
84e3c114 285
fd4b0742 286acca: Alexander Kuznetsov <acca@cpan.org>
287
6d84db2c 288aherzog: Adam Herzog <adam@herzogdesigns.com>
289
daeb1865 290Alexander Keusch <cpan@keusch.at>
291
c3de6b51 292alexrj: Alessandro Ranellucci <aar@cpan.org>
293
630ba6e5 294alnewkirk: Al Newkirk <we@ana.im>
295
6ebf5cbb 296amiri: Amiri Barksdale <amiri@metalabel.com>
297
b703fec7 298amoore: Andrew Moore <amoore@cpan.org>
299
913b4bae 300andrewalker: Andre Walker <andre@andrewalker.net>
301
266bdcc3 302andyg: Andy Grundman <andy@hybridized.org>
3942ab4d 303
266bdcc3 304ank: Andres Kievsky
3942ab4d 305
59ac6523 306arc: Aaron Crane <arc@cpan.org>
307
624764ae 308arcanez: Justin Hunter <justin.d.hunter@gmail.com>
309
ce4c07df 310ash: Ash Berlin <ash@cpan.org>
311
f8213ab0 312bert: Norbert Csongrádi <bert@cpan.org>
3d5bd2af 313
967a4c40 314blblack: Brandon L. Black <blblack@gmail.com>
3942ab4d 315
62eb8fe8 316bluefeet: Aran Deltac <bluefeet@cpan.org>
317
d6170b26 318bphillips: Brian Phillips <bphillips@cpan.org>
319
f856fe01 320boghead: Bryan Beeley <cpan@beeley.org>
321
23d9df41 322brd: Brad Davis <brd@FreeBSD.org>
323
bcb8f3ed 324bricas: Brian Cassidy <bricas@cpan.org>
325
3d7e3e05 326brunov: Bruno Vecchi <vecchi.b@gmail.com>
327
281719d2 328caelum: Rafael Kitover <rkitover@cpan.org>
329
f5f2af8f 330caldrin: Maik Hentsche <maik.hentsche@amd.com>
331
d3b0e369 332castaway: Jess Robinson
3942ab4d 333
266bdcc3 334claco: Christopher H. Laco
ccb9c9b1 335
266bdcc3 336clkao: CL Kao
3942ab4d 337
e21dfd6a 338da5id: David Jack Olrik <djo@cpan.org>
18360aed 339
11343b34 340dariusj: Darius Jokilehto <dariusjokilehto@yahoo.co.uk>
341
5f7ff3f0 342davewood: David Schmidt <davewood@gmx.at>
343
97ad6fb8 344daxim: Lars Dɪᴇᴄᴋᴏᴡ 迪拉斯 <daxim@cpan.org>
345
13de943d 346debolaz: Anders Nor Berle <berle@cpan.org>
347
d1f542db 348dew: Dan Thomas <dan@godders.org>
349
266bdcc3 350dkubb: Dan Kubb <dan.kubb-cpan@onautopilot.com>
ccb9c9b1 351
9382ad07 352dnm: Justin Wheeler <jwheeler@datademons.com>
353
0818c9a7 354dpetrov: Dimitar Petrov <mitakaa@gmail.com>
355
266bdcc3 356dwc: Daniel Westermann-Clark <danieltwc@cpan.org>
4685e006 357
5cffe785 358dyfrgi: Michael Leuchtenburg <michael@slashhome.org>
8fe164b9 359
7b71391b 360edenc: Eden Cardim <edencardim@gmail.com>
361
bfcecabc 362ether: Karen Etheridge <ether@cpan.org>
363
307f1265 364felliott: Fitz Elliott <fitz.elliott@gmail.com>
365
0ffada27 366freetime: Bill Moseley <moseley@hank.org>
367
ade0fe3b 368frew: Arthur Axel "fREW" Schmidt <frioux@gmail.com>
369
b4987ed0 370goraxe: Gordon Irving <goraxe@cpan.org>
371
d3b0e369 372gphat: Cory G Watson <gphat@cpan.org>
ad3d2d7c 373
61f031bf 374Grant Street Group L<http://www.grantstreet.com/>
375
e758ffe6 376groditi: Guillermo Roditi <groditi@cpan.org>
377
6dad89f5 378Haarg: Graham Knop <haarg@haarg.org>
379
157ce0cf 380hobbs: Andrew Rodland <arodland@cpan.org>
381
5d779578 382ilmari: Dagfinn Ilmari MannsE<aring>ker <ilmari@ilmari.org>
383
0ac0af6c 384initself: Mike Baas <mike@initselftech.com>
385
2bb4c37b 386ironcamel: Naveed Massjouni <naveedm9@gmail.com>
387
f165eda8 388jawnsy: Jonathan Yu <jawnsy@cpan.org>
389
bee21976 390jasonmay: Jason May <jason.a.may@gmail.com>
391
d3b0e369 392jesper: Jesper Krogh
5fb0c64c 393
4a743a00 394jgoulah: John Goulah <jgoulah@cpan.org>
395
102a2984 396jguenther: Justin Guenther <jguenther@cpan.org>
d7c4c15c 397
a14a46e2 398jhannah: Jay Hannah <jay@jays.net>
399
dffda3a2 400jmac: Jason McIntosh <jmac@appleseed-sc.com>
401
8b93a938 402jnapiorkowski: John Napiorkowski <jjn1056@yahoo.com>
403
11736b4c 404jon: Jon Schutz <jjschutz@cpan.org>
405
1aec4bac 406jshirley: J. Shirley <jshirley@gmail.com>
407
41519379 408kaare: Kaare Rasmussen
409
d3b0e369 410konobi: Scott McWhirter
535fc2ee 411
4e0a89e4 412littlesavage: Alexey Illarionov <littlesavage@orionet.ru>
413
4367679a 414lukes: Luke Saunders <luke.saunders@gmail.com>
415
709ea492 416marcus: Marcus Ramberg <mramberg@cpan.org>
417
114780ee 418mattlaw: Matt Lawrence
419
45bffdf0 420mattp: Matt Phillips <mattp@cpan.org>
421
58755bba 422michaelr: Michael Reddick <michael.reddick@gmail.com>
423
91d0c99f 424milki: Jonathan Chu <milki@rescomp.berkeley.edu>
425
b81d8515 426mithaldu: Christian Walde <walde.christian@gmail.com>
427
582fe49d 428mjemmeson: Michael Jemmeson <michael.jemmeson@gmail.com>
429
167e7634 430mstratman: Mark A. Stratman <stratman@gmail.com>
431
77e7e47d 432ned: Neil de Carteret
433
266bdcc3 434nigel: Nigel Metheringham <nigelm@cpan.org>
6565b410 435
d3b0e369 436ningu: David Kamholz <dkamholz@cpan.org>
437
66cf3a84 438Nniuq: Ron "Quinn" Straight" <quinnfazigu@gmail.org>
439
20b4c148 440norbi: Norbert Buchmuller <norbi@nix.hu>
441
48580715 442nuba: Nuba Princigalli <nuba@cpan.org>
443
d3b0e369 444Numa: Dan Sully <daniel@cpan.org>
445
dc571b76 446ovid: Curtis "Ovid" Poe <ovid@cpan.org>
447
6c30f9c3 448oyse: E<Oslash>ystein Torget <oystein.torget@dnv.com>
bf356c54 449
266bdcc3 450paulm: Paul Makepeace
4763f4b7 451
d3b0e369 452penguin: K J Cheetham
453
8cfef6f5 454perigrin: Chris Prather <chris@prather.org>
455
14899528 456peter: Peter Collingbourne <peter@pcc.me.uk>
caac1708 457
f8213ab0 458Peter Siklósi <einon@einon.hu>
459
6c30f9c3 460Peter Valdemar ME<oslash>rch <peter@morch.com>
461
266bdcc3 462phaylon: Robert Sedlacek <phaylon@dunkelheit.at>
a53b95f1 463
56fadd8f 464plu: Johannes Plunien <plu@cpan.org>
465
0c1a4a15 466Possum: Daniel LeWarne <possum@cpan.org>
467
d3b0e369 468quicksilver: Jules Bean
022e0893 469
4ed01b34 470rafl: Florian Ragwitz <rafl@debian.org>
471
0c1a4a15 472rainboxx: Matthias Dietrich <perl@rb.ly>
473
868a7b26 474rbo: Robert Bohne <rbo@cpan.org>
475
7ff0dace 476rbuels: Robert Buels <rmb32@cornell.edu>
477
0da8b7da 478rdj: Ryan D Johnson <ryan@innerfence.com>
479
66b1e361 480ribasushi: Peter Rabbitson <ribasushi@cpan.org>
d76e282a 481
b487918c 482rjbs: Ricardo Signes <rjbs@cpan.org>
483
6ffb5be5 484robkinyon: Rob Kinyon <rkinyon@cpan.org>
485
726c8f65 486Robert Olson <bob@rdolson.org>
487
a93441f2 488moltar: Roman Filippov <romanf@cpan.org>
e4c9f3f0 489
dc81dba3 490Sadrak: Felix Antonius Wilhelm Ostmann <sadrak@cpan.org>
491
d3b0e369 492sc_: Just Another Perl Hacker
ba606e58 493
266bdcc3 494scotty: Scotty Allen <scotty@scottyallen.com>
181a28f4 495
1c133e22 496semifor: Marc Mims <marc@questright.com>
497
16667b3a 498SineSwiper: Brendan Byrd <bbyrd@cpan.org>
499
20b4c148 500solomon: Jared Johnson <jaredj@nmgi.com>
501
88f937fb 502spb: Stephen Bennett <stephen@freenode.net>
503
59187a3b 504Squeeks <squeek@cpan.org>
505
637ca936 506sszabo: Stephan Szabo <sszabo@bigpanda.com>
507
a9e8284f 508talexb: Alex Beamish <talexb@gmail.com>
509
7bf0d0d6 510tamias: Ronald J Kimball <rjk@tamias.net>
f92a9d79 511
386c2272 512teejay : Aaron Trevena <teejay@cpan.org>
513
84e3c114 514Todd Lipcon
e063fe2c 515
6eb6264e 516Tom Hukins
517
2e4b6d78 518tonvoon: Ton Voon <tonvoon@cpan.org>
519
d15e3fc5 520triode: Pete Gamache <gamache@cpan.org>
521
d3b0e369 522typester: Daisuke Murase <typester@cpan.org>
c0e7b4e5 523
4740bdb7 524victori: Victor Igumnov <victori@cpan.org>
525
d3b0e369 526wdh: Will Hawes
4c248161 527
124162a0 528wesm: Wes Malone <wes@mitsi.com>
529
00c03ced 530willert: Sebastian Willert <willert@cpan.org>
531
66d2a14e 532wreis: Wallace Reis <wreis@cpan.org>
533
d7c37d66 534xenoterracide: Caleb Cushing <xenoterracide@gmail.com>
535
d52d4d6e 536yrlnry: Mark Jason Dominus <mjd@plover.com>
537
d3b0e369 538zamolxes: Bogdan Lucaciu <bogdan@wiz.ro>
78060df8 539
f9139687 540Zefram: Andrew Main <zefram@fysh.org>
541
b38e10bd 542=head1 COPYRIGHT
543
16be93fe 544Copyright (c) 2005 - 2011 the DBIx::Class L</AUTHOR> and L</CONTRIBUTORS>
b38e10bd 545as listed above.
546
96154ef7 547=head1 LICENSE
548
549This library is free software and may be distributed under the same terms
550as perl itself.