Stop stripping table name qualifier on complex $rs->update/delete
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class.pm
CommitLineData
ea2e61bf 1package DBIx::Class;
2
5d283305 3use strict;
4use warnings;
5
f9cc85ce 6our $VERSION;
7# Always remember to do all digits for the version even if they're 0
8# i.e. first release of 0.XX *must* be 0.XX000. This avoids fBSD ports
9# brain damage and presumably various other packaging systems too
10
11# $VERSION declaration must stay up here, ahead of any other package
12# declarations, as to not confuse various modules attempting to determine
13# this ones version, whether that be s.c.o. or Module::Metadata, etc
8b4fa23f 14$VERSION = '0.08204';
f9cc85ce 15
16$VERSION = eval $VERSION if $VERSION =~ /_/; # numify for warning-free dev releases
17
87bf71d5 18BEGIN {
6cefa7ab 19 package # hide from pause
20 DBIx::Class::_ENV_;
e0b2dc74 21
0d8817bc 22 use Config;
4bea1fe7 23
0d8817bc 24 use constant {
e0b2dc74 25
0d8817bc 26 # but of course
27 BROKEN_FORK => ($^O eq 'MSWin32') ? 1 : 0,
b4ad8a74 28
0d8817bc 29 HAS_ITHREADS => $Config{useithreads} ? 1 : 0,
e0b2dc74 30
0d8817bc 31 # ::Runmode would only be loaded by DBICTest, which in turn implies t/
32 DBICTEST => eval { DBICTest::RunMode->is_author } ? 1 : 0,
e0b2dc74 33
0d8817bc 34 # During 5.13 dev cycle HELEMs started to leak on copy
35 PEEPEENESS =>
36 # request for all tests would force "non-leaky" illusion and vice-versa
37 defined $ENV{DBICTEST_ALL_LEAKS} ? !$ENV{DBICTEST_ALL_LEAKS}
38 # otherwise confess that this perl is busted ONLY on smokers
39 : eval { DBICTest::RunMode->is_smoker } && ($] >= 5.013005 and $] <= 5.013006) ? 1
40 # otherwise we are good
41 : 0
42 ,
e0b2dc74 43
0d8817bc 44 # There was a brief period of p5p insanity when $@ was invisible in a DESTROY
45 INVISIBLE_DOLLAR_AT => ($] >= 5.013001 and $] <= 5.013007) ? 1 : 0,
e0b2dc74 46
0d8817bc 47 };
b4ad8a74 48
0d8817bc 49 if ($] < 5.009_005) {
50 require MRO::Compat;
51 constant->import( OLD_MRO => 1 );
52 }
53 else {
54 require mro;
55 constant->import( OLD_MRO => 0 );
56 }
87bf71d5 57}
58
d38cd95c 59use mro 'c3';
329d7385 60
2527233b 61use DBIx::Class::Optional::Dependencies;
62
db29433c 63use base qw/DBIx::Class::Componentised DBIx::Class::AccessorGroup/;
11736b4c 64use DBIx::Class::StartupCheck;
3e110410 65
70c28808 66__PACKAGE__->mk_group_accessors(inherited => '_skip_namespace_frames');
9345b14c 67__PACKAGE__->_skip_namespace_frames('^DBIx::Class|^SQL::Abstract|^Try::Tiny|^Class::Accessor::Grouped|^Context::Preserve');
70c28808 68
ade0fe3b 69sub mk_classdata {
77d518d1 70 shift->mk_classaccessor(@_);
71}
72
73sub mk_classaccessor {
74 my $self = shift;
ade0fe3b 75 $self->mk_group_accessors('inherited', $_[0]);
77d518d1 76 $self->set_inherited(@_) if @_ > 1;
3e110410 77}
3c0068c1 78
7411204b 79sub component_base_class { 'DBIx::Class' }
227d4dee 80
f0750722 81sub MODIFY_CODE_ATTRIBUTES {
b5d2c57f 82 my ($class,$code,@attrs) = @_;
83 $class->mk_classdata('__attr_cache' => {})
84 unless $class->can('__attr_cache');
85 $class->__attr_cache->{$code} = [@attrs];
86 return ();
f0750722 87}
88
da95b45f 89sub _attr_cache {
b5d2c57f 90 my $self = shift;
91 my $cache = $self->can('__attr_cache') ? $self->__attr_cache : {};
9780718f 92
93 return {
94 %$cache,
95 %{ $self->maybe::next::method || {} },
20674fcd 96 };
da95b45f 97}
98
ea2e61bf 991;
34d52be2 100
75d07914 101=head1 NAME
34d52be2 102
7e4b2f59 103DBIx::Class - Extensible and flexible object <-> relational mapper.
34d52be2 104
3b1c2bbd 105=head1 GETTING HELP/SUPPORT
106
107The community can be found via:
108
a06e1181 109=over
3b1c2bbd 110
c6fdaf2a 111=item * IRC: irc.perl.org#dbix-class
112
113=for html
e3194e31 114<a href="http://chat.mibbit.com/#dbix-class@irc.perl.org">(click for instant chatroom login)</a>
3b1c2bbd 115
a06e1181 116=item * Mailing list: L<http://lists.scsys.co.uk/mailman/listinfo/dbix-class>
3b1c2bbd 117
86a23587 118=item * Twitter L<http://www.twitter.com/dbix_class>
119
120=item * Web Site: L<http://www.dbix-class.org/>
121
a06e1181 122=item * RT Bug Tracker: L<https://rt.cpan.org/Dist/Display.html?Queue=DBIx-Class>
123
86a23587 124=back
125
126The project is maintained in a git repository, accessible from the following sources:
127
128=over
a06e1181 129
aeb669b8 130=item * git: L<git://git.shadowcat.co.uk/dbsrgits/DBIx-Class.git>
a06e1181 131
86a23587 132=item * gitweb: L<http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=dbsrgits/DBIx-Class.git>
133
134=item * github mirror: L<https://github.com/dbsrgits/DBIx-Class>
135
136=item * authorized committers: L<ssh://dbsrgits@git.shadowcat.co.uk/DBIx-Class.git>
43517a60 137
a06e1181 138=back
3b1c2bbd 139
34d52be2 140=head1 SYNOPSIS
141
03460bef 142Create a schema class called MyApp/Schema.pm:
34d52be2 143
03460bef 144 package MyApp::Schema;
a0638a7b 145 use base qw/DBIx::Class::Schema/;
34d52be2 146
f0bb26f3 147 __PACKAGE__->load_namespaces();
daec44b8 148
a0638a7b 149 1;
daec44b8 150
30e1753a 151Create a result class to represent artists, who have many CDs, in
03460bef 152MyApp/Schema/Result/Artist.pm:
daec44b8 153
30e1753a 154See L<DBIx::Class::ResultSource> for docs on defining result classes.
155
03460bef 156 package MyApp::Schema::Result::Artist;
d88ecca6 157 use base qw/DBIx::Class::Core/;
daec44b8 158
a0638a7b 159 __PACKAGE__->table('artist');
160 __PACKAGE__->add_columns(qw/ artistid name /);
161 __PACKAGE__->set_primary_key('artistid');
326dacbf 162 __PACKAGE__->has_many(cds => 'MyApp::Schema::Result::CD', 'artistid');
daec44b8 163
a0638a7b 164 1;
daec44b8 165
30e1753a 166A result class to represent a CD, which belongs to an artist, in
03460bef 167MyApp/Schema/Result/CD.pm:
39fe0e65 168
03460bef 169 package MyApp::Schema::Result::CD;
d88ecca6 170 use base qw/DBIx::Class::Core/;
39fe0e65 171
d88ecca6 172 __PACKAGE__->load_components(qw/InflateColumn::DateTime/);
a0638a7b 173 __PACKAGE__->table('cd');
bd077b47 174 __PACKAGE__->add_columns(qw/ cdid artistid title year /);
a0638a7b 175 __PACKAGE__->set_primary_key('cdid');
03460bef 176 __PACKAGE__->belongs_to(artist => 'MyApp::Schema::Result::Artist', 'artistid');
39fe0e65 177
a0638a7b 178 1;
39fe0e65 179
a0638a7b 180Then you can use these classes in your application's code:
39fe0e65 181
a0638a7b 182 # Connect to your database.
03460bef 183 use MyApp::Schema;
184 my $schema = MyApp::Schema->connect($dbi_dsn, $user, $pass, \%dbi_params);
a0638a7b 185
186 # Query for all artists and put them in an array,
187 # or retrieve them as a result set object.
30e1753a 188 # $schema->resultset returns a DBIx::Class::ResultSet
2053ab2a 189 my @all_artists = $schema->resultset('Artist')->all;
190 my $all_artists_rs = $schema->resultset('Artist');
126042ee 191
30e1753a 192 # Output all artists names
4e8ffded 193 # $artist here is a DBIx::Class::Row, which has accessors
16ccb4fe 194 # for all its columns. Rows are also subclasses of your Result class.
85067746 195 foreach $artist (@all_artists) {
30e1753a 196 print $artist->name, "\n";
197 }
198
a0638a7b 199 # Create a result set to search for artists.
86beca1d 200 # This does not query the DB.
2053ab2a 201 my $johns_rs = $schema->resultset('Artist')->search(
6576ef54 202 # Build your WHERE using an SQL::Abstract structure:
2053ab2a 203 { name => { like => 'John%' } }
a0638a7b 204 );
39fe0e65 205
2053ab2a 206 # Execute a joined query to get the cds.
a0638a7b 207 my @all_john_cds = $johns_rs->search_related('cds')->all;
448c8424 208
f0bb26f3 209 # Fetch the next available row.
a0638a7b 210 my $first_john = $johns_rs->next;
448c8424 211
2053ab2a 212 # Specify ORDER BY on the query.
a0638a7b 213 my $first_john_cds_by_title_rs = $first_john->cds(
214 undef,
215 { order_by => 'title' }
216 );
448c8424 217
bd077b47 218 # Create a result set that will fetch the artist data
2053ab2a 219 # at the same time as it fetches CDs, using only one query.
884559b1 220 my $millennium_cds_rs = $schema->resultset('CD')->search(
a0638a7b 221 { year => 2000 },
222 { prefetch => 'artist' }
223 );
448c8424 224
880a1a0c 225 my $cd = $millennium_cds_rs->next; # SELECT ... FROM cds JOIN artists ...
bd077b47 226 my $cd_artist_name = $cd->artist->name; # Already has the data so no 2nd query
076652e8 227
264f1571 228 # new() makes a DBIx::Class::Row object but doesnt insert it into the DB.
229 # create() is the same as new() then insert().
884559b1 230 my $new_cd = $schema->resultset('CD')->new({ title => 'Spoon' });
f183eccd 231 $new_cd->artist($cd->artist);
f183eccd 232 $new_cd->insert; # Auto-increment primary key filled in after INSERT
f183eccd 233 $new_cd->title('Fork');
234
884559b1 235 $schema->txn_do(sub { $new_cd->update }); # Runs the update in a transaction
f183eccd 236
bd077b47 237 # change the year of all the millennium CDs at once
238 $millennium_cds_rs->update({ year => 2002 });
f183eccd 239
240=head1 DESCRIPTION
241
242This is an SQL to OO mapper with an object API inspired by L<Class::DBI>
bd077b47 243(with a compatibility layer as a springboard for porting) and a resultset API
f183eccd 244that allows abstract encapsulation of database operations. It aims to make
245representing queries in your code as perl-ish as possible while still
a0638a7b 246providing access to as many of the capabilities of the database as possible,
f183eccd 247including retrieving related records from multiple tables in a single query,
bd077b47 248JOIN, LEFT JOIN, COUNT, DISTINCT, GROUP BY, ORDER BY and HAVING support.
f183eccd 249
250DBIx::Class can handle multi-column primary and foreign keys, complex
251queries and database-level paging, and does its best to only query the
75d07914 252database in order to return something you've directly asked for. If a
253resultset is used as an iterator it only fetches rows off the statement
254handle as requested in order to minimise memory usage. It has auto-increment
2053ab2a 255support for SQLite, MySQL, PostgreSQL, Oracle, SQL Server and DB2 and is
256known to be used in production on at least the first four, and is fork-
ec6415a9 257and thread-safe out of the box (although
9361b05d 258L<your DBD may not be|DBI/Threads and Thread Safety>).
f183eccd 259
dfccde48 260This project is still under rapid development, so large new features may be
261marked EXPERIMENTAL - such APIs are still usable but may have edge bugs.
262Failing test cases are *always* welcome and point releases are put out rapidly
263as bugs are found and fixed.
264
265We do our best to maintain full backwards compatibility for published
266APIs, since DBIx::Class is used in production in many organisations,
267and even backwards incompatible changes to non-published APIs will be fixed
268if they're reported and doing so doesn't cost the codebase anything.
269
264f1571 270The test suite is quite substantial, and several developer releases
271are generally made to CPAN before the branch for the next release is
272merged back to trunk for a major release.
f183eccd 273
f183eccd 274=head1 WHERE TO GO NEXT
275
2ca930b4 276L<DBIx::Class::Manual::DocMap> lists each task you might want help on, and
277the modules where you will find documentation.
076652e8 278
3942ab4d 279=head1 AUTHOR
34d52be2 280
266bdcc3 281mst: Matt S. Trout <mst@shadowcatsystems.co.uk>
34d52be2 282
dfccde48 283(I mostly consider myself "project founder" these days but the AUTHOR heading
284is traditional :)
285
3942ab4d 286=head1 CONTRIBUTORS
287
907ed671 288abraxxa: Alexander Hartmaier <abraxxa@cpan.org>
84e3c114 289
fd4b0742 290acca: Alexander Kuznetsov <acca@cpan.org>
291
6d84db2c 292aherzog: Adam Herzog <adam@herzogdesigns.com>
293
daeb1865 294Alexander Keusch <cpan@keusch.at>
295
c3de6b51 296alexrj: Alessandro Ranellucci <aar@cpan.org>
297
630ba6e5 298alnewkirk: Al Newkirk <we@ana.im>
299
6ebf5cbb 300amiri: Amiri Barksdale <amiri@metalabel.com>
301
b703fec7 302amoore: Andrew Moore <amoore@cpan.org>
303
266bdcc3 304andyg: Andy Grundman <andy@hybridized.org>
3942ab4d 305
266bdcc3 306ank: Andres Kievsky
3942ab4d 307
59ac6523 308arc: Aaron Crane <arc@cpan.org>
309
624764ae 310arcanez: Justin Hunter <justin.d.hunter@gmail.com>
311
ce4c07df 312ash: Ash Berlin <ash@cpan.org>
313
3d5bd2af 314bert: Norbert Csongradi <bert@cpan.org>
315
967a4c40 316blblack: Brandon L. Black <blblack@gmail.com>
3942ab4d 317
62eb8fe8 318bluefeet: Aran Deltac <bluefeet@cpan.org>
319
d6170b26 320bphillips: Brian Phillips <bphillips@cpan.org>
321
f856fe01 322boghead: Bryan Beeley <cpan@beeley.org>
323
23d9df41 324brd: Brad Davis <brd@FreeBSD.org>
325
bcb8f3ed 326bricas: Brian Cassidy <bricas@cpan.org>
327
3d7e3e05 328brunov: Bruno Vecchi <vecchi.b@gmail.com>
329
281719d2 330caelum: Rafael Kitover <rkitover@cpan.org>
331
f5f2af8f 332caldrin: Maik Hentsche <maik.hentsche@amd.com>
333
d3b0e369 334castaway: Jess Robinson
3942ab4d 335
266bdcc3 336claco: Christopher H. Laco
ccb9c9b1 337
266bdcc3 338clkao: CL Kao
3942ab4d 339
e21dfd6a 340da5id: David Jack Olrik <djo@cpan.org>
18360aed 341
13de943d 342debolaz: Anders Nor Berle <berle@cpan.org>
343
d1f542db 344dew: Dan Thomas <dan@godders.org>
345
266bdcc3 346dkubb: Dan Kubb <dan.kubb-cpan@onautopilot.com>
ccb9c9b1 347
9382ad07 348dnm: Justin Wheeler <jwheeler@datademons.com>
349
0818c9a7 350dpetrov: Dimitar Petrov <mitakaa@gmail.com>
351
266bdcc3 352dwc: Daniel Westermann-Clark <danieltwc@cpan.org>
4685e006 353
5cffe785 354dyfrgi: Michael Leuchtenburg <michael@slashhome.org>
8fe164b9 355
7b71391b 356edenc: Eden Cardim <edencardim@gmail.com>
357
307f1265 358felliott: Fitz Elliott <fitz.elliott@gmail.com>
359
0ffada27 360freetime: Bill Moseley <moseley@hank.org>
361
ade0fe3b 362frew: Arthur Axel "fREW" Schmidt <frioux@gmail.com>
363
b4987ed0 364goraxe: Gordon Irving <goraxe@cpan.org>
365
d3b0e369 366gphat: Cory G Watson <gphat@cpan.org>
ad3d2d7c 367
61f031bf 368Grant Street Group L<http://www.grantstreet.com/>
369
e758ffe6 370groditi: Guillermo Roditi <groditi@cpan.org>
371
6dad89f5 372Haarg: Graham Knop <haarg@haarg.org>
373
157ce0cf 374hobbs: Andrew Rodland <arodland@cpan.org>
375
5d779578 376ilmari: Dagfinn Ilmari MannsE<aring>ker <ilmari@ilmari.org>
377
0ac0af6c 378initself: Mike Baas <mike@initselftech.com>
379
2bb4c37b 380ironcamel: Naveed Massjouni <naveedm9@gmail.com>
381
f165eda8 382jawnsy: Jonathan Yu <jawnsy@cpan.org>
383
bee21976 384jasonmay: Jason May <jason.a.may@gmail.com>
385
d3b0e369 386jesper: Jesper Krogh
5fb0c64c 387
4a743a00 388jgoulah: John Goulah <jgoulah@cpan.org>
389
102a2984 390jguenther: Justin Guenther <jguenther@cpan.org>
d7c4c15c 391
a14a46e2 392jhannah: Jay Hannah <jay@jays.net>
393
8b93a938 394jnapiorkowski: John Napiorkowski <jjn1056@yahoo.com>
395
11736b4c 396jon: Jon Schutz <jjschutz@cpan.org>
397
1aec4bac 398jshirley: J. Shirley <jshirley@gmail.com>
399
41519379 400kaare: Kaare Rasmussen
401
d3b0e369 402konobi: Scott McWhirter
535fc2ee 403
4e0a89e4 404littlesavage: Alexey Illarionov <littlesavage@orionet.ru>
405
4367679a 406lukes: Luke Saunders <luke.saunders@gmail.com>
407
709ea492 408marcus: Marcus Ramberg <mramberg@cpan.org>
409
114780ee 410mattlaw: Matt Lawrence
411
45bffdf0 412mattp: Matt Phillips <mattp@cpan.org>
413
58755bba 414michaelr: Michael Reddick <michael.reddick@gmail.com>
415
91d0c99f 416milki: Jonathan Chu <milki@rescomp.berkeley.edu>
417
582fe49d 418mjemmeson: Michael Jemmeson <michael.jemmeson@gmail.com>
419
167e7634 420mstratman: Mark A. Stratman <stratman@gmail.com>
421
77e7e47d 422ned: Neil de Carteret
423
266bdcc3 424nigel: Nigel Metheringham <nigelm@cpan.org>
6565b410 425
d3b0e369 426ningu: David Kamholz <dkamholz@cpan.org>
427
66cf3a84 428Nniuq: Ron "Quinn" Straight" <quinnfazigu@gmail.org>
429
20b4c148 430norbi: Norbert Buchmuller <norbi@nix.hu>
431
48580715 432nuba: Nuba Princigalli <nuba@cpan.org>
433
d3b0e369 434Numa: Dan Sully <daniel@cpan.org>
435
dc571b76 436ovid: Curtis "Ovid" Poe <ovid@cpan.org>
437
6c30f9c3 438oyse: E<Oslash>ystein Torget <oystein.torget@dnv.com>
bf356c54 439
266bdcc3 440paulm: Paul Makepeace
4763f4b7 441
d3b0e369 442penguin: K J Cheetham
443
8cfef6f5 444perigrin: Chris Prather <chris@prather.org>
445
14899528 446peter: Peter Collingbourne <peter@pcc.me.uk>
caac1708 447
6c30f9c3 448Peter Valdemar ME<oslash>rch <peter@morch.com>
449
266bdcc3 450phaylon: Robert Sedlacek <phaylon@dunkelheit.at>
a53b95f1 451
56fadd8f 452plu: Johannes Plunien <plu@cpan.org>
453
0c1a4a15 454Possum: Daniel LeWarne <possum@cpan.org>
455
d3b0e369 456quicksilver: Jules Bean
022e0893 457
4ed01b34 458rafl: Florian Ragwitz <rafl@debian.org>
459
0c1a4a15 460rainboxx: Matthias Dietrich <perl@rb.ly>
461
868a7b26 462rbo: Robert Bohne <rbo@cpan.org>
463
7ff0dace 464rbuels: Robert Buels <rmb32@cornell.edu>
465
0da8b7da 466rdj: Ryan D Johnson <ryan@innerfence.com>
467
66b1e361 468ribasushi: Peter Rabbitson <ribasushi@cpan.org>
d76e282a 469
b487918c 470rjbs: Ricardo Signes <rjbs@cpan.org>
471
6ffb5be5 472robkinyon: Rob Kinyon <rkinyon@cpan.org>
473
726c8f65 474Robert Olson <bob@rdolson.org>
475
e4c9f3f0 476Roman: Roman Filippov <romanf@cpan.org>
477
dc81dba3 478Sadrak: Felix Antonius Wilhelm Ostmann <sadrak@cpan.org>
479
d3b0e369 480sc_: Just Another Perl Hacker
ba606e58 481
266bdcc3 482scotty: Scotty Allen <scotty@scottyallen.com>
181a28f4 483
1c133e22 484semifor: Marc Mims <marc@questright.com>
485
16667b3a 486SineSwiper: Brendan Byrd <bbyrd@cpan.org>
487
20b4c148 488solomon: Jared Johnson <jaredj@nmgi.com>
489
88f937fb 490spb: Stephen Bennett <stephen@freenode.net>
491
59187a3b 492Squeeks <squeek@cpan.org>
493
637ca936 494sszabo: Stephan Szabo <sszabo@bigpanda.com>
495
a9e8284f 496talexb: Alex Beamish <talexb@gmail.com>
497
7bf0d0d6 498tamias: Ronald J Kimball <rjk@tamias.net>
f92a9d79 499
386c2272 500teejay : Aaron Trevena <teejay@cpan.org>
501
84e3c114 502Todd Lipcon
e063fe2c 503
6eb6264e 504Tom Hukins
505
2e4b6d78 506tonvoon: Ton Voon <tonvoon@cpan.org>
507
d15e3fc5 508triode: Pete Gamache <gamache@cpan.org>
509
d3b0e369 510typester: Daisuke Murase <typester@cpan.org>
c0e7b4e5 511
4740bdb7 512victori: Victor Igumnov <victori@cpan.org>
513
d3b0e369 514wdh: Will Hawes
4c248161 515
124162a0 516wesm: Wes Malone <wes@mitsi.com>
517
00c03ced 518willert: Sebastian Willert <willert@cpan.org>
519
66d2a14e 520wreis: Wallace Reis <wreis@cpan.org>
521
d7c37d66 522xenoterracide: Caleb Cushing <xenoterracide@gmail.com>
523
d52d4d6e 524yrlnry: Mark Jason Dominus <mjd@plover.com>
525
d3b0e369 526zamolxes: Bogdan Lucaciu <bogdan@wiz.ro>
78060df8 527
b38e10bd 528=head1 COPYRIGHT
529
16be93fe 530Copyright (c) 2005 - 2011 the DBIx::Class L</AUTHOR> and L</CONTRIBUTORS>
b38e10bd 531as listed above.
532
96154ef7 533=head1 LICENSE
534
535This library is free software and may be distributed under the same terms
536as perl itself.
537
34d52be2 538=cut