Autogen comment for Dependencies.pod
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class.pm
1 package DBIx::Class;
2
3 use strict;
4 use warnings;
5
6 use MRO::Compat;
7 use mro 'c3';
8
9 use DBIx::Class::Optional::Dependencies;
10
11 use vars qw($VERSION);
12 use base qw/DBIx::Class::Componentised Class::Accessor::Grouped/;
13 use DBIx::Class::StartupCheck;
14
15 sub mk_classdata {
16   shift->mk_classaccessor(@_);
17 }
18
19 sub mk_classaccessor {
20   my $self = shift;
21   $self->mk_group_accessors('inherited', $_[0]);
22   $self->set_inherited(@_) if @_ > 1;
23 }
24
25 sub component_base_class { 'DBIx::Class' }
26
27 # Always remember to do all digits for the version even if they're 0
28 # i.e. first release of 0.XX *must* be 0.XX000. This avoids fBSD ports
29 # brain damage and presumably various other packaging systems too
30 $VERSION = '0.08118_01';
31
32 $VERSION = eval $VERSION; # numify for warning-free dev releases
33
34 sub MODIFY_CODE_ATTRIBUTES {
35   my ($class,$code,@attrs) = @_;
36   $class->mk_classdata('__attr_cache' => {})
37     unless $class->can('__attr_cache');
38   $class->__attr_cache->{$code} = [@attrs];
39   return ();
40 }
41
42 sub _attr_cache {
43   my $self = shift;
44   my $cache = $self->can('__attr_cache') ? $self->__attr_cache : {};
45   my $rest = eval { $self->next::method };
46   return $@ ? $cache : { %$cache, %$rest };
47 }
48
49 1;
50
51 =head1 NAME
52
53 DBIx::Class - Extensible and flexible object <-> relational mapper.
54
55 =head1 GETTING HELP/SUPPORT
56
57 The community can be found via:
58
59   Mailing list: http://lists.scsys.co.uk/mailman/listinfo/dbix-class/
60
61   SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
62
63   SVNWeb: http://dev.catalyst.perl.org/svnweb/bast/browse/DBIx-Class/
64
65   IRC: irc.perl.org#dbix-class
66
67 =head1 SYNOPSIS
68
69 Create a schema class called MyDB/Schema.pm:
70
71   package MyDB::Schema;
72   use base qw/DBIx::Class::Schema/;
73
74   __PACKAGE__->load_namespaces();
75
76   1;
77
78 Create a result class to represent artists, who have many CDs, in
79 MyDB/Schema/Result/Artist.pm:
80
81 See L<DBIx::Class::ResultSource> for docs on defining result classes.
82
83   package MyDB::Schema::Result::Artist;
84   use base qw/DBIx::Class::Core/;
85
86   __PACKAGE__->table('artist');
87   __PACKAGE__->add_columns(qw/ artistid name /);
88   __PACKAGE__->set_primary_key('artistid');
89   __PACKAGE__->has_many(cds => 'MyDB::Schema::Result::CD');
90
91   1;
92
93 A result class to represent a CD, which belongs to an artist, in
94 MyDB/Schema/Result/CD.pm:
95
96   package MyDB::Schema::Result::CD;
97   use base qw/DBIx::Class::Core/;
98
99   __PACKAGE__->load_components(qw/InflateColumn::DateTime/);
100   __PACKAGE__->table('cd');
101   __PACKAGE__->add_columns(qw/ cdid artistid title year /);
102   __PACKAGE__->set_primary_key('cdid');
103   __PACKAGE__->belongs_to(artist => 'MyDB::Schema::Artist', 'artistid');
104
105   1;
106
107 Then you can use these classes in your application's code:
108
109   # Connect to your database.
110   use MyDB::Schema;
111   my $schema = MyDB::Schema->connect($dbi_dsn, $user, $pass, \%dbi_params);
112
113   # Query for all artists and put them in an array,
114   # or retrieve them as a result set object.
115   # $schema->resultset returns a DBIx::Class::ResultSet
116   my @all_artists = $schema->resultset('Artist')->all;
117   my $all_artists_rs = $schema->resultset('Artist');
118
119   # Output all artists names
120   # $artist here is a DBIx::Class::Row, which has accessors
121   # for all its columns. Rows are also subclasses of your Result class.
122   foreach $artist (@all_artists) {
123     print $artist->name, "\n";
124   }
125
126   # Create a result set to search for artists.
127   # This does not query the DB.
128   my $johns_rs = $schema->resultset('Artist')->search(
129     # Build your WHERE using an SQL::Abstract structure:
130     { name => { like => 'John%' } }
131   );
132
133   # Execute a joined query to get the cds.
134   my @all_john_cds = $johns_rs->search_related('cds')->all;
135
136   # Fetch the next available row.
137   my $first_john = $johns_rs->next;
138
139   # Specify ORDER BY on the query.
140   my $first_john_cds_by_title_rs = $first_john->cds(
141     undef,
142     { order_by => 'title' }
143   );
144
145   # Create a result set that will fetch the artist data
146   # at the same time as it fetches CDs, using only one query.
147   my $millennium_cds_rs = $schema->resultset('CD')->search(
148     { year => 2000 },
149     { prefetch => 'artist' }
150   );
151
152   my $cd = $millennium_cds_rs->next; # SELECT ... FROM cds JOIN artists ...
153   my $cd_artist_name = $cd->artist->name; # Already has the data so no 2nd query
154
155   # new() makes a DBIx::Class::Row object but doesnt insert it into the DB.
156   # create() is the same as new() then insert().
157   my $new_cd = $schema->resultset('CD')->new({ title => 'Spoon' });
158   $new_cd->artist($cd->artist);
159   $new_cd->insert; # Auto-increment primary key filled in after INSERT
160   $new_cd->title('Fork');
161
162   $schema->txn_do(sub { $new_cd->update }); # Runs the update in a transaction
163
164   # change the year of all the millennium CDs at once
165   $millennium_cds_rs->update({ year => 2002 });
166
167 =head1 DESCRIPTION
168
169 This is an SQL to OO mapper with an object API inspired by L<Class::DBI>
170 (with a compatibility layer as a springboard for porting) and a resultset API
171 that allows abstract encapsulation of database operations. It aims to make
172 representing queries in your code as perl-ish as possible while still
173 providing access to as many of the capabilities of the database as possible,
174 including retrieving related records from multiple tables in a single query,
175 JOIN, LEFT JOIN, COUNT, DISTINCT, GROUP BY, ORDER BY and HAVING support.
176
177 DBIx::Class can handle multi-column primary and foreign keys, complex
178 queries and database-level paging, and does its best to only query the
179 database in order to return something you've directly asked for. If a
180 resultset is used as an iterator it only fetches rows off the statement
181 handle as requested in order to minimise memory usage. It has auto-increment
182 support for SQLite, MySQL, PostgreSQL, Oracle, SQL Server and DB2 and is
183 known to be used in production on at least the first four, and is fork-
184 and thread-safe out of the box (although your DBD may not be).
185
186 This project is still under rapid development, so large new features may be
187 marked EXPERIMENTAL - such APIs are still usable but may have edge bugs.
188 Failing test cases are *always* welcome and point releases are put out rapidly
189 as bugs are found and fixed.
190
191 We do our best to maintain full backwards compatibility for published
192 APIs, since DBIx::Class is used in production in many organisations,
193 and even backwards incompatible changes to non-published APIs will be fixed
194 if they're reported and doing so doesn't cost the codebase anything.
195
196 The test suite is quite substantial, and several developer releases
197 are generally made to CPAN before the branch for the next release is
198 merged back to trunk for a major release.
199
200 =head1 WHERE TO GO NEXT
201
202 L<DBIx::Class::Manual::DocMap> lists each task you might want help on, and
203 the modules where you will find documentation.
204
205 =head1 AUTHOR
206
207 mst: Matt S. Trout <mst@shadowcatsystems.co.uk>
208
209 (I mostly consider myself "project founder" these days but the AUTHOR heading
210 is traditional :)
211
212 =head1 CONTRIBUTORS
213
214 abraxxa: Alexander Hartmaier <alex_hartmaier@hotmail.com>
215
216 aherzog: Adam Herzog <adam@herzogdesigns.com>
217
218 andyg: Andy Grundman <andy@hybridized.org>
219
220 ank: Andres Kievsky
221
222 arcanez: Justin Hunter <justin.d.hunter@gmail.com>
223
224 ash: Ash Berlin <ash@cpan.org>
225
226 bert: Norbert Csongradi <bert@cpan.org>
227
228 blblack: Brandon L. Black <blblack@gmail.com>
229
230 bluefeet: Aran Deltac <bluefeet@cpan.org>
231
232 boghead: Bryan Beeley <cpan@beeley.org>
233
234 bricas: Brian Cassidy <bricas@cpan.org>
235
236 brunov: Bruno Vecchi <vecchi.b@gmail.com>
237
238 caelum: Rafael Kitover <rkitover@cpan.org>
239
240 castaway: Jess Robinson
241
242 claco: Christopher H. Laco
243
244 clkao: CL Kao
245
246 da5id: David Jack Olrik <djo@cpan.org>
247
248 debolaz: Anders Nor Berle <berle@cpan.org>
249
250 dew: Dan Thomas <dan@godders.org>
251
252 dkubb: Dan Kubb <dan.kubb-cpan@onautopilot.com>
253
254 dnm: Justin Wheeler <jwheeler@datademons.com>
255
256 dwc: Daniel Westermann-Clark <danieltwc@cpan.org>
257
258 dyfrgi: Michael Leuchtenburg <michael@slashhome.org>
259
260 frew: Arthur Axel "fREW" Schmidt <frioux@gmail.com>
261
262 gphat: Cory G Watson <gphat@cpan.org>
263
264 groditi: Guillermo Roditi <groditi@cpan.org>
265
266 ilmari: Dagfinn Ilmari MannsE<aring>ker <ilmari@ilmari.org>
267
268 jasonmay: Jason May <jason.a.may@gmail.com>
269
270 jesper: Jesper Krogh
271
272 jgoulah: John Goulah <jgoulah@cpan.org>
273
274 jguenther: Justin Guenther <jguenther@cpan.org>
275
276 jhannah: Jay Hannah <jay@jays.net>
277
278 jnapiorkowski: John Napiorkowski <jjn1056@yahoo.com>
279
280 jon: Jon Schutz <jjschutz@cpan.org>
281
282 jshirley: J. Shirley <jshirley@gmail.com>
283
284 konobi: Scott McWhirter
285
286 lukes: Luke Saunders <luke.saunders@gmail.com>
287
288 marcus: Marcus Ramberg <mramberg@cpan.org>
289
290 mattlaw: Matt Lawrence
291
292 michaelr: Michael Reddick <michael.reddick@gmail.com>
293
294 ned: Neil de Carteret
295
296 nigel: Nigel Metheringham <nigelm@cpan.org>
297
298 ningu: David Kamholz <dkamholz@cpan.org>
299
300 Nniuq: Ron "Quinn" Straight" <quinnfazigu@gmail.org>
301
302 norbi: Norbert Buchmuller <norbi@nix.hu>
303
304 Numa: Dan Sully <daniel@cpan.org>
305
306 ovid: Curtis "Ovid" Poe <ovid@cpan.org>
307
308 oyse: Ã˜ystein Torget <oystein.torget@dnv.com>
309
310 paulm: Paul Makepeace
311
312 penguin: K J Cheetham
313
314 perigrin: Chris Prather <chris@prather.org>
315
316 peter: Peter Collingbourne <peter@pcc.me.uk>
317
318 phaylon: Robert Sedlacek <phaylon@dunkelheit.at>
319
320 plu: Johannes Plunien <plu@cpan.org>
321
322 quicksilver: Jules Bean
323
324 rafl: Florian Ragwitz <rafl@debian.org>
325
326 rbuels: Robert Buels <rmb32@cornell.edu>
327
328 rdj: Ryan D Johnson <ryan@innerfence.com>
329
330 ribasushi: Peter Rabbitson <ribasushi@cpan.org>
331
332 rjbs: Ricardo Signes <rjbs@cpan.org>
333
334 robkinyon: Rob Kinyon <rkinyon@cpan.org>
335
336 Roman: Roman Filippov <romanf@cpan.org>
337
338 sc_: Just Another Perl Hacker
339
340 scotty: Scotty Allen <scotty@scottyallen.com>
341
342 semifor: Marc Mims <marc@questright.com>
343
344 solomon: Jared Johnson <jaredj@nmgi.com>
345
346 spb: Stephen Bennett <stephen@freenode.net>
347
348 sszabo: Stephan Szabo <sszabo@bigpanda.com>
349
350 teejay : Aaron Trevena <teejay@cpan.org>
351
352 Todd Lipcon
353
354 Tom Hukins
355
356 triode: Pete Gamache <gamache@cpan.org>
357
358 typester: Daisuke Murase <typester@cpan.org>
359
360 victori: Victor Igumnov <victori@cpan.org>
361
362 wdh: Will Hawes
363
364 willert: Sebastian Willert <willert@cpan.org>
365
366 wreis: Wallace Reis <wreis@cpan.org>
367
368 zamolxes: Bogdan Lucaciu <bogdan@wiz.ro>
369
370 =head1 COPYRIGHT
371
372 Copyright (c) 2005 - 2009 the DBIx::Class L</AUTHOR> and L</CONTRIBUTORS>
373 as listed above.
374
375 =head1 LICENSE
376
377 This library is free software and may be distributed under the same terms
378 as perl itself.
379
380 =cut