changed Foo/Bar in docs to more meaningful names
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class.pm
CommitLineData
ea2e61bf 1package DBIx::Class;
2
5d283305 3use strict;
4use warnings;
5
5d283305 6use vars qw($VERSION);
3c0068c1 7use base qw/DBIx::Class::Componentised Class::Data::Accessor/;
8
9sub mk_classdata { shift->mk_classaccessor(@_); }
7411204b 10sub component_base_class { 'DBIx::Class' }
227d4dee 11
95da6f35 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
15
1346e22d 16$VERSION = '0.05999_04';
b8777a0d 17
f0750722 18sub MODIFY_CODE_ATTRIBUTES {
19 my ($class,$code,@attrs) = @_;
a58cd74b 20 $class->mk_classdata('__attr_cache' => {}) unless $class->can('__attr_cache');
da95b45f 21 $class->__attr_cache->{$code} = [@attrs];
f0750722 22 return ();
23}
24
da95b45f 25sub _attr_cache {
26 my $self = shift;
27 my $cache = $self->can('__attr_cache') ? $self->__attr_cache : {};
28 my $rest = eval { $self->next::method };
29 return $@ ? $cache : { %$cache, %$rest };
30}
31
ea2e61bf 321;
34d52be2 33
34=head1 NAME
35
7e4b2f59 36DBIx::Class - Extensible and flexible object <-> relational mapper.
34d52be2 37
38=head1 SYNOPSIS
39
f183eccd 40DB/Main.pm:
34d52be2 41
f183eccd 42 package DB::Main;
43 use base qw/DBIx::Class::Schema/;
44 __PACKAGE__->load_classes();
34d52be2 45
f183eccd 46 1;
daec44b8 47
daec44b8 48
f183eccd 49DB/Main/Artist.pm:
daec44b8 50
f183eccd 51 package DB::Main::Artist;
52 use base qw/DBIx::Class/;
53 __PACKAGE__->load_components(qw/PK::Auto Core/);
54 __PACKAGE__->table('artist');
55 __PACKAGE__->add_columns(qw/ artistid name /);
56 __PACKAGE__->set_primary_key('artistid');
57 __PACKAGE__->has_many('cds' => 'DB::Main::CD');
daec44b8 58
f183eccd 59 1;
daec44b8 60
daec44b8 61
f183eccd 62DB/Main/CD.pm:
39fe0e65 63
f183eccd 64 package DB::Main::CD;
65 use base qw/DBIx::Class/;
66 __PACKAGE__->load_components(qw/PK::Auto Core/);
67 __PACKAGE__->table('cd');
68 __PACKAGE__->add_columns(qw/ cdid artist title year/);
69 __PACKAGE__->set_primary_key('cdid');
70 __PACKAGE__->belongs_to('artist' => 'DB::Main::Artist');
39fe0e65 71
f183eccd 72 1;
39fe0e65 73
f183eccd 74in application code:
39fe0e65 75
f183eccd 76 my $ds = DB::Main->connect(@dbi_dsn); # DBI connect will happen on-demand
39fe0e65 77
f183eccd 78 my @all_artists = $ds->resultset('Artist')->all;
126042ee 79
f183eccd 80 my $johns_rs = $ds->resultset('Artist')->search(
81 { 'name' => { 'like', 'John%' } }); # Doesn't query the db
39fe0e65 82
f183eccd 83 my @all_john_cds = $johns_rs->search_related('cds')->all; # Now it queries
448c8424 84
f183eccd 85 my $first_john = $johns_rs->next; # Queries but only fetches one row so far
448c8424 86
f183eccd 87 my $first_john_cds_by_title_rs = $first_john->cds(undef,
88 { order_by => 'title' });
448c8424 89
f183eccd 90 my $millenium_cds_rs = $ds->resultset('CD')->search(
91 { year => 2000 },
92 { prefetch => 'artist' } );
448c8424 93
f183eccd 94 my $cd = $millenium_cds_rs->next; # SELECT ... FROM cds JOIN artists ...
39fe0e65 95
f183eccd 96 my $cd_artist_name = $cd->artist->name; # Already has the data so no query
076652e8 97
f183eccd 98 my $new_cd = $ds->resultset('CD')->new({ title => 'Spoon' });
8091aa91 99
f183eccd 100 $new_cd->artist($cd->artist);
101
102 $new_cd->insert; # Auto-increment primary key filled in after INSERT
103
104 $new_cd->title('Fork');
105
106 $ds->txn_do(sub { $new_cd->update }); # Runs the update in a transaction
107
108 $millenium_cds_rs->update({ year => 2002 }); # Single-query bulk update
109
110=head1 DESCRIPTION
111
112This is an SQL to OO mapper with an object API inspired by L<Class::DBI>
113(and a compability layer as a springboard for porting) and a resultset API
114that allows abstract encapsulation of database operations. It aims to make
115representing queries in your code as perl-ish as possible while still
116providing access to as mant of the capabilities of the database as possible,
117including retrieving related records from multiple tables in a single query,
118JOIN, LEFT JOIN, COUNT, DISTINCT, GROUP BY and HAVING support.
119
120DBIx::Class can handle multi-column primary and foreign keys, complex
121queries and database-level paging, and does its best to only query the
122database when it actually needs to in order to return something the user's
123asked for. If a resultset is used as an iterator it only fetches rows off
124the statement handle as requested in order to minimise memory usage. It
125has auto-increment support for SQLite, MySQL, PostgreSQL, Oracle, SQL
126Server and DB2 and is known to be used in production on at least the first
127four, and is fork- and thread-safe out of the box (although your DBD may not
128be).
129
130This project is still under rapid development, so features added in the
131latest major release may not work 100% yet - check the Changes if you run
132into trouble, and beware of anything explicitly marked EXPERIMENTAL. Failing
133test cases are *always* welcome and point releases are put out rapidly as
134bugs are found and fixed.
135
136Even so, we do your best to maintain full backwards compatibility for published
137APIs since DBIx::Class is used in production in a number of organisations;
138the test suite is now fairly substantial and several developer releases are
139generally made to CPAN before the -current branch is merged back to trunk.
140
141The community can be found via -
142
143 Mailing list: http://lists.rawmode.org/mailman/listinfo/dbix-class/
144
145 SVN: http://dev.catalyst.perl.org/repos/bast/trunk/DBIx-Class/
146
147 Wiki: http://dbix-class.shadowcatsystems.co.uk/
148
149 IRC: irc.perl.org#dbix-class
150
151=head1 WHERE TO GO NEXT
152
153=over 4
8091aa91 154
a39e84a3 155=item L<DBIx::Class::Manual> - User's manual
8091aa91 156
f183eccd 157=item L<DBIx::Class::Core> - DBIC Core Classes
158
a39e84a3 159=item L<DBIx::Class::CDBICompat> - L<Class::DBI> Compat layer
076652e8 160
f183eccd 161=item L<DBIx::Class::Schema> - schema and connection container
076652e8 162
f183eccd 163=item L<DBIx::Class::ResultSource> - tables and table-like things
076652e8 164
f183eccd 165=item L<DBIx::Class::ResultSet> - encapsulates a query and its results
076652e8 166
a39e84a3 167=item L<DBIx::Class::Row> - row-level methods
4db53147 168
a39e84a3 169=item L<DBIx::Class::PK> - primary key methods
170
171=item L<DBIx::Class::Relationship> - relationships between tables
172
173=back
076652e8 174
3942ab4d 175=head1 AUTHOR
34d52be2 176
daec44b8 177Matt S. Trout <mst@shadowcatsystems.co.uk>
34d52be2 178
3942ab4d 179=head1 CONTRIBUTORS
180
84e3c114 181Alexander Hartmaier <alex_hartmaier@hotmail.com>
182
3942ab4d 183Andy Grundman <andy@hybridized.org>
184
84e3c114 185Andres Kievsky
3942ab4d 186
84e3c114 187Brandon Black
3942ab4d 188
84e3c114 189Brian Cassidy <bricas@cpan.org>
3942ab4d 190
84e3c114 191Christopher H. Laco
ccb9c9b1 192
84e3c114 193CL Kao
3942ab4d 194
84e3c114 195Daisuke Murase <typester@cpan.org>
3942ab4d 196
84e3c114 197Dan Kubb <dan.kubb-cpan@onautopilot.com>
ccb9c9b1 198
84e3c114 199Dan Sully <daniel@cpan.org>
4685e006 200
84e3c114 201Daniel Westermann-Clark <danieltwc@cpan.org>
4685e006 202
84e3c114 203David Kamholz <dkamholz@cpan.org>
4685e006 204
84e3c114 205Jesper Krogh
8fe164b9 206
84e3c114 207Jess Robinson
ad3d2d7c 208
84e3c114 209Jules Bean
5fb0c64c 210
84e3c114 211Justin Guenther <guentherj@agr.gc.ca>
d7c4c15c 212
84e3c114 213Marcus Ramberg <mramberg@cpan.org>
103e3e03 214
6565b410 215Nigel Metheringham <nigelm@cpan.org>
216
84e3c114 217Paul Makepeace
4763f4b7 218
84e3c114 219Robert Sedlacek <phaylon@dunkelheit.at>
a53b95f1 220
84e3c114 221sc_ of irc.perl.org#dbix-class
022e0893 222
84e3c114 223Scott McWhirter (konobi)
ba606e58 224
84e3c114 225Scotty Allen <scotty@scottyallen.com>
181a28f4 226
84e3c114 227Todd Lipcon
e063fe2c 228
84e3c114 229Will Hawes
c0e7b4e5 230
34d52be2 231=head1 LICENSE
232
233You may distribute this code under the same terms as Perl itself.
234
235=cut
236