| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package FormValidator::Simple::Plugin::CDBI::Unique; |
|
2
|
1
|
|
|
1
|
|
41765
|
use strict; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
42
|
|
|
3
|
1
|
|
|
1
|
|
6
|
use warnings; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
31
|
|
|
4
|
1
|
|
|
1
|
|
1860
|
use UNIVERSAL; |
|
|
1
|
|
|
|
|
20
|
|
|
|
1
|
|
|
|
|
6
|
|
|
5
|
1
|
|
|
1
|
|
998
|
use UNIVERSAL::require; |
|
|
1
|
|
|
|
|
761
|
|
|
|
1
|
|
|
|
|
10
|
|
|
6
|
1
|
|
|
1
|
|
1449
|
use SQL::Abstract; |
|
|
1
|
|
|
|
|
12178
|
|
|
|
1
|
|
|
|
|
17
|
|
|
7
|
1
|
|
|
1
|
|
997
|
use FormValidator::Simple::Exception; |
|
|
1
|
|
|
|
|
87
|
|
|
|
1
|
|
|
|
|
12
|
|
|
8
|
1
|
|
|
1
|
|
1092
|
use FormValidator::Simple::Constants; |
|
|
1
|
|
|
|
|
245
|
|
|
|
1
|
|
|
|
|
1051
|
|
|
9
|
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our $VERSION = '0.03'; |
|
11
|
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub CDBI_UNIQUE { |
|
13
|
|
|
|
|
|
|
|
|
14
|
0
|
|
|
0
|
0
|
|
my ($class, $params, $args) = @_; |
|
15
|
|
|
|
|
|
|
|
|
16
|
0
|
0
|
|
|
|
|
unless ( scalar(@$args) >= 2 ) { |
|
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
|
0
|
|
|
|
|
unless ( scalar(@$params) == scalar(@$args) ) { |
|
25
|
0
|
|
|
|
|
|
FormValidator::Simple::Exception->throw( |
|
26
|
|
|
|
|
|
|
qq/Validation CDBI_UNIQUE: number of keys and validation arguments aren't same./ |
|
27
|
|
|
|
|
|
|
); |
|
28
|
|
|
|
|
|
|
} |
|
29
|
0
|
0
|
|
|
|
|
if ( $class->options->{cdbi_base_class} ) { |
|
30
|
0
|
|
|
|
|
|
$table = $class->options->{cdbi_base_class}."::".$table; |
|
31
|
|
|
|
|
|
|
} |
|
32
|
0
|
|
|
|
|
|
$table->require; |
|
33
|
0
|
0
|
|
|
|
|
if ($@) { |
|
34
|
0
|
|
|
|
|
|
FormValidator::Simple::Exception->throw( |
|
35
|
|
|
|
|
|
|
qq/Validation CDBI_UNIQUE: faild to require $table. "$@"/ |
|
36
|
|
|
|
|
|
|
); |
|
37
|
|
|
|
|
|
|
} |
|
38
|
0
|
0
|
|
|
|
|
unless ( UNIVERSAL::isa( $table => 'Class::DBI' ) ) { |
|
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++ ) { |
|
45
|
0
|
|
|
|
|
|
my $key = $args->[$i]; |
|
46
|
0
|
|
|
|
|
|
my $value = $params->[$i]; |
|
47
|
0
|
0
|
|
|
|
|
if ( $key =~ /^!(.+)$/ ) { |
|
48
|
0
|
|
|
|
|
|
$criteria{$1} = { '!=' => $value }; |
|
49
|
|
|
|
|
|
|
} |
|
50
|
|
|
|
|
|
|
else { |
|
51
|
0
|
|
0
|
|
|
|
$criteria{$key} = $value || ''; |
|
52
|
|
|
|
|
|
|
} |
|
53
|
|
|
|
|
|
|
} |
|
54
|
0
|
|
|
|
|
|
foreach my $column ( keys %criteria ) { |
|
55
|
0
|
0
|
|
|
|
|
$table->find_column($column) |
|
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
|
0
|
|
|
|
|
if($@){ |
|
71
|
0
|
|
|
|
|
|
FormValidator::Simple::Exception->throw( |
|
72
|
|
|
|
|
|
|
qq/Validation CDBI_UNIQUE: "$@"./ |
|
73
|
|
|
|
|
|
|
); |
|
74
|
|
|
|
|
|
|
} |
|
75
|
0
|
0
|
|
|
|
|
return $count > 0 ? FALSE : TRUE; |
|
76
|
|
|
|
|
|
|
} |
|
77
|
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
1; |
|
79
|
|
|
|
|
|
|
__END__ |