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