Autogen comment for Dependencies.pod
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class.pm
CommitLineData
ea2e61bf 1package DBIx::Class;
2
5d283305 3use strict;
4use warnings;
5
329d7385 6use MRO::Compat;
d38cd95c 7use mro 'c3';
329d7385 8
2527233b 9use DBIx::Class::Optional::Dependencies;
10
5d283305 11use vars qw($VERSION);
d38cd95c 12use base qw/DBIx::Class::Componentised Class::Accessor::Grouped/;
11736b4c 13use DBIx::Class::StartupCheck;
3e110410 14
ade0fe3b 15sub mk_classdata {
77d518d1 16 shift->mk_classaccessor(@_);
17}
18
19sub mk_classaccessor {
20 my $self = shift;
ade0fe3b 21 $self->mk_group_accessors('inherited', $_[0]);
77d518d1 22 $self->set_inherited(@_) if @_ > 1;
3e110410 23}
3c0068c1 24
7411204b 25sub component_base_class { 'DBIx::Class' }
227d4dee 26
95da6f35 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
d100eb32 30$VERSION = '0.08118_01';
748ab0dc 31
32$VERSION = eval $VERSION; # numify for warning-free dev releases
b8777a0d 33
f0750722 34sub MODIFY_CODE_ATTRIBUTES {
b5d2c57f 35 my ($class,$code,@attrs) = @_;
36 $class->mk_classdata('__attr_cache' => {})
37 unless $class->can('__attr_cache');
38 $class->__attr_cache->{$code} = [@attrs];
39 return ();
f0750722 40}
41
da95b45f 42sub _attr_cache {
b5d2c57f 43 my $self = shift;
44 my $cache = $self->can('__attr_cache') ? $self->__attr_cache : {};
45 my $rest = eval { $self->next::method };
46 return $@ ? $cache : { %$cache, %$rest };
da95b45f 47}
48
ea2e61bf 491;
34d52be2 50
75d07914 51=head1 NAME
34d52be2 52
7e4b2f59 53DBIx::Class - Extensible and flexible object <-> relational mapper.
34d52be2 54
3b1c2bbd 55=head1 GETTING HELP/SUPPORT
56
57The community can be found via:
58
59 Mailing list: http://lists.scsys.co.uk/mailman/listinfo/dbix-class/
60
61 SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
62
63 SVNWeb: http://dev.catalyst.perl.org/svnweb/bast/browse/DBIx-Class/
64
65 IRC: irc.perl.org#dbix-class
66
34d52be2 67=head1 SYNOPSIS
68
bd077b47 69Create a schema class called MyDB/Schema.pm:
34d52be2 70
bd077b47 71 package MyDB::Schema;
a0638a7b 72 use base qw/DBIx::Class::Schema/;
34d52be2 73
f0bb26f3 74 __PACKAGE__->load_namespaces();
daec44b8 75
a0638a7b 76 1;
daec44b8 77
30e1753a 78Create a result class to represent artists, who have many CDs, in
f0bb26f3 79MyDB/Schema/Result/Artist.pm:
daec44b8 80
30e1753a 81See L<DBIx::Class::ResultSource> for docs on defining result classes.
82
f0bb26f3 83 package MyDB::Schema::Result::Artist;
d88ecca6 84 use base qw/DBIx::Class::Core/;
daec44b8 85
a0638a7b 86 __PACKAGE__->table('artist');
87 __PACKAGE__->add_columns(qw/ artistid name /);
88 __PACKAGE__->set_primary_key('artistid');
f0bb26f3 89 __PACKAGE__->has_many(cds => 'MyDB::Schema::Result::CD');
daec44b8 90
a0638a7b 91 1;
daec44b8 92
30e1753a 93A result class to represent a CD, which belongs to an artist, in
f0bb26f3 94MyDB/Schema/Result/CD.pm:
39fe0e65 95
f0bb26f3 96 package MyDB::Schema::Result::CD;
d88ecca6 97 use base qw/DBIx::Class::Core/;
39fe0e65 98
d88ecca6 99 __PACKAGE__->load_components(qw/InflateColumn::DateTime/);
a0638a7b 100 __PACKAGE__->table('cd');
bd077b47 101 __PACKAGE__->add_columns(qw/ cdid artistid title year /);
a0638a7b 102 __PACKAGE__->set_primary_key('cdid');
bd077b47 103 __PACKAGE__->belongs_to(artist => 'MyDB::Schema::Artist', 'artistid');
39fe0e65 104
a0638a7b 105 1;
39fe0e65 106
a0638a7b 107Then you can use these classes in your application's code:
39fe0e65 108
a0638a7b 109 # Connect to your database.
bd077b47 110 use MyDB::Schema;
111 my $schema = MyDB::Schema->connect($dbi_dsn, $user, $pass, \%dbi_params);
a0638a7b 112
113 # Query for all artists and put them in an array,
114 # or retrieve them as a result set object.
30e1753a 115 # $schema->resultset returns a DBIx::Class::ResultSet
2053ab2a 116 my @all_artists = $schema->resultset('Artist')->all;
117 my $all_artists_rs = $schema->resultset('Artist');
126042ee 118
30e1753a 119 # Output all artists names
4e8ffded 120 # $artist here is a DBIx::Class::Row, which has accessors
16ccb4fe 121 # for all its columns. Rows are also subclasses of your Result class.
85067746 122 foreach $artist (@all_artists) {
30e1753a 123 print $artist->name, "\n";
124 }
125
a0638a7b 126 # Create a result set to search for artists.
86beca1d 127 # This does not query the DB.
2053ab2a 128 my $johns_rs = $schema->resultset('Artist')->search(
6576ef54 129 # Build your WHERE using an SQL::Abstract structure:
2053ab2a 130 { name => { like => 'John%' } }
a0638a7b 131 );
39fe0e65 132
2053ab2a 133 # Execute a joined query to get the cds.
a0638a7b 134 my @all_john_cds = $johns_rs->search_related('cds')->all;
448c8424 135
f0bb26f3 136 # Fetch the next available row.
a0638a7b 137 my $first_john = $johns_rs->next;
448c8424 138
2053ab2a 139 # Specify ORDER BY on the query.
a0638a7b 140 my $first_john_cds_by_title_rs = $first_john->cds(
141 undef,
142 { order_by => 'title' }
143 );
448c8424 144
bd077b47 145 # Create a result set that will fetch the artist data
2053ab2a 146 # at the same time as it fetches CDs, using only one query.
884559b1 147 my $millennium_cds_rs = $schema->resultset('CD')->search(
a0638a7b 148 { year => 2000 },
149 { prefetch => 'artist' }
150 );
448c8424 151
880a1a0c 152 my $cd = $millennium_cds_rs->next; # SELECT ... FROM cds JOIN artists ...
bd077b47 153 my $cd_artist_name = $cd->artist->name; # Already has the data so no 2nd query
076652e8 154
264f1571 155 # new() makes a DBIx::Class::Row object but doesnt insert it into the DB.
156 # create() is the same as new() then insert().
884559b1 157 my $new_cd = $schema->resultset('CD')->new({ title => 'Spoon' });
f183eccd 158 $new_cd->artist($cd->artist);
f183eccd 159 $new_cd->insert; # Auto-increment primary key filled in after INSERT
f183eccd 160 $new_cd->title('Fork');
161
884559b1 162 $schema->txn_do(sub { $new_cd->update }); # Runs the update in a transaction
f183eccd 163
bd077b47 164 # change the year of all the millennium CDs at once
165 $millennium_cds_rs->update({ year => 2002 });
f183eccd 166
167=head1 DESCRIPTION
168
169This is an SQL to OO mapper with an object API inspired by L<Class::DBI>
bd077b47 170(with a compatibility layer as a springboard for porting) and a resultset API
f183eccd 171that allows abstract encapsulation of database operations. It aims to make
172representing queries in your code as perl-ish as possible while still
a0638a7b 173providing access to as many of the capabilities of the database as possible,
f183eccd 174including retrieving related records from multiple tables in a single query,
bd077b47 175JOIN, LEFT JOIN, COUNT, DISTINCT, GROUP BY, ORDER BY and HAVING support.
f183eccd 176
177DBIx::Class can handle multi-column primary and foreign keys, complex
178queries and database-level paging, and does its best to only query the
75d07914 179database in order to return something you've directly asked for. If a
180resultset is used as an iterator it only fetches rows off the statement
181handle as requested in order to minimise memory usage. It has auto-increment
2053ab2a 182support for SQLite, MySQL, PostgreSQL, Oracle, SQL Server and DB2 and is
183known to be used in production on at least the first four, and is fork-
75d07914 184and thread-safe out of the box (although your DBD may not be).
f183eccd 185
dfccde48 186This project is still under rapid development, so large new features may be
187marked EXPERIMENTAL - such APIs are still usable but may have edge bugs.
188Failing test cases are *always* welcome and point releases are put out rapidly
189as bugs are found and fixed.
190
191We do our best to maintain full backwards compatibility for published
192APIs, since DBIx::Class is used in production in many organisations,
193and even backwards incompatible changes to non-published APIs will be fixed
194if they're reported and doing so doesn't cost the codebase anything.
195
264f1571 196The test suite is quite substantial, and several developer releases
197are generally made to CPAN before the branch for the next release is
198merged back to trunk for a major release.
f183eccd 199
f183eccd 200=head1 WHERE TO GO NEXT
201
2ca930b4 202L<DBIx::Class::Manual::DocMap> lists each task you might want help on, and
203the modules where you will find documentation.
076652e8 204
3942ab4d 205=head1 AUTHOR
34d52be2 206
266bdcc3 207mst: Matt S. Trout <mst@shadowcatsystems.co.uk>
34d52be2 208
dfccde48 209(I mostly consider myself "project founder" these days but the AUTHOR heading
210is traditional :)
211
3942ab4d 212=head1 CONTRIBUTORS
213
266bdcc3 214abraxxa: Alexander Hartmaier <alex_hartmaier@hotmail.com>
84e3c114 215
6d84db2c 216aherzog: Adam Herzog <adam@herzogdesigns.com>
217
266bdcc3 218andyg: Andy Grundman <andy@hybridized.org>
3942ab4d 219
266bdcc3 220ank: Andres Kievsky
3942ab4d 221
624764ae 222arcanez: Justin Hunter <justin.d.hunter@gmail.com>
223
ce4c07df 224ash: Ash Berlin <ash@cpan.org>
225
3d5bd2af 226bert: Norbert Csongradi <bert@cpan.org>
227
967a4c40 228blblack: Brandon L. Black <blblack@gmail.com>
3942ab4d 229
62eb8fe8 230bluefeet: Aran Deltac <bluefeet@cpan.org>
231
f856fe01 232boghead: Bryan Beeley <cpan@beeley.org>
233
bcb8f3ed 234bricas: Brian Cassidy <bricas@cpan.org>
235
3d7e3e05 236brunov: Bruno Vecchi <vecchi.b@gmail.com>
237
281719d2 238caelum: Rafael Kitover <rkitover@cpan.org>
239
d3b0e369 240castaway: Jess Robinson
3942ab4d 241
266bdcc3 242claco: Christopher H. Laco
ccb9c9b1 243
266bdcc3 244clkao: CL Kao
3942ab4d 245
e21dfd6a 246da5id: David Jack Olrik <djo@cpan.org>
18360aed 247
13de943d 248debolaz: Anders Nor Berle <berle@cpan.org>
249
d1f542db 250dew: Dan Thomas <dan@godders.org>
251
266bdcc3 252dkubb: Dan Kubb <dan.kubb-cpan@onautopilot.com>
ccb9c9b1 253
9382ad07 254dnm: Justin Wheeler <jwheeler@datademons.com>
255
266bdcc3 256dwc: Daniel Westermann-Clark <danieltwc@cpan.org>
4685e006 257
5cffe785 258dyfrgi: Michael Leuchtenburg <michael@slashhome.org>
8fe164b9 259
ade0fe3b 260frew: Arthur Axel "fREW" Schmidt <frioux@gmail.com>
261
d3b0e369 262gphat: Cory G Watson <gphat@cpan.org>
ad3d2d7c 263
e758ffe6 264groditi: Guillermo Roditi <groditi@cpan.org>
265
5d779578 266ilmari: Dagfinn Ilmari MannsE<aring>ker <ilmari@ilmari.org>
267
bee21976 268jasonmay: Jason May <jason.a.may@gmail.com>
269
d3b0e369 270jesper: Jesper Krogh
5fb0c64c 271
4a743a00 272jgoulah: John Goulah <jgoulah@cpan.org>
273
102a2984 274jguenther: Justin Guenther <jguenther@cpan.org>
d7c4c15c 275
a14a46e2 276jhannah: Jay Hannah <jay@jays.net>
277
8b93a938 278jnapiorkowski: John Napiorkowski <jjn1056@yahoo.com>
279
11736b4c 280jon: Jon Schutz <jjschutz@cpan.org>
281
1aec4bac 282jshirley: J. Shirley <jshirley@gmail.com>
283
d3b0e369 284konobi: Scott McWhirter
535fc2ee 285
4367679a 286lukes: Luke Saunders <luke.saunders@gmail.com>
287
709ea492 288marcus: Marcus Ramberg <mramberg@cpan.org>
289
114780ee 290mattlaw: Matt Lawrence
291
58755bba 292michaelr: Michael Reddick <michael.reddick@gmail.com>
293
77e7e47d 294ned: Neil de Carteret
295
266bdcc3 296nigel: Nigel Metheringham <nigelm@cpan.org>
6565b410 297
d3b0e369 298ningu: David Kamholz <dkamholz@cpan.org>
299
66cf3a84 300Nniuq: Ron "Quinn" Straight" <quinnfazigu@gmail.org>
301
20b4c148 302norbi: Norbert Buchmuller <norbi@nix.hu>
303
d3b0e369 304Numa: Dan Sully <daniel@cpan.org>
305
dc571b76 306ovid: Curtis "Ovid" Poe <ovid@cpan.org>
307
bf356c54 308oyse: Øystein Torget <oystein.torget@dnv.com>
309
266bdcc3 310paulm: Paul Makepeace
4763f4b7 311
d3b0e369 312penguin: K J Cheetham
313
8cfef6f5 314perigrin: Chris Prather <chris@prather.org>
315
14899528 316peter: Peter Collingbourne <peter@pcc.me.uk>
caac1708 317
266bdcc3 318phaylon: Robert Sedlacek <phaylon@dunkelheit.at>
a53b95f1 319
56fadd8f 320plu: Johannes Plunien <plu@cpan.org>
321
d3b0e369 322quicksilver: Jules Bean
022e0893 323
4ed01b34 324rafl: Florian Ragwitz <rafl@debian.org>
325
7ff0dace 326rbuels: Robert Buels <rmb32@cornell.edu>
327
0da8b7da 328rdj: Ryan D Johnson <ryan@innerfence.com>
329
66b1e361 330ribasushi: Peter Rabbitson <ribasushi@cpan.org>
d76e282a 331
b487918c 332rjbs: Ricardo Signes <rjbs@cpan.org>
333
6ffb5be5 334robkinyon: Rob Kinyon <rkinyon@cpan.org>
335
e4c9f3f0 336Roman: Roman Filippov <romanf@cpan.org>
337
d3b0e369 338sc_: Just Another Perl Hacker
ba606e58 339
266bdcc3 340scotty: Scotty Allen <scotty@scottyallen.com>
181a28f4 341
1c133e22 342semifor: Marc Mims <marc@questright.com>
343
20b4c148 344solomon: Jared Johnson <jaredj@nmgi.com>
345
88f937fb 346spb: Stephen Bennett <stephen@freenode.net>
347
637ca936 348sszabo: Stephan Szabo <sszabo@bigpanda.com>
349
386c2272 350teejay : Aaron Trevena <teejay@cpan.org>
351
84e3c114 352Todd Lipcon
e063fe2c 353
6eb6264e 354Tom Hukins
355
d15e3fc5 356triode: Pete Gamache <gamache@cpan.org>
357
d3b0e369 358typester: Daisuke Murase <typester@cpan.org>
c0e7b4e5 359
4740bdb7 360victori: Victor Igumnov <victori@cpan.org>
361
d3b0e369 362wdh: Will Hawes
4c248161 363
00c03ced 364willert: Sebastian Willert <willert@cpan.org>
365
66d2a14e 366wreis: Wallace Reis <wreis@cpan.org>
367
d3b0e369 368zamolxes: Bogdan Lucaciu <bogdan@wiz.ro>
78060df8 369
b38e10bd 370=head1 COPYRIGHT
371
372Copyright (c) 2005 - 2009 the DBIx::Class L</AUTHOR> and L</CONTRIBUTORS>
373as listed above.
374
96154ef7 375=head1 LICENSE
376
377This library is free software and may be distributed under the same terms
378as perl itself.
379
34d52be2 380=cut