Make irc/mailing list much more prominent in the docs
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class.pm
CommitLineData
ea2e61bf 1package DBIx::Class;
2
5d283305 3use strict;
4use warnings;
5
5d283305 6use vars qw($VERSION);
3e110410 7use base qw/DBIx::Class::Componentised Class::Accessor::Grouped/;
11736b4c 8use DBIx::Class::StartupCheck;
3e110410 9
77d518d1 10
3e110410 11sub mk_classdata {
77d518d1 12 shift->mk_classaccessor(@_);
13}
14
15sub mk_classaccessor {
16 my $self = shift;
17 $self->mk_group_accessors('inherited', $_[0]);
18 $self->set_inherited(@_) if @_ > 1;
3e110410 19}
3c0068c1 20
7411204b 21sub component_base_class { 'DBIx::Class' }
227d4dee 22
95da6f35 23# Always remember to do all digits for the version even if they're 0
24# i.e. first release of 0.XX *must* be 0.XX000. This avoids fBSD ports
25# brain damage and presumably various other packaging systems too
26
748ab0dc 27$VERSION = '0.08099_01';
28
29$VERSION = eval $VERSION; # numify for warning-free dev releases
b8777a0d 30
f0750722 31sub MODIFY_CODE_ATTRIBUTES {
b5d2c57f 32 my ($class,$code,@attrs) = @_;
33 $class->mk_classdata('__attr_cache' => {})
34 unless $class->can('__attr_cache');
35 $class->__attr_cache->{$code} = [@attrs];
36 return ();
f0750722 37}
38
da95b45f 39sub _attr_cache {
b5d2c57f 40 my $self = shift;
41 my $cache = $self->can('__attr_cache') ? $self->__attr_cache : {};
42 my $rest = eval { $self->next::method };
43 return $@ ? $cache : { %$cache, %$rest };
da95b45f 44}
45
ea2e61bf 461;
34d52be2 47
75d07914 48=head1 NAME
34d52be2 49
7e4b2f59 50DBIx::Class - Extensible and flexible object <-> relational mapper.
34d52be2 51
3b1c2bbd 52=head1 GETTING HELP/SUPPORT
53
54The community can be found via:
55
56 Mailing list: http://lists.scsys.co.uk/mailman/listinfo/dbix-class/
57
58 SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
59
60 SVNWeb: http://dev.catalyst.perl.org/svnweb/bast/browse/DBIx-Class/
61
62 IRC: irc.perl.org#dbix-class
63
34d52be2 64=head1 SYNOPSIS
65
2053ab2a 66Create a schema class called DB/Main.pm:
34d52be2 67
a0638a7b 68 package DB::Main;
69 use base qw/DBIx::Class::Schema/;
34d52be2 70
a0638a7b 71 __PACKAGE__->load_classes();
daec44b8 72
a0638a7b 73 1;
daec44b8 74
2053ab2a 75Create a table class to represent artists, who have many CDs, in DB/Main/Artist.pm:
daec44b8 76
a0638a7b 77 package DB::Main::Artist;
78 use base qw/DBIx::Class/;
daec44b8 79
a0638a7b 80 __PACKAGE__->load_components(qw/PK::Auto Core/);
81 __PACKAGE__->table('artist');
82 __PACKAGE__->add_columns(qw/ artistid name /);
83 __PACKAGE__->set_primary_key('artistid');
2053ab2a 84 __PACKAGE__->has_many(cds => 'DB::Main::CD');
daec44b8 85
a0638a7b 86 1;
daec44b8 87
2053ab2a 88A table class to represent a CD, which belongs to an artist, in DB/Main/CD.pm:
39fe0e65 89
a0638a7b 90 package DB::Main::CD;
91 use base qw/DBIx::Class/;
39fe0e65 92
a0638a7b 93 __PACKAGE__->load_components(qw/PK::Auto Core/);
94 __PACKAGE__->table('cd');
2053ab2a 95 __PACKAGE__->add_columns(qw/ cdid artist title year /);
a0638a7b 96 __PACKAGE__->set_primary_key('cdid');
2053ab2a 97 __PACKAGE__->belongs_to(artist => 'DB::Main::Artist');
39fe0e65 98
a0638a7b 99 1;
39fe0e65 100
a0638a7b 101Then you can use these classes in your application's code:
39fe0e65 102
a0638a7b 103 # Connect to your database.
2053ab2a 104 use DB::Main;
105 my $schema = DB::Main->connect($dbi_dsn, $user, $pass, \%dbi_params);
a0638a7b 106
107 # Query for all artists and put them in an array,
108 # or retrieve them as a result set object.
2053ab2a 109 my @all_artists = $schema->resultset('Artist')->all;
110 my $all_artists_rs = $schema->resultset('Artist');
126042ee 111
a0638a7b 112 # Create a result set to search for artists.
86beca1d 113 # This does not query the DB.
2053ab2a 114 my $johns_rs = $schema->resultset('Artist')->search(
6576ef54 115 # Build your WHERE using an SQL::Abstract structure:
2053ab2a 116 { name => { like => 'John%' } }
a0638a7b 117 );
39fe0e65 118
2053ab2a 119 # Execute a joined query to get the cds.
a0638a7b 120 my @all_john_cds = $johns_rs->search_related('cds')->all;
448c8424 121
2053ab2a 122 # Fetch only the next row.
a0638a7b 123 my $first_john = $johns_rs->next;
448c8424 124
2053ab2a 125 # Specify ORDER BY on the query.
a0638a7b 126 my $first_john_cds_by_title_rs = $first_john->cds(
127 undef,
128 { order_by => 'title' }
129 );
448c8424 130
2053ab2a 131 # Create a result set that will fetch the artist relationship
132 # at the same time as it fetches CDs, using only one query.
884559b1 133 my $millennium_cds_rs = $schema->resultset('CD')->search(
a0638a7b 134 { year => 2000 },
135 { prefetch => 'artist' }
136 );
448c8424 137
880a1a0c 138 my $cd = $millennium_cds_rs->next; # SELECT ... FROM cds JOIN artists ...
f183eccd 139 my $cd_artist_name = $cd->artist->name; # Already has the data so no query
076652e8 140
264f1571 141 # new() makes a DBIx::Class::Row object but doesnt insert it into the DB.
142 # create() is the same as new() then insert().
884559b1 143 my $new_cd = $schema->resultset('CD')->new({ title => 'Spoon' });
f183eccd 144 $new_cd->artist($cd->artist);
f183eccd 145 $new_cd->insert; # Auto-increment primary key filled in after INSERT
f183eccd 146 $new_cd->title('Fork');
147
884559b1 148 $schema->txn_do(sub { $new_cd->update }); # Runs the update in a transaction
f183eccd 149
880a1a0c 150 $millennium_cds_rs->update({ year => 2002 }); # Single-query bulk update
f183eccd 151
152=head1 DESCRIPTION
153
154This is an SQL to OO mapper with an object API inspired by L<Class::DBI>
a0638a7b 155(and a compatibility layer as a springboard for porting) and a resultset API
f183eccd 156that allows abstract encapsulation of database operations. It aims to make
157representing queries in your code as perl-ish as possible while still
a0638a7b 158providing access to as many of the capabilities of the database as possible,
f183eccd 159including retrieving related records from multiple tables in a single query,
160JOIN, LEFT JOIN, COUNT, DISTINCT, GROUP BY and HAVING support.
161
162DBIx::Class can handle multi-column primary and foreign keys, complex
163queries and database-level paging, and does its best to only query the
75d07914 164database in order to return something you've directly asked for. If a
165resultset is used as an iterator it only fetches rows off the statement
166handle as requested in order to minimise memory usage. It has auto-increment
2053ab2a 167support for SQLite, MySQL, PostgreSQL, Oracle, SQL Server and DB2 and is
168known to be used in production on at least the first four, and is fork-
75d07914 169and thread-safe out of the box (although your DBD may not be).
f183eccd 170
dfccde48 171This project is still under rapid development, so large new features may be
172marked EXPERIMENTAL - such APIs are still usable but may have edge bugs.
173Failing test cases are *always* welcome and point releases are put out rapidly
174as bugs are found and fixed.
175
176We do our best to maintain full backwards compatibility for published
177APIs, since DBIx::Class is used in production in many organisations,
178and even backwards incompatible changes to non-published APIs will be fixed
179if they're reported and doing so doesn't cost the codebase anything.
180
264f1571 181The test suite is quite substantial, and several developer releases
182are generally made to CPAN before the branch for the next release is
183merged back to trunk for a major release.
f183eccd 184
f183eccd 185=head1 WHERE TO GO NEXT
186
2ca930b4 187L<DBIx::Class::Manual::DocMap> lists each task you might want help on, and
188the modules where you will find documentation.
076652e8 189
3942ab4d 190=head1 AUTHOR
34d52be2 191
266bdcc3 192mst: Matt S. Trout <mst@shadowcatsystems.co.uk>
34d52be2 193
dfccde48 194(I mostly consider myself "project founder" these days but the AUTHOR heading
195is traditional :)
196
3942ab4d 197=head1 CONTRIBUTORS
198
266bdcc3 199abraxxa: Alexander Hartmaier <alex_hartmaier@hotmail.com>
84e3c114 200
6d84db2c 201aherzog: Adam Herzog <adam@herzogdesigns.com>
202
266bdcc3 203andyg: Andy Grundman <andy@hybridized.org>
3942ab4d 204
266bdcc3 205ank: Andres Kievsky
3942ab4d 206
ce4c07df 207ash: Ash Berlin <ash@cpan.org>
208
3d5bd2af 209bert: Norbert Csongradi <bert@cpan.org>
210
967a4c40 211blblack: Brandon L. Black <blblack@gmail.com>
3942ab4d 212
62eb8fe8 213bluefeet: Aran Deltac <bluefeet@cpan.org>
214
d3b0e369 215captainL: Luke Saunders <luke.saunders@gmail.com>
216
217castaway: Jess Robinson
3942ab4d 218
266bdcc3 219claco: Christopher H. Laco
ccb9c9b1 220
266bdcc3 221clkao: CL Kao
3942ab4d 222
e21dfd6a 223da5id: David Jack Olrik <djo@cpan.org>
18360aed 224
13de943d 225debolaz: Anders Nor Berle <berle@cpan.org>
226
266bdcc3 227dkubb: Dan Kubb <dan.kubb-cpan@onautopilot.com>
ccb9c9b1 228
9382ad07 229dnm: Justin Wheeler <jwheeler@datademons.com>
230
d3b0e369 231draven: Marcus Ramberg <mramberg@cpan.org>
4685e006 232
266bdcc3 233dwc: Daniel Westermann-Clark <danieltwc@cpan.org>
4685e006 234
5cffe785 235dyfrgi: Michael Leuchtenburg <michael@slashhome.org>
8fe164b9 236
d3b0e369 237gphat: Cory G Watson <gphat@cpan.org>
ad3d2d7c 238
d3b0e369 239jesper: Jesper Krogh
5fb0c64c 240
102a2984 241jguenther: Justin Guenther <jguenther@cpan.org>
d7c4c15c 242
8b93a938 243jnapiorkowski: John Napiorkowski <jjn1056@yahoo.com>
244
11736b4c 245jon: Jon Schutz <jjschutz@cpan.org>
246
1aec4bac 247jshirley: J. Shirley <jshirley@gmail.com>
248
d3b0e369 249konobi: Scott McWhirter
535fc2ee 250
d3b0e369 251LTJake: Brian Cassidy <bricas@cpan.org>
103e3e03 252
114780ee 253mattlaw: Matt Lawrence
254
77e7e47d 255ned: Neil de Carteret
256
266bdcc3 257nigel: Nigel Metheringham <nigelm@cpan.org>
6565b410 258
d3b0e369 259ningu: David Kamholz <dkamholz@cpan.org>
260
261Numa: Dan Sully <daniel@cpan.org>
262
bf356c54 263oyse: Øystein Torget <oystein.torget@dnv.com>
264
266bdcc3 265paulm: Paul Makepeace
4763f4b7 266
d3b0e369 267penguin: K J Cheetham
268
8cfef6f5 269perigrin: Chris Prather <chris@prather.org>
270
266bdcc3 271phaylon: Robert Sedlacek <phaylon@dunkelheit.at>
a53b95f1 272
d3b0e369 273quicksilver: Jules Bean
022e0893 274
0da8b7da 275rdj: Ryan D Johnson <ryan@innerfence.com>
276
d3b0e369 277sc_: Just Another Perl Hacker
ba606e58 278
266bdcc3 279scotty: Scotty Allen <scotty@scottyallen.com>
181a28f4 280
1c133e22 281semifor: Marc Mims <marc@questright.com>
282
637ca936 283sszabo: Stephan Szabo <sszabo@bigpanda.com>
284
386c2272 285teejay : Aaron Trevena <teejay@cpan.org>
286
84e3c114 287Todd Lipcon
e063fe2c 288
6eb6264e 289Tom Hukins
290
d3b0e369 291typester: Daisuke Murase <typester@cpan.org>
c0e7b4e5 292
4740bdb7 293victori: Victor Igumnov <victori@cpan.org>
294
d3b0e369 295wdh: Will Hawes
4c248161 296
00c03ced 297willert: Sebastian Willert <willert@cpan.org>
298
d3b0e369 299zamolxes: Bogdan Lucaciu <bogdan@wiz.ro>
78060df8 300
34d52be2 301=head1 LICENSE
302
303You may distribute this code under the same terms as Perl itself.
304
305=cut