File Coverage

blib/lib/DBIx/Sunny/Util.pm
Criterion Covered Total %
statement 35 45 77.7
branch 6 10 60.0
condition 4 12 33.3
subroutine 7 8 87.5
pod 0 2 0.0
total 52 77 67.5


line stmt bran cond sub pod time code
1             package DBIx::Sunny::Util;
2              
3 12     12   114581 use strict;
  12         44  
  12         356  
4 12     12   62 use warnings;
  12         27  
  12         344  
5              
6 12     12   59 use Exporter 'import';
  12         22  
  12         513  
7 12     12   76 use Scalar::Util qw/blessed/;
  12         22  
  12         707  
8 12     12   5548 use SQL::NamedPlaceholder 0.10;
  12         20832  
  12         67  
9 12     12   618 use Carp qw/croak/;
  12         32  
  12         4353  
10              
11             our @EXPORT_OK = qw/bind_and_execute expand_placeholder/;
12              
13             sub bind_and_execute {
14 0     0 0 0 my ($sth, @bind) = @_;
15 0         0 my $i = 0;
16 0         0 for my $bind ( @bind ) {
17 0 0 0     0 if ( blessed($bind) && $bind->can('value_ref') && $bind->can('type') ) {
      0        
18             # If $bind is an SQL::Maker::SQLType or compatible object, use its type info.
19 0         0 $sth->bind_param(++$i, ${ $bind->value_ref }, $bind->type);
  0         0  
20             } else {
21 0         0 $sth->bind_param(++$i, $bind);
22             }
23             }
24 0         0 return $sth->execute;
25             }
26              
27             sub expand_placeholder {
28 13     13 0 18118 my ($query, @bind) = @_;
29              
30 13 50       42 return if ! defined $query;
31 13 50 66     55 if (@bind == 1 && ref $bind[0] eq 'HASH') {
32 0         0 ($query, my $bind_param) = SQL::NamedPlaceholder::bind_named($query, $bind[0]);
33 0         0 return $query, @$bind_param;
34             }
35              
36 13         23 my @bind_param;
37 13         18 my $orig_num_binds = @bind;
38 13         16 my $num_bounds = 0;
39 13         58 $query =~ s{\?}{
40 10         16 my $bind = shift @bind;
41 10         19 $num_bounds++;
42 10 100 66     44 if (ref($bind) && ref($bind) eq 'ARRAY') {
43 4         10 push @bind_param, @$bind;
44 4         18 join ',', ('?') x @$bind;
45             } else {
46 6         13 push @bind_param, $bind;
47 6         19 '?';
48             }
49             }ge;
50              
51 13 100       30 if ($num_bounds != $orig_num_binds) {
52 8         84 croak "Num of binds doesn't match. expected = $num_bounds, but passed $orig_num_binds";
53             }
54              
55 5         23 return ( $query, @bind_param );
56             }
57              
58             1