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