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