Fix syntax error that slipped into 9c1700e3
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class.pm
CommitLineData
ea2e61bf 1package DBIx::Class;
2
5d283305 3use strict;
4use warnings;
5
87bf71d5 6BEGIN {
7 if ($] < 5.009_005) {
8 require MRO::Compat;
9 *DBIx::Class::_ENV_::OLD_MRO = sub () { 1 };
10 }
11 else {
12 require mro;
13 *DBIx::Class::_ENV_::OLD_MRO = sub () { 0 };
14 }
4bea1fe7 15
16 # ::Runmode would only be loaded by DBICTest, which in turn implies t/
17 *DBIx::Class::_ENV_::DBICTEST = eval { DBICTest::RunMode->is_author }
18 ? sub () { 1 }
19 : sub () { 0 }
20 ;
87bf71d5 21}
22
d38cd95c 23use mro 'c3';
329d7385 24
2527233b 25use DBIx::Class::Optional::Dependencies;
26
5d283305 27use vars qw($VERSION);
db29433c 28use base qw/DBIx::Class::Componentised DBIx::Class::AccessorGroup/;
11736b4c 29use DBIx::Class::StartupCheck;
3e110410 30
ade0fe3b 31sub mk_classdata {
77d518d1 32 shift->mk_classaccessor(@_);
33}
34
35sub mk_classaccessor {
36 my $self = shift;
ade0fe3b 37 $self->mk_group_accessors('inherited', $_[0]);
77d518d1 38 $self->set_inherited(@_) if @_ > 1;
3e110410 39}
3c0068c1 40
7411204b 41sub component_base_class { 'DBIx::Class' }
227d4dee 42
95da6f35 43# Always remember to do all digits for the version even if they're 0
44# i.e. first release of 0.XX *must* be 0.XX000. This avoids fBSD ports
45# brain damage and presumably various other packaging systems too
f4591846 46$VERSION = '0.08127';
748ab0dc 47
033955f9 48$VERSION = eval $VERSION if $VERSION =~ /_/; # numify for warning-free dev releases
b8777a0d 49
f0750722 50sub MODIFY_CODE_ATTRIBUTES {
b5d2c57f 51 my ($class,$code,@attrs) = @_;
52 $class->mk_classdata('__attr_cache' => {})
53 unless $class->can('__attr_cache');
54 $class->__attr_cache->{$code} = [@attrs];
55 return ();
f0750722 56}
57
da95b45f 58sub _attr_cache {
b5d2c57f 59 my $self = shift;
60 my $cache = $self->can('__attr_cache') ? $self->__attr_cache : {};
9780718f 61
62 return {
63 %$cache,
64 %{ $self->maybe::next::method || {} },
20674fcd 65 };
da95b45f 66}
67
ea2e61bf 681;
34d52be2 69
75d07914 70=head1 NAME
34d52be2 71
7e4b2f59 72DBIx::Class - Extensible and flexible object <-> relational mapper.
34d52be2 73
3b1c2bbd 74=head1 GETTING HELP/SUPPORT
75
76The community can be found via:
77
a06e1181 78=over
3b1c2bbd 79
054235c6 80=item * Web Site: L<http://www.dbix-class.org/>
81
c6fdaf2a 82=item * IRC: irc.perl.org#dbix-class
83
84=for html
e3194e31 85<a href="http://chat.mibbit.com/#dbix-class@irc.perl.org">(click for instant chatroom login)</a>
3b1c2bbd 86
a06e1181 87=item * Mailing list: L<http://lists.scsys.co.uk/mailman/listinfo/dbix-class>
3b1c2bbd 88
a06e1181 89=item * RT Bug Tracker: L<https://rt.cpan.org/Dist/Display.html?Queue=DBIx-Class>
90
aeb669b8 91=item * gitweb: L<http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=dbsrgits/DBIx-Class.git>
a06e1181 92
aeb669b8 93=item * git: L<git://git.shadowcat.co.uk/dbsrgits/DBIx-Class.git>
a06e1181 94
43517a60 95=item * twitter L<http://www.twitter.com/dbix_class>
96
a06e1181 97=back
3b1c2bbd 98
34d52be2 99=head1 SYNOPSIS
100
bd077b47 101Create a schema class called MyDB/Schema.pm:
34d52be2 102
bd077b47 103 package MyDB::Schema;
a0638a7b 104 use base qw/DBIx::Class::Schema/;
34d52be2 105
f0bb26f3 106 __PACKAGE__->load_namespaces();
daec44b8 107
a0638a7b 108 1;
daec44b8 109
30e1753a 110Create a result class to represent artists, who have many CDs, in
f0bb26f3 111MyDB/Schema/Result/Artist.pm:
daec44b8 112
30e1753a 113See L<DBIx::Class::ResultSource> for docs on defining result classes.
114
f0bb26f3 115 package MyDB::Schema::Result::Artist;
d88ecca6 116 use base qw/DBIx::Class::Core/;
daec44b8 117
a0638a7b 118 __PACKAGE__->table('artist');
119 __PACKAGE__->add_columns(qw/ artistid name /);
120 __PACKAGE__->set_primary_key('artistid');
f0bb26f3 121 __PACKAGE__->has_many(cds => 'MyDB::Schema::Result::CD');
daec44b8 122
a0638a7b 123 1;
daec44b8 124
30e1753a 125A result class to represent a CD, which belongs to an artist, in
f0bb26f3 126MyDB/Schema/Result/CD.pm:
39fe0e65 127
f0bb26f3 128 package MyDB::Schema::Result::CD;
d88ecca6 129 use base qw/DBIx::Class::Core/;
39fe0e65 130
d88ecca6 131 __PACKAGE__->load_components(qw/InflateColumn::DateTime/);
a0638a7b 132 __PACKAGE__->table('cd');
bd077b47 133 __PACKAGE__->add_columns(qw/ cdid artistid title year /);
a0638a7b 134 __PACKAGE__->set_primary_key('cdid');
7e3df949 135 __PACKAGE__->belongs_to(artist => 'MyDB::Schema::Result::Artist', 'artistid');
39fe0e65 136
a0638a7b 137 1;
39fe0e65 138
a0638a7b 139Then you can use these classes in your application's code:
39fe0e65 140
a0638a7b 141 # Connect to your database.
bd077b47 142 use MyDB::Schema;
143 my $schema = MyDB::Schema->connect($dbi_dsn, $user, $pass, \%dbi_params);
a0638a7b 144
145 # Query for all artists and put them in an array,
146 # or retrieve them as a result set object.
30e1753a 147 # $schema->resultset returns a DBIx::Class::ResultSet
2053ab2a 148 my @all_artists = $schema->resultset('Artist')->all;
149 my $all_artists_rs = $schema->resultset('Artist');
126042ee 150
30e1753a 151 # Output all artists names
4e8ffded 152 # $artist here is a DBIx::Class::Row, which has accessors
16ccb4fe 153 # for all its columns. Rows are also subclasses of your Result class.
85067746 154 foreach $artist (@all_artists) {
30e1753a 155 print $artist->name, "\n";
156 }
157
a0638a7b 158 # Create a result set to search for artists.
86beca1d 159 # This does not query the DB.
2053ab2a 160 my $johns_rs = $schema->resultset('Artist')->search(
6576ef54 161 # Build your WHERE using an SQL::Abstract structure:
2053ab2a 162 { name => { like => 'John%' } }
a0638a7b 163 );
39fe0e65 164
2053ab2a 165 # Execute a joined query to get the cds.
a0638a7b 166 my @all_john_cds = $johns_rs->search_related('cds')->all;
448c8424 167
f0bb26f3 168 # Fetch the next available row.
a0638a7b 169 my $first_john = $johns_rs->next;
448c8424 170
2053ab2a 171 # Specify ORDER BY on the query.
a0638a7b 172 my $first_john_cds_by_title_rs = $first_john->cds(
173 undef,
174 { order_by => 'title' }
175 );
448c8424 176
bd077b47 177 # Create a result set that will fetch the artist data
2053ab2a 178 # at the same time as it fetches CDs, using only one query.
884559b1 179 my $millennium_cds_rs = $schema->resultset('CD')->search(
a0638a7b 180 { year => 2000 },
181 { prefetch => 'artist' }
182 );
448c8424 183
880a1a0c 184 my $cd = $millennium_cds_rs->next; # SELECT ... FROM cds JOIN artists ...
bd077b47 185 my $cd_artist_name = $cd->artist->name; # Already has the data so no 2nd query
076652e8 186
264f1571 187 # new() makes a DBIx::Class::Row object but doesnt insert it into the DB.
188 # create() is the same as new() then insert().
884559b1 189 my $new_cd = $schema->resultset('CD')->new({ title => 'Spoon' });
f183eccd 190 $new_cd->artist($cd->artist);
f183eccd 191 $new_cd->insert; # Auto-increment primary key filled in after INSERT
f183eccd 192 $new_cd->title('Fork');
193
884559b1 194 $schema->txn_do(sub { $new_cd->update }); # Runs the update in a transaction
f183eccd 195
bd077b47 196 # change the year of all the millennium CDs at once
197 $millennium_cds_rs->update({ year => 2002 });
f183eccd 198
199=head1 DESCRIPTION
200
201This is an SQL to OO mapper with an object API inspired by L<Class::DBI>
bd077b47 202(with a compatibility layer as a springboard for porting) and a resultset API
f183eccd 203that allows abstract encapsulation of database operations. It aims to make
204representing queries in your code as perl-ish as possible while still
a0638a7b 205providing access to as many of the capabilities of the database as possible,
f183eccd 206including retrieving related records from multiple tables in a single query,
bd077b47 207JOIN, LEFT JOIN, COUNT, DISTINCT, GROUP BY, ORDER BY and HAVING support.
f183eccd 208
209DBIx::Class can handle multi-column primary and foreign keys, complex
210queries and database-level paging, and does its best to only query the
75d07914 211database in order to return something you've directly asked for. If a
212resultset is used as an iterator it only fetches rows off the statement
213handle as requested in order to minimise memory usage. It has auto-increment
2053ab2a 214support for SQLite, MySQL, PostgreSQL, Oracle, SQL Server and DB2 and is
215known to be used in production on at least the first four, and is fork-
ec6415a9 216and thread-safe out of the box (although
217L<your DBD may not be|DBI/Threads_and_Thread_Safety>).
f183eccd 218
dfccde48 219This project is still under rapid development, so large new features may be
220marked EXPERIMENTAL - such APIs are still usable but may have edge bugs.
221Failing test cases are *always* welcome and point releases are put out rapidly
222as bugs are found and fixed.
223
224We do our best to maintain full backwards compatibility for published
225APIs, since DBIx::Class is used in production in many organisations,
226and even backwards incompatible changes to non-published APIs will be fixed
227if they're reported and doing so doesn't cost the codebase anything.
228
264f1571 229The test suite is quite substantial, and several developer releases
230are generally made to CPAN before the branch for the next release is
231merged back to trunk for a major release.
f183eccd 232
f183eccd 233=head1 WHERE TO GO NEXT
234
2ca930b4 235L<DBIx::Class::Manual::DocMap> lists each task you might want help on, and
236the modules where you will find documentation.
076652e8 237
3942ab4d 238=head1 AUTHOR
34d52be2 239
266bdcc3 240mst: Matt S. Trout <mst@shadowcatsystems.co.uk>
34d52be2 241
dfccde48 242(I mostly consider myself "project founder" these days but the AUTHOR heading
243is traditional :)
244
3942ab4d 245=head1 CONTRIBUTORS
246
907ed671 247abraxxa: Alexander Hartmaier <abraxxa@cpan.org>
84e3c114 248
6d84db2c 249aherzog: Adam Herzog <adam@herzogdesigns.com>
250
daeb1865 251Alexander Keusch <cpan@keusch.at>
252
630ba6e5 253alnewkirk: Al Newkirk <we@ana.im>
254
6ebf5cbb 255amiri: Amiri Barksdale <amiri@metalabel.com>
256
b703fec7 257amoore: Andrew Moore <amoore@cpan.org>
258
266bdcc3 259andyg: Andy Grundman <andy@hybridized.org>
3942ab4d 260
266bdcc3 261ank: Andres Kievsky
3942ab4d 262
59ac6523 263arc: Aaron Crane <arc@cpan.org>
264
624764ae 265arcanez: Justin Hunter <justin.d.hunter@gmail.com>
266
ce4c07df 267ash: Ash Berlin <ash@cpan.org>
268
3d5bd2af 269bert: Norbert Csongradi <bert@cpan.org>
270
967a4c40 271blblack: Brandon L. Black <blblack@gmail.com>
3942ab4d 272
62eb8fe8 273bluefeet: Aran Deltac <bluefeet@cpan.org>
274
d6170b26 275bphillips: Brian Phillips <bphillips@cpan.org>
276
f856fe01 277boghead: Bryan Beeley <cpan@beeley.org>
278
bcb8f3ed 279bricas: Brian Cassidy <bricas@cpan.org>
280
3d7e3e05 281brunov: Bruno Vecchi <vecchi.b@gmail.com>
282
281719d2 283caelum: Rafael Kitover <rkitover@cpan.org>
284
f5f2af8f 285caldrin: Maik Hentsche <maik.hentsche@amd.com>
286
d3b0e369 287castaway: Jess Robinson
3942ab4d 288
266bdcc3 289claco: Christopher H. Laco
ccb9c9b1 290
266bdcc3 291clkao: CL Kao
3942ab4d 292
e21dfd6a 293da5id: David Jack Olrik <djo@cpan.org>
18360aed 294
13de943d 295debolaz: Anders Nor Berle <berle@cpan.org>
296
d1f542db 297dew: Dan Thomas <dan@godders.org>
298
266bdcc3 299dkubb: Dan Kubb <dan.kubb-cpan@onautopilot.com>
ccb9c9b1 300
9382ad07 301dnm: Justin Wheeler <jwheeler@datademons.com>
302
0818c9a7 303dpetrov: Dimitar Petrov <mitakaa@gmail.com>
304
266bdcc3 305dwc: Daniel Westermann-Clark <danieltwc@cpan.org>
4685e006 306
5cffe785 307dyfrgi: Michael Leuchtenburg <michael@slashhome.org>
8fe164b9 308
0ffada27 309freetime: Bill Moseley <moseley@hank.org>
310
ade0fe3b 311frew: Arthur Axel "fREW" Schmidt <frioux@gmail.com>
312
b4987ed0 313goraxe: Gordon Irving <goraxe@cpan.org>
314
d3b0e369 315gphat: Cory G Watson <gphat@cpan.org>
ad3d2d7c 316
61f031bf 317Grant Street Group L<http://www.grantstreet.com/>
318
e758ffe6 319groditi: Guillermo Roditi <groditi@cpan.org>
320
6dad89f5 321Haarg: Graham Knop <haarg@haarg.org>
322
157ce0cf 323hobbs: Andrew Rodland <arodland@cpan.org>
324
5d779578 325ilmari: Dagfinn Ilmari MannsE<aring>ker <ilmari@ilmari.org>
326
0ac0af6c 327initself: Mike Baas <mike@initselftech.com>
328
f165eda8 329jawnsy: Jonathan Yu <jawnsy@cpan.org>
330
bee21976 331jasonmay: Jason May <jason.a.may@gmail.com>
332
d3b0e369 333jesper: Jesper Krogh
5fb0c64c 334
4a743a00 335jgoulah: John Goulah <jgoulah@cpan.org>
336
102a2984 337jguenther: Justin Guenther <jguenther@cpan.org>
d7c4c15c 338
a14a46e2 339jhannah: Jay Hannah <jay@jays.net>
340
8b93a938 341jnapiorkowski: John Napiorkowski <jjn1056@yahoo.com>
342
11736b4c 343jon: Jon Schutz <jjschutz@cpan.org>
344
1aec4bac 345jshirley: J. Shirley <jshirley@gmail.com>
346
41519379 347kaare: Kaare Rasmussen
348
d3b0e369 349konobi: Scott McWhirter
535fc2ee 350
4e0a89e4 351littlesavage: Alexey Illarionov <littlesavage@orionet.ru>
352
4367679a 353lukes: Luke Saunders <luke.saunders@gmail.com>
354
709ea492 355marcus: Marcus Ramberg <mramberg@cpan.org>
356
114780ee 357mattlaw: Matt Lawrence
358
45bffdf0 359mattp: Matt Phillips <mattp@cpan.org>
360
58755bba 361michaelr: Michael Reddick <michael.reddick@gmail.com>
362
91d0c99f 363milki: Jonathan Chu <milki@rescomp.berkeley.edu>
364
77e7e47d 365ned: Neil de Carteret
366
266bdcc3 367nigel: Nigel Metheringham <nigelm@cpan.org>
6565b410 368
d3b0e369 369ningu: David Kamholz <dkamholz@cpan.org>
370
66cf3a84 371Nniuq: Ron "Quinn" Straight" <quinnfazigu@gmail.org>
372
20b4c148 373norbi: Norbert Buchmuller <norbi@nix.hu>
374
48580715 375nuba: Nuba Princigalli <nuba@cpan.org>
376
d3b0e369 377Numa: Dan Sully <daniel@cpan.org>
378
dc571b76 379ovid: Curtis "Ovid" Poe <ovid@cpan.org>
380
bf356c54 381oyse: Øystein Torget <oystein.torget@dnv.com>
382
266bdcc3 383paulm: Paul Makepeace
4763f4b7 384
d3b0e369 385penguin: K J Cheetham
386
8cfef6f5 387perigrin: Chris Prather <chris@prather.org>
388
14899528 389peter: Peter Collingbourne <peter@pcc.me.uk>
caac1708 390
266bdcc3 391phaylon: Robert Sedlacek <phaylon@dunkelheit.at>
a53b95f1 392
56fadd8f 393plu: Johannes Plunien <plu@cpan.org>
394
0c1a4a15 395Possum: Daniel LeWarne <possum@cpan.org>
396
d3b0e369 397quicksilver: Jules Bean
022e0893 398
4ed01b34 399rafl: Florian Ragwitz <rafl@debian.org>
400
0c1a4a15 401rainboxx: Matthias Dietrich <perl@rb.ly>
402
868a7b26 403rbo: Robert Bohne <rbo@cpan.org>
404
7ff0dace 405rbuels: Robert Buels <rmb32@cornell.edu>
406
0da8b7da 407rdj: Ryan D Johnson <ryan@innerfence.com>
408
66b1e361 409ribasushi: Peter Rabbitson <ribasushi@cpan.org>
d76e282a 410
b487918c 411rjbs: Ricardo Signes <rjbs@cpan.org>
412
6ffb5be5 413robkinyon: Rob Kinyon <rkinyon@cpan.org>
414
e4c9f3f0 415Roman: Roman Filippov <romanf@cpan.org>
416
dc81dba3 417Sadrak: Felix Antonius Wilhelm Ostmann <sadrak@cpan.org>
418
d3b0e369 419sc_: Just Another Perl Hacker
ba606e58 420
266bdcc3 421scotty: Scotty Allen <scotty@scottyallen.com>
181a28f4 422
1c133e22 423semifor: Marc Mims <marc@questright.com>
424
20b4c148 425solomon: Jared Johnson <jaredj@nmgi.com>
426
88f937fb 427spb: Stephen Bennett <stephen@freenode.net>
428
59187a3b 429Squeeks <squeek@cpan.org>
430
637ca936 431sszabo: Stephan Szabo <sszabo@bigpanda.com>
432
a9e8284f 433talexb: Alex Beamish <talexb@gmail.com>
434
7bf0d0d6 435tamias: Ronald J Kimball <rjk@tamias.net>
f92a9d79 436
386c2272 437teejay : Aaron Trevena <teejay@cpan.org>
438
84e3c114 439Todd Lipcon
e063fe2c 440
6eb6264e 441Tom Hukins
442
2e4b6d78 443tonvoon: Ton Voon <tonvoon@cpan.org>
444
d15e3fc5 445triode: Pete Gamache <gamache@cpan.org>
446
d3b0e369 447typester: Daisuke Murase <typester@cpan.org>
c0e7b4e5 448
4740bdb7 449victori: Victor Igumnov <victori@cpan.org>
450
d3b0e369 451wdh: Will Hawes
4c248161 452
00c03ced 453willert: Sebastian Willert <willert@cpan.org>
454
66d2a14e 455wreis: Wallace Reis <wreis@cpan.org>
456
d52d4d6e 457yrlnry: Mark Jason Dominus <mjd@plover.com>
458
d3b0e369 459zamolxes: Bogdan Lucaciu <bogdan@wiz.ro>
78060df8 460
b38e10bd 461=head1 COPYRIGHT
462
48580715 463Copyright (c) 2005 - 2010 the DBIx::Class L</AUTHOR> and L</CONTRIBUTORS>
b38e10bd 464as listed above.
465
96154ef7 466=head1 LICENSE
467
468This library is free software and may be distributed under the same terms
469as perl itself.
470
34d52be2 471=cut