7 use base qw/DBIx::Class::Componentised Class::Accessor::Grouped/;
8 use DBIx::Class::StartupCheck;
12 shift->mk_classaccessor(@_);
15 sub mk_classaccessor {
17 $self->mk_group_accessors('inherited', $_[0]);
18 $self->set_inherited(@_) if @_ > 1;
21 sub component_base_class { 'DBIx::Class' }
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
27 $VERSION = '0.08099_01';
29 $VERSION = eval $VERSION; # numify for warning-free dev releases
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];
41 my $cache = $self->can('__attr_cache') ? $self->__attr_cache : {};
42 my $rest = eval { $self->next::method };
43 return $@ ? $cache : { %$cache, %$rest };
50 DBIx::Class - Extensible and flexible object <-> relational mapper.
54 Create a schema class called DB/Main.pm:
57 use base qw/DBIx::Class::Schema/;
59 __PACKAGE__->load_classes();
63 Create a table class to represent artists, who have many CDs, in DB/Main/Artist.pm:
65 package DB::Main::Artist;
66 use base qw/DBIx::Class/;
68 __PACKAGE__->load_components(qw/PK::Auto Core/);
69 __PACKAGE__->table('artist');
70 __PACKAGE__->add_columns(qw/ artistid name /);
71 __PACKAGE__->set_primary_key('artistid');
72 __PACKAGE__->has_many(cds => 'DB::Main::CD');
76 A table class to represent a CD, which belongs to an artist, in DB/Main/CD.pm:
79 use base qw/DBIx::Class/;
81 __PACKAGE__->load_components(qw/PK::Auto Core/);
82 __PACKAGE__->table('cd');
83 __PACKAGE__->add_columns(qw/ cdid artist title year /);
84 __PACKAGE__->set_primary_key('cdid');
85 __PACKAGE__->belongs_to(artist => 'DB::Main::Artist');
89 Then you can use these classes in your application's code:
91 # Connect to your database.
93 my $schema = DB::Main->connect($dbi_dsn, $user, $pass, \%dbi_params);
95 # Query for all artists and put them in an array,
96 # or retrieve them as a result set object.
97 my @all_artists = $schema->resultset('Artist')->all;
98 my $all_artists_rs = $schema->resultset('Artist');
100 # Create a result set to search for artists.
101 # This does not query the DB.
102 my $johns_rs = $schema->resultset('Artist')->search(
103 # Build your WHERE using an SQL::Abstract structure:
104 { name => { like => 'John%' } }
107 # Execute a joined query to get the cds.
108 my @all_john_cds = $johns_rs->search_related('cds')->all;
110 # Fetch only the next row.
111 my $first_john = $johns_rs->next;
113 # Specify ORDER BY on the query.
114 my $first_john_cds_by_title_rs = $first_john->cds(
116 { order_by => 'title' }
119 # Create a result set that will fetch the artist relationship
120 # at the same time as it fetches CDs, using only one query.
121 my $millennium_cds_rs = $schema->resultset('CD')->search(
123 { prefetch => 'artist' }
126 my $cd = $millennium_cds_rs->next; # SELECT ... FROM cds JOIN artists ...
127 my $cd_artist_name = $cd->artist->name; # Already has the data so no query
129 # new() makes a DBIx::Class::Row object but doesnt insert it into the DB.
130 # create() is the same as new() then insert().
131 my $new_cd = $schema->resultset('CD')->new({ title => 'Spoon' });
132 $new_cd->artist($cd->artist);
133 $new_cd->insert; # Auto-increment primary key filled in after INSERT
134 $new_cd->title('Fork');
136 $schema->txn_do(sub { $new_cd->update }); # Runs the update in a transaction
138 $millennium_cds_rs->update({ year => 2002 }); # Single-query bulk update
142 This is an SQL to OO mapper with an object API inspired by L<Class::DBI>
143 (and a compatibility layer as a springboard for porting) and a resultset API
144 that allows abstract encapsulation of database operations. It aims to make
145 representing queries in your code as perl-ish as possible while still
146 providing access to as many of the capabilities of the database as possible,
147 including retrieving related records from multiple tables in a single query,
148 JOIN, LEFT JOIN, COUNT, DISTINCT, GROUP BY and HAVING support.
150 DBIx::Class can handle multi-column primary and foreign keys, complex
151 queries and database-level paging, and does its best to only query the
152 database in order to return something you've directly asked for. If a
153 resultset is used as an iterator it only fetches rows off the statement
154 handle as requested in order to minimise memory usage. It has auto-increment
155 support for SQLite, MySQL, PostgreSQL, Oracle, SQL Server and DB2 and is
156 known to be used in production on at least the first four, and is fork-
157 and thread-safe out of the box (although your DBD may not be).
159 This project is still under rapid development, so large new features may be
160 marked EXPERIMENTAL - such APIs are still usable but may have edge bugs.
161 Failing test cases are *always* welcome and point releases are put out rapidly
162 as bugs are found and fixed.
164 We do our best to maintain full backwards compatibility for published
165 APIs, since DBIx::Class is used in production in many organisations,
166 and even backwards incompatible changes to non-published APIs will be fixed
167 if they're reported and doing so doesn't cost the codebase anything.
169 The test suite is quite substantial, and several developer releases
170 are generally made to CPAN before the branch for the next release is
171 merged back to trunk for a major release.
173 The community can be found via:
175 Mailing list: http://lists.scsys.co.uk/mailman/listinfo/dbix-class/
177 SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
179 SVNWeb: http://dev.catalyst.perl.org/svnweb/bast/browse/DBIx-Class/
181 IRC: irc.perl.org#dbix-class
183 =head1 WHERE TO GO NEXT
185 L<DBIx::Class::Manual::DocMap> lists each task you might want help on, and
186 the modules where you will find documentation.
190 mst: Matt S. Trout <mst@shadowcatsystems.co.uk>
192 (I mostly consider myself "project founder" these days but the AUTHOR heading
197 abraxxa: Alexander Hartmaier <alex_hartmaier@hotmail.com>
199 aherzog: Adam Herzog <adam@herzogdesigns.com>
201 andyg: Andy Grundman <andy@hybridized.org>
205 ash: Ash Berlin <ash@cpan.org>
207 bert: Norbert Csongradi <bert@cpan.org>
209 blblack: Brandon L. Black <blblack@gmail.com>
211 bluefeet: Aran Deltac <bluefeet@cpan.org>
213 captainL: Luke Saunders <luke.saunders@gmail.com>
215 castaway: Jess Robinson
217 claco: Christopher H. Laco
221 da5id: David Jack Olrik <djo@cpan.org>
223 debolaz: Anders Nor Berle <berle@cpan.org>
225 dkubb: Dan Kubb <dan.kubb-cpan@onautopilot.com>
227 dnm: Justin Wheeler <jwheeler@datademons.com>
229 draven: Marcus Ramberg <mramberg@cpan.org>
231 dwc: Daniel Westermann-Clark <danieltwc@cpan.org>
233 dyfrgi: Michael Leuchtenburg <michael@slashhome.org>
235 gphat: Cory G Watson <gphat@cpan.org>
239 jguenther: Justin Guenther <jguenther@cpan.org>
241 jnapiorkowski: John Napiorkowski <jjn1056@yahoo.com>
243 jon: Jon Schutz <jjschutz@cpan.org>
245 jshirley: J. Shirley <jshirley@gmail.com>
247 konobi: Scott McWhirter
249 LTJake: Brian Cassidy <bricas@cpan.org>
251 mattlaw: Matt Lawrence
253 ned: Neil de Carteret
255 nigel: Nigel Metheringham <nigelm@cpan.org>
257 ningu: David Kamholz <dkamholz@cpan.org>
259 Numa: Dan Sully <daniel@cpan.org>
261 oyse: Øystein Torget <oystein.torget@dnv.com>
263 paulm: Paul Makepeace
265 penguin: K J Cheetham
267 perigrin: Chris Prather <chris@prather.org>
269 phaylon: Robert Sedlacek <phaylon@dunkelheit.at>
271 quicksilver: Jules Bean
273 rdj: Ryan D Johnson <ryan@innerfence.com>
275 sc_: Just Another Perl Hacker
277 scotty: Scotty Allen <scotty@scottyallen.com>
279 semifor: Marc Mims <marc@questright.com>
281 sszabo: Stephan Szabo <sszabo@bigpanda.com>
283 teejay : Aaron Trevena <teejay@cpan.org>
289 typester: Daisuke Murase <typester@cpan.org>
291 victori: Victor Igumnov <victori@cpan.org>
295 willert: Sebastian Willert <willert@cpan.org>
297 zamolxes: Bogdan Lucaciu <bogdan@wiz.ro>
301 You may distribute this code under the same terms as Perl itself.