9 *DBIx::Class::_ENV_::OLD_MRO = sub () { 1 };
13 *DBIx::Class::_ENV_::OLD_MRO = sub () { 0 };
16 # ::Runmode would only be loaded by DBICTest, which in turn implies t/
17 *DBIx::Class::_ENV_::DBICTEST = eval { DBICTest::RunMode->is_author }
25 use DBIx::Class::Optional::Dependencies;
27 use vars qw($VERSION);
28 use base qw/DBIx::Class::Componentised DBIx::Class::AccessorGroup/;
29 use DBIx::Class::StartupCheck;
31 __PACKAGE__->mk_group_accessors(inherited => '_skip_namespace_frames');
32 __PACKAGE__->_skip_namespace_frames('^DBIx::Class|^SQL::Abstract|^Try::Tiny');
35 shift->mk_classaccessor(@_);
38 sub mk_classaccessor {
40 $self->mk_group_accessors('inherited', $_[0]);
41 $self->set_inherited(@_) if @_ > 1;
44 sub component_base_class { 'DBIx::Class' }
46 # Always remember to do all digits for the version even if they're 0
47 # i.e. first release of 0.XX *must* be 0.XX000. This avoids fBSD ports
48 # brain damage and presumably various other packaging systems too
51 $VERSION = eval $VERSION if $VERSION =~ /_/; # numify for warning-free dev releases
53 sub MODIFY_CODE_ATTRIBUTES {
54 my ($class,$code,@attrs) = @_;
55 $class->mk_classdata('__attr_cache' => {})
56 unless $class->can('__attr_cache');
57 $class->__attr_cache->{$code} = [@attrs];
63 my $cache = $self->can('__attr_cache') ? $self->__attr_cache : {};
67 %{ $self->maybe::next::method || {} },
75 DBIx::Class - Extensible and flexible object <-> relational mapper.
77 =head1 GETTING HELP/SUPPORT
79 The community can be found via:
83 =item * Web Site: L<http://www.dbix-class.org/>
85 =item * IRC: irc.perl.org#dbix-class
88 <a href="http://chat.mibbit.com/#dbix-class@irc.perl.org">(click for instant chatroom login)</a>
90 =item * Mailing list: L<http://lists.scsys.co.uk/mailman/listinfo/dbix-class>
92 =item * RT Bug Tracker: L<https://rt.cpan.org/Dist/Display.html?Queue=DBIx-Class>
94 =item * gitweb: L<http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=dbsrgits/DBIx-Class.git>
96 =item * git: L<git://git.shadowcat.co.uk/dbsrgits/DBIx-Class.git>
98 =item * twitter L<http://www.twitter.com/dbix_class>
104 Create a schema class called MyDB/Schema.pm:
106 package MyDB::Schema;
107 use base qw/DBIx::Class::Schema/;
109 __PACKAGE__->load_namespaces();
113 Create a result class to represent artists, who have many CDs, in
114 MyDB/Schema/Result/Artist.pm:
116 See L<DBIx::Class::ResultSource> for docs on defining result classes.
118 package MyDB::Schema::Result::Artist;
119 use base qw/DBIx::Class::Core/;
121 __PACKAGE__->table('artist');
122 __PACKAGE__->add_columns(qw/ artistid name /);
123 __PACKAGE__->set_primary_key('artistid');
124 __PACKAGE__->has_many(cds => 'MyDB::Schema::Result::CD');
128 A result class to represent a CD, which belongs to an artist, in
129 MyDB/Schema/Result/CD.pm:
131 package MyDB::Schema::Result::CD;
132 use base qw/DBIx::Class::Core/;
134 __PACKAGE__->load_components(qw/InflateColumn::DateTime/);
135 __PACKAGE__->table('cd');
136 __PACKAGE__->add_columns(qw/ cdid artistid title year /);
137 __PACKAGE__->set_primary_key('cdid');
138 __PACKAGE__->belongs_to(artist => 'MyDB::Schema::Result::Artist', 'artistid');
142 Then you can use these classes in your application's code:
144 # Connect to your database.
146 my $schema = MyDB::Schema->connect($dbi_dsn, $user, $pass, \%dbi_params);
148 # Query for all artists and put them in an array,
149 # or retrieve them as a result set object.
150 # $schema->resultset returns a DBIx::Class::ResultSet
151 my @all_artists = $schema->resultset('Artist')->all;
152 my $all_artists_rs = $schema->resultset('Artist');
154 # Output all artists names
155 # $artist here is a DBIx::Class::Row, which has accessors
156 # for all its columns. Rows are also subclasses of your Result class.
157 foreach $artist (@all_artists) {
158 print $artist->name, "\n";
161 # Create a result set to search for artists.
162 # This does not query the DB.
163 my $johns_rs = $schema->resultset('Artist')->search(
164 # Build your WHERE using an SQL::Abstract structure:
165 { name => { like => 'John%' } }
168 # Execute a joined query to get the cds.
169 my @all_john_cds = $johns_rs->search_related('cds')->all;
171 # Fetch the next available row.
172 my $first_john = $johns_rs->next;
174 # Specify ORDER BY on the query.
175 my $first_john_cds_by_title_rs = $first_john->cds(
177 { order_by => 'title' }
180 # Create a result set that will fetch the artist data
181 # at the same time as it fetches CDs, using only one query.
182 my $millennium_cds_rs = $schema->resultset('CD')->search(
184 { prefetch => 'artist' }
187 my $cd = $millennium_cds_rs->next; # SELECT ... FROM cds JOIN artists ...
188 my $cd_artist_name = $cd->artist->name; # Already has the data so no 2nd query
190 # new() makes a DBIx::Class::Row object but doesnt insert it into the DB.
191 # create() is the same as new() then insert().
192 my $new_cd = $schema->resultset('CD')->new({ title => 'Spoon' });
193 $new_cd->artist($cd->artist);
194 $new_cd->insert; # Auto-increment primary key filled in after INSERT
195 $new_cd->title('Fork');
197 $schema->txn_do(sub { $new_cd->update }); # Runs the update in a transaction
199 # change the year of all the millennium CDs at once
200 $millennium_cds_rs->update({ year => 2002 });
204 This is an SQL to OO mapper with an object API inspired by L<Class::DBI>
205 (with a compatibility layer as a springboard for porting) and a resultset API
206 that allows abstract encapsulation of database operations. It aims to make
207 representing queries in your code as perl-ish as possible while still
208 providing access to as many of the capabilities of the database as possible,
209 including retrieving related records from multiple tables in a single query,
210 JOIN, LEFT JOIN, COUNT, DISTINCT, GROUP BY, ORDER BY and HAVING support.
212 DBIx::Class can handle multi-column primary and foreign keys, complex
213 queries and database-level paging, and does its best to only query the
214 database in order to return something you've directly asked for. If a
215 resultset is used as an iterator it only fetches rows off the statement
216 handle as requested in order to minimise memory usage. It has auto-increment
217 support for SQLite, MySQL, PostgreSQL, Oracle, SQL Server and DB2 and is
218 known to be used in production on at least the first four, and is fork-
219 and thread-safe out of the box (although
220 L<your DBD may not be|DBI/Threads_and_Thread_Safety>).
222 This project is still under rapid development, so large new features may be
223 marked EXPERIMENTAL - such APIs are still usable but may have edge bugs.
224 Failing test cases are *always* welcome and point releases are put out rapidly
225 as bugs are found and fixed.
227 We do our best to maintain full backwards compatibility for published
228 APIs, since DBIx::Class is used in production in many organisations,
229 and even backwards incompatible changes to non-published APIs will be fixed
230 if they're reported and doing so doesn't cost the codebase anything.
232 The test suite is quite substantial, and several developer releases
233 are generally made to CPAN before the branch for the next release is
234 merged back to trunk for a major release.
236 =head1 WHERE TO GO NEXT
238 L<DBIx::Class::Manual::DocMap> lists each task you might want help on, and
239 the modules where you will find documentation.
243 mst: Matt S. Trout <mst@shadowcatsystems.co.uk>
245 (I mostly consider myself "project founder" these days but the AUTHOR heading
250 abraxxa: Alexander Hartmaier <abraxxa@cpan.org>
252 aherzog: Adam Herzog <adam@herzogdesigns.com>
254 Alexander Keusch <cpan@keusch.at>
256 alnewkirk: Al Newkirk <we@ana.im>
258 amiri: Amiri Barksdale <amiri@metalabel.com>
260 amoore: Andrew Moore <amoore@cpan.org>
262 andyg: Andy Grundman <andy@hybridized.org>
266 arc: Aaron Crane <arc@cpan.org>
268 arcanez: Justin Hunter <justin.d.hunter@gmail.com>
270 ash: Ash Berlin <ash@cpan.org>
272 bert: Norbert Csongradi <bert@cpan.org>
274 blblack: Brandon L. Black <blblack@gmail.com>
276 bluefeet: Aran Deltac <bluefeet@cpan.org>
278 bphillips: Brian Phillips <bphillips@cpan.org>
280 boghead: Bryan Beeley <cpan@beeley.org>
282 bricas: Brian Cassidy <bricas@cpan.org>
284 brunov: Bruno Vecchi <vecchi.b@gmail.com>
286 caelum: Rafael Kitover <rkitover@cpan.org>
288 caldrin: Maik Hentsche <maik.hentsche@amd.com>
290 castaway: Jess Robinson
292 claco: Christopher H. Laco
296 da5id: David Jack Olrik <djo@cpan.org>
298 debolaz: Anders Nor Berle <berle@cpan.org>
300 dew: Dan Thomas <dan@godders.org>
302 dkubb: Dan Kubb <dan.kubb-cpan@onautopilot.com>
304 dnm: Justin Wheeler <jwheeler@datademons.com>
306 dpetrov: Dimitar Petrov <mitakaa@gmail.com>
308 dwc: Daniel Westermann-Clark <danieltwc@cpan.org>
310 dyfrgi: Michael Leuchtenburg <michael@slashhome.org>
312 freetime: Bill Moseley <moseley@hank.org>
314 frew: Arthur Axel "fREW" Schmidt <frioux@gmail.com>
316 goraxe: Gordon Irving <goraxe@cpan.org>
318 gphat: Cory G Watson <gphat@cpan.org>
320 Grant Street Group L<http://www.grantstreet.com/>
322 groditi: Guillermo Roditi <groditi@cpan.org>
324 Haarg: Graham Knop <haarg@haarg.org>
326 hobbs: Andrew Rodland <arodland@cpan.org>
328 ilmari: Dagfinn Ilmari MannsE<aring>ker <ilmari@ilmari.org>
330 initself: Mike Baas <mike@initselftech.com>
332 jawnsy: Jonathan Yu <jawnsy@cpan.org>
334 jasonmay: Jason May <jason.a.may@gmail.com>
338 jgoulah: John Goulah <jgoulah@cpan.org>
340 jguenther: Justin Guenther <jguenther@cpan.org>
342 jhannah: Jay Hannah <jay@jays.net>
344 jnapiorkowski: John Napiorkowski <jjn1056@yahoo.com>
346 jon: Jon Schutz <jjschutz@cpan.org>
348 jshirley: J. Shirley <jshirley@gmail.com>
350 kaare: Kaare Rasmussen
352 konobi: Scott McWhirter
354 littlesavage: Alexey Illarionov <littlesavage@orionet.ru>
356 lukes: Luke Saunders <luke.saunders@gmail.com>
358 marcus: Marcus Ramberg <mramberg@cpan.org>
360 mattlaw: Matt Lawrence
362 mattp: Matt Phillips <mattp@cpan.org>
364 michaelr: Michael Reddick <michael.reddick@gmail.com>
366 milki: Jonathan Chu <milki@rescomp.berkeley.edu>
368 ned: Neil de Carteret
370 nigel: Nigel Metheringham <nigelm@cpan.org>
372 ningu: David Kamholz <dkamholz@cpan.org>
374 Nniuq: Ron "Quinn" Straight" <quinnfazigu@gmail.org>
376 norbi: Norbert Buchmuller <norbi@nix.hu>
378 nuba: Nuba Princigalli <nuba@cpan.org>
380 Numa: Dan Sully <daniel@cpan.org>
382 ovid: Curtis "Ovid" Poe <ovid@cpan.org>
384 oyse: Øystein Torget <oystein.torget@dnv.com>
386 paulm: Paul Makepeace
388 penguin: K J Cheetham
390 perigrin: Chris Prather <chris@prather.org>
392 peter: Peter Collingbourne <peter@pcc.me.uk>
394 phaylon: Robert Sedlacek <phaylon@dunkelheit.at>
396 plu: Johannes Plunien <plu@cpan.org>
398 Possum: Daniel LeWarne <possum@cpan.org>
400 quicksilver: Jules Bean
402 rafl: Florian Ragwitz <rafl@debian.org>
404 rainboxx: Matthias Dietrich <perl@rb.ly>
406 rbo: Robert Bohne <rbo@cpan.org>
408 rbuels: Robert Buels <rmb32@cornell.edu>
410 rdj: Ryan D Johnson <ryan@innerfence.com>
412 ribasushi: Peter Rabbitson <ribasushi@cpan.org>
414 rjbs: Ricardo Signes <rjbs@cpan.org>
416 robkinyon: Rob Kinyon <rkinyon@cpan.org>
418 Robert Olson <bob@rdolson.org>
420 Roman: Roman Filippov <romanf@cpan.org>
422 Sadrak: Felix Antonius Wilhelm Ostmann <sadrak@cpan.org>
424 sc_: Just Another Perl Hacker
426 scotty: Scotty Allen <scotty@scottyallen.com>
428 semifor: Marc Mims <marc@questright.com>
430 solomon: Jared Johnson <jaredj@nmgi.com>
432 spb: Stephen Bennett <stephen@freenode.net>
434 Squeeks <squeek@cpan.org>
436 sszabo: Stephan Szabo <sszabo@bigpanda.com>
438 talexb: Alex Beamish <talexb@gmail.com>
440 tamias: Ronald J Kimball <rjk@tamias.net>
442 teejay : Aaron Trevena <teejay@cpan.org>
448 tonvoon: Ton Voon <tonvoon@cpan.org>
450 triode: Pete Gamache <gamache@cpan.org>
452 typester: Daisuke Murase <typester@cpan.org>
454 victori: Victor Igumnov <victori@cpan.org>
458 willert: Sebastian Willert <willert@cpan.org>
460 wreis: Wallace Reis <wreis@cpan.org>
462 yrlnry: Mark Jason Dominus <mjd@plover.com>
464 zamolxes: Bogdan Lucaciu <bogdan@wiz.ro>
468 Copyright (c) 2005 - 2010 the DBIx::Class L</AUTHOR> and L</CONTRIBUTORS>
473 This library is free software and may be distributed under the same terms