7 use base qw/DBIx::Class::Componentised Class::Data::Accessor/;
9 sub mk_classdata { shift->mk_classaccessor(@_); }
10 sub component_base_class { 'DBIx::Class' }
12 # Always remember to do all digits for the version even if they're 0
13 # i.e. first release of 0.XX *must* be 0.XX000. This avoids fBSD ports
14 # brain damage and presumably various other packaging systems too
18 sub MODIFY_CODE_ATTRIBUTES {
19 my ($class,$code,@attrs) = @_;
20 $class->mk_classdata('__attr_cache' => {})
21 unless $class->can('__attr_cache');
22 $class->__attr_cache->{$code} = [@attrs];
28 my $cache = $self->can('__attr_cache') ? $self->__attr_cache : {};
29 my $rest = eval { $self->next::method };
30 return $@ ? $cache : { %$cache, %$rest };
37 DBIx::Class - Extensible and flexible object <-> relational mapper.
41 Create a schema class called DB/Main.pm:
44 use base qw/DBIx::Class::Schema/;
46 __PACKAGE__->load_classes();
50 Create a table class to represent artists, who have many CDs, in DB/Main/Artist.pm:
52 package DB::Main::Artist;
53 use base qw/DBIx::Class/;
55 __PACKAGE__->load_components(qw/PK::Auto Core/);
56 __PACKAGE__->table('artist');
57 __PACKAGE__->add_columns(qw/ artistid name /);
58 __PACKAGE__->set_primary_key('artistid');
59 __PACKAGE__->has_many(cds => 'DB::Main::CD');
63 A table class to represent a CD, which belongs to an artist, in DB/Main/CD.pm:
66 use base qw/DBIx::Class/;
68 __PACKAGE__->load_components(qw/PK::Auto Core/);
69 __PACKAGE__->table('cd');
70 __PACKAGE__->add_columns(qw/ cdid artist title year /);
71 __PACKAGE__->set_primary_key('cdid');
72 __PACKAGE__->belongs_to(artist => 'DB::Main::Artist');
76 Then you can use these classes in your application's code:
78 # Connect to your database.
80 my $schema = DB::Main->connect($dbi_dsn, $user, $pass, \%dbi_params);
82 # Query for all artists and put them in an array,
83 # or retrieve them as a result set object.
84 my @all_artists = $schema->resultset('Artist')->all;
85 my $all_artists_rs = $schema->resultset('Artist');
87 # Create a result set to search for artists.
88 # This does not query the DB.
89 my $johns_rs = $schema->resultset('Artist')->search(
90 # Build your WHERE using an L<SQL::Abstract> structure:
91 { name => { like => 'John%' } }
94 # Execute a joined query to get the cds.
95 my @all_john_cds = $johns_rs->search_related('cds')->all;
97 # Fetch only the next row.
98 my $first_john = $johns_rs->next;
100 # Specify ORDER BY on the query.
101 my $first_john_cds_by_title_rs = $first_john->cds(
103 { order_by => 'title' }
106 # Create a result set that will fetch the artist relationship
107 # at the same time as it fetches CDs, using only one query.
108 my $millennium_cds_rs = $schema->resultset('CD')->search(
110 { prefetch => 'artist' }
113 my $cd = $millennium_cds_rs->next; # SELECT ... FROM cds JOIN artists ...
114 my $cd_artist_name = $cd->artist->name; # Already has the data so no query
116 my $new_cd = $schema->resultset('CD')->new({ title => 'Spoon' });
117 $new_cd->artist($cd->artist);
118 $new_cd->insert; # Auto-increment primary key filled in after INSERT
119 $new_cd->title('Fork');
121 $schema->txn_do(sub { $new_cd->update }); # Runs the update in a transaction
123 $millennium_cds_rs->update({ year => 2002 }); # Single-query bulk update
127 This is an SQL to OO mapper with an object API inspired by L<Class::DBI>
128 (and a compatibility layer as a springboard for porting) and a resultset API
129 that allows abstract encapsulation of database operations. It aims to make
130 representing queries in your code as perl-ish as possible while still
131 providing access to as many of the capabilities of the database as possible,
132 including retrieving related records from multiple tables in a single query,
133 JOIN, LEFT JOIN, COUNT, DISTINCT, GROUP BY and HAVING support.
135 DBIx::Class can handle multi-column primary and foreign keys, complex
136 queries and database-level paging, and does its best to only query the
137 database in order to return something you've directly asked for. If a
138 resultset is used as an iterator it only fetches rows off the statement
139 handle as requested in order to minimise memory usage. It has auto-increment
140 support for SQLite, MySQL, PostgreSQL, Oracle, SQL Server and DB2 and is
141 known to be used in production on at least the first four, and is fork-
142 and thread-safe out of the box (although your DBD may not be).
144 This project is still under rapid development, so features added in the
145 latest major release may not work 100% yet -- check the Changes if you run
146 into trouble, and beware of anything explicitly marked EXPERIMENTAL. Failing
147 test cases are *always* welcome and point releases are put out rapidly as
148 bugs are found and fixed.
150 Even so, we do our best to maintain full backwards compatibility for published
151 APIs, since DBIx::Class is used in production in a number of organisations.
152 The test suite is quite substantial, and several developer releases are
153 generally made to CPAN before the -current branch is merged back to trunk for
156 The community can be found via:
158 Mailing list: http://lists.rawmode.org/mailman/listinfo/dbix-class/
160 SVN: http://dev.catalyst.perl.org/repos/bast/trunk/DBIx-Class/
162 Wiki: http://dbix-class.shadowcatsystems.co.uk/
164 IRC: irc.perl.org#dbix-class
166 =head1 WHERE TO GO NEXT
168 L<DBIx::Class::Manual::DocMap> lists each task you might want help on, and
169 the modules where you will find documentation.
173 mst: Matt S. Trout <mst@shadowcatsystems.co.uk>
177 abraxxa: Alexander Hartmaier <alex_hartmaier@hotmail.com>
179 andyg: Andy Grundman <andy@hybridized.org>
183 blblack: Brandon Black
185 LTJake: Brian Cassidy <bricas@cpan.org>
187 claco: Christopher H. Laco
191 typester: Daisuke Murase <typester@cpan.org>
193 dkubb: Dan Kubb <dan.kubb-cpan@onautopilot.com>
195 Numa: Dan Sully <daniel@cpan.org>
197 dwc: Daniel Westermann-Clark <danieltwc@cpan.org>
199 ningu: David Kamholz <dkamholz@cpan.org>
203 castaway: Jess Robinson
205 quicksilver: Jules Bean
207 jguenther: Justin Guenther <guentherj@agr.gc.ca>
209 draven: Marcus Ramberg <mramberg@cpan.org>
211 nigel: Nigel Metheringham <nigelm@cpan.org>
213 paulm: Paul Makepeace
215 phaylon: Robert Sedlacek <phaylon@dunkelheit.at>
217 sc_: Just Another Perl Hacker
219 konobi: Scott McWhirter
221 scotty: Scotty Allen <scotty@scottyallen.com>
223 sszabo: Stephan Szabo <sszabo@bigpanda.com>
225 captainL: Luke Saunders <luke.saunders@gmail.com>
233 You may distribute this code under the same terms as Perl itself.