Proper end-of-file for DBIx/Class.pm
[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.082700_06';
15
16 $VERSION = eval $VERSION if $VERSION =~ /_/; # numify for warning-free dev releases
17
18 use DBIx::Class::_Util;
19 use mro 'c3';
20
21 use DBIx::Class::Optional::Dependencies;
22
23 use base qw/DBIx::Class::Componentised DBIx::Class::AccessorGroup/;
24 use DBIx::Class::StartupCheck;
25 use DBIx::Class::Exception;
26
27 __PACKAGE__->mk_group_accessors(inherited => '_skip_namespace_frames');
28 __PACKAGE__->_skip_namespace_frames('^DBIx::Class|^SQL::Abstract|^Try::Tiny|^Class::Accessor::Grouped|^Context::Preserve');
29
30 sub mk_classdata {
31   shift->mk_classaccessor(@_);
32 }
33
34 sub mk_classaccessor {
35   my $self = shift;
36   $self->mk_group_accessors('inherited', $_[0]);
37   $self->set_inherited(@_) if @_ > 1;
38 }
39
40 sub component_base_class { 'DBIx::Class' }
41
42 sub MODIFY_CODE_ATTRIBUTES {
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 ();
48 }
49
50 sub _attr_cache {
51   my $self = shift;
52   my $cache = $self->can('__attr_cache') ? $self->__attr_cache : {};
53
54   return {
55     %$cache,
56     %{ $self->maybe::next::method || {} },
57   };
58 }
59
60 # *DO NOT* change this URL nor the identically named =head1 below
61 # it is linked throughout the ecosystem
62 sub DBIx::Class::_ENV_::HELP_URL () {
63   'http://p3rl.org/DBIx::Class#GETTING_HELP/SUPPORT'
64 }
65
66 1;
67
68 __END__
69
70 =encoding UTF-8
71
72 =head1 NAME
73
74 DBIx::Class - Extensible and flexible object <-> relational mapper.
75
76 =head1 WHERE TO START READING
77
78 See L<DBIx::Class::Manual::DocMap> for an overview of the exhaustive documentation.
79 To get the most out of DBIx::Class with the least confusion it is strongly
80 recommended to read (at the very least) the
81 L<Manuals|DBIx::Class::Manual::DocMap/Manuals> in the order presented there.
82
83 =cut
84
85 =head1 GETTING HELP/SUPPORT
86
87 Due to the sheer size of its problem domain, DBIx::Class is a relatively
88 complex framework. After you start using DBIx::Class questions will inevitably
89 arise. If you are stuck with a problem or have doubts about a particular
90 approach do not hesitate to contact us via any of the following options (the
91 list is sorted by "fastest response time"):
92
93 =over
94
95 =item * IRC: irc.perl.org#dbix-class
96
97 =for html
98 <a href="https://chat.mibbit.com/#dbix-class@irc.perl.org">(click for instant chatroom login)</a>
99
100 =item * Mailing list: L<http://lists.scsys.co.uk/mailman/listinfo/dbix-class>
101
102 =item * RT Bug Tracker: L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=DBIx-Class>
103
104 =item * Twitter: L<https://www.twitter.com/dbix_class>
105
106 =item * Web Site: L<http://www.dbix-class.org/>
107
108 =back
109
110 =head1 SYNOPSIS
111
112 For the very impatient: L<DBIx::Class::Manual::QuickStart>
113
114 This code in the next step can be generated automatically from an existing
115 database, see L<dbicdump> from the distribution C<DBIx-Class-Schema-Loader>.
116
117 =head2 Schema classes preparation
118
119 Create a schema class called F<MyApp/Schema.pm>:
120
121   package MyApp::Schema;
122   use base qw/DBIx::Class::Schema/;
123
124   __PACKAGE__->load_namespaces();
125
126   1;
127
128 Create a result class to represent artists, who have many CDs, in
129 F<MyApp/Schema/Result/Artist.pm>:
130
131 See L<DBIx::Class::ResultSource> for docs on defining result classes.
132
133   package MyApp::Schema::Result::Artist;
134   use base qw/DBIx::Class::Core/;
135
136   __PACKAGE__->table('artist');
137   __PACKAGE__->add_columns(qw/ artistid name /);
138   __PACKAGE__->set_primary_key('artistid');
139   __PACKAGE__->has_many(cds => 'MyApp::Schema::Result::CD', 'artistid');
140
141   1;
142
143 A result class to represent a CD, which belongs to an artist, in
144 F<MyApp/Schema/Result/CD.pm>:
145
146   package MyApp::Schema::Result::CD;
147   use base qw/DBIx::Class::Core/;
148
149   __PACKAGE__->load_components(qw/InflateColumn::DateTime/);
150   __PACKAGE__->table('cd');
151   __PACKAGE__->add_columns(qw/ cdid artistid title year /);
152   __PACKAGE__->set_primary_key('cdid');
153   __PACKAGE__->belongs_to(artist => 'MyApp::Schema::Result::Artist', 'artistid');
154
155   1;
156
157 =head2 API usage
158
159 Then you can use these classes in your application's code:
160
161   # Connect to your database.
162   use MyApp::Schema;
163   my $schema = MyApp::Schema->connect($dbi_dsn, $user, $pass, \%dbi_params);
164
165   # Query for all artists and put them in an array,
166   # or retrieve them as a result set object.
167   # $schema->resultset returns a DBIx::Class::ResultSet
168   my @all_artists = $schema->resultset('Artist')->all;
169   my $all_artists_rs = $schema->resultset('Artist');
170
171   # Output all artists names
172   # $artist here is a DBIx::Class::Row, which has accessors
173   # for all its columns. Rows are also subclasses of your Result class.
174   foreach $artist (@all_artists) {
175     print $artist->name, "\n";
176   }
177
178   # Create a result set to search for artists.
179   # This does not query the DB.
180   my $johns_rs = $schema->resultset('Artist')->search(
181     # Build your WHERE using an SQL::Abstract structure:
182     { name => { like => 'John%' } }
183   );
184
185   # Execute a joined query to get the cds.
186   my @all_john_cds = $johns_rs->search_related('cds')->all;
187
188   # Fetch the next available row.
189   my $first_john = $johns_rs->next;
190
191   # Specify ORDER BY on the query.
192   my $first_john_cds_by_title_rs = $first_john->cds(
193     undef,
194     { order_by => 'title' }
195   );
196
197   # Create a result set that will fetch the artist data
198   # at the same time as it fetches CDs, using only one query.
199   my $millennium_cds_rs = $schema->resultset('CD')->search(
200     { year => 2000 },
201     { prefetch => 'artist' }
202   );
203
204   my $cd = $millennium_cds_rs->next; # SELECT ... FROM cds JOIN artists ...
205   my $cd_artist_name = $cd->artist->name; # Already has the data so no 2nd query
206
207   # new() makes a Result object but doesnt insert it into the DB.
208   # create() is the same as new() then insert().
209   my $new_cd = $schema->resultset('CD')->new({ title => 'Spoon' });
210   $new_cd->artist($cd->artist);
211   $new_cd->insert; # Auto-increment primary key filled in after INSERT
212   $new_cd->title('Fork');
213
214   $schema->txn_do(sub { $new_cd->update }); # Runs the update in a transaction
215
216   # change the year of all the millennium CDs at once
217   $millennium_cds_rs->update({ year => 2002 });
218
219 =head1 DESCRIPTION
220
221 This is an SQL to OO mapper with an object API inspired by L<Class::DBI>
222 (with a compatibility layer as a springboard for porting) and a resultset API
223 that allows abstract encapsulation of database operations. It aims to make
224 representing queries in your code as perl-ish as possible while still
225 providing access to as many of the capabilities of the database as possible,
226 including retrieving related records from multiple tables in a single query,
227 C<JOIN>, C<LEFT JOIN>, C<COUNT>, C<DISTINCT>, C<GROUP BY>, C<ORDER BY> and
228 C<HAVING> support.
229
230 DBIx::Class can handle multi-column primary and foreign keys, complex
231 queries and database-level paging, and does its best to only query the
232 database in order to return something you've directly asked for. If a
233 resultset is used as an iterator it only fetches rows off the statement
234 handle as requested in order to minimise memory usage. It has auto-increment
235 support for SQLite, MySQL, PostgreSQL, Oracle, SQL Server and DB2 and is
236 known to be used in production on at least the first four, and is fork-
237 and thread-safe out of the box (although
238 L<your DBD may not be|DBI/Threads and Thread Safety>).
239
240 This project is still under rapid development, so large new features may be
241 marked B<experimental> - such APIs are still usable but may have edge bugs.
242 Failing test cases are I<always> welcome and point releases are put out rapidly
243 as bugs are found and fixed.
244
245 We do our best to maintain full backwards compatibility for published
246 APIs, since DBIx::Class is used in production in many organisations,
247 and even backwards incompatible changes to non-published APIs will be fixed
248 if they're reported and doing so doesn't cost the codebase anything.
249
250 The test suite is quite substantial, and several developer releases
251 are generally made to CPAN before the branch for the next release is
252 merged back to trunk for a major release.
253
254 =head1 HOW TO CONTRIBUTE
255
256 Contributions are always welcome, in all usable forms (we especially
257 welcome documentation improvements). The delivery methods include git-
258 or unified-diff formatted patches, GitHub pull requests, or plain bug
259 reports either via RT or the Mailing list. Contributors are generally
260 granted full access to the official repository after their first patch
261 passes successful review.
262
263 =for comment
264 FIXME: Getty, frew and jnap need to get off their asses and finish the contrib section so we can link it here ;)
265
266 This project is maintained in a git repository. The code and related tools are
267 accessible at the following locations:
268
269 =over
270
271 =item * Official repo: L<git://git.shadowcat.co.uk/dbsrgits/DBIx-Class.git>
272
273 =item * Official gitweb: L<http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=dbsrgits/DBIx-Class.git>
274
275 =item * GitHub mirror: L<https://github.com/dbsrgits/DBIx-Class>
276
277 =item * Authorized committers: L<ssh://dbsrgits@git.shadowcat.co.uk/DBIx-Class.git>
278
279 =item * Travis-CI log: L<https://travis-ci.org/dbsrgits/dbix-class/builds>
280
281 =for html
282 &#x21AA; Stable branch CI status: <img src="https://secure.travis-ci.org/dbsrgits/dbix-class.png?branch=master"></img>
283
284 =back
285
286 =head1 AUTHOR
287
288 mst: Matt S. Trout <mst@shadowcatsystems.co.uk>
289
290 (I mostly consider myself "project founder" these days but the AUTHOR heading
291 is traditional :)
292
293 =head1 CONTRIBUTORS
294
295 abraxxa: Alexander Hartmaier <abraxxa@cpan.org>
296
297 acca: Alexander Kuznetsov <acca@cpan.org>
298
299 aherzog: Adam Herzog <adam@herzogdesigns.com>
300
301 Alexander Keusch <cpan@keusch.at>
302
303 alexrj: Alessandro Ranellucci <aar@cpan.org>
304
305 alnewkirk: Al Newkirk <we@ana.im>
306
307 amiri: Amiri Barksdale <amiri@metalabel.com>
308
309 amoore: Andrew Moore <amoore@cpan.org>
310
311 Andrew Mehta <Andrew@unitedgames.co.uk>
312
313 andrewalker: Andre Walker <andre@andrewalker.net>
314
315 andyg: Andy Grundman <andy@hybridized.org>
316
317 ank: Andres Kievsky
318
319 arc: Aaron Crane <arc@cpan.org>
320
321 arcanez: Justin Hunter <justin.d.hunter@gmail.com>
322
323 ash: Ash Berlin <ash@cpan.org>
324
325 bert: Norbert Csongrádi <bert@cpan.org>
326
327 bfwg: Colin Newell <colin.newell@gmail.com>
328
329 blblack: Brandon L. Black <blblack@gmail.com>
330
331 bluefeet: Aran Deltac <bluefeet@cpan.org>
332
333 bphillips: Brian Phillips <bphillips@cpan.org>
334
335 boghead: Bryan Beeley <cpan@beeley.org>
336
337 brd: Brad Davis <brd@FreeBSD.org>
338
339 bricas: Brian Cassidy <bricas@cpan.org>
340
341 brunov: Bruno Vecchi <vecchi.b@gmail.com>
342
343 caelum: Rafael Kitover <rkitover@cpan.org>
344
345 caldrin: Maik Hentsche <maik.hentsche@amd.com>
346
347 castaway: Jess Robinson
348
349 claco: Christopher H. Laco
350
351 clkao: CL Kao
352
353 Ctrl-o L<http://ctrlo.com/>
354
355 da5id: David Jack Olrik <djo@cpan.org>
356
357 dariusj: Darius Jokilehto <dariusjokilehto@yahoo.co.uk>
358
359 davewood: David Schmidt <davewood@gmx.at>
360
361 daxim: Lars Dɪᴇᴄᴋᴏᴡ 迪拉斯 <daxim@cpan.org>
362
363 debolaz: Anders Nor Berle <berle@cpan.org>
364
365 dew: Dan Thomas <dan@godders.org>
366
367 dim0xff: Dmitry Latin <dim0xff@gmail.com>
368
369 dkubb: Dan Kubb <dan.kubb-cpan@onautopilot.com>
370
371 dnm: Justin Wheeler <jwheeler@datademons.com>
372
373 dpetrov: Dimitar Petrov <mitakaa@gmail.com>
374
375 duncan_dmg: Duncan Garland <Duncan.Garland@motortrak.com>
376
377 dwc: Daniel Westermann-Clark <danieltwc@cpan.org>
378
379 dyfrgi: Michael Leuchtenburg <michael@slashhome.org>
380
381 edenc: Eden Cardim <edencardim@gmail.com>
382
383 Eligo L<http://eligo.co.uk/>
384
385 ether: Karen Etheridge <ether@cpan.org>
386
387 felliott: Fitz Elliott <fitz.elliott@gmail.com>
388
389 freetime: Bill Moseley <moseley@hank.org>
390
391 frew: Arthur Axel "fREW" Schmidt <frioux@gmail.com>
392
393 goraxe: Gordon Irving <goraxe@cpan.org>
394
395 gphat: Cory G Watson <gphat@cpan.org>
396
397 Grant Street Group L<http://www.grantstreet.com/>
398
399 groditi: Guillermo Roditi <groditi@cpan.org>
400
401 guacamole: Fred Steinberg <fred.steinberg@gmail.com>
402
403 Haarg: Graham Knop <haarg@haarg.org>
404
405 hobbs: Andrew Rodland <arodland@cpan.org>
406
407 idn: Ian Norton <i.norton@shadowcat.co.uk>
408
409 ilmari: Dagfinn Ilmari MannsE<aring>ker <ilmari@ilmari.org>
410
411 initself: Mike Baas <mike@initselftech.com>
412
413 ironcamel: Naveed Massjouni <naveedm9@gmail.com>
414
415 jawnsy: Jonathan Yu <jawnsy@cpan.org>
416
417 jasonmay: Jason May <jason.a.may@gmail.com>
418
419 jegade: Jens Gassmann <jens.gassmann@atomix.de>
420
421 jeneric: Eric A. Miller <emiller@cpan.org>
422
423 jesper: Jesper Krogh
424
425 jgoulah: John Goulah <jgoulah@cpan.org>
426
427 jguenther: Justin Guenther <jguenther@cpan.org>
428
429 jhannah: Jay Hannah <jay@jays.net>
430
431 jmac: Jason McIntosh <jmac@appleseed-sc.com>
432
433 jnapiorkowski: John Napiorkowski <jjn1056@yahoo.com>
434
435 jon: Jon Schutz <jjschutz@cpan.org>
436
437 Joe Carlson <jwcarlson@lbl.gov>
438
439 jshirley: J. Shirley <jshirley@gmail.com>
440
441 kaare: Kaare Rasmussen
442
443 konobi: Scott McWhirter
444
445 littlesavage: Alexey Illarionov <littlesavage@orionet.ru>
446
447 lukes: Luke Saunders <luke.saunders@gmail.com>
448
449 marcus: Marcus Ramberg <mramberg@cpan.org>
450
451 mattlaw: Matt Lawrence
452
453 mattp: Matt Phillips <mattp@cpan.org>
454
455 mdk: Mark Keating <m.keating@shadowcat.co.uk>
456
457 mna: Maya
458
459 michaelr: Michael Reddick <michael.reddick@gmail.com>
460
461 milki: Jonathan Chu <milki@rescomp.berkeley.edu>
462
463 minty: Murray Walker <perl@minty.org>
464
465 mithaldu: Christian Walde <walde.christian@gmail.com>
466
467 mjemmeson: Michael Jemmeson <michael.jemmeson@gmail.com>
468
469 mrf: Mike Francis <ungrim97@gmail.com>
470
471 mstratman: Mark A. Stratman <stratman@gmail.com>
472
473 ned: Neil de Carteret
474
475 nigel: Nigel Metheringham <nigelm@cpan.org>
476
477 ningu: David Kamholz <dkamholz@cpan.org>
478
479 Nniuq: Ron "Quinn" Straight" <quinnfazigu@gmail.org>
480
481 norbi: Norbert Buchmuller <norbi@nix.hu>
482
483 nuba: Nuba Princigalli <nuba@cpan.org>
484
485 Numa: Dan Sully <daniel@cpan.org>
486
487 oalders: Olaf Alders <olaf@wundersolutions.com>
488
489 ovid: Curtis "Ovid" Poe <ovid@cpan.org>
490
491 oyse: E<Oslash>ystein Torget <oystein.torget@dnv.com>
492
493 paulm: Paul Makepeace
494
495 penguin: K J Cheetham
496
497 perigrin: Chris Prather <chris@prather.org>
498
499 peter: Peter Collingbourne <peter@pcc.me.uk>
500
501 Peter Siklósi <einon@einon.hu>
502
503 Peter Valdemar ME<oslash>rch <peter@morch.com>
504
505 phaylon: Robert Sedlacek <phaylon@dunkelheit.at>
506
507 plu: Johannes Plunien <plu@cpan.org>
508
509 pplu: Jose Luis Martinez <jlmartinez@capside.com>
510
511 Possum: Daniel LeWarne <possum@cpan.org>
512
513 quicksilver: Jules Bean
514
515 rafl: Florian Ragwitz <rafl@debian.org>
516
517 rainboxx: Matthias Dietrich <perl@rb.ly>
518
519 Relequestual: Ben Hutton <relequestual@gmail.com>
520
521 rbo: Robert Bohne <rbo@cpan.org>
522
523 rbuels: Robert Buels <rmb32@cornell.edu>
524
525 rdj: Ryan D Johnson <ryan@innerfence.com>
526
527 ribasushi: Peter Rabbitson <ribasushi@cpan.org>
528
529 rjbs: Ricardo Signes <rjbs@cpan.org>
530
531 robkinyon: Rob Kinyon <rkinyon@cpan.org>
532
533 Robert Olson <bob@rdolson.org>
534
535 moltar: Roman Filippov <romanf@cpan.org>
536
537 Sadrak: Felix Antonius Wilhelm Ostmann <sadrak@cpan.org>
538
539 sc_: Just Another Perl Hacker
540
541 scotty: Scotty Allen <scotty@scottyallen.com>
542
543 semifor: Marc Mims <marc@questright.com>
544
545 SineSwiper: Brendan Byrd <bbyrd@cpan.org>
546
547 skaufman: Samuel Kaufman <sam@socialflow.com>
548
549 solomon: Jared Johnson <jaredj@nmgi.com>
550
551 spb: Stephen Bennett <stephen@freenode.net>
552
553 Squeeks <squeek@cpan.org>
554
555 sszabo: Stephan Szabo <sszabo@bigpanda.com>
556
557 Stephen Peters <steve@stephenpeters.me>
558
559 talexb: Alex Beamish <talexb@gmail.com>
560
561 tamias: Ronald J Kimball <rjk@tamias.net>
562
563 TBSliver: Tom Bloor <t.bloor@shadowcat.co.uk>
564
565 teejay : Aaron Trevena <teejay@cpan.org>
566
567 theorbtwo: James Mastros <james@mastros.biz>
568
569 Todd Lipcon
570
571 Tom Hukins
572
573 tonvoon: Ton Voon <tonvoon@cpan.org>
574
575 triode: Pete Gamache <gamache@cpan.org>
576
577 typester: Daisuke Murase <typester@cpan.org>
578
579 victori: Victor Igumnov <victori@cpan.org>
580
581 wdh: Will Hawes
582
583 wesm: Wes Malone <wes@mitsi.com>
584
585 willert: Sebastian Willert <willert@cpan.org>
586
587 wreis: Wallace Reis <wreis@cpan.org>
588
589 xenoterracide: Caleb Cushing <xenoterracide@gmail.com>
590
591 uree: Oriol Soriano <oriol.soriano@capside.com>
592
593 yrlnry: Mark Jason Dominus <mjd@plover.com>
594
595 zamolxes: Bogdan Lucaciu <bogdan@wiz.ro>
596
597 Zefram: Andrew Main <zefram@fysh.org>
598
599 =head1 COPYRIGHT
600
601 Copyright (c) 2005 - 2011 the DBIx::Class L</AUTHOR> and L</CONTRIBUTORS>
602 as listed above.
603
604 =head1 LICENSE
605
606 This library is free software and may be distributed under the same terms
607 as perl itself.