Make sure handling of exception_action is recursion-safe
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class.pm
CommitLineData
ea2e61bf 1package DBIx::Class;
2
5d283305 3use strict;
4use warnings;
5
f9cc85ce 6our $VERSION;
7# Always remember to do all digits for the version even if they're 0
8# i.e. first release of 0.XX *must* be 0.XX000. This avoids fBSD ports
9# brain damage and presumably various other packaging systems too
10
11# $VERSION declaration must stay up here, ahead of any other package
12# declarations, as to not confuse various modules attempting to determine
13# this ones version, whether that be s.c.o. or Module::Metadata, etc
c6b73be9 14$VERSION = '0.082899_15';
f9cc85ce 15
16$VERSION = eval $VERSION if $VERSION =~ /_/; # numify for warning-free dev releases
17
37873f78 18use DBIx::Class::_Util;
d38cd95c 19use mro 'c3';
329d7385 20
db29433c 21use base qw/DBIx::Class::Componentised DBIx::Class::AccessorGroup/;
11736b4c 22use DBIx::Class::StartupCheck;
f9080e45 23use DBIx::Class::Exception;
3e110410 24
70c28808 25__PACKAGE__->mk_group_accessors(inherited => '_skip_namespace_frames');
8b60b921 26__PACKAGE__->_skip_namespace_frames('^DBIx::Class|^SQL::Abstract|^Try::Tiny|^Class::Accessor::Grouped|^Context::Preserve|^Moose::Meta::');
70c28808 27
e1d9e578 28# FIXME - this is not really necessary, and is in
29# fact going to slow things down a bit
30# However it is the right thing to do in order to get
31# various install bases to highlight their brokenness
32# Remove at some unknown point in the future
5f74ed3a 33#
34# The oddball BEGIN is there for... reason unknown
35# It does make non-segfaulty difference on pre-5.8.5 perls, so shrug
36BEGIN {
37 sub DESTROY { &DBIx::Class::_Util::detected_reinvoked_destructor };
38}
e1d9e578 39
ade0fe3b 40sub mk_classdata {
77d518d1 41 shift->mk_classaccessor(@_);
42}
43
44sub mk_classaccessor {
45 my $self = shift;
ade0fe3b 46 $self->mk_group_accessors('inherited', $_[0]);
77d518d1 47 $self->set_inherited(@_) if @_ > 1;
3e110410 48}
3c0068c1 49
7411204b 50sub component_base_class { 'DBIx::Class' }
227d4dee 51
f0750722 52sub MODIFY_CODE_ATTRIBUTES {
b5d2c57f 53 my ($class,$code,@attrs) = @_;
54 $class->mk_classdata('__attr_cache' => {})
55 unless $class->can('__attr_cache');
56 $class->__attr_cache->{$code} = [@attrs];
57 return ();
f0750722 58}
59
da95b45f 60sub _attr_cache {
b5d2c57f 61 my $self = shift;
62 my $cache = $self->can('__attr_cache') ? $self->__attr_cache : {};
9780718f 63
64 return {
65 %$cache,
66 %{ $self->maybe::next::method || {} },
20674fcd 67 };
da95b45f 68}
69
d095c62d 70# *DO NOT* change this URL nor the identically named =head1 below
71# it is linked throughout the ecosystem
72sub DBIx::Class::_ENV_::HELP_URL () {
73 'http://p3rl.org/DBIx::Class#GETTING_HELP/SUPPORT'
74}
75
ea2e61bf 761;
34d52be2 77
d095c62d 78__END__
79
75d07914 80=head1 NAME
34d52be2 81
7e4b2f59 82DBIx::Class - Extensible and flexible object <-> relational mapper.
34d52be2 83
06752a03 84=head1 WHERE TO START READING
3b1c2bbd 85
06752a03 86See L<DBIx::Class::Manual::DocMap> for an overview of the exhaustive documentation.
87To get the most out of DBIx::Class with the least confusion it is strongly
88recommended to read (at the very least) the
89L<Manuals|DBIx::Class::Manual::DocMap/Manuals> in the order presented there.
90
32250d01 91=cut
92
32250d01 93=head1 GETTING HELP/SUPPORT
06752a03 94
32250d01 95Due to the sheer size of its problem domain, DBIx::Class is a relatively
06752a03 96complex framework. After you start using DBIx::Class questions will inevitably
97arise. If you are stuck with a problem or have doubts about a particular
32250d01 98approach do not hesitate to contact us via any of the following options (the
99list is sorted by "fastest response time"):
3b1c2bbd 100
a06e1181 101=over
3b1c2bbd 102
c6fdaf2a 103=item * IRC: irc.perl.org#dbix-class
104
105=for html
e1ddfc8a 106<a href="https://chat.mibbit.com/#dbix-class@irc.perl.org">(click for instant chatroom login)</a>
3b1c2bbd 107
a06e1181 108=item * Mailing list: L<http://lists.scsys.co.uk/mailman/listinfo/dbix-class>
3b1c2bbd 109
e1ddfc8a 110=item * RT Bug Tracker: L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=DBIx-Class>
86a23587 111
e1ddfc8a 112=item * Twitter: L<https://www.twitter.com/dbix_class>
86a23587 113
86a23587 114=item * Web Site: L<http://www.dbix-class.org/>
a06e1181 115
86a23587 116=back
117
34d52be2 118=head1 SYNOPSIS
119
113e8d16 120For the very impatient: L<DBIx::Class::Manual::QuickStart>
121
122This code in the next step can be generated automatically from an existing
123database, see L<dbicdump> from the distribution C<DBIx-Class-Schema-Loader>.
124
5b56d1ac 125=head2 Schema classes preparation
126
53aa53f3 127Create a schema class called F<MyApp/Schema.pm>:
34d52be2 128
03460bef 129 package MyApp::Schema;
a0638a7b 130 use base qw/DBIx::Class::Schema/;
34d52be2 131
f0bb26f3 132 __PACKAGE__->load_namespaces();
daec44b8 133
a0638a7b 134 1;
daec44b8 135
30e1753a 136Create a result class to represent artists, who have many CDs, in
53aa53f3 137F<MyApp/Schema/Result/Artist.pm>:
daec44b8 138
30e1753a 139See L<DBIx::Class::ResultSource> for docs on defining result classes.
140
03460bef 141 package MyApp::Schema::Result::Artist;
d88ecca6 142 use base qw/DBIx::Class::Core/;
daec44b8 143
a0638a7b 144 __PACKAGE__->table('artist');
145 __PACKAGE__->add_columns(qw/ artistid name /);
146 __PACKAGE__->set_primary_key('artistid');
326dacbf 147 __PACKAGE__->has_many(cds => 'MyApp::Schema::Result::CD', 'artistid');
daec44b8 148
a0638a7b 149 1;
daec44b8 150
30e1753a 151A result class to represent a CD, which belongs to an artist, in
53aa53f3 152F<MyApp/Schema/Result/CD.pm>:
39fe0e65 153
03460bef 154 package MyApp::Schema::Result::CD;
d88ecca6 155 use base qw/DBIx::Class::Core/;
39fe0e65 156
d88ecca6 157 __PACKAGE__->load_components(qw/InflateColumn::DateTime/);
a0638a7b 158 __PACKAGE__->table('cd');
bd077b47 159 __PACKAGE__->add_columns(qw/ cdid artistid title year /);
a0638a7b 160 __PACKAGE__->set_primary_key('cdid');
03460bef 161 __PACKAGE__->belongs_to(artist => 'MyApp::Schema::Result::Artist', 'artistid');
39fe0e65 162
a0638a7b 163 1;
39fe0e65 164
5b56d1ac 165=head2 API usage
166
a0638a7b 167Then you can use these classes in your application's code:
39fe0e65 168
a0638a7b 169 # Connect to your database.
03460bef 170 use MyApp::Schema;
171 my $schema = MyApp::Schema->connect($dbi_dsn, $user, $pass, \%dbi_params);
a0638a7b 172
173 # Query for all artists and put them in an array,
174 # or retrieve them as a result set object.
30e1753a 175 # $schema->resultset returns a DBIx::Class::ResultSet
2053ab2a 176 my @all_artists = $schema->resultset('Artist')->all;
177 my $all_artists_rs = $schema->resultset('Artist');
126042ee 178
30e1753a 179 # Output all artists names
4e8ffded 180 # $artist here is a DBIx::Class::Row, which has accessors
16ccb4fe 181 # for all its columns. Rows are also subclasses of your Result class.
85067746 182 foreach $artist (@all_artists) {
30e1753a 183 print $artist->name, "\n";
184 }
185
a0638a7b 186 # Create a result set to search for artists.
86beca1d 187 # This does not query the DB.
2053ab2a 188 my $johns_rs = $schema->resultset('Artist')->search(
6576ef54 189 # Build your WHERE using an SQL::Abstract structure:
2053ab2a 190 { name => { like => 'John%' } }
a0638a7b 191 );
39fe0e65 192
2053ab2a 193 # Execute a joined query to get the cds.
a0638a7b 194 my @all_john_cds = $johns_rs->search_related('cds')->all;
448c8424 195
f0bb26f3 196 # Fetch the next available row.
a0638a7b 197 my $first_john = $johns_rs->next;
448c8424 198
2053ab2a 199 # Specify ORDER BY on the query.
a0638a7b 200 my $first_john_cds_by_title_rs = $first_john->cds(
201 undef,
202 { order_by => 'title' }
203 );
448c8424 204
bd077b47 205 # Create a result set that will fetch the artist data
2053ab2a 206 # at the same time as it fetches CDs, using only one query.
884559b1 207 my $millennium_cds_rs = $schema->resultset('CD')->search(
a0638a7b 208 { year => 2000 },
209 { prefetch => 'artist' }
210 );
448c8424 211
880a1a0c 212 my $cd = $millennium_cds_rs->next; # SELECT ... FROM cds JOIN artists ...
bd077b47 213 my $cd_artist_name = $cd->artist->name; # Already has the data so no 2nd query
076652e8 214
4b0a90fd 215 # new() makes a Result object but doesn't insert it into the DB.
264f1571 216 # create() is the same as new() then insert().
884559b1 217 my $new_cd = $schema->resultset('CD')->new({ title => 'Spoon' });
f183eccd 218 $new_cd->artist($cd->artist);
f183eccd 219 $new_cd->insert; # Auto-increment primary key filled in after INSERT
f183eccd 220 $new_cd->title('Fork');
221
884559b1 222 $schema->txn_do(sub { $new_cd->update }); # Runs the update in a transaction
f183eccd 223
bd077b47 224 # change the year of all the millennium CDs at once
225 $millennium_cds_rs->update({ year => 2002 });
f183eccd 226
227=head1 DESCRIPTION
228
229This is an SQL to OO mapper with an object API inspired by L<Class::DBI>
bd077b47 230(with a compatibility layer as a springboard for porting) and a resultset API
f183eccd 231that allows abstract encapsulation of database operations. It aims to make
232representing queries in your code as perl-ish as possible while still
a0638a7b 233providing access to as many of the capabilities of the database as possible,
f183eccd 234including retrieving related records from multiple tables in a single query,
53aa53f3 235C<JOIN>, C<LEFT JOIN>, C<COUNT>, C<DISTINCT>, C<GROUP BY>, C<ORDER BY> and
236C<HAVING> support.
f183eccd 237
238DBIx::Class can handle multi-column primary and foreign keys, complex
239queries and database-level paging, and does its best to only query the
75d07914 240database in order to return something you've directly asked for. If a
241resultset is used as an iterator it only fetches rows off the statement
242handle as requested in order to minimise memory usage. It has auto-increment
2053ab2a 243support for SQLite, MySQL, PostgreSQL, Oracle, SQL Server and DB2 and is
244known to be used in production on at least the first four, and is fork-
ec6415a9 245and thread-safe out of the box (although
9361b05d 246L<your DBD may not be|DBI/Threads and Thread Safety>).
f183eccd 247
dfccde48 248This project is still under rapid development, so large new features may be
53aa53f3 249marked B<experimental> - such APIs are still usable but may have edge bugs.
250Failing test cases are I<always> welcome and point releases are put out rapidly
dfccde48 251as bugs are found and fixed.
252
253We do our best to maintain full backwards compatibility for published
254APIs, since DBIx::Class is used in production in many organisations,
255and even backwards incompatible changes to non-published APIs will be fixed
256if they're reported and doing so doesn't cost the codebase anything.
257
264f1571 258The test suite is quite substantial, and several developer releases
259are generally made to CPAN before the branch for the next release is
260merged back to trunk for a major release.
f183eccd 261
6ed05cfd 262=head1 HOW TO CONTRIBUTE
263
264Contributions are always welcome, in all usable forms (we especially
265welcome documentation improvements). The delivery methods include git-
266or unified-diff formatted patches, GitHub pull requests, or plain bug
267reports either via RT or the Mailing list. Contributors are generally
cb32addc 268granted access to the official repository after their first several
269patches pass successful review. Don't hesitate to
270L<contact|/GETTING HELP/SUPPORT> either of the L</CAT HERDERS> with
271any further questions you may have.
6ed05cfd 272
273=for comment
274FIXME: Getty, frew and jnap need to get off their asses and finish the contrib section so we can link it here ;)
275
276This project is maintained in a git repository. The code and related tools are
277accessible at the following locations:
278
279=over
280
281=item * Official repo: L<git://git.shadowcat.co.uk/dbsrgits/DBIx-Class.git>
282
283=item * Official gitweb: L<http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=dbsrgits/DBIx-Class.git>
284
285=item * GitHub mirror: L<https://github.com/dbsrgits/DBIx-Class>
286
287=item * Authorized committers: L<ssh://dbsrgits@git.shadowcat.co.uk/DBIx-Class.git>
288
289=item * Travis-CI log: L<https://travis-ci.org/dbsrgits/dbix-class/builds>
290
291=for html
33d0570d 292&#x21AA; Bleeding edge dev CI status: <img src="https://secure.travis-ci.org/dbsrgits/dbix-class.png?branch=master"></img>
6ed05cfd 293
294=back
295
3440100b 296=head1 AUTHORS
34d52be2 297
3440100b 298Even though a large portion of the source I<appears> to be written by just a
299handful of people, this library continues to remain a collaborative effort -
300perhaps one of the most successful such projects on L<CPAN|http://cpan.org>.
301It is important to remember that ideas do not always result in a direct code
302contribution, but deserve acknowledgement just the same. Time and time again
303the seemingly most insignificant questions and suggestions have been shown
304to catalyze monumental improvements in consistency, accuracy and performance.
34d52be2 305
3440100b 306=for comment this line is replaced with the author list at dist-building time
dfccde48 307
3440100b 308The canonical source of authors and their details is the F<AUTHORS> file at
309the root of this distribution (or repository). The canonical source of
310per-line authorship is the L<git repository|/HOW TO CONTRIBUTE> history
311itself.
f9139687 312
cb32addc 313=head1 CAT HERDERS
314
315The fine folks nudging the project in a particular direction:
316
317=over
318
f06eb015 319B<ribasushi>: Peter Rabbitson <ribasushi@cpan.org>
cb32addc 320(present day maintenance and controlled evolution)
321
f06eb015 322B<castaway>: Jess Robinson <castaway@desert-island.me.uk>
cb32addc 323(lions share of the reference documentation and manuals)
324
f06eb015 325B<mst>: Matt S Trout <mst@shadowcat.co.uk> (project founder -
cb32addc 326original idea, architecture and implementation)
327
328=back
329
a2bd3796 330=head1 COPYRIGHT AND LICENSE
b38e10bd 331
a2bd3796 332Copyright (c) 2005 by mst, castaway, ribasushi, and other DBIx::Class
333L</AUTHORS> as listed above and in F<AUTHORS>.
96154ef7 334
335This library is free software and may be distributed under the same terms
a2bd3796 336as perl5 itself. See F<LICENSE> for the complete licensing terms.