File Coverage

blib/lib/LINQ/Database.pm
Criterion Covered Total %
statement 31 35 88.5
branch 3 6 50.0
condition 2 6 33.3
subroutine 10 10 100.0
pod 0 5 0.0
total 46 62 74.1


line stmt bran cond sub pod time code
1 1     1   126859 use 5.008003;
  1         3  
2 1     1   6 use strict;
  1         2  
  1         19  
3 1     1   4 use warnings;
  1         2  
  1         52  
4              
5             package LINQ::Database;
6              
7             our $AUTHORITY = 'cpan:TOBYINK';
8             our $VERSION = '0.000_002';
9              
10 1     1   537 use Class::Tiny qw( dbh );
  1         1803  
  1         4  
11 1     1   204 use Scalar::Util ();
  1         3  
  1         330  
12              
13             sub BUILDARGS {
14 1     1 0 188 my ( $self ) = ( shift );
15            
16 1 50 33     7 if ( @_ == 1 and Scalar::Util::blessed( $_[0] ) ) {
17 0         0 return { dbh => $_[0] };
18             }
19             else {
20 1         1614 require DBI;
21 1         17721 return { dbh => 'DBI'->connect( @_ ) };
22             }
23             }
24              
25             sub table {
26 6     6 0 23513 my ( $self ) = ( shift );
27 6         11 my %args;
28 6 50 33     65 if ( @_ == 1 and ref($_[0]) eq 'HASH' ) {
    50          
29 0         0 %args = %{ $_[0] };
  0         0  
30             }
31             elsif ( @_ % 2 == 1 ) {
32 6         22 %args = ( name => @_ );
33             }
34             else {
35 0         0 %args = @_;
36             }
37            
38 6         605 require LINQ::Database::Table;
39 6         40 'LINQ::Database::Table'->new( { database => $self, %args } );
40             }
41              
42             sub prepare {
43 6     6 0 163 my ( $self, $sql ) = ( shift, @_ );
44 6         12 $self->{last_sql} = $sql;
45 6         92 $self->dbh->prepare( $sql );
46             }
47              
48             sub quote {
49 2     2 0 14 my ( $self ) = ( shift );
50 2         65 $self->dbh->quote( @_ );
51             }
52              
53             sub quote_identifier {
54 6     6 0 39 my ( $self ) = ( shift );
55 6         98 $self->dbh->quote_identifier( @_ );
56             }
57              
58             1;
59              
60             __END__
61              
62             =pod
63              
64             =encoding utf-8
65              
66             =head1 NAME
67              
68             LINQ::Database - LINQ extension for working with databases
69              
70             =head1 SYNOPSIS
71              
72             use LINQ;
73             use LINQ::Util -all;
74             use LINQ::Database;
75             use DBI;
76            
77             my $db = 'LINQ::Database'->new( 'DBI'->connect( ... ) );
78            
79             $db
80             ->table( 'pet' )
81             ->where( check_fields 'name', -like => 'P%', -nocase )
82             ->select( fields 'name', 'species' )
83             ->foreach( sub {
84             printf( "%s is a %s\n", $_->name, $_->species );
85             } );
86              
87             =head1 DESCRIPTION
88              
89             L<LINQ::Database> provides a L<LINQ::Collection>-compatible interface for
90             accessing SQL databases. It's basically B<< DLinq for Perl >>.
91              
92             =head1 BUGS
93              
94             Please report any bugs to
95             L<http://rt.cpan.org/Dist/Display.html?Queue=LINQ-Database>.
96              
97             =head1 SEE ALSO
98              
99             L<LINQ>.
100              
101             =head1 AUTHOR
102              
103             Toby Inkster E<lt>tobyink@cpan.orgE<gt>.
104              
105             =head1 COPYRIGHT AND LICENCE
106              
107             This software is copyright (c) 2021 by Toby Inkster.
108              
109             This is free software; you can redistribute it and/or modify it under
110             the same terms as the Perl 5 programming language system itself.
111              
112             =head1 DISCLAIMER OF WARRANTIES
113              
114             THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
115             WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
116             MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
117