Bump for New relase
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class.pm
1 package DBIx::Class;
2
3 use strict;
4 use warnings;
5
6 use vars qw($VERSION);
7 use base qw/DBIx::Class::Componentised Class::Accessor::Grouped/;
8
9
10 sub mk_classdata { 
11   shift->mk_classaccessor(@_);
12 }
13
14 sub mk_classaccessor {
15   my $self = shift;
16   $self->mk_group_accessors('inherited', $_[0]); 
17   $self->set_inherited(@_) if @_ > 1;
18 }
19
20 sub component_base_class { 'DBIx::Class' }
21
22 # Always remember to do all digits for the version even if they're 0
23 # i.e. first release of 0.XX *must* be 0.XX000. This avoids fBSD ports
24 # brain damage and presumably various other packaging systems too
25
26 $VERSION = '0.08007';
27
28 sub MODIFY_CODE_ATTRIBUTES {
29   my ($class,$code,@attrs) = @_;
30   $class->mk_classdata('__attr_cache' => {})
31     unless $class->can('__attr_cache');
32   $class->__attr_cache->{$code} = [@attrs];
33   return ();
34 }
35
36 sub _attr_cache {
37   my $self = shift;
38   my $cache = $self->can('__attr_cache') ? $self->__attr_cache : {};
39   my $rest = eval { $self->next::method };
40   return $@ ? $cache : { %$cache, %$rest };
41 }
42
43 1;
44
45 =head1 NAME
46
47 DBIx::Class - Extensible and flexible object <-> relational mapper.
48
49 =head1 SYNOPSIS
50
51 Create a schema class called DB/Main.pm:
52
53   package DB::Main;
54   use base qw/DBIx::Class::Schema/;
55
56   __PACKAGE__->load_classes();
57
58   1;
59
60 Create a table class to represent artists, who have many CDs, in DB/Main/Artist.pm:
61
62   package DB::Main::Artist;
63   use base qw/DBIx::Class/;
64
65   __PACKAGE__->load_components(qw/PK::Auto Core/);
66   __PACKAGE__->table('artist');
67   __PACKAGE__->add_columns(qw/ artistid name /);
68   __PACKAGE__->set_primary_key('artistid');
69   __PACKAGE__->has_many(cds => 'DB::Main::CD');
70
71   1;
72
73 A table class to represent a CD, which belongs to an artist, in DB/Main/CD.pm:
74
75   package DB::Main::CD;
76   use base qw/DBIx::Class/;
77
78   __PACKAGE__->load_components(qw/PK::Auto Core/);
79   __PACKAGE__->table('cd');
80   __PACKAGE__->add_columns(qw/ cdid artist title year /);
81   __PACKAGE__->set_primary_key('cdid');
82   __PACKAGE__->belongs_to(artist => 'DB::Main::Artist');
83
84   1;
85
86 Then you can use these classes in your application's code:
87
88   # Connect to your database.
89   use DB::Main;
90   my $schema = DB::Main->connect($dbi_dsn, $user, $pass, \%dbi_params);
91
92   # Query for all artists and put them in an array,
93   # or retrieve them as a result set object.
94   my @all_artists = $schema->resultset('Artist')->all;
95   my $all_artists_rs = $schema->resultset('Artist');
96
97   # Create a result set to search for artists.
98   # This does not query the DB.
99   my $johns_rs = $schema->resultset('Artist')->search(
100     # Build your WHERE using an SQL::Abstract structure:
101     { name => { like => 'John%' } }
102   );
103
104   # Execute a joined query to get the cds.
105   my @all_john_cds = $johns_rs->search_related('cds')->all;
106
107   # Fetch only the next row.
108   my $first_john = $johns_rs->next;
109
110   # Specify ORDER BY on the query.
111   my $first_john_cds_by_title_rs = $first_john->cds(
112     undef,
113     { order_by => 'title' }
114   );
115
116   # Create a result set that will fetch the artist relationship
117   # at the same time as it fetches CDs, using only one query.
118   my $millennium_cds_rs = $schema->resultset('CD')->search(
119     { year => 2000 },
120     { prefetch => 'artist' }
121   );
122
123   my $cd = $millennium_cds_rs->next; # SELECT ... FROM cds JOIN artists ...
124   my $cd_artist_name = $cd->artist->name; # Already has the data so no query
125
126   # new() makes a DBIx::Class::Row object but doesnt insert it into the DB.
127   # create() is the same as new() then insert().
128   my $new_cd = $schema->resultset('CD')->new({ title => 'Spoon' });
129   $new_cd->artist($cd->artist);
130   $new_cd->insert; # Auto-increment primary key filled in after INSERT
131   $new_cd->title('Fork');
132
133   $schema->txn_do(sub { $new_cd->update }); # Runs the update in a transaction
134
135   $millennium_cds_rs->update({ year => 2002 }); # Single-query bulk update
136
137 =head1 DESCRIPTION
138
139 This is an SQL to OO mapper with an object API inspired by L<Class::DBI>
140 (and a compatibility layer as a springboard for porting) and a resultset API
141 that allows abstract encapsulation of database operations. It aims to make
142 representing queries in your code as perl-ish as possible while still
143 providing access to as many of the capabilities of the database as possible,
144 including retrieving related records from multiple tables in a single query,
145 JOIN, LEFT JOIN, COUNT, DISTINCT, GROUP BY and HAVING support.
146
147 DBIx::Class can handle multi-column primary and foreign keys, complex
148 queries and database-level paging, and does its best to only query the
149 database in order to return something you've directly asked for. If a
150 resultset is used as an iterator it only fetches rows off the statement
151 handle as requested in order to minimise memory usage. It has auto-increment
152 support for SQLite, MySQL, PostgreSQL, Oracle, SQL Server and DB2 and is
153 known to be used in production on at least the first four, and is fork-
154 and thread-safe out of the box (although your DBD may not be).
155
156 This project is still under rapid development, so large new features may be
157 marked EXPERIMENTAL - such APIs are still usable but may have edge bugs.
158 Failing test cases are *always* welcome and point releases are put out rapidly
159 as bugs are found and fixed.
160
161 We do our best to maintain full backwards compatibility for published
162 APIs, since DBIx::Class is used in production in many organisations,
163 and even backwards incompatible changes to non-published APIs will be fixed
164 if they're reported and doing so doesn't cost the codebase anything.
165
166 The test suite is quite substantial, and several developer releases
167 are generally made to CPAN before the branch for the next release is
168 merged back to trunk for a major release.
169
170 The community can be found via:
171
172   Mailing list: http://lists.scsys.co.uk/mailman/listinfo/dbix-class/
173
174   SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
175
176   SVNWeb: http://dev.catalyst.perl.org/svnweb/bast/browse/DBIx-Class/
177
178   IRC: irc.perl.org#dbix-class
179
180 =head1 WHERE TO GO NEXT
181
182 L<DBIx::Class::Manual::DocMap> lists each task you might want help on, and
183 the modules where you will find documentation.
184
185 =head1 AUTHOR
186
187 mst: Matt S. Trout <mst@shadowcatsystems.co.uk>
188
189 (I mostly consider myself "project founder" these days but the AUTHOR heading
190 is traditional :)
191
192 =head1 CONTRIBUTORS
193
194 abraxxa: Alexander Hartmaier <alex_hartmaier@hotmail.com>
195
196 aherzog: Adam Herzog <adam@herzogdesigns.com>
197
198 andyg: Andy Grundman <andy@hybridized.org>
199
200 ank: Andres Kievsky
201
202 ash: Ash Berlin <ash@cpan.org>
203
204 blblack: Brandon L. Black <blblack@gmail.com>
205
206 bluefeet: Aran Deltac <bluefeet@cpan.org>
207
208 captainL: Luke Saunders <luke.saunders@gmail.com>
209
210 castaway: Jess Robinson
211
212 claco: Christopher H. Laco
213
214 clkao: CL Kao
215
216 da5id: David Jack Olrik <djo@cpan.org>
217
218 dkubb: Dan Kubb <dan.kubb-cpan@onautopilot.com>
219
220 dnm: Justin Wheeler <jwheeler@datademons.com>
221
222 draven: Marcus Ramberg <mramberg@cpan.org>
223
224 dwc: Daniel Westermann-Clark <danieltwc@cpan.org>
225
226 dyfrgi: Michael Leuchtenburg <michael@slashhome.org>
227
228 gphat: Cory G Watson <gphat@cpan.org>
229
230 jesper: Jesper Krogh
231
232 jguenther: Justin Guenther <jguenther@cpan.org>
233
234 jnapiorkowski: John Napiorkowski <jjn1056@yahoo.com>
235
236 jshirley: J. Shirley <jshirley@gmail.com>
237
238 konobi: Scott McWhirter
239
240 LTJake: Brian Cassidy <bricas@cpan.org>
241
242 mattlaw: Matt Lawrence
243
244 ned: Neil de Carteret
245
246 nigel: Nigel Metheringham <nigelm@cpan.org>
247
248 ningu: David Kamholz <dkamholz@cpan.org>
249
250 Numa: Dan Sully <daniel@cpan.org>
251
252 paulm: Paul Makepeace
253
254 penguin: K J Cheetham
255
256 phaylon: Robert Sedlacek <phaylon@dunkelheit.at>
257
258 quicksilver: Jules Bean
259
260 sc_: Just Another Perl Hacker
261
262 scotty: Scotty Allen <scotty@scottyallen.com>
263
264 sszabo: Stephan Szabo <sszabo@bigpanda.com>
265
266 Todd Lipcon
267
268 Tom Hukins
269
270 typester: Daisuke Murase <typester@cpan.org>
271
272 victori: Victor Igumnov <victori@cpan.org>
273
274 wdh: Will Hawes
275
276 willert: Sebastian Willert <willert@cpan.org>
277
278 zamolxes: Bogdan Lucaciu <bogdan@wiz.ro>
279
280 =head1 LICENSE
281
282 You may distribute this code under the same terms as Perl itself.
283
284 =cut