File Coverage

blib/lib/DBIx/Squirrel.pm
Criterion Covered Total %
statement 120 132 90.9
branch 19 32 59.3
condition 7 9 77.7
subroutine 25 25 100.0
pod n/a
total 171 198 86.3


line stmt bran cond sub pod time code
1 9     9   1417123 use strict;
  9         19  
  9         439  
2 9     9   70 use warnings;
  9         18  
  9         493  
3 9     9   190 use 5.010_001;
  9         35  
4              
5             package DBIx::Squirrel;
6              
7             # ABSTRACT: The little Perl DBI extension that makes working with databases a lot easier.
8              
9 9     9   14178 use DBI ();
  9         218434  
  9         436  
10 9     9   81 use Exporter ();
  9         17  
  9         229  
11 9     9   44 use Scalar::Util 'reftype';
  9         15  
  9         597  
12 9     9   4899 use Sub::Name 'subname';
  9         6082  
  9         572  
13 9     9   4487 use DBIx::Squirrel::db ();
  9         44  
  9         341  
14 9     9   4996 use DBIx::Squirrel::dr ();
  9         33  
  9         300  
15 9     9   5029 use DBIx::Squirrel::it ();
  9         38  
  9         344  
16 9     9   5029 use DBIx::Squirrel::rc ();
  9         40  
  9         300  
17 9     9   4756 use DBIx::Squirrel::rs ();
  9         37  
  9         295  
18 9     9   65 use DBIx::Squirrel::st ();
  9         37  
  9         227  
19 9     9   47 use DBIx::Squirrel::util 'confessf';
  9         17  
  9         495  
20 9     9   52 use namespace::clean;
  9         16  
  9         42  
21              
22 9     9   2595 use constant E_BAD_ENT_BIND => 'Cannot associate with an invalid object';
  9         16  
  9         626  
23 9     9   84 use constant E_EXP_HASH_ARR_REF => 'Expected a reference to a HASH or ARRAY';
  9         20  
  9         2116  
