7 use base qw/DBIx::Class::Componentised Class::Accessor::Grouped/;
11 shift->mk_classaccessor(@_);
14 sub mk_classaccessor {
16 $self->mk_group_accessors('inherited', $_[0]);
17 $self->set_inherited(@_) if @_ > 1;
20 sub component_base_class { 'DBIx::Class' }
22 # Always remember to do all digits for the version even if they're 0
23 # i.e. first release of 0.XX *must* be 0.XX000. This avoids fBSD ports
24 # brain damage and presumably various other packaging systems too
28 sub MODIFY_CODE_ATTRIBUTES {
29 my ($class,$code,@attrs) = @_;
30 $class->mk_classdata('__attr_cache' => {})
31 unless $class->can('__attr_cache');
32 $class->__attr_cache->{$code} = [@attrs];
38 my $cache = $self->can('__attr_cache') ? $self->__attr_cache : {};
39 my $rest = eval { $self->next::method };
40 return $@ ? $cache : { %$cache, %$rest };
47 DBIx::Class - Extensible and flexible object <-> relational mapper.
51 Create a schema class called DB/Main.pm:
54 use base qw/DBIx::Class::Schema/;
56 __PACKAGE__->load_classes();
60 Create a table class to represent artists, who have many CDs, in DB/Main/Artist.pm:
62 package DB::Main::Artist;
63 use base qw/DBIx::Class/;
65 __PACKAGE__->load_components(qw/PK::Auto Core/);
66 __PACKAGE__->table('artist');
67 __PACKAGE__->add_columns(qw/ artistid name /);
68 __PACKAGE__->set_primary_key('artistid');
69 __PACKAGE__->has_many(cds => 'DB::Main::CD');
73 A table class to represent a CD, which belongs to an artist, in DB/Main/CD.pm:
76 use base qw/DBIx::Class/;
78 __PACKAGE__->load_components(qw/PK::Auto Core/);
79 __PACKAGE__->table('cd');
80 __PACKAGE__->add_columns(qw/ cdid artist title year /);
81 __PACKAGE__->set_primary_key('cdid');
82 __PACKAGE__->belongs_to(artist => 'DB::Main::Artist');
86 Then you can use these classes in your application's code:
88 # Connect to your database.
90 my $schema = DB::Main->connect($dbi_dsn, $user, $pass, \%dbi_params);
92 # Query for all artists and put them in an array,
93 # or retrieve them as a result set object.
94 my @all_artists = $schema->resultset('Artist')->all;
95 my $all_artists_rs = $schema->resultset('Artist');
97 # Create a result set to search for artists.
98 # This does not query the DB.
99 my $johns_rs = $schema->resultset('Artist')->search(
100 # Build your WHERE using an SQL::Abstract structure:
101 { name => { like => 'John%' } }
104 # Execute a joined query to get the cds.
105 my @all_john_cds = $johns_rs->search_related('cds')->all;
107 # Fetch only the next row.
108 my $first_john = $johns_rs->next;
110 # Specify ORDER BY on the query.
111 my $first_john_cds_by_title_rs = $first_john->cds(
113 { order_by => 'title' }
116 # Create a result set that will fetch the artist relationship
117 # at the same time as it fetches CDs, using only one query.
118 my $millennium_cds_rs = $schema->resultset('CD')->search(
120 { prefetch => 'artist' }
123 my $cd = $millennium_cds_rs->next; # SELECT ... FROM cds JOIN artists ...
124 my $cd_artist_name = $cd->artist->name; # Already has the data so no query
126 my $new_cd = $schema->resultset('CD')->new({ title => 'Spoon' });
127 $new_cd->artist($cd->artist);
128 $new_cd->insert; # Auto-increment primary key filled in after INSERT
129 $new_cd->title('Fork');
131 $schema->txn_do(sub { $new_cd->update }); # Runs the update in a transaction
133 $millennium_cds_rs->update({ year => 2002 }); # Single-query bulk update
137 This is an SQL to OO mapper with an object API inspired by L<Class::DBI>
138 (and a compatibility layer as a springboard for porting) and a resultset API
139 that allows abstract encapsulation of database operations. It aims to make
140 representing queries in your code as perl-ish as possible while still
141 providing access to as many of the capabilities of the database as possible,
142 including retrieving related records from multiple tables in a single query,
143 JOIN, LEFT JOIN, COUNT, DISTINCT, GROUP BY and HAVING support.
145 DBIx::Class can handle multi-column primary and foreign keys, complex
146 queries and database-level paging, and does its best to only query the
147 database in order to return something you've directly asked for. If a
148 resultset is used as an iterator it only fetches rows off the statement
149 handle as requested in order to minimise memory usage. It has auto-increment
150 support for SQLite, MySQL, PostgreSQL, Oracle, SQL Server and DB2 and is
151 known to be used in production on at least the first four, and is fork-
152 and thread-safe out of the box (although your DBD may not be).
154 This project is still under rapid development, so features added in the
155 latest major release may not work 100% yet -- check the Changes if you run
156 into trouble, and beware of anything explicitly marked EXPERIMENTAL. Failing
157 test cases are *always* welcome and point releases are put out rapidly as
158 bugs are found and fixed.
160 Even so, we do our best to maintain full backwards compatibility for published
161 APIs, since DBIx::Class is used in production in a number of organisations.
162 The test suite is quite substantial, and several developer releases are
163 generally made to CPAN before the -current branch is merged back to trunk for
166 The community can be found via:
168 Mailing list: http://lists.rawmode.org/mailman/listinfo/dbix-class/
170 SVN: http://dev.catalyst.perl.org/repos/bast/trunk/DBIx-Class/
172 Wiki: http://dbix-class.shadowcatsystems.co.uk/
174 IRC: irc.perl.org#dbix-class
176 =head1 WHERE TO GO NEXT
178 L<DBIx::Class::Manual::DocMap> lists each task you might want help on, and
179 the modules where you will find documentation.
183 mst: Matt S. Trout <mst@shadowcatsystems.co.uk>
187 abraxxa: Alexander Hartmaier <alex_hartmaier@hotmail.com>
189 andyg: Andy Grundman <andy@hybridized.org>
193 ash: Ash Berlin <ash@cpan.org>
195 blblack: Brandon L. Black <blblack@gmail.com>
197 bluefeet: Aran Deltac <bluefeet@cpan.org>
199 captainL: Luke Saunders <luke.saunders@gmail.com>
201 castaway: Jess Robinson
203 claco: Christopher H. Laco
207 da5id: David Jack Olrik <djo@cpan.org>
209 dkubb: Dan Kubb <dan.kubb-cpan@onautopilot.com>
211 dnm: Justin Wheeler <jwheeler@datademons.com>
213 draven: Marcus Ramberg <mramberg@cpan.org>
215 dwc: Daniel Westermann-Clark <danieltwc@cpan.org>
217 dyfrgi: Michael Leuchtenburg <michael@slashhome.org>
219 gphat: Cory G Watson <gphat@cpan.org>
223 jguenther: Justin Guenther <jguenther@cpan.org>
225 jnapiorkowski: John Napiorkowski <jjn1056@yahoo.com>
227 jshirley: J. Shirley <jshirley@gmail.com>
229 konobi: Scott McWhirter
231 LTJake: Brian Cassidy <bricas@cpan.org>
233 ned: Neil de Carteret
235 nigel: Nigel Metheringham <nigelm@cpan.org>
237 ningu: David Kamholz <dkamholz@cpan.org>
239 Numa: Dan Sully <daniel@cpan.org>
241 paulm: Paul Makepeace
243 penguin: K J Cheetham
245 phaylon: Robert Sedlacek <phaylon@dunkelheit.at>
247 quicksilver: Jules Bean
249 sc_: Just Another Perl Hacker
251 scotty: Scotty Allen <scotty@scottyallen.com>
253 sszabo: Stephan Szabo <sszabo@bigpanda.com>
257 typester: Daisuke Murase <typester@cpan.org>
259 victori: Victor Igumnov <victori@cpan.org>
263 willert: Sebastian Willert <willert@cpan.org>
265 zamolxes: Bogdan Lucaciu <bogdan@wiz.ro>
269 You may distribute this code under the same terms as Perl itself.