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