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_02';
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.
52 =head1 GETTING HELP/SUPPORT
54 The community can be found via:
56 Mailing list: http://lists.scsys.co.uk/mailman/listinfo/dbix-class/
58 SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
60 SVNWeb: http://dev.catalyst.perl.org/svnweb/bast/browse/DBIx-Class/
62 IRC: irc.perl.org#dbix-class
66 Create a schema class called MyDB/Schema.pm:
69 use base qw/DBIx::Class::Schema/;
71 __PACKAGE__->load_classes();
75 Create a table class to represent artists, who have many CDs, in
76 MyDB/Schema/Artist.pm:
78 package MyDB::Schema::Artist;
79 use base qw/DBIx::Class/;
81 __PACKAGE__->load_components(qw/Core/);
82 __PACKAGE__->table('artist');
83 __PACKAGE__->add_columns(qw/ artistid name /);
84 __PACKAGE__->set_primary_key('artistid');
85 __PACKAGE__->has_many(cds => 'MyDB::Schema::CD');
89 A table class to represent a CD, which belongs to an artist, in
92 package MyDB::Schema::CD;
93 use base qw/DBIx::Class/;
95 __PACKAGE__->load_components(qw/Core/);
96 __PACKAGE__->table('cd');
97 __PACKAGE__->add_columns(qw/ cdid artistid title year /);
98 __PACKAGE__->set_primary_key('cdid');
99 __PACKAGE__->belongs_to(artist => 'MyDB::Schema::Artist', 'artistid');
103 Then you can use these classes in your application's code:
105 # Connect to your database.
107 my $schema = MyDB::Schema->connect($dbi_dsn, $user, $pass, \%dbi_params);
109 # Query for all artists and put them in an array,
110 # or retrieve them as a result set object.
111 my @all_artists = $schema->resultset('Artist')->all;
112 my $all_artists_rs = $schema->resultset('Artist');
114 # Create a result set to search for artists.
115 # This does not query the DB.
116 my $johns_rs = $schema->resultset('Artist')->search(
117 # Build your WHERE using an SQL::Abstract structure:
118 { name => { like => 'John%' } }
121 # Execute a joined query to get the cds.
122 my @all_john_cds = $johns_rs->search_related('cds')->all;
124 # Fetch only the next row.
125 my $first_john = $johns_rs->next;
127 # Specify ORDER BY on the query.
128 my $first_john_cds_by_title_rs = $first_john->cds(
130 { order_by => 'title' }
133 # Create a result set that will fetch the artist data
134 # at the same time as it fetches CDs, using only one query.
135 my $millennium_cds_rs = $schema->resultset('CD')->search(
137 { prefetch => 'artist' }
140 my $cd = $millennium_cds_rs->next; # SELECT ... FROM cds JOIN artists ...
141 my $cd_artist_name = $cd->artist->name; # Already has the data so no 2nd query
143 # new() makes a DBIx::Class::Row object but doesnt insert it into the DB.
144 # create() is the same as new() then insert().
145 my $new_cd = $schema->resultset('CD')->new({ title => 'Spoon' });
146 $new_cd->artist($cd->artist);
147 $new_cd->insert; # Auto-increment primary key filled in after INSERT
148 $new_cd->title('Fork');
150 $schema->txn_do(sub { $new_cd->update }); # Runs the update in a transaction
152 # change the year of all the millennium CDs at once
153 $millennium_cds_rs->update({ year => 2002 });
157 This is an SQL to OO mapper with an object API inspired by L<Class::DBI>
158 (with a compatibility layer as a springboard for porting) and a resultset API
159 that allows abstract encapsulation of database operations. It aims to make
160 representing queries in your code as perl-ish as possible while still
161 providing access to as many of the capabilities of the database as possible,
162 including retrieving related records from multiple tables in a single query,
163 JOIN, LEFT JOIN, COUNT, DISTINCT, GROUP BY, ORDER BY and HAVING support.
165 DBIx::Class can handle multi-column primary and foreign keys, complex
166 queries and database-level paging, and does its best to only query the
167 database in order to return something you've directly asked for. If a
168 resultset is used as an iterator it only fetches rows off the statement
169 handle as requested in order to minimise memory usage. It has auto-increment
170 support for SQLite, MySQL, PostgreSQL, Oracle, SQL Server and DB2 and is
171 known to be used in production on at least the first four, and is fork-
172 and thread-safe out of the box (although your DBD may not be).
174 This project is still under rapid development, so large new features may be
175 marked EXPERIMENTAL - such APIs are still usable but may have edge bugs.
176 Failing test cases are *always* welcome and point releases are put out rapidly
177 as bugs are found and fixed.
179 We do our best to maintain full backwards compatibility for published
180 APIs, since DBIx::Class is used in production in many organisations,
181 and even backwards incompatible changes to non-published APIs will be fixed
182 if they're reported and doing so doesn't cost the codebase anything.
184 The test suite is quite substantial, and several developer releases
185 are generally made to CPAN before the branch for the next release is
186 merged back to trunk for a major release.
188 =head1 WHERE TO GO NEXT
190 L<DBIx::Class::Manual::DocMap> lists each task you might want help on, and
191 the modules where you will find documentation.
195 mst: Matt S. Trout <mst@shadowcatsystems.co.uk>
197 (I mostly consider myself "project founder" these days but the AUTHOR heading
202 abraxxa: Alexander Hartmaier <alex_hartmaier@hotmail.com>
204 aherzog: Adam Herzog <adam@herzogdesigns.com>
206 andyg: Andy Grundman <andy@hybridized.org>
210 ash: Ash Berlin <ash@cpan.org>
212 bert: Norbert Csongradi <bert@cpan.org>
214 blblack: Brandon L. Black <blblack@gmail.com>
216 bluefeet: Aran Deltac <bluefeet@cpan.org>
218 captainL: Luke Saunders <luke.saunders@gmail.com>
220 castaway: Jess Robinson
222 claco: Christopher H. Laco
226 da5id: David Jack Olrik <djo@cpan.org>
228 debolaz: Anders Nor Berle <berle@cpan.org>
230 dkubb: Dan Kubb <dan.kubb-cpan@onautopilot.com>
232 dnm: Justin Wheeler <jwheeler@datademons.com>
234 draven: Marcus Ramberg <mramberg@cpan.org>
236 dwc: Daniel Westermann-Clark <danieltwc@cpan.org>
238 dyfrgi: Michael Leuchtenburg <michael@slashhome.org>
240 gphat: Cory G Watson <gphat@cpan.org>
244 jguenther: Justin Guenther <jguenther@cpan.org>
246 jnapiorkowski: John Napiorkowski <jjn1056@yahoo.com>
248 jon: Jon Schutz <jjschutz@cpan.org>
250 jshirley: J. Shirley <jshirley@gmail.com>
252 konobi: Scott McWhirter
254 LTJake: Brian Cassidy <bricas@cpan.org>
256 mattlaw: Matt Lawrence
258 ned: Neil de Carteret
260 nigel: Nigel Metheringham <nigelm@cpan.org>
262 ningu: David Kamholz <dkamholz@cpan.org>
264 Numa: Dan Sully <daniel@cpan.org>
266 oyse: Øystein Torget <oystein.torget@dnv.com>
268 paulm: Paul Makepeace
270 penguin: K J Cheetham
272 perigrin: Chris Prather <chris@prather.org>
274 phaylon: Robert Sedlacek <phaylon@dunkelheit.at>
276 quicksilver: Jules Bean
278 rdj: Ryan D Johnson <ryan@innerfence.com>
280 sc_: Just Another Perl Hacker
282 scotty: Scotty Allen <scotty@scottyallen.com>
284 semifor: Marc Mims <marc@questright.com>
286 sszabo: Stephan Szabo <sszabo@bigpanda.com>
288 teejay : Aaron Trevena <teejay@cpan.org>
294 typester: Daisuke Murase <typester@cpan.org>
296 victori: Victor Igumnov <victori@cpan.org>
300 willert: Sebastian Willert <willert@cpan.org>
302 zamolxes: Bogdan Lucaciu <bogdan@wiz.ro>
306 You may distribute this code under the same terms as Perl itself.