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   88818 use strict;
  12         34  
  12         335  
4 12     12   56 use warnings;
  12         19  
  12         327  
5              
6 12     12   51 use Exporter 'import';
  12         21  
  12         446  
7 12     12   65 use Scalar::Util qw/blessed/;
  12         20  
  12         701  
8 12     12   4749 use SQL::NamedPlaceholder 0.10;
  12         17909  
  12         64  
9 12     12   537 use Carp qw/croak/;
  12         21  
  12         3809  
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 14093 my ($query, @bind) = @_;
29              
30 13 50       31 return if ! defined $query;
31 13 50 66     44 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         16 my @bind_param;
37 13         17 my $orig_num_binds = @bind;
38 13         15 my $num_bounds = 0;
39 13         47 $query =~ s{\?}{
40 10         18 my $bind = shift @bind;
41 10         12 $num_bounds++;
42 10 100 66     32 if (ref($bind) && ref($bind) eq 'ARRAY') {
43 4         7 push @bind_param, @$bind;
44 4         17 join ',', ('?') x @$bind;
45             } else {
46 6         8 push @bind_param, $bind;
47 6         17 '?';
48             }
49             }ge;
50              
51 13 100       27 if ($num_bounds != $orig_num_binds) {
52 8         78 croak "Num of binds doesn't match. expected = $num_bounds, but passed $orig_num_binds";
53             }
54              
55 5         17 return ( $query, @bind_param );
56             }
57              
58             1