File Coverage

blib/lib/DBIx/Class.pm
Criterion Covered Total %
statement 38 38 100.0
branch 5 8 62.5
condition n/a
subroutine 14 14 100.0
pod 0 3 0.0
total 57 63 90.4


line stmt bran cond sub pod time code
1             package DBIx::Class;
2              
3 380     380   9873 use strict;
  380         1221  
  380         11224  
4 380     380   2152 use warnings;
  380         1143  
  380         28385  
5              
6             our $VERSION;
7             # Always remember to do all digits for the version even if they're 0
8             # i.e. first release of 0.XX *must* be 0.XX000. This avoids fBSD ports
9             # brain damage and presumably various other packaging systems too
10              
11             # $VERSION declaration must stay up here, ahead of any other package
12             # declarations, as to not confuse various modules attempting to determine
13             # this ones version, whether that be s.c.o. or Module::Metadata, etc
14             $VERSION = '0.082841';
15              
16             $VERSION = eval $VERSION if $VERSION =~ /_/; # numify for warning-free dev releases
17              
18 380     380   3120 use DBIx::Class::_Util;
  380         1172  
  380         17659  
19 380     380   2809 use mro 'c3';
  380         1288  
  380         4142  
20              
21 380     380   192069 use DBIx::Class::Optional::Dependencies;
  380         1473  
  380         17582  
22              
23 380     380   3235 use base qw/DBIx::Class::Componentised DBIx::Class::AccessorGroup/;
  380         1160  
  380         173356  
24 380     380   167934 use DBIx::Class::StartupCheck;
  380         1217  
  380         12293  
25 380     380   156300 use DBIx::Class::Exception;
  380         1197  
  380         110823  
