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