24              
25             BEGIN {
26 9     9   210 @DBIx::Squirrel::ISA = 'DBI';
27 9         30 $DBIx::Squirrel::VERSION = '1.6.5';
28 9         309 @DBIx::Squirrel::EXPORT_OK = @DBI::EXPORT_OK;
29 9         80 %DBIx::Squirrel::EXPORT_TAGS = %DBI::EXPORT_TAGS;
30 9         72 *DBIx::Squirrel::err = *DBI::err;
31 9         25 *DBIx::Squirrel::errstr = *DBI::errstr;
32 9         21 *DBIx::Squirrel::rows = *DBI::rows;
33 9         21 *DBIx::Squirrel::lasth = *DBI::lasth;
34 9         27 *DBIx::Squirrel::state = *DBI::state;
35 9         24 *DBIx::Squirrel::connect = *DBIx::Squirrel::dr::connect;
36 9         21 *DBIx::Squirrel::connect_cached = *DBIx::Squirrel::dr::connect_cached;
37 9         47 *DBIx::Squirrel::FINISH_ACTIVE_BEFORE_EXECUTE
38             = *DBIx::Squirrel::st::FINISH_ACTIVE_BEFORE_EXECUTE;
39 9         36 *DBIx::Squirrel::DEFAULT_SLICE = *DBIx::Squirrel::it::DEFAULT_SLICE;
40 9         29 *DBIx::Squirrel::DEFAULT_CACHE_SIZE = *DBIx::Squirrel::it::DEFAULT_CACHE_SIZE;
41 9         2528 *DBIx::Squirrel::CACHE_SIZE_LIMIT = *DBIx::Squirrel::it::CACHE_SIZE_LIMIT;
42             }
43              
44             # Divide the argumments into two lists:
45             # 1. a list of helper function names;
46             # 2. a list of names to be imported from the DBI.
47             sub _partition_imports_into_helpers_and_dbi_imports {
48 9     9   19 my( @helpers, @dbi );
49 9         49 while (@_) {
50 9 50       24 next unless defined( $_[0] );
51 9 100       62 if ( $_[0] =~ m/^database_entit(?:y|ies)$/i ) {
52 7         13 shift;
53 7 100       21 if ( ref( $_[0] ) ) {
54 5 50       20 if ( UNIVERSAL::isa( $_[0], 'ARRAY' ) ) {
55 5         9 push @helpers, @{ +shift };
  5         27  
56             }
57             else {
58 0         0 shift;
59             }
60             }
61             else {
62 2         8 push @helpers, shift();
63             }
64             }
65             else {
66 2         4 push @dbi, shift();
67             }
68             }
69 9         30 return ( \@helpers, \@dbi );
70             }
71              
72             sub import {
73 9     9   75 no strict 'refs'; ## no critic
  9         20  
  9         6282  
74 9     9   121 my $class = shift;
75 9         34 my $caller = caller;
76 9         118 my( $helpers, $dbi ) = _partition_imports_into_helpers_and_dbi_imports(@_);
77 9         23 for my $name ( @{$helpers} ) {
  9         24  
78 15         28 my $symbol = $class . '::' . $name;
79             my $helper = sub {
80 52 100   52   14672 if (@_) {
        57      
        47      
        47      
81 12 100 100     132 if ( UNIVERSAL::isa( $_[0], 'DBI::db' )
      100        
82             or UNIVERSAL::isa( $_[0], 'DBI::st' )
83             or UNIVERSAL::isa( $_[0], 'DBIx::Squirrel::it' ) )
84             {
85 11         20 ${$symbol} = shift;
  11         53  
86 11         18 return ${$symbol};
  11         58  
87             }
88             }
89 41 50       92 return unless defined( ${$symbol} );
  41         181  
90 41 100       108 if (@_) {
91 1         1 my @params = do {
92 1 50 33     7 if ( @_ == 1 && ref $_[0] ) {
93 0 0       0 if ( reftype( $_[0] ) eq 'ARRAY' ) {
    0          
94 0         0 @{ +shift };
  0         0  
95             }
96             elsif ( reftype( $_[0] ) eq 'HASH' ) {
97 0         0 %{ +shift };
  0         0  
98             }
99             else {
100 0         0 confessf E_EXP_HASH_ARR_REF;
101             }
102             }
103             else {
104 1         3 @_;
105             }
106             };
107 1 50       3 if ( UNIVERSAL::isa( ${$symbol}, 'DBI::db' ) ) {
  1 50       7  
    0          
108 0         0 return ${$symbol}->prepare(@params);
  0         0  
109             }
110 1         4 elsif ( UNIVERSAL::isa( ${$symbol}, 'DBI::st' ) ) {
111 1         2 return ${$symbol}->execute(@params);
  1         6  
112             }
113 0         0 elsif ( UNIVERSAL::isa( ${$symbol}, 'DBIx::Squirrel::it' ) ) {
114 0         0 return ${$symbol}->iterate(@params);
  0         0  
115             }
116             else {
117             # ok - no worries
118             }
119             }
120 40         62 return ${$symbol};
  40         401  
121 15         69 };
122 15         85 *{$symbol} = subname( $name => $helper );
  15         65  
123 15         64 *{ $caller . '::' . $name } = subname( $caller . '::' . $name => \&{$symbol} )
  15         85  
124 15 50       26 unless defined( &{ $caller . '::' . $name } );
  15         83  
125             }
126 9 100       18 if ( @{$dbi} ) {
  9         40  
127 1         1 DBI->import( @{$dbi} );
  1         413  
128 1         3 @_ = ( 'DBIx::Squirrel', @{$dbi} );
  1         4  
129 1         320 goto &Exporter::import;
130             }
131 8         157 return $class;
132             }
133              
134             1;