9 use DBIx::Class::Optional::Dependencies;
11 use vars qw($VERSION);
12 use base qw/DBIx::Class::Componentised Class::Accessor::Grouped/;
13 use DBIx::Class::StartupCheck;
16 shift->mk_classaccessor(@_);
19 sub mk_classaccessor {
21 $self->mk_group_accessors('inherited', $_[0]);
22 $self->set_inherited(@_) if @_ > 1;
25 sub component_base_class { 'DBIx::Class' }
27 # Always remember to do all digits for the version even if they're 0
28 # i.e. first release of 0.XX *must* be 0.XX000. This avoids fBSD ports
29 # brain damage and presumably various other packaging systems too
32 $VERSION = eval $VERSION if $VERSION =~ /_/; # numify for warning-free dev releases
34 sub MODIFY_CODE_ATTRIBUTES {
35 my ($class,$code,@attrs) = @_;
36 $class->mk_classdata('__attr_cache' => {})
37 unless $class->can('__attr_cache');
38 $class->__attr_cache->{$code} = [@attrs];
44 my $cache = $self->can('__attr_cache') ? $self->__attr_cache : {};
48 %{ $self->maybe::next::method || {} },
56 DBIx::Class - Extensible and flexible object <-> relational mapper.
58 =head1 GETTING HELP/SUPPORT
60 The community can be found via:
64 =item * Web Site: L<http://www.dbix-class.org/>
66 =item * IRC: irc.perl.org#dbix-class
69 <a href="http://chat.mibbit.com/#dbix-class@irc.perl.org">(click for instant chatroom login)</a>
71 =item * Mailing list: L<http://lists.scsys.co.uk/mailman/listinfo/dbix-class>
73 =item * RT Bug Tracker: L<https://rt.cpan.org/Dist/Display.html?Queue=DBIx-Class>
75 =item * gitweb: L<http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=dbsrgits/DBIx-Class.git>
77 =item * git: L<git://git.shadowcat.co.uk/dbsrgits/DBIx-Class.git>
79 =item * twitter L<http://www.twitter.com/dbix_class>
85 Create a schema class called MyDB/Schema.pm:
88 use base qw/DBIx::Class::Schema/;
90 __PACKAGE__->load_namespaces();
94 Create a result class to represent artists, who have many CDs, in
95 MyDB/Schema/Result/Artist.pm:
97 See L<DBIx::Class::ResultSource> for docs on defining result classes.
99 package MyDB::Schema::Result::Artist;
100 use base qw/DBIx::Class::Core/;
102 __PACKAGE__->table('artist');
103 __PACKAGE__->add_columns(qw/ artistid name /);
104 __PACKAGE__->set_primary_key('artistid');
105 __PACKAGE__->has_many(cds => 'MyDB::Schema::Result::CD');
109 A result class to represent a CD, which belongs to an artist, in
110 MyDB/Schema/Result/CD.pm:
112 package MyDB::Schema::Result::CD;
113 use base qw/DBIx::Class::Core/;
115 __PACKAGE__->load_components(qw/InflateColumn::DateTime/);
116 __PACKAGE__->table('cd');
117 __PACKAGE__->add_columns(qw/ cdid artistid title year /);
118 __PACKAGE__->set_primary_key('cdid');
119 __PACKAGE__->belongs_to(artist => 'MyDB::Schema::Result::Artist', 'artistid');
123 Then you can use these classes in your application's code:
125 # Connect to your database.
127 my $schema = MyDB::Schema->connect($dbi_dsn, $user, $pass, \%dbi_params);
129 # Query for all artists and put them in an array,
130 # or retrieve them as a result set object.
131 # $schema->resultset returns a DBIx::Class::ResultSet
132 my @all_artists = $schema->resultset('Artist')->all;
133 my $all_artists_rs = $schema->resultset('Artist');
135 # Output all artists names
136 # $artist here is a DBIx::Class::Row, which has accessors
137 # for all its columns. Rows are also subclasses of your Result class.
138 foreach $artist (@all_artists) {
139 print $artist->name, "\n";
142 # Create a result set to search for artists.
143 # This does not query the DB.
144 my $johns_rs = $schema->resultset('Artist')->search(
145 # Build your WHERE using an SQL::Abstract structure:
146 { name => { like => 'John%' } }
149 # Execute a joined query to get the cds.
150 my @all_john_cds = $johns_rs->search_related('cds')->all;
152 # Fetch the next available row.
153 my $first_john = $johns_rs->next;
155 # Specify ORDER BY on the query.
156 my $first_john_cds_by_title_rs = $first_john->cds(
158 { order_by => 'title' }
161 # Create a result set that will fetch the artist data
162 # at the same time as it fetches CDs, using only one query.
163 my $millennium_cds_rs = $schema->resultset('CD')->search(
165 { prefetch => 'artist' }
168 my $cd = $millennium_cds_rs->next; # SELECT ... FROM cds JOIN artists ...
169 my $cd_artist_name = $cd->artist->name; # Already has the data so no 2nd query
171 # new() makes a DBIx::Class::Row object but doesnt insert it into the DB.
172 # create() is the same as new() then insert().
173 my $new_cd = $schema->resultset('CD')->new({ title => 'Spoon' });
174 $new_cd->artist($cd->artist);
175 $new_cd->insert; # Auto-increment primary key filled in after INSERT
176 $new_cd->title('Fork');
178 $schema->txn_do(sub { $new_cd->update }); # Runs the update in a transaction
180 # change the year of all the millennium CDs at once
181 $millennium_cds_rs->update({ year => 2002 });
185 This is an SQL to OO mapper with an object API inspired by L<Class::DBI>
186 (with a compatibility layer as a springboard for porting) and a resultset API
187 that allows abstract encapsulation of database operations. It aims to make
188 representing queries in your code as perl-ish as possible while still
189 providing access to as many of the capabilities of the database as possible,
190 including retrieving related records from multiple tables in a single query,
191 JOIN, LEFT JOIN, COUNT, DISTINCT, GROUP BY, ORDER BY and HAVING support.
193 DBIx::Class can handle multi-column primary and foreign keys, complex
194 queries and database-level paging, and does its best to only query the
195 database in order to return something you've directly asked for. If a
196 resultset is used as an iterator it only fetches rows off the statement
197 handle as requested in order to minimise memory usage. It has auto-increment
198 support for SQLite, MySQL, PostgreSQL, Oracle, SQL Server and DB2 and is
199 known to be used in production on at least the first four, and is fork-
200 and thread-safe out of the box (although
201 L<your DBD may not be|DBI/Threads_and_Thread_Safety>).
203 This project is still under rapid development, so large new features may be
204 marked EXPERIMENTAL - such APIs are still usable but may have edge bugs.
205 Failing test cases are *always* welcome and point releases are put out rapidly
206 as bugs are found and fixed.
208 We do our best to maintain full backwards compatibility for published
209 APIs, since DBIx::Class is used in production in many organisations,
210 and even backwards incompatible changes to non-published APIs will be fixed
211 if they're reported and doing so doesn't cost the codebase anything.
213 The test suite is quite substantial, and several developer releases
214 are generally made to CPAN before the branch for the next release is
215 merged back to trunk for a major release.
217 =head1 WHERE TO GO NEXT
219 L<DBIx::Class::Manual::DocMap> lists each task you might want help on, and
220 the modules where you will find documentation.
224 mst: Matt S. Trout <mst@shadowcatsystems.co.uk>
226 (I mostly consider myself "project founder" these days but the AUTHOR heading
231 abraxxa: Alexander Hartmaier <abraxxa@cpan.org>
233 aherzog: Adam Herzog <adam@herzogdesigns.com>
235 Alexander Keusch <cpan@keusch.at>
237 alnewkirk: Al Newkirk <we@ana.im>
239 amiri: Amiri Barksdale <amiri@metalabel.com>
241 amoore: Andrew Moore <amoore@cpan.org>
243 andyg: Andy Grundman <andy@hybridized.org>
247 arc: Aaron Crane <arc@cpan.org>
249 arcanez: Justin Hunter <justin.d.hunter@gmail.com>
251 ash: Ash Berlin <ash@cpan.org>
253 bert: Norbert Csongradi <bert@cpan.org>
255 blblack: Brandon L. Black <blblack@gmail.com>
257 bluefeet: Aran Deltac <bluefeet@cpan.org>
259 bphillips: Brian Phillips <bphillips@cpan.org>
261 boghead: Bryan Beeley <cpan@beeley.org>
263 bricas: Brian Cassidy <bricas@cpan.org>
265 brunov: Bruno Vecchi <vecchi.b@gmail.com>
267 caelum: Rafael Kitover <rkitover@cpan.org>
269 caldrin: Maik Hentsche <maik.hentsche@amd.com>
271 castaway: Jess Robinson
273 claco: Christopher H. Laco
277 da5id: David Jack Olrik <djo@cpan.org>
279 debolaz: Anders Nor Berle <berle@cpan.org>
281 dew: Dan Thomas <dan@godders.org>
283 dkubb: Dan Kubb <dan.kubb-cpan@onautopilot.com>
285 dnm: Justin Wheeler <jwheeler@datademons.com>
287 dpetrov: Dimitar Petrov <mitakaa@gmail.com>
289 dwc: Daniel Westermann-Clark <danieltwc@cpan.org>
291 dyfrgi: Michael Leuchtenburg <michael@slashhome.org>
293 freetime: Bill Moseley <moseley@hank.org>
295 frew: Arthur Axel "fREW" Schmidt <frioux@gmail.com>
297 goraxe: Gordon Irving <goraxe@cpan.org>
299 gphat: Cory G Watson <gphat@cpan.org>
301 groditi: Guillermo Roditi <groditi@cpan.org>
303 Haarg: Graham Knop <haarg@haarg.org>
305 hobbs: Andrew Rodland <arodland@cpan.org>
307 ilmari: Dagfinn Ilmari MannsE<aring>ker <ilmari@ilmari.org>
309 initself: Mike Baas <mike@initselftech.com>
311 jasonmay: Jason May <jason.a.may@gmail.com>
315 jgoulah: John Goulah <jgoulah@cpan.org>
317 jguenther: Justin Guenther <jguenther@cpan.org>
319 jhannah: Jay Hannah <jay@jays.net>
321 jnapiorkowski: John Napiorkowski <jjn1056@yahoo.com>
323 jon: Jon Schutz <jjschutz@cpan.org>
325 jshirley: J. Shirley <jshirley@gmail.com>
327 kaare: Kaare Rasmussen
329 konobi: Scott McWhirter
331 littlesavage: Alexey Illarionov <littlesavage@orionet.ru>
333 lukes: Luke Saunders <luke.saunders@gmail.com>
335 marcus: Marcus Ramberg <mramberg@cpan.org>
337 mattlaw: Matt Lawrence
339 michaelr: Michael Reddick <michael.reddick@gmail.com>
341 ned: Neil de Carteret
343 nigel: Nigel Metheringham <nigelm@cpan.org>
345 ningu: David Kamholz <dkamholz@cpan.org>
347 Nniuq: Ron "Quinn" Straight" <quinnfazigu@gmail.org>
349 norbi: Norbert Buchmuller <norbi@nix.hu>
351 nuba: Nuba Princigalli <nuba@cpan.org>
353 Numa: Dan Sully <daniel@cpan.org>
355 ovid: Curtis "Ovid" Poe <ovid@cpan.org>
357 oyse: Øystein Torget <oystein.torget@dnv.com>
359 paulm: Paul Makepeace
361 penguin: K J Cheetham
363 perigrin: Chris Prather <chris@prather.org>
365 peter: Peter Collingbourne <peter@pcc.me.uk>
367 phaylon: Robert Sedlacek <phaylon@dunkelheit.at>
369 plu: Johannes Plunien <plu@cpan.org>
371 Possum: Daniel LeWarne <possum@cpan.org>
373 quicksilver: Jules Bean
375 rafl: Florian Ragwitz <rafl@debian.org>
377 rainboxx: Matthias Dietrich <perl@rb.ly>
379 rbo: Robert Bohne <rbo@cpan.org>
381 rbuels: Robert Buels <rmb32@cornell.edu>
383 rdj: Ryan D Johnson <ryan@innerfence.com>
385 ribasushi: Peter Rabbitson <ribasushi@cpan.org>
387 rjbs: Ricardo Signes <rjbs@cpan.org>
389 robkinyon: Rob Kinyon <rkinyon@cpan.org>
391 Roman: Roman Filippov <romanf@cpan.org>
393 Sadrak: Felix Antonius Wilhelm Ostmann <sadrak@cpan.org>
395 sc_: Just Another Perl Hacker
397 scotty: Scotty Allen <scotty@scottyallen.com>
399 semifor: Marc Mims <marc@questright.com>
401 solomon: Jared Johnson <jaredj@nmgi.com>
403 spb: Stephen Bennett <stephen@freenode.net>
405 Squeeks <squeek@cpan.org>
407 sszabo: Stephan Szabo <sszabo@bigpanda.com>
409 talexb: Alex Beamish <talexb@gmail.com>
411 teejay : Aaron Trevena <teejay@cpan.org>
417 tonvoon: Ton Voon <tonvoon@cpan.org>
419 triode: Pete Gamache <gamache@cpan.org>
421 typester: Daisuke Murase <typester@cpan.org>
423 victori: Victor Igumnov <victori@cpan.org>
427 willert: Sebastian Willert <willert@cpan.org>
429 wreis: Wallace Reis <wreis@cpan.org>
431 yrlnry: Mark Jason Dominus <mjd@plover.com>
433 zamolxes: Bogdan Lucaciu <bogdan@wiz.ro>
437 Copyright (c) 2005 - 2010 the DBIx::Class L</AUTHOR> and L</CONTRIBUTORS>
442 This library is free software and may be distributed under the same terms