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