Commit | Line | Data |
ea2e61bf |
1 | package DBIx::Class; |
2 | |
95b76469 |
3 | # important to load early |
4 | use DBIx::Class::_Util; |
5 | |
5d283305 |
6 | use strict; |
7 | use warnings; |
8 | |
f9cc85ce |
9 | our $VERSION; |
10 | # Always remember to do all digits for the version even if they're 0 |
11 | # i.e. first release of 0.XX *must* be 0.XX000. This avoids fBSD ports |
12 | # brain damage and presumably various other packaging systems too |
13 | |
14 | # $VERSION declaration must stay up here, ahead of any other package |
15 | # declarations, as to not confuse various modules attempting to determine |
16 | # this ones version, whether that be s.c.o. or Module::Metadata, etc |
c6b73be9 |
17 | $VERSION = '0.082899_15'; |
f9cc85ce |
18 | |
19 | $VERSION = eval $VERSION if $VERSION =~ /_/; # numify for warning-free dev releases |
20 | |
d38cd95c |
21 | use mro 'c3'; |
329d7385 |
22 | |
db29433c |
23 | use base qw/DBIx::Class::Componentised DBIx::Class::AccessorGroup/; |
f9080e45 |
24 | use DBIx::Class::Exception; |
3e110410 |
25 | |
70c28808 |
26 | __PACKAGE__->mk_group_accessors(inherited => '_skip_namespace_frames'); |
8b60b921 |
27 | __PACKAGE__->_skip_namespace_frames('^DBIx::Class|^SQL::Abstract|^Try::Tiny|^Class::Accessor::Grouped|^Context::Preserve|^Moose::Meta::'); |
70c28808 |
28 | |
e1d9e578 |
29 | # FIXME - this is not really necessary, and is in |
30 | # fact going to slow things down a bit |
31 | # However it is the right thing to do in order to get |
32 | # various install bases to highlight their brokenness |
33 | # Remove at some unknown point in the future |
5f74ed3a |
34 | # |
35 | # The oddball BEGIN is there for... reason unknown |
36 | # It does make non-segfaulty difference on pre-5.8.5 perls, so shrug |
37 | BEGIN { |
38 | sub DESTROY { &DBIx::Class::_Util::detected_reinvoked_destructor }; |
39 | } |
e1d9e578 |
40 | |
d009cb7d |
41 | sub component_base_class { 'DBIx::Class' } |
77d518d1 |
42 | |
f0750722 |
43 | sub MODIFY_CODE_ATTRIBUTES { |
b5d2c57f |
44 | my ($class,$code,@attrs) = @_; |
e5053694 |
45 | $class->mk_classaccessor('__attr_cache' => {}) |
b5d2c57f |
46 | unless $class->can('__attr_cache'); |
47 | $class->__attr_cache->{$code} = [@attrs]; |
48 | return (); |
f0750722 |
49 | } |
50 | |
140bcb6a |
51 | sub FETCH_CODE_ATTRIBUTES { |
52 | my ($class,$code) = @_; |
53 | @{ $class->_attr_cache->{$code} || [] } |
54 | } |
55 | |
da95b45f |
56 | sub _attr_cache { |
b5d2c57f |
57 | my $self = shift; |
58 | my $cache = $self->can('__attr_cache') ? $self->__attr_cache : {}; |
9780718f |
59 | |
60 | return { |
61 | %$cache, |
62 | %{ $self->maybe::next::method || {} }, |
20674fcd |
63 | }; |
da95b45f |
64 | } |
65 | |
d095c62d |
66 | # *DO NOT* change this URL nor the identically named =head1 below |
67 | # it is linked throughout the ecosystem |
68 | sub DBIx::Class::_ENV_::HELP_URL () { |
69 | 'http://p3rl.org/DBIx::Class#GETTING_HELP/SUPPORT' |
70 | } |
71 | |
ea2e61bf |
72 | 1; |
34d52be2 |
73 | |
d095c62d |
74 | __END__ |
75 | |
75d07914 |
76 | =head1 NAME |
34d52be2 |
77 | |
7e4b2f59 |
78 | DBIx::Class - Extensible and flexible object <-> relational mapper. |
34d52be2 |
79 | |
06752a03 |
80 | =head1 WHERE TO START READING |
3b1c2bbd |
81 | |
06752a03 |
82 | See L<DBIx::Class::Manual::DocMap> for an overview of the exhaustive documentation. |
83 | To get the most out of DBIx::Class with the least confusion it is strongly |
84 | recommended to read (at the very least) the |
85 | L<Manuals|DBIx::Class::Manual::DocMap/Manuals> in the order presented there. |
86 | |
32250d01 |
87 | =cut |
88 | |
32250d01 |
89 | =head1 GETTING HELP/SUPPORT |
06752a03 |
90 | |
32250d01 |
91 | Due to the sheer size of its problem domain, DBIx::Class is a relatively |
06752a03 |
92 | complex framework. After you start using DBIx::Class questions will inevitably |
93 | arise. If you are stuck with a problem or have doubts about a particular |
32250d01 |
94 | approach do not hesitate to contact us via any of the following options (the |
95 | list is sorted by "fastest response time"): |
3b1c2bbd |
96 | |
a06e1181 |
97 | =over |
3b1c2bbd |
98 | |
c6fdaf2a |
99 | =item * IRC: irc.perl.org#dbix-class |
100 | |
101 | =for html |
e1ddfc8a |
102 | <a href="https://chat.mibbit.com/#dbix-class@irc.perl.org">(click for instant chatroom login)</a> |
3b1c2bbd |
103 | |
a06e1181 |
104 | =item * Mailing list: L<http://lists.scsys.co.uk/mailman/listinfo/dbix-class> |
3b1c2bbd |
105 | |
e1ddfc8a |
106 | =item * RT Bug Tracker: L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=DBIx-Class> |
86a23587 |
107 | |
e1ddfc8a |
108 | =item * Twitter: L<https://www.twitter.com/dbix_class> |
86a23587 |
109 | |
86a23587 |
110 | =item * Web Site: L<http://www.dbix-class.org/> |
a06e1181 |
111 | |
86a23587 |
112 | =back |
113 | |
34d52be2 |
114 | =head1 SYNOPSIS |
115 | |
113e8d16 |
116 | For the very impatient: L<DBIx::Class::Manual::QuickStart> |
117 | |
118 | This code in the next step can be generated automatically from an existing |
119 | database, see L<dbicdump> from the distribution C<DBIx-Class-Schema-Loader>. |
120 | |
5b56d1ac |
121 | =head2 Schema classes preparation |
122 | |
53aa53f3 |
123 | Create a schema class called F<MyApp/Schema.pm>: |
34d52be2 |
124 | |
03460bef |
125 | package MyApp::Schema; |
a0638a7b |
126 | use base qw/DBIx::Class::Schema/; |
34d52be2 |
127 | |
f0bb26f3 |
128 | __PACKAGE__->load_namespaces(); |
daec44b8 |
129 | |
a0638a7b |
130 | 1; |
daec44b8 |
131 | |
30e1753a |
132 | Create a result class to represent artists, who have many CDs, in |
53aa53f3 |
133 | F<MyApp/Schema/Result/Artist.pm>: |
daec44b8 |
134 | |
30e1753a |
135 | See L<DBIx::Class::ResultSource> for docs on defining result classes. |
136 | |
03460bef |
137 | package MyApp::Schema::Result::Artist; |
d88ecca6 |
138 | use base qw/DBIx::Class::Core/; |
daec44b8 |
139 | |
a0638a7b |
140 | __PACKAGE__->table('artist'); |
141 | __PACKAGE__->add_columns(qw/ artistid name /); |
142 | __PACKAGE__->set_primary_key('artistid'); |
326dacbf |
143 | __PACKAGE__->has_many(cds => 'MyApp::Schema::Result::CD', 'artistid'); |
daec44b8 |
144 | |
a0638a7b |
145 | 1; |
daec44b8 |
146 | |
30e1753a |
147 | A result class to represent a CD, which belongs to an artist, in |
53aa53f3 |
148 | F<MyApp/Schema/Result/CD.pm>: |
39fe0e65 |
149 | |
03460bef |
150 | package MyApp::Schema::Result::CD; |
d88ecca6 |
151 | use base qw/DBIx::Class::Core/; |
39fe0e65 |
152 | |
d88ecca6 |
153 | __PACKAGE__->load_components(qw/InflateColumn::DateTime/); |
a0638a7b |
154 | __PACKAGE__->table('cd'); |
bd077b47 |
155 | __PACKAGE__->add_columns(qw/ cdid artistid title year /); |
a0638a7b |
156 | __PACKAGE__->set_primary_key('cdid'); |
03460bef |
157 | __PACKAGE__->belongs_to(artist => 'MyApp::Schema::Result::Artist', 'artistid'); |
39fe0e65 |
158 | |
a0638a7b |
159 | 1; |
39fe0e65 |
160 | |
5b56d1ac |
161 | =head2 API usage |
162 | |
a0638a7b |
163 | Then you can use these classes in your application's code: |
39fe0e65 |
164 | |
a0638a7b |
165 | # Connect to your database. |
03460bef |
166 | use MyApp::Schema; |
167 | my $schema = MyApp::Schema->connect($dbi_dsn, $user, $pass, \%dbi_params); |
a0638a7b |
168 | |
169 | # Query for all artists and put them in an array, |
170 | # or retrieve them as a result set object. |
30e1753a |
171 | # $schema->resultset returns a DBIx::Class::ResultSet |
2053ab2a |
172 | my @all_artists = $schema->resultset('Artist')->all; |
173 | my $all_artists_rs = $schema->resultset('Artist'); |
126042ee |
174 | |
30e1753a |
175 | # Output all artists names |
4e8ffded |
176 | # $artist here is a DBIx::Class::Row, which has accessors |
16ccb4fe |
177 | # for all its columns. Rows are also subclasses of your Result class. |
85067746 |
178 | foreach $artist (@all_artists) { |
30e1753a |
179 | print $artist->name, "\n"; |
180 | } |
181 | |
a0638a7b |
182 | # Create a result set to search for artists. |
86beca1d |
183 | # This does not query the DB. |
2053ab2a |
184 | my $johns_rs = $schema->resultset('Artist')->search( |
6576ef54 |
185 | # Build your WHERE using an SQL::Abstract structure: |
2053ab2a |
186 | { name => { like => 'John%' } } |
a0638a7b |
187 | ); |
39fe0e65 |
188 | |
2053ab2a |
189 | # Execute a joined query to get the cds. |
a0638a7b |
190 | my @all_john_cds = $johns_rs->search_related('cds')->all; |
448c8424 |
191 | |
f0bb26f3 |
192 | # Fetch the next available row. |
a0638a7b |
193 | my $first_john = $johns_rs->next; |
448c8424 |
194 | |
2053ab2a |
195 | # Specify ORDER BY on the query. |
a0638a7b |
196 | my $first_john_cds_by_title_rs = $first_john->cds( |
197 | undef, |
198 | { order_by => 'title' } |
199 | ); |
448c8424 |
200 | |
bd077b47 |
201 | # Create a result set that will fetch the artist data |
2053ab2a |
202 | # at the same time as it fetches CDs, using only one query. |
884559b1 |
203 | my $millennium_cds_rs = $schema->resultset('CD')->search( |
a0638a7b |
204 | { year => 2000 }, |
205 | { prefetch => 'artist' } |
206 | ); |
448c8424 |
207 | |
880a1a0c |
208 | my $cd = $millennium_cds_rs->next; # SELECT ... FROM cds JOIN artists ... |
bd077b47 |
209 | my $cd_artist_name = $cd->artist->name; # Already has the data so no 2nd query |
076652e8 |
210 | |
4b0a90fd |
211 | # new() makes a Result object but doesn't insert it into the DB. |
264f1571 |
212 | # create() is the same as new() then insert(). |
884559b1 |
213 | my $new_cd = $schema->resultset('CD')->new({ title => 'Spoon' }); |
f183eccd |
214 | $new_cd->artist($cd->artist); |
f183eccd |
215 | $new_cd->insert; # Auto-increment primary key filled in after INSERT |
f183eccd |
216 | $new_cd->title('Fork'); |
217 | |
884559b1 |
218 | $schema->txn_do(sub { $new_cd->update }); # Runs the update in a transaction |
f183eccd |
219 | |
bd077b47 |
220 | # change the year of all the millennium CDs at once |
221 | $millennium_cds_rs->update({ year => 2002 }); |
f183eccd |
222 | |
223 | =head1 DESCRIPTION |
224 | |
225 | This is an SQL to OO mapper with an object API inspired by L<Class::DBI> |
bd077b47 |
226 | (with a compatibility layer as a springboard for porting) and a resultset API |
f183eccd |
227 | that allows abstract encapsulation of database operations. It aims to make |
228 | representing queries in your code as perl-ish as possible while still |
a0638a7b |
229 | providing access to as many of the capabilities of the database as possible, |
f183eccd |
230 | including retrieving related records from multiple tables in a single query, |
53aa53f3 |
231 | C<JOIN>, C<LEFT JOIN>, C<COUNT>, C<DISTINCT>, C<GROUP BY>, C<ORDER BY> and |
232 | C<HAVING> support. |
f183eccd |
233 | |
234 | DBIx::Class can handle multi-column primary and foreign keys, complex |
235 | queries and database-level paging, and does its best to only query the |
75d07914 |
236 | database in order to return something you've directly asked for. If a |
237 | resultset is used as an iterator it only fetches rows off the statement |
238 | handle as requested in order to minimise memory usage. It has auto-increment |
2053ab2a |
239 | support for SQLite, MySQL, PostgreSQL, Oracle, SQL Server and DB2 and is |
240 | known to be used in production on at least the first four, and is fork- |
ec6415a9 |
241 | and thread-safe out of the box (although |
9361b05d |
242 | L<your DBD may not be|DBI/Threads and Thread Safety>). |
f183eccd |
243 | |
dfccde48 |
244 | This project is still under rapid development, so large new features may be |
53aa53f3 |
245 | marked B<experimental> - such APIs are still usable but may have edge bugs. |
246 | Failing test cases are I<always> welcome and point releases are put out rapidly |
dfccde48 |
247 | as bugs are found and fixed. |
248 | |
249 | We do our best to maintain full backwards compatibility for published |
250 | APIs, since DBIx::Class is used in production in many organisations, |
251 | and even backwards incompatible changes to non-published APIs will be fixed |
252 | if they're reported and doing so doesn't cost the codebase anything. |
253 | |
264f1571 |
254 | The test suite is quite substantial, and several developer releases |
255 | are generally made to CPAN before the branch for the next release is |
256 | merged back to trunk for a major release. |
f183eccd |
257 | |
6ed05cfd |
258 | =head1 HOW TO CONTRIBUTE |
259 | |
260 | Contributions are always welcome, in all usable forms (we especially |
261 | welcome documentation improvements). The delivery methods include git- |
262 | or unified-diff formatted patches, GitHub pull requests, or plain bug |
263 | reports either via RT or the Mailing list. Contributors are generally |
cb32addc |
264 | granted access to the official repository after their first several |
265 | patches pass successful review. Don't hesitate to |
266 | L<contact|/GETTING HELP/SUPPORT> either of the L</CAT HERDERS> with |
267 | any further questions you may have. |
6ed05cfd |
268 | |
269 | =for comment |
270 | FIXME: Getty, frew and jnap need to get off their asses and finish the contrib section so we can link it here ;) |
271 | |
272 | This project is maintained in a git repository. The code and related tools are |
273 | accessible at the following locations: |
274 | |
275 | =over |
276 | |
277 | =item * Official repo: L<git://git.shadowcat.co.uk/dbsrgits/DBIx-Class.git> |
278 | |
279 | =item * Official gitweb: L<http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=dbsrgits/DBIx-Class.git> |
280 | |
281 | =item * GitHub mirror: L<https://github.com/dbsrgits/DBIx-Class> |
282 | |
283 | =item * Authorized committers: L<ssh://dbsrgits@git.shadowcat.co.uk/DBIx-Class.git> |
284 | |
285 | =item * Travis-CI log: L<https://travis-ci.org/dbsrgits/dbix-class/builds> |
286 | |
287 | =for html |
33d0570d |
288 | ↪ Bleeding edge dev CI status: <img src="https://secure.travis-ci.org/dbsrgits/dbix-class.png?branch=master"></img> |
6ed05cfd |
289 | |
290 | =back |
291 | |
3440100b |
292 | =head1 AUTHORS |
34d52be2 |
293 | |
3440100b |
294 | Even though a large portion of the source I<appears> to be written by just a |
295 | handful of people, this library continues to remain a collaborative effort - |
296 | perhaps one of the most successful such projects on L<CPAN|http://cpan.org>. |
297 | It is important to remember that ideas do not always result in a direct code |
298 | contribution, but deserve acknowledgement just the same. Time and time again |
299 | the seemingly most insignificant questions and suggestions have been shown |
300 | to catalyze monumental improvements in consistency, accuracy and performance. |
34d52be2 |
301 | |
3440100b |
302 | =for comment this line is replaced with the author list at dist-building time |
dfccde48 |
303 | |
3440100b |
304 | The canonical source of authors and their details is the F<AUTHORS> file at |
305 | the root of this distribution (or repository). The canonical source of |
306 | per-line authorship is the L<git repository|/HOW TO CONTRIBUTE> history |
307 | itself. |
f9139687 |
308 | |
cb32addc |
309 | =head1 CAT HERDERS |
310 | |
311 | The fine folks nudging the project in a particular direction: |
312 | |
313 | =over |
314 | |
f06eb015 |
315 | B<ribasushi>: Peter Rabbitson <ribasushi@cpan.org> |
cb32addc |
316 | (present day maintenance and controlled evolution) |
317 | |
f06eb015 |
318 | B<castaway>: Jess Robinson <castaway@desert-island.me.uk> |
cb32addc |
319 | (lions share of the reference documentation and manuals) |
320 | |
f06eb015 |
321 | B<mst>: Matt S Trout <mst@shadowcat.co.uk> (project founder - |
cb32addc |
322 | original idea, architecture and implementation) |
323 | |
324 | =back |
325 | |
a2bd3796 |
326 | =head1 COPYRIGHT AND LICENSE |
b38e10bd |
327 | |
a2bd3796 |
328 | Copyright (c) 2005 by mst, castaway, ribasushi, and other DBIx::Class |
329 | L</AUTHORS> as listed above and in F<AUTHORS>. |
96154ef7 |
330 | |
331 | This library is free software and may be distributed under the same terms |
a2bd3796 |
332 | as perl5 itself. See F<LICENSE> for the complete licensing terms. |