Commit | Line | Data |
ea2e61bf |
1 | package DBIx::Class; |
2 | |
5d283305 |
3 | use strict; |
4 | use warnings; |
5 | |
329d7385 |
6 | use MRO::Compat; |
d38cd95c |
7 | use mro 'c3'; |
329d7385 |
8 | |
2527233b |
9 | use DBIx::Class::Optional::Dependencies; |
10 | |
5d283305 |
11 | use vars qw($VERSION); |
d38cd95c |
12 | use base qw/DBIx::Class::Componentised Class::Accessor::Grouped/; |
11736b4c |
13 | use DBIx::Class::StartupCheck; |
3e110410 |
14 | |
ade0fe3b |
15 | sub mk_classdata { |
77d518d1 |
16 | shift->mk_classaccessor(@_); |
17 | } |
18 | |
19 | sub mk_classaccessor { |
20 | my $self = shift; |
ade0fe3b |
21 | $self->mk_group_accessors('inherited', $_[0]); |
77d518d1 |
22 | $self->set_inherited(@_) if @_ > 1; |
3e110410 |
23 | } |
3c0068c1 |
24 | |
7411204b |
25 | sub component_base_class { 'DBIx::Class' } |
227d4dee |
26 | |
95da6f35 |
27 | # Always remember to do all digits for the version even if they're 0 |
28 | # i.e. first release of 0.XX *must* be 0.XX000. This avoids fBSD ports |
29 | # brain damage and presumably various other packaging systems too |
a7aed21b |
30 | $VERSION = '0.08120_1'; |
748ab0dc |
31 | |
033955f9 |
32 | $VERSION = eval $VERSION if $VERSION =~ /_/; # numify for warning-free dev releases |
b8777a0d |
33 | |
f0750722 |
34 | sub MODIFY_CODE_ATTRIBUTES { |
b5d2c57f |
35 | my ($class,$code,@attrs) = @_; |
36 | $class->mk_classdata('__attr_cache' => {}) |
37 | unless $class->can('__attr_cache'); |
38 | $class->__attr_cache->{$code} = [@attrs]; |
39 | return (); |
f0750722 |
40 | } |
41 | |
da95b45f |
42 | sub _attr_cache { |
b5d2c57f |
43 | my $self = shift; |
44 | my $cache = $self->can('__attr_cache') ? $self->__attr_cache : {}; |
45 | my $rest = eval { $self->next::method }; |
46 | return $@ ? $cache : { %$cache, %$rest }; |
da95b45f |
47 | } |
48 | |
ea2e61bf |
49 | 1; |
34d52be2 |
50 | |
75d07914 |
51 | =head1 NAME |
34d52be2 |
52 | |
7e4b2f59 |
53 | DBIx::Class - Extensible and flexible object <-> relational mapper. |
34d52be2 |
54 | |
3b1c2bbd |
55 | =head1 GETTING HELP/SUPPORT |
56 | |
57 | The community can be found via: |
58 | |
a06e1181 |
59 | =over |
3b1c2bbd |
60 | |
a06e1181 |
61 | =item * IRC: L<irc.perl.org#dbix-class (click for instant chatroom login) |
62 | |http://mibbit.com/chat/#dbix-class@irc.perl.org> |
3b1c2bbd |
63 | |
a06e1181 |
64 | =item * Mailing list: L<http://lists.scsys.co.uk/mailman/listinfo/dbix-class> |
3b1c2bbd |
65 | |
a06e1181 |
66 | =item * RT Bug Tracker: L<https://rt.cpan.org/Dist/Display.html?Queue=DBIx-Class> |
67 | |
68 | =item * SVNWeb: L<http://dev.catalyst.perl.org/svnweb/bast/browse/DBIx-Class/0.08> |
69 | |
70 | =item * SVN: L<http://dev.catalyst.perl.org/repos/bast/DBIx-Class/0.08> |
71 | |
72 | =back |
3b1c2bbd |
73 | |
34d52be2 |
74 | =head1 SYNOPSIS |
75 | |
bd077b47 |
76 | Create a schema class called MyDB/Schema.pm: |
34d52be2 |
77 | |
bd077b47 |
78 | package MyDB::Schema; |
a0638a7b |
79 | use base qw/DBIx::Class::Schema/; |
34d52be2 |
80 | |
f0bb26f3 |
81 | __PACKAGE__->load_namespaces(); |
daec44b8 |
82 | |
a0638a7b |
83 | 1; |
daec44b8 |
84 | |
30e1753a |
85 | Create a result class to represent artists, who have many CDs, in |
f0bb26f3 |
86 | MyDB/Schema/Result/Artist.pm: |
daec44b8 |
87 | |
30e1753a |
88 | See L<DBIx::Class::ResultSource> for docs on defining result classes. |
89 | |
f0bb26f3 |
90 | package MyDB::Schema::Result::Artist; |
d88ecca6 |
91 | use base qw/DBIx::Class::Core/; |
daec44b8 |
92 | |
a0638a7b |
93 | __PACKAGE__->table('artist'); |
94 | __PACKAGE__->add_columns(qw/ artistid name /); |
95 | __PACKAGE__->set_primary_key('artistid'); |
f0bb26f3 |
96 | __PACKAGE__->has_many(cds => 'MyDB::Schema::Result::CD'); |
daec44b8 |
97 | |
a0638a7b |
98 | 1; |
daec44b8 |
99 | |
30e1753a |
100 | A result class to represent a CD, which belongs to an artist, in |
f0bb26f3 |
101 | MyDB/Schema/Result/CD.pm: |
39fe0e65 |
102 | |
f0bb26f3 |
103 | package MyDB::Schema::Result::CD; |
d88ecca6 |
104 | use base qw/DBIx::Class::Core/; |
39fe0e65 |
105 | |
d88ecca6 |
106 | __PACKAGE__->load_components(qw/InflateColumn::DateTime/); |
a0638a7b |
107 | __PACKAGE__->table('cd'); |
bd077b47 |
108 | __PACKAGE__->add_columns(qw/ cdid artistid title year /); |
a0638a7b |
109 | __PACKAGE__->set_primary_key('cdid'); |
bd077b47 |
110 | __PACKAGE__->belongs_to(artist => 'MyDB::Schema::Artist', 'artistid'); |
39fe0e65 |
111 | |
a0638a7b |
112 | 1; |
39fe0e65 |
113 | |
a0638a7b |
114 | Then you can use these classes in your application's code: |
39fe0e65 |
115 | |
a0638a7b |
116 | # Connect to your database. |
bd077b47 |
117 | use MyDB::Schema; |
118 | my $schema = MyDB::Schema->connect($dbi_dsn, $user, $pass, \%dbi_params); |
a0638a7b |
119 | |
120 | # Query for all artists and put them in an array, |
121 | # or retrieve them as a result set object. |
30e1753a |
122 | # $schema->resultset returns a DBIx::Class::ResultSet |
2053ab2a |
123 | my @all_artists = $schema->resultset('Artist')->all; |
124 | my $all_artists_rs = $schema->resultset('Artist'); |
126042ee |
125 | |
30e1753a |
126 | # Output all artists names |
4e8ffded |
127 | # $artist here is a DBIx::Class::Row, which has accessors |
16ccb4fe |
128 | # for all its columns. Rows are also subclasses of your Result class. |
85067746 |
129 | foreach $artist (@all_artists) { |
30e1753a |
130 | print $artist->name, "\n"; |
131 | } |
132 | |
a0638a7b |
133 | # Create a result set to search for artists. |
86beca1d |
134 | # This does not query the DB. |
2053ab2a |
135 | my $johns_rs = $schema->resultset('Artist')->search( |
6576ef54 |
136 | # Build your WHERE using an SQL::Abstract structure: |
2053ab2a |
137 | { name => { like => 'John%' } } |
a0638a7b |
138 | ); |
39fe0e65 |
139 | |
2053ab2a |
140 | # Execute a joined query to get the cds. |
a0638a7b |
141 | my @all_john_cds = $johns_rs->search_related('cds')->all; |
448c8424 |
142 | |
f0bb26f3 |
143 | # Fetch the next available row. |
a0638a7b |
144 | my $first_john = $johns_rs->next; |
448c8424 |
145 | |
2053ab2a |
146 | # Specify ORDER BY on the query. |
a0638a7b |
147 | my $first_john_cds_by_title_rs = $first_john->cds( |
148 | undef, |
149 | { order_by => 'title' } |
150 | ); |
448c8424 |
151 | |
bd077b47 |
152 | # Create a result set that will fetch the artist data |
2053ab2a |
153 | # at the same time as it fetches CDs, using only one query. |
884559b1 |
154 | my $millennium_cds_rs = $schema->resultset('CD')->search( |
a0638a7b |
155 | { year => 2000 }, |
156 | { prefetch => 'artist' } |
157 | ); |
448c8424 |
158 | |
880a1a0c |
159 | my $cd = $millennium_cds_rs->next; # SELECT ... FROM cds JOIN artists ... |
bd077b47 |
160 | my $cd_artist_name = $cd->artist->name; # Already has the data so no 2nd query |
076652e8 |
161 | |
264f1571 |
162 | # new() makes a DBIx::Class::Row object but doesnt insert it into the DB. |
163 | # create() is the same as new() then insert(). |
884559b1 |
164 | my $new_cd = $schema->resultset('CD')->new({ title => 'Spoon' }); |
f183eccd |
165 | $new_cd->artist($cd->artist); |
f183eccd |
166 | $new_cd->insert; # Auto-increment primary key filled in after INSERT |
f183eccd |
167 | $new_cd->title('Fork'); |
168 | |
884559b1 |
169 | $schema->txn_do(sub { $new_cd->update }); # Runs the update in a transaction |
f183eccd |
170 | |
bd077b47 |
171 | # change the year of all the millennium CDs at once |
172 | $millennium_cds_rs->update({ year => 2002 }); |
f183eccd |
173 | |
174 | =head1 DESCRIPTION |
175 | |
176 | This is an SQL to OO mapper with an object API inspired by L<Class::DBI> |
bd077b47 |
177 | (with a compatibility layer as a springboard for porting) and a resultset API |
f183eccd |
178 | that allows abstract encapsulation of database operations. It aims to make |
179 | representing queries in your code as perl-ish as possible while still |
a0638a7b |
180 | providing access to as many of the capabilities of the database as possible, |
f183eccd |
181 | including retrieving related records from multiple tables in a single query, |
bd077b47 |
182 | JOIN, LEFT JOIN, COUNT, DISTINCT, GROUP BY, ORDER BY and HAVING support. |
f183eccd |
183 | |
184 | DBIx::Class can handle multi-column primary and foreign keys, complex |
185 | queries and database-level paging, and does its best to only query the |
75d07914 |
186 | database in order to return something you've directly asked for. If a |
187 | resultset is used as an iterator it only fetches rows off the statement |
188 | handle as requested in order to minimise memory usage. It has auto-increment |
2053ab2a |
189 | support for SQLite, MySQL, PostgreSQL, Oracle, SQL Server and DB2 and is |
190 | known to be used in production on at least the first four, and is fork- |
75d07914 |
191 | and thread-safe out of the box (although your DBD may not be). |
f183eccd |
192 | |
dfccde48 |
193 | This project is still under rapid development, so large new features may be |
194 | marked EXPERIMENTAL - such APIs are still usable but may have edge bugs. |
195 | Failing test cases are *always* welcome and point releases are put out rapidly |
196 | as bugs are found and fixed. |
197 | |
198 | We do our best to maintain full backwards compatibility for published |
199 | APIs, since DBIx::Class is used in production in many organisations, |
200 | and even backwards incompatible changes to non-published APIs will be fixed |
201 | if they're reported and doing so doesn't cost the codebase anything. |
202 | |
264f1571 |
203 | The test suite is quite substantial, and several developer releases |
204 | are generally made to CPAN before the branch for the next release is |
205 | merged back to trunk for a major release. |
f183eccd |
206 | |
f183eccd |
207 | =head1 WHERE TO GO NEXT |
208 | |
2ca930b4 |
209 | L<DBIx::Class::Manual::DocMap> lists each task you might want help on, and |
210 | the modules where you will find documentation. |
076652e8 |
211 | |
3942ab4d |
212 | =head1 AUTHOR |
34d52be2 |
213 | |
266bdcc3 |
214 | mst: Matt S. Trout <mst@shadowcatsystems.co.uk> |
34d52be2 |
215 | |
dfccde48 |
216 | (I mostly consider myself "project founder" these days but the AUTHOR heading |
217 | is traditional :) |
218 | |
3942ab4d |
219 | =head1 CONTRIBUTORS |
220 | |
266bdcc3 |
221 | abraxxa: Alexander Hartmaier <alex_hartmaier@hotmail.com> |
84e3c114 |
222 | |
6d84db2c |
223 | aherzog: Adam Herzog <adam@herzogdesigns.com> |
224 | |
b703fec7 |
225 | amoore: Andrew Moore <amoore@cpan.org> |
226 | |
266bdcc3 |
227 | andyg: Andy Grundman <andy@hybridized.org> |
3942ab4d |
228 | |
266bdcc3 |
229 | ank: Andres Kievsky |
3942ab4d |
230 | |
624764ae |
231 | arcanez: Justin Hunter <justin.d.hunter@gmail.com> |
232 | |
ce4c07df |
233 | ash: Ash Berlin <ash@cpan.org> |
234 | |
3d5bd2af |
235 | bert: Norbert Csongradi <bert@cpan.org> |
236 | |
967a4c40 |
237 | blblack: Brandon L. Black <blblack@gmail.com> |
3942ab4d |
238 | |
62eb8fe8 |
239 | bluefeet: Aran Deltac <bluefeet@cpan.org> |
240 | |
f856fe01 |
241 | boghead: Bryan Beeley <cpan@beeley.org> |
242 | |
bcb8f3ed |
243 | bricas: Brian Cassidy <bricas@cpan.org> |
244 | |
3d7e3e05 |
245 | brunov: Bruno Vecchi <vecchi.b@gmail.com> |
246 | |
281719d2 |
247 | caelum: Rafael Kitover <rkitover@cpan.org> |
248 | |
d3b0e369 |
249 | castaway: Jess Robinson |
3942ab4d |
250 | |
266bdcc3 |
251 | claco: Christopher H. Laco |
ccb9c9b1 |
252 | |
266bdcc3 |
253 | clkao: CL Kao |
3942ab4d |
254 | |
e21dfd6a |
255 | da5id: David Jack Olrik <djo@cpan.org> |
18360aed |
256 | |
13de943d |
257 | debolaz: Anders Nor Berle <berle@cpan.org> |
258 | |
d1f542db |
259 | dew: Dan Thomas <dan@godders.org> |
260 | |
266bdcc3 |
261 | dkubb: Dan Kubb <dan.kubb-cpan@onautopilot.com> |
ccb9c9b1 |
262 | |
9382ad07 |
263 | dnm: Justin Wheeler <jwheeler@datademons.com> |
264 | |
0818c9a7 |
265 | dpetrov: Dimitar Petrov <mitakaa@gmail.com> |
266 | |
266bdcc3 |
267 | dwc: Daniel Westermann-Clark <danieltwc@cpan.org> |
4685e006 |
268 | |
5cffe785 |
269 | dyfrgi: Michael Leuchtenburg <michael@slashhome.org> |
8fe164b9 |
270 | |
ade0fe3b |
271 | frew: Arthur Axel "fREW" Schmidt <frioux@gmail.com> |
272 | |
b4987ed0 |
273 | goraxe: Gordon Irving <goraxe@cpan.org> |
274 | |
d3b0e369 |
275 | gphat: Cory G Watson <gphat@cpan.org> |
ad3d2d7c |
276 | |
e758ffe6 |
277 | groditi: Guillermo Roditi <groditi@cpan.org> |
278 | |
5d779578 |
279 | ilmari: Dagfinn Ilmari MannsE<aring>ker <ilmari@ilmari.org> |
280 | |
bee21976 |
281 | jasonmay: Jason May <jason.a.may@gmail.com> |
282 | |
d3b0e369 |
283 | jesper: Jesper Krogh |
5fb0c64c |
284 | |
4a743a00 |
285 | jgoulah: John Goulah <jgoulah@cpan.org> |
286 | |
102a2984 |
287 | jguenther: Justin Guenther <jguenther@cpan.org> |
d7c4c15c |
288 | |
a14a46e2 |
289 | jhannah: Jay Hannah <jay@jays.net> |
290 | |
8b93a938 |
291 | jnapiorkowski: John Napiorkowski <jjn1056@yahoo.com> |
292 | |
11736b4c |
293 | jon: Jon Schutz <jjschutz@cpan.org> |
294 | |
1aec4bac |
295 | jshirley: J. Shirley <jshirley@gmail.com> |
296 | |
d3b0e369 |
297 | konobi: Scott McWhirter |
535fc2ee |
298 | |
4367679a |
299 | lukes: Luke Saunders <luke.saunders@gmail.com> |
300 | |
709ea492 |
301 | marcus: Marcus Ramberg <mramberg@cpan.org> |
302 | |
114780ee |
303 | mattlaw: Matt Lawrence |
304 | |
58755bba |
305 | michaelr: Michael Reddick <michael.reddick@gmail.com> |
306 | |
77e7e47d |
307 | ned: Neil de Carteret |
308 | |
266bdcc3 |
309 | nigel: Nigel Metheringham <nigelm@cpan.org> |
6565b410 |
310 | |
d3b0e369 |
311 | ningu: David Kamholz <dkamholz@cpan.org> |
312 | |
66cf3a84 |
313 | Nniuq: Ron "Quinn" Straight" <quinnfazigu@gmail.org> |
314 | |
20b4c148 |
315 | norbi: Norbert Buchmuller <norbi@nix.hu> |
316 | |
48580715 |
317 | nuba: Nuba Princigalli <nuba@cpan.org> |
318 | |
d3b0e369 |
319 | Numa: Dan Sully <daniel@cpan.org> |
320 | |
dc571b76 |
321 | ovid: Curtis "Ovid" Poe <ovid@cpan.org> |
322 | |
bf356c54 |
323 | oyse: Øystein Torget <oystein.torget@dnv.com> |
324 | |
266bdcc3 |
325 | paulm: Paul Makepeace |
4763f4b7 |
326 | |
d3b0e369 |
327 | penguin: K J Cheetham |
328 | |
8cfef6f5 |
329 | perigrin: Chris Prather <chris@prather.org> |
330 | |
14899528 |
331 | peter: Peter Collingbourne <peter@pcc.me.uk> |
caac1708 |
332 | |
266bdcc3 |
333 | phaylon: Robert Sedlacek <phaylon@dunkelheit.at> |
a53b95f1 |
334 | |
56fadd8f |
335 | plu: Johannes Plunien <plu@cpan.org> |
336 | |
d3b0e369 |
337 | quicksilver: Jules Bean |
022e0893 |
338 | |
4ed01b34 |
339 | rafl: Florian Ragwitz <rafl@debian.org> |
340 | |
7ff0dace |
341 | rbuels: Robert Buels <rmb32@cornell.edu> |
342 | |
0da8b7da |
343 | rdj: Ryan D Johnson <ryan@innerfence.com> |
344 | |
66b1e361 |
345 | ribasushi: Peter Rabbitson <ribasushi@cpan.org> |
d76e282a |
346 | |
b487918c |
347 | rjbs: Ricardo Signes <rjbs@cpan.org> |
348 | |
6ffb5be5 |
349 | robkinyon: Rob Kinyon <rkinyon@cpan.org> |
350 | |
e4c9f3f0 |
351 | Roman: Roman Filippov <romanf@cpan.org> |
352 | |
d3b0e369 |
353 | sc_: Just Another Perl Hacker |
ba606e58 |
354 | |
266bdcc3 |
355 | scotty: Scotty Allen <scotty@scottyallen.com> |
181a28f4 |
356 | |
1c133e22 |
357 | semifor: Marc Mims <marc@questright.com> |
358 | |
20b4c148 |
359 | solomon: Jared Johnson <jaredj@nmgi.com> |
360 | |
88f937fb |
361 | spb: Stephen Bennett <stephen@freenode.net> |
362 | |
637ca936 |
363 | sszabo: Stephan Szabo <sszabo@bigpanda.com> |
364 | |
386c2272 |
365 | teejay : Aaron Trevena <teejay@cpan.org> |
366 | |
84e3c114 |
367 | Todd Lipcon |
e063fe2c |
368 | |
6eb6264e |
369 | Tom Hukins |
370 | |
d15e3fc5 |
371 | triode: Pete Gamache <gamache@cpan.org> |
372 | |
d3b0e369 |
373 | typester: Daisuke Murase <typester@cpan.org> |
c0e7b4e5 |
374 | |
4740bdb7 |
375 | victori: Victor Igumnov <victori@cpan.org> |
376 | |
d3b0e369 |
377 | wdh: Will Hawes |
4c248161 |
378 | |
00c03ced |
379 | willert: Sebastian Willert <willert@cpan.org> |
380 | |
66d2a14e |
381 | wreis: Wallace Reis <wreis@cpan.org> |
382 | |
d3b0e369 |
383 | zamolxes: Bogdan Lucaciu <bogdan@wiz.ro> |
78060df8 |
384 | |
b38e10bd |
385 | =head1 COPYRIGHT |
386 | |
48580715 |
387 | Copyright (c) 2005 - 2010 the DBIx::Class L</AUTHOR> and L</CONTRIBUTORS> |
b38e10bd |
388 | as listed above. |
389 | |
96154ef7 |
390 | =head1 LICENSE |
391 | |
392 | This library is free software and may be distributed under the same terms |
393 | as perl itself. |
394 | |
34d52be2 |
395 | =cut |