blib/lib/FormValidator/Simple/Plugin/CDBI/Unique.pm

filter: Statementstmt 41.8% 23 / 55 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 87.5% 7 / 8 Podpod 0.0% 0 / 1 totaltotal 34.4% 30 / 87 CC 13 SCAR 47.8
CC
13
Cov
30%
CDBI_UNIQUE
182.0
BEGIN
1.0
BEGIN
1.0
CRAP 118.7
↑ Summary
1 package FormValidator::Simple::Plugin::CDBI::Unique;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use UNIVERSAL;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use UNIVERSAL::require;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use SQL::Abstract;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use FormValidator::Simple::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use FormValidator::Simple::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 our $VERSION = '0.03';
11  
12 0 ▶sub CDBI_UNIQUE {
Subroutine CDBI_UNIQUE: not called CC=13 SCAR=52.0
Pod undocumented
13  
14 0 my ($class, $params, $args) = @_;
15  
16 0 ▶ unless ( scalar(@$args) >= 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @$args >= 2) 0 0 0
17 0 FormValidator::Simple::Exception->throw(
18 qq/Validation CDBI_UNIQUE needs two arguments at least. /
19 .qq/Set name of CDBI table class and unique column(s). /
20 );
21 }
22  
23 0 my $table = shift @$args;
24 0 ▶ unless ( scalar(@$params) == scalar(@$args) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @$params == scalar @$args) 0 0 0
25 0 FormValidator::Simple::Exception->throw(
26 qq/Validation CDBI_UNIQUE: number of keys and validation arguments aren't same./
27 );
28 }
29 0 ▶ if ( $class->options->{cdbi_base_class} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->options->{"cdbi_base_class"}) 0 0 0
30 0 $table = $class->options->{cdbi_base_class}."::".$table;
31 }
32 0 $table->require;
33 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
34 0 FormValidator::Simple::Exception->throw(
35 qq/Validation CDBI_UNIQUE: faild to require $table. "$@"/
36 );
37 }
38 0 ▶ unless ( UNIVERSAL::isa( $table => 'Class::DBI' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (UNIVERSAL::isa($table, "Class::DBI")) 0 0 0
39 0 FormValidator::Simple::Exception->throw(
40 qq/Validation CDBI_UNIQUE: set CDBI table class as first argument./
41 );
42 }
43 0 my %criteria = ();
44 0 ▶ for ( my $i = 0; $i < scalar(@$args); $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @$args) 0 0 0
45 0 my $key = $args->[$i];
46 0 my $value = $params->[$i];
47 0 ▶ if ( $key =~ /^!(.+)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /^!(.+)$/) { } 0 0 0
48 0 $criteria{$1} = { '!=' => $value };
49 }
50 else {
51 0 ▶ $criteria{$key} = $value || '';
Condition0%0/2
$value || ''
l!l
00
MC/DC0%0/1
$value || ''
$value
Truth table0%0/2
A
A= $value
Aresult
11
00
52 }
53 }
54 0 foreach my $column ( keys %criteria ) {
55 0 ▶ $table->find_column($column)
Branch: 1 branch
BranchTrueFalseTotal
$table->find_column($column) or "FormValidator::Simple::Exception"->throw("Validation CDBI_UNIQUE: $column is not a column of $table.") 0 0 0
56 or FormValidator::Simple::Exception->throw(
57 qq/Validation CDBI_UNIQUE: $column is not a column of $table./
58 );
59 }
60 0 my($stmt, @bind) =
61 SQL::Abstract->new->select($table->table, "COUNT(*)", \%criteria);
62  
63 0 my $count;
64 0 eval{
65 0 my $sth = $table->db_Main->prepare($stmt);
66 0 $sth->execute(@bind);
67 0 $sth->bind_columns(\$count);
68 0 $sth->fetch;
69 };
70 0 ▶ if($@){
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
71 0 FormValidator::Simple::Exception->throw(
72 qq/Validation CDBI_UNIQUE: "$@"./
73 );
74 }
75 0 ▶ return $count > 0 ? FALSE : TRUE;
Branch: 1 branch
BranchTrueFalseTotal
$count > 0 ? : 0 0 0
76 }
77  
78 1 1;
79 __END__
↑ Summary