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