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