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