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