26              
27             __PACKAGE__->mk_group_accessors(inherited => '_skip_namespace_frames');
28             __PACKAGE__->_skip_namespace_frames('^DBIx::Class|^SQL::Abstract|^Try::Tiny|^Class::Accessor::Grouped|^Context::Preserve');
29              
30             # FIXME - this is not really necessary, and is in
31             # fact going to slow things down a bit
32             # However it is the right thing to do in order to get
33             # various install bases to highlight their brokenness
34             # Remove at some unknown point in the future
35 45732     45732   4767371 sub DESTROY { &DBIx::Class::_Util::detected_reinvoked_destructor }
36              
37             sub mk_classdata {
38 5847     5847 0 28100 shift->mk_classaccessor(@_);
39             }
40              
41             sub mk_classaccessor {
42 5847     5847 0 11035 my $self = shift;
43 5847         35490 $self->mk_group_accessors('inherited', $_[0]);
44 5847 100       2387472 $self->set_inherited(@_) if @_ > 1;
45             }
46              
47 6555     6555 0 1168992 sub component_base_class { 'DBIx::Class' }
48              
49             sub MODIFY_CODE_ATTRIBUTES {
50 1     1   620 my ($class,$code,@attrs) = @_;
51 1 50       55 $class->mk_classdata('__attr_cache' => {})
52             unless $class->can('__attr_cache');
53 1         35 $class->__attr_cache->{$code} = [@attrs];
54 1         30 return ();
55             }
56              
57             sub _attr_cache {
58 1     1   3 my $self = shift;
59 1 50       26 my $cache = $self->can('__attr_cache') ? $self->__attr_cache : {};
60              
61             return {
62             %$cache,
63 1 50       18 %{ $self->maybe::next::method || {} },
  1         6  
64             };
65             }
66              
67             # *DO NOT* change this URL nor the identically named =head1 below
68             # it is linked throughout the ecosystem
69             sub DBIx::Class::_ENV_::HELP_URL () {
70             'http://p3rl.org/DBIx::Class#GETTING_HELP/SUPPORT'
71             }
72              
73             1;
74              
75             __END__
76              
77             # This is the only file where an explicit =encoding is needed,
78             # as the distbuild-time injected author list is utf8 encoded
79             # Without this pod2text output is less than ideal
80             #
81             # A bit regarding selection/compatiblity:
82             # Before 5.8.7 UTF-8 was == utf8, both behaving like the (lax) utf8 we know today
83             # Then https://www.nntp.perl.org/group/perl.unicode/2004/12/msg2705.html happened
84             # Encode way way before 5.8.0 supported UTF-8: https://metacpan.org/source/DANKOGAI/Encode-1.00/lib/Encode/Supported.pod#L44
85             # so it is safe for the oldest toolchains.
86             # Additionally we inject all the utf8 programattically and test its well-formedness
87             # so all is well
88             #
89             =encoding UTF-8
90              
91             =head1 NAME
92              
93             DBIx::Class - Extensible and flexible object <-> relational mapper.
94              
95             =head1 WHERE TO START READING
96              
97             See L<DBIx::Class::Manual::DocMap> for an overview of the exhaustive documentation.
98             To get the most out of DBIx::Class with the least confusion it is strongly
99             recommended to read (at the very least) the
100             L<Manuals|DBIx::Class::Manual::DocMap/Manuals> in the order presented there.
101              
102             =cut
103              
104             =head1 GETTING HELP/SUPPORT
105              
106             Due to the sheer size of its problem domain, DBIx::Class is a relatively
107             complex framework. After you start using DBIx::Class questions will inevitably
108             arise. If you are stuck with a problem or have doubts about a particular
109             approach do not hesitate to contact us via any of the following options (the
110             list is sorted by "fastest response time"):
111              
112             =over
113              
114             =item * IRC: irc.perl.org#dbix-class
115              
116             =for html
117             <a href="https://chat.mibbit.com/#dbix-class@irc.perl.org">(click for instant chatroom login)</a>
118              
119             =item * Mailing list: L<http://lists.scsys.co.uk/mailman/listinfo/dbix-class>
120              
121             =item * RT Bug Tracker: L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=DBIx-Class>
122              
123             =item * Twitter: L<https://www.twitter.com/dbix_class>
124              
125             =item * Web Site: L<http://www.dbix-class.org/>
126              
127             =back
128              
129             =head1 SYNOPSIS
130              
131             For the very impatient: L<DBIx::Class::Manual::QuickStart>
132              
133             This code in the next step can be generated automatically from an existing
134             database, see L<dbicdump> from the distribution C<DBIx-Class-Schema-Loader>.
135              
136             =head2 Schema classes preparation
137              
138             Create a schema class called F<MyApp/Schema.pm>:
139              
140             package MyApp::Schema;
141             use base qw/DBIx::Class::Schema/;
142              
143             __PACKAGE__->load_namespaces();
144              
145             1;
146              
147             Create a result class to represent artists, who have many CDs, in
148             F<MyApp/Schema/Result/Artist.pm>:
149              
150             See L<DBIx::Class::ResultSource> for docs on defining result classes.
151              
152             package MyApp::Schema::Result::Artist;
153             use base qw/DBIx::Class::Core/;
154              
155             __PACKAGE__->table('artist');
156             __PACKAGE__->add_columns(qw/ artistid name /);
157             __PACKAGE__->set_primary_key('artistid');
158             __PACKAGE__->has_many(cds => 'MyApp::Schema::Result::CD', 'artistid');
159              
160             1;
161              
162             A result class to represent a CD, which belongs to an artist, in
163             F<MyApp/Schema/Result/CD.pm>:
164              
165             package MyApp::Schema::Result::CD;
166             use base qw/DBIx::Class::Core/;
167              
168             __PACKAGE__->load_components(qw/InflateColumn::DateTime/);
169             __PACKAGE__->table('cd');
170             __PACKAGE__->add_columns(qw/ cdid artistid title year /);
171             __PACKAGE__->set_primary_key('cdid');
172             __PACKAGE__->belongs_to(artist => 'MyApp::Schema::Result::Artist', 'artistid');
173              
174             1;
175              
176             =head2 API usage
177              
178             Then you can use these classes in your application's code:
179              
180             # Connect to your database.
181             use MyApp::Schema;
182             my $schema = MyApp::Schema->connect($dbi_dsn, $user, $pass, \%dbi_params);
183              
184             # Query for all artists and put them in an array,
185             # or retrieve them as a result set object.
186             # $schema->resultset returns a DBIx::Class::ResultSet
187             my @all_artists = $schema->resultset('Artist')->all;
188             my $all_artists_rs = $schema->resultset('Artist');
189              
190             # Output all artists names
191             # $artist here is a DBIx::Class::Row, which has accessors
192             # for all its columns. Rows are also subclasses of your Result class.
193             foreach $artist (@all_artists) {
194             print $artist->name, "\n";
195             }
196              
197             # Create a result set to search for artists.
198             # This does not query the DB.
199             my $johns_rs = $schema->resultset('Artist')->search(
200             # Build your WHERE using an SQL::Abstract structure:
201             { name => { like => 'John%' } }
202             );
203              
204             # Execute a joined query to get the cds.
205             my @all_john_cds = $johns_rs->search_related('cds')->all;
206              
207             # Fetch the next available row.
208             my $first_john = $johns_rs->next;
209              
210             # Specify ORDER BY on the query.
211             my $first_john_cds_by_title_rs = $first_john->cds(
212             undef,
213             { order_by => 'title' }
214             );
215              
216             # Create a result set that will fetch the artist data
217             # at the same time as it fetches CDs, using only one query.
218             my $millennium_cds_rs = $schema->resultset('CD')->search(
219             { year => 2000 },
220             { prefetch => 'artist' }
221             );
222              
223             my $cd = $millennium_cds_rs->next; # SELECT ... FROM cds JOIN artists ...
224             my $cd_artist_name = $cd->artist->name; # Already has the data so no 2nd query
225              
226             # new() makes a Result object but doesn't insert it into the DB.
227             # create() is the same as new() then insert().
228             my $new_cd = $schema->resultset('CD')->new({ title => 'Spoon' });
229             $new_cd->artist($cd->artist);
230             $new_cd->insert; # Auto-increment primary key filled in after INSERT
231             $new_cd->title('Fork');
232              
233             $schema->txn_do(sub { $new_cd->update }); # Runs the update in a transaction
234              
235             # change the year of all the millennium CDs at once
236             $millennium_cds_rs->update({ year => 2002 });
237              
238             =head1 DESCRIPTION
239              
240             This is an SQL to OO mapper with an object API inspired by L<Class::DBI>
241             (with a compatibility layer as a springboard for porting) and a resultset API
242             that allows abstract encapsulation of database operations. It aims to make
243             representing queries in your code as perl-ish as possible while still
244             providing access to as many of the capabilities of the database as possible,
245             including retrieving related records from multiple tables in a single query,
246             C<JOIN>, C<LEFT JOIN>, C<COUNT>, C<DISTINCT>, C<GROUP BY>, C<ORDER BY> and
247             C<HAVING> support.
248              
249             DBIx::Class can handle multi-column primary and foreign keys, complex
250             queries and database-level paging, and does its best to only query the
251             database in order to return something you've directly asked for. If a
252             resultset is used as an iterator it only fetches rows off the statement
253             handle as requested in order to minimise memory usage. It has auto-increment
254             support for SQLite, MySQL, PostgreSQL, Oracle, SQL Server and DB2 and is
255             known to be used in production on at least the first four, and is fork-
256             and thread-safe out of the box (although
257             L<your DBD may not be|DBI/Threads and Thread Safety>).
258              
259             This project is still under rapid development, so large new features may be
260             marked B<experimental> - such APIs are still usable but may have edge bugs.
261             Failing test cases are I<always> welcome and point releases are put out rapidly
262             as bugs are found and fixed.
263              
264             We do our best to maintain full backwards compatibility for published
265             APIs, since DBIx::Class is used in production in many organisations,
266             and even backwards incompatible changes to non-published APIs will be fixed
267             if they're reported and doing so doesn't cost the codebase anything.
268              
269             The test suite is quite substantial, and several developer releases
270             are generally made to CPAN before the branch for the next release is
271             merged back to trunk for a major release.
272              
273             =head1 HOW TO CONTRIBUTE
274              
275             Contributions are always welcome, in all usable forms (we especially
276             welcome documentation improvements). The delivery methods include git-
277             or unified-diff formatted patches, GitHub pull requests, or plain bug
278             reports either via RT or the Mailing list. Do not hesitate to
279             L<get in touch|/GETTING HELP/SUPPORT> with any further questions you may
280             have.
281              
282             =for comment
283             FIXME: Getty, frew and jnap need to get off their asses and finish the contrib section so we can link it here ;)
284              
285             This project is maintained in a git repository. The code and related tools are
286             accessible at the following locations:
287              
288             =over
289              
290             =item * Current git repository: L<https://github.com/Perl5/DBIx-Class>
291              
292             =item * Travis-CI log: L<https://travis-ci.org/Perl5/DBIx-Class/branches>
293              
294             =back
295              
296             =head1 AUTHORS
297              
298             Even though a large portion of the source I<appears> to be written by just a
299             handful of people, this library continues to remain a collaborative effort -
300             perhaps one of the most successful such projects on L<CPAN|http://cpan.org>.
301             It is important to remember that ideas do not always result in a direct code
302             contribution, but deserve acknowledgement just the same. Time and time again
303             the seemingly most insignificant questions and suggestions have been shown
304             to catalyze monumental improvements in consistency, accuracy and performance.
305              
306             =for comment this line is replaced with the author list at dist-building time
307              
308             The canonical source of authors and their details is the F<AUTHORS> file at
309             the root of this distribution (or repository). The canonical source of
310             per-line authorship is the L<git repository|/HOW TO CONTRIBUTE> history
311             itself.
312              
313             =head1 COPYRIGHT AND LICENSE
314              
315             Copyright (c) 2005 by mst, castaway, ribasushi, and other DBIx::Class
316             L</AUTHORS> as listed above and in F<AUTHORS>.
317              
318             This library is free software and may be distributed under the same terms
319             as perl5 itself. See F<LICENSE> for the complete licensing terms.