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