Added use strict / use warnings everywhere it was missing
[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) = @_;
bc0c9800 20 $class->mk_classdata('__attr_cache' => {})
21 unless $class->can('__attr_cache');
da95b45f 22 $class->__attr_cache->{$code} = [@attrs];
f0750722 23 return ();
24}
25
da95b45f 26sub _attr_cache {
27 my $self = shift;
28 my $cache = $self->can('__attr_cache') ? $self->__attr_cache : {};
29 my $rest = eval { $self->next::method };
30 return $@ ? $cache : { %$cache, %$rest };
31}
32
ea2e61bf 331;
34d52be2 34
35=head1 NAME
36
7e4b2f59 37DBIx::Class - Extensible and flexible object <-> relational mapper.
34d52be2 38
39=head1 SYNOPSIS
40
a0638a7b 41Create a base schema class called DB/Main.pm:
34d52be2 42
a0638a7b 43 package DB::Main;
44 use base qw/DBIx::Class::Schema/;
34d52be2 45
a0638a7b 46 __PACKAGE__->load_classes();
daec44b8 47
a0638a7b 48 1;
daec44b8 49
86beca1d 50Create a class that represent artists, who have many CDs, 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
86beca1d 63A class to represent a CD, which belongs to an artist, in DB/Main/CD.pm:
39fe0e65 64
a0638a7b 65 package DB::Main::CD;
66 use base qw/DBIx::Class/;
39fe0e65 67
a0638a7b 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');
39fe0e65 73
a0638a7b 74 1;
39fe0e65 75
a0638a7b 76Then you can use these classes in your application's code:
39fe0e65 77
a0638a7b 78 # Connect to your database.
79 my $ds = DB::Main->connect(@dbi_dsn);
80
81 # Query for all artists and put them in an array,
82 # or retrieve them as a result set object.
f183eccd 83 my @all_artists = $ds->resultset('Artist')->all;
a0638a7b 84 my $all_artists_rs = $ds->resultset('Artist');
126042ee 85
a0638a7b 86 # Create a result set to search for artists.
86beca1d 87 # This does not query the DB.
f183eccd 88 my $johns_rs = $ds->resultset('Artist')->search(
a0638a7b 89 # Build your WHERE using an SQL::Abstract structure:
90 { 'name' => { 'like', 'John%' } }
91 );
39fe0e65 92
86beca1d 93 # This executes a joined query to get the cds
a0638a7b 94 my @all_john_cds = $johns_rs->search_related('cds')->all;
448c8424 95
a0638a7b 96 # Queries but only fetches one row so far.
97 my $first_john = $johns_rs->next;
448c8424 98
a0638a7b 99 my $first_john_cds_by_title_rs = $first_john->cds(
100 undef,
101 { order_by => 'title' }
102 );
448c8424 103
f183eccd 104 my $millenium_cds_rs = $ds->resultset('CD')->search(
a0638a7b 105 { year => 2000 },
106 { prefetch => 'artist' }
107 );
448c8424 108
f183eccd 109 my $cd = $millenium_cds_rs->next; # SELECT ... FROM cds JOIN artists ...
f183eccd 110 my $cd_artist_name = $cd->artist->name; # Already has the data so no query
076652e8 111
f183eccd 112 my $new_cd = $ds->resultset('CD')->new({ title => 'Spoon' });
f183eccd 113 $new_cd->artist($cd->artist);
f183eccd 114 $new_cd->insert; # Auto-increment primary key filled in after INSERT
f183eccd 115 $new_cd->title('Fork');
116
117 $ds->txn_do(sub { $new_cd->update }); # Runs the update in a transaction
118
119 $millenium_cds_rs->update({ year => 2002 }); # Single-query bulk update
120
121=head1 DESCRIPTION
122
123This is an SQL to OO mapper with an object API inspired by L<Class::DBI>
a0638a7b 124(and a compatibility layer as a springboard for porting) and a resultset API
f183eccd 125that allows abstract encapsulation of database operations. It aims to make
126representing queries in your code as perl-ish as possible while still
a0638a7b 127providing access to as many of the capabilities of the database as possible,
f183eccd 128including retrieving related records from multiple tables in a single query,
129JOIN, LEFT JOIN, COUNT, DISTINCT, GROUP BY and HAVING support.
130
131DBIx::Class can handle multi-column primary and foreign keys, complex
132queries and database-level paging, and does its best to only query the
86beca1d 133database when it actually needs to in order to return something you've directly
f183eccd 134asked for. If a resultset is used as an iterator it only fetches rows off
135the statement handle as requested in order to minimise memory usage. It
136has auto-increment support for SQLite, MySQL, PostgreSQL, Oracle, SQL
137Server and DB2 and is known to be used in production on at least the first
138four, and is fork- and thread-safe out of the box (although your DBD may not
139be).
140
141This project is still under rapid development, so features added in the
142latest major release may not work 100% yet - check the Changes if you run
143into trouble, and beware of anything explicitly marked EXPERIMENTAL. Failing
144test cases are *always* welcome and point releases are put out rapidly as
145bugs are found and fixed.
146
86beca1d 147Even so, we do our best to maintain full backwards compatibility for published
f183eccd 148APIs since DBIx::Class is used in production in a number of organisations;
149the test suite is now fairly substantial and several developer releases are
86beca1d 150generally made to CPAN before the -current branch is merged back to trunk for
151a major release.
f183eccd 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