Require latest EU::MM at dist-building time to avoid $-escaping issues
[dbsrgits/DBIx-Class-Historic.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
4f364257 14$VERSION = '0.08209';
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 ,
0d8817bc 43 };
b4ad8a74 44
0d8817bc 45 if ($] < 5.009_005) {
46 require MRO::Compat;
47 constant->import( OLD_MRO => 1 );
48 }
49 else {
50 require mro;
51 constant->import( OLD_MRO => 0 );
52 }
87bf71d5 53}
54
d38cd95c 55use mro 'c3';
329d7385 56
2527233b 57use DBIx::Class::Optional::Dependencies;
58
db29433c 59use base qw/DBIx::Class::Componentised DBIx::Class::AccessorGroup/;
11736b4c 60use DBIx::Class::StartupCheck;
f9080e45 61use DBIx::Class::Exception;
3e110410 62
70c28808 63__PACKAGE__->mk_group_accessors(inherited => '_skip_namespace_frames');
9345b14c 64__PACKAGE__->_skip_namespace_frames('^DBIx::Class|^SQL::Abstract|^Try::Tiny|^Class::Accessor::Grouped|^Context::Preserve');
70c28808 65
ade0fe3b 66sub mk_classdata {
77d518d1 67 shift->mk_classaccessor(@_);
68}
69
70sub mk_classaccessor {
71 my $self = shift;
ade0fe3b 72 $self->mk_group_accessors('inherited', $_[0]);
77d518d1 73 $self->set_inherited(@_) if @_ > 1;
3e110410 74}
3c0068c1 75
7411204b 76sub component_base_class { 'DBIx::Class' }
227d4dee 77
f0750722 78sub MODIFY_CODE_ATTRIBUTES {
b5d2c57f 79 my ($class,$code,@attrs) = @_;
80 $class->mk_classdata('__attr_cache' => {})
81 unless $class->can('__attr_cache');
82 $class->__attr_cache->{$code} = [@attrs];
83 return ();
f0750722 84}
85
da95b45f 86sub _attr_cache {
b5d2c57f 87 my $self = shift;
88 my $cache = $self->can('__attr_cache') ? $self->__attr_cache : {};
9780718f 89
90 return {
91 %$cache,
92 %{ $self->maybe::next::method || {} },
20674fcd 93 };
da95b45f 94}
95
ea2e61bf 961;
34d52be2 97
75d07914 98=head1 NAME
34d52be2 99
7e4b2f59 100DBIx::Class - Extensible and flexible object <-> relational mapper.
34d52be2 101
3b1c2bbd 102=head1 GETTING HELP/SUPPORT
103
104The community can be found via:
105
a06e1181 106=over
3b1c2bbd 107
c6fdaf2a 108=item * IRC: irc.perl.org#dbix-class
109
110=for html
e3194e31 111<a href="http://chat.mibbit.com/#dbix-class@irc.perl.org">(click for instant chatroom login)</a>
3b1c2bbd 112
a06e1181 113=item * Mailing list: L<http://lists.scsys.co.uk/mailman/listinfo/dbix-class>
3b1c2bbd 114
86a23587 115=item * Twitter L<http://www.twitter.com/dbix_class>
116
117=item * Web Site: L<http://www.dbix-class.org/>
118
a06e1181 119=item * RT Bug Tracker: L<https://rt.cpan.org/Dist/Display.html?Queue=DBIx-Class>
120
86a23587 121=back
122
123The project is maintained in a git repository, accessible from the following sources:
124
125=over
a06e1181 126
aeb669b8 127=item * git: L<git://git.shadowcat.co.uk/dbsrgits/DBIx-Class.git>
a06e1181 128
86a23587 129=item * gitweb: L<http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=dbsrgits/DBIx-Class.git>
130
131=item * github mirror: L<https://github.com/dbsrgits/DBIx-Class>
132
133=item * authorized committers: L<ssh://dbsrgits@git.shadowcat.co.uk/DBIx-Class.git>
43517a60 134
ada3d340 135=item * Travis-CI log: L<http://travis-ci.org/dbsrgits/dbix-class/builds>
136
137=for html
138<img src="https://secure.travis-ci.org/dbsrgits/dbix-class.png?branch=master"></img>
139
a06e1181 140=back
3b1c2bbd 141
34d52be2 142=head1 SYNOPSIS
143
03460bef 144Create a schema class called MyApp/Schema.pm:
34d52be2 145
03460bef 146 package MyApp::Schema;
a0638a7b 147 use base qw/DBIx::Class::Schema/;
34d52be2 148
f0bb26f3 149 __PACKAGE__->load_namespaces();
daec44b8 150
a0638a7b 151 1;
daec44b8 152
30e1753a 153Create a result class to represent artists, who have many CDs, in
03460bef 154MyApp/Schema/Result/Artist.pm:
daec44b8 155
30e1753a 156See L<DBIx::Class::ResultSource> for docs on defining result classes.
157
03460bef 158 package MyApp::Schema::Result::Artist;
d88ecca6 159 use base qw/DBIx::Class::Core/;
daec44b8 160
a0638a7b 161 __PACKAGE__->table('artist');
162 __PACKAGE__->add_columns(qw/ artistid name /);
163 __PACKAGE__->set_primary_key('artistid');
326dacbf 164 __PACKAGE__->has_many(cds => 'MyApp::Schema::Result::CD', 'artistid');
daec44b8 165
a0638a7b 166 1;
daec44b8 167
30e1753a 168A result class to represent a CD, which belongs to an artist, in
03460bef 169MyApp/Schema/Result/CD.pm:
39fe0e65 170
03460bef 171 package MyApp::Schema::Result::CD;
d88ecca6 172 use base qw/DBIx::Class::Core/;
39fe0e65 173
d88ecca6 174 __PACKAGE__->load_components(qw/InflateColumn::DateTime/);
a0638a7b 175 __PACKAGE__->table('cd');
bd077b47 176 __PACKAGE__->add_columns(qw/ cdid artistid title year /);
a0638a7b 177 __PACKAGE__->set_primary_key('cdid');
03460bef 178 __PACKAGE__->belongs_to(artist => 'MyApp::Schema::Result::Artist', 'artistid');
39fe0e65 179
a0638a7b 180 1;
39fe0e65 181
a0638a7b 182Then you can use these classes in your application's code:
39fe0e65 183
a0638a7b 184 # Connect to your database.
03460bef 185 use MyApp::Schema;
186 my $schema = MyApp::Schema->connect($dbi_dsn, $user, $pass, \%dbi_params);
a0638a7b 187
188 # Query for all artists and put them in an array,
189 # or retrieve them as a result set object.
30e1753a 190 # $schema->resultset returns a DBIx::Class::ResultSet
2053ab2a 191 my @all_artists = $schema->resultset('Artist')->all;
192 my $all_artists_rs = $schema->resultset('Artist');
126042ee 193
30e1753a 194 # Output all artists names
4e8ffded 195 # $artist here is a DBIx::Class::Row, which has accessors
16ccb4fe 196 # for all its columns. Rows are also subclasses of your Result class.
85067746 197 foreach $artist (@all_artists) {
30e1753a 198 print $artist->name, "\n";
199 }
200
a0638a7b 201 # Create a result set to search for artists.
86beca1d 202 # This does not query the DB.
2053ab2a 203 my $johns_rs = $schema->resultset('Artist')->search(
6576ef54 204 # Build your WHERE using an SQL::Abstract structure:
2053ab2a 205 { name => { like => 'John%' } }
a0638a7b 206 );
39fe0e65 207
2053ab2a 208 # Execute a joined query to get the cds.
a0638a7b 209 my @all_john_cds = $johns_rs->search_related('cds')->all;
448c8424 210
f0bb26f3 211 # Fetch the next available row.
a0638a7b 212 my $first_john = $johns_rs->next;
448c8424 213
2053ab2a 214 # Specify ORDER BY on the query.
a0638a7b 215 my $first_john_cds_by_title_rs = $first_john->cds(
216 undef,
217 { order_by => 'title' }
218 );
448c8424 219
bd077b47 220 # Create a result set that will fetch the artist data
2053ab2a 221 # at the same time as it fetches CDs, using only one query.
884559b1 222 my $millennium_cds_rs = $schema->resultset('CD')->search(
a0638a7b 223 { year => 2000 },
224 { prefetch => 'artist' }
225 );
448c8424 226
880a1a0c 227 my $cd = $millennium_cds_rs->next; # SELECT ... FROM cds JOIN artists ...
bd077b47 228 my $cd_artist_name = $cd->artist->name; # Already has the data so no 2nd query
076652e8 229
fb13a49f 230 # new() makes a Result object but doesnt insert it into the DB.
264f1571 231 # create() is the same as new() then insert().
884559b1 232 my $new_cd = $schema->resultset('CD')->new({ title => 'Spoon' });
f183eccd 233 $new_cd->artist($cd->artist);
f183eccd 234 $new_cd->insert; # Auto-increment primary key filled in after INSERT
f183eccd 235 $new_cd->title('Fork');
236
884559b1 237 $schema->txn_do(sub { $new_cd->update }); # Runs the update in a transaction
f183eccd 238
bd077b47 239 # change the year of all the millennium CDs at once
240 $millennium_cds_rs->update({ year => 2002 });
f183eccd 241
242=head1 DESCRIPTION
243
244This is an SQL to OO mapper with an object API inspired by L<Class::DBI>
bd077b47 245(with a compatibility layer as a springboard for porting) and a resultset API
f183eccd 246that allows abstract encapsulation of database operations. It aims to make
247representing queries in your code as perl-ish as possible while still
a0638a7b 248providing access to as many of the capabilities of the database as possible,
f183eccd 249including retrieving related records from multiple tables in a single query,
bd077b47 250JOIN, LEFT JOIN, COUNT, DISTINCT, GROUP BY, ORDER BY and HAVING support.
f183eccd 251
252DBIx::Class can handle multi-column primary and foreign keys, complex
253queries and database-level paging, and does its best to only query the
75d07914 254database in order to return something you've directly asked for. If a
255resultset is used as an iterator it only fetches rows off the statement
256handle as requested in order to minimise memory usage. It has auto-increment
2053ab2a 257support for SQLite, MySQL, PostgreSQL, Oracle, SQL Server and DB2 and is
258known to be used in production on at least the first four, and is fork-
ec6415a9 259and thread-safe out of the box (although
9361b05d 260L<your DBD may not be|DBI/Threads and Thread Safety>).
f183eccd 261
dfccde48 262This project is still under rapid development, so large new features may be
263marked EXPERIMENTAL - such APIs are still usable but may have edge bugs.
264Failing test cases are *always* welcome and point releases are put out rapidly
265as bugs are found and fixed.
266
267We do our best to maintain full backwards compatibility for published
268APIs, since DBIx::Class is used in production in many organisations,
269and even backwards incompatible changes to non-published APIs will be fixed
270if they're reported and doing so doesn't cost the codebase anything.
271
264f1571 272The test suite is quite substantial, and several developer releases
273are generally made to CPAN before the branch for the next release is
274merged back to trunk for a major release.
f183eccd 275
f183eccd 276=head1 WHERE TO GO NEXT
277
2ca930b4 278L<DBIx::Class::Manual::DocMap> lists each task you might want help on, and
279the modules where you will find documentation.
076652e8 280
3942ab4d 281=head1 AUTHOR
34d52be2 282
266bdcc3 283mst: Matt S. Trout <mst@shadowcatsystems.co.uk>
34d52be2 284
dfccde48 285(I mostly consider myself "project founder" these days but the AUTHOR heading
286is traditional :)
287
3942ab4d 288=head1 CONTRIBUTORS
289
907ed671 290abraxxa: Alexander Hartmaier <abraxxa@cpan.org>
84e3c114 291
fd4b0742 292acca: Alexander Kuznetsov <acca@cpan.org>
293
6d84db2c 294aherzog: Adam Herzog <adam@herzogdesigns.com>
295
daeb1865 296Alexander Keusch <cpan@keusch.at>
297
c3de6b51 298alexrj: Alessandro Ranellucci <aar@cpan.org>
299
630ba6e5 300alnewkirk: Al Newkirk <we@ana.im>
301
6ebf5cbb 302amiri: Amiri Barksdale <amiri@metalabel.com>
303
b703fec7 304amoore: Andrew Moore <amoore@cpan.org>
305
913b4bae 306andrewalker: Andre Walker <andre@andrewalker.net>
307
266bdcc3 308andyg: Andy Grundman <andy@hybridized.org>
3942ab4d 309
266bdcc3 310ank: Andres Kievsky
3942ab4d 311
59ac6523 312arc: Aaron Crane <arc@cpan.org>
313
624764ae 314arcanez: Justin Hunter <justin.d.hunter@gmail.com>
315
ce4c07df 316ash: Ash Berlin <ash@cpan.org>
317
3d5bd2af 318bert: Norbert Csongradi <bert@cpan.org>
319
967a4c40 320blblack: Brandon L. Black <blblack@gmail.com>
3942ab4d 321
62eb8fe8 322bluefeet: Aran Deltac <bluefeet@cpan.org>
323
d6170b26 324bphillips: Brian Phillips <bphillips@cpan.org>
325
f856fe01 326boghead: Bryan Beeley <cpan@beeley.org>
327
23d9df41 328brd: Brad Davis <brd@FreeBSD.org>
329
bcb8f3ed 330bricas: Brian Cassidy <bricas@cpan.org>
331
3d7e3e05 332brunov: Bruno Vecchi <vecchi.b@gmail.com>
333
281719d2 334caelum: Rafael Kitover <rkitover@cpan.org>
335
f5f2af8f 336caldrin: Maik Hentsche <maik.hentsche@amd.com>
337
d3b0e369 338castaway: Jess Robinson
3942ab4d 339
266bdcc3 340claco: Christopher H. Laco
ccb9c9b1 341
266bdcc3 342clkao: CL Kao
3942ab4d 343
e21dfd6a 344da5id: David Jack Olrik <djo@cpan.org>
18360aed 345
5f7ff3f0 346davewood: David Schmidt <davewood@gmx.at>
347
13de943d 348debolaz: Anders Nor Berle <berle@cpan.org>
349
d1f542db 350dew: Dan Thomas <dan@godders.org>
351
266bdcc3 352dkubb: Dan Kubb <dan.kubb-cpan@onautopilot.com>
ccb9c9b1 353
9382ad07 354dnm: Justin Wheeler <jwheeler@datademons.com>
355
0818c9a7 356dpetrov: Dimitar Petrov <mitakaa@gmail.com>
357
266bdcc3 358dwc: Daniel Westermann-Clark <danieltwc@cpan.org>
4685e006 359
5cffe785 360dyfrgi: Michael Leuchtenburg <michael@slashhome.org>
8fe164b9 361
7b71391b 362edenc: Eden Cardim <edencardim@gmail.com>
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
6c30f9c3 458Peter Valdemar ME<oslash>rch <peter@morch.com>
459
266bdcc3 460phaylon: Robert Sedlacek <phaylon@dunkelheit.at>
a53b95f1 461
56fadd8f 462plu: Johannes Plunien <plu@cpan.org>
463
0c1a4a15 464Possum: Daniel LeWarne <possum@cpan.org>
465
d3b0e369 466quicksilver: Jules Bean
022e0893 467
4ed01b34 468rafl: Florian Ragwitz <rafl@debian.org>
469
0c1a4a15 470rainboxx: Matthias Dietrich <perl@rb.ly>
471
868a7b26 472rbo: Robert Bohne <rbo@cpan.org>
473
7ff0dace 474rbuels: Robert Buels <rmb32@cornell.edu>
475
0da8b7da 476rdj: Ryan D Johnson <ryan@innerfence.com>
477
66b1e361 478ribasushi: Peter Rabbitson <ribasushi@cpan.org>
d76e282a 479
b487918c 480rjbs: Ricardo Signes <rjbs@cpan.org>
481
6ffb5be5 482robkinyon: Rob Kinyon <rkinyon@cpan.org>
483
726c8f65 484Robert Olson <bob@rdolson.org>
485
a93441f2 486moltar: Roman Filippov <romanf@cpan.org>
e4c9f3f0 487
dc81dba3 488Sadrak: Felix Antonius Wilhelm Ostmann <sadrak@cpan.org>
489
d3b0e369 490sc_: Just Another Perl Hacker
ba606e58 491
266bdcc3 492scotty: Scotty Allen <scotty@scottyallen.com>
181a28f4 493
1c133e22 494semifor: Marc Mims <marc@questright.com>
495
16667b3a 496SineSwiper: Brendan Byrd <bbyrd@cpan.org>
497
20b4c148 498solomon: Jared Johnson <jaredj@nmgi.com>
499
88f937fb 500spb: Stephen Bennett <stephen@freenode.net>
501
59187a3b 502Squeeks <squeek@cpan.org>
503
637ca936 504sszabo: Stephan Szabo <sszabo@bigpanda.com>
505
a9e8284f 506talexb: Alex Beamish <talexb@gmail.com>
507
7bf0d0d6 508tamias: Ronald J Kimball <rjk@tamias.net>
f92a9d79 509
386c2272 510teejay : Aaron Trevena <teejay@cpan.org>
511
84e3c114 512Todd Lipcon
e063fe2c 513
6eb6264e 514Tom Hukins
515
2e4b6d78 516tonvoon: Ton Voon <tonvoon@cpan.org>
517
d15e3fc5 518triode: Pete Gamache <gamache@cpan.org>
519
d3b0e369 520typester: Daisuke Murase <typester@cpan.org>
c0e7b4e5 521
4740bdb7 522victori: Victor Igumnov <victori@cpan.org>
523
d3b0e369 524wdh: Will Hawes
4c248161 525
124162a0 526wesm: Wes Malone <wes@mitsi.com>
527
00c03ced 528willert: Sebastian Willert <willert@cpan.org>
529
66d2a14e 530wreis: Wallace Reis <wreis@cpan.org>
531
d7c37d66 532xenoterracide: Caleb Cushing <xenoterracide@gmail.com>
533
d52d4d6e 534yrlnry: Mark Jason Dominus <mjd@plover.com>
535
d3b0e369 536zamolxes: Bogdan Lucaciu <bogdan@wiz.ro>
78060df8 537
b38e10bd 538=head1 COPYRIGHT
539
16be93fe 540Copyright (c) 2005 - 2011 the DBIx::Class L</AUTHOR> and L</CONTRIBUTORS>
b38e10bd 541as listed above.
542
96154ef7 543=head1 LICENSE
544
545This library is free software and may be distributed under the same terms
546as perl itself.
547
34d52be2 548=cut