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