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