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