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