line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package FormValidator::Simple::Plugin::CDBI::Unique; |
2
|
1
|
|
|
1
|
|
49564
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
25
|
|
3
|
1
|
|
|
1
|
|
3
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
21
|
|
4
|
1
|
|
|
1
|
|
545
|
use UNIVERSAL; |
|
1
|
|
|
|
|
12
|
|
|
1
|
|
|
|
|
3
|
|
5
|
1
|
|
|
1
|
|
377
|
use UNIVERSAL::require; |
|
1
|
|
|
|
|
476
|
|
|
1
|
|
|
|
|
8
|
|
6
|
1
|
|
|
1
|
|
663
|
use SQL::Abstract; |
|
1
|
|
|
|
|
8086
|
|
|
1
|
|
|
|
|
57
|
|
7
|
1
|
|
|
1
|
|
506
|
use FormValidator::Simple::Exception; |
|
1
|
|
|
|
|
62
|
|
|
1
|
|
|
|
|
11
|
|
8
|
1
|
|
|
1
|
|
417
|
use FormValidator::Simple::Constants; |
|
1
|
|
|
|
|
187
|
|
|
1
|
|
|
|
|
351
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our $VERSION = '0.01_02'; |
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
|
|
|
|
|
if ( $class->options->{cdbi_base_class} ) { |
25
|
0
|
|
|
|
|
|
$table = $class->options->{cdbi_base_class}."::".$table; |
26
|
|
|
|
|
|
|
} |
27
|
0
|
|
|
|
|
|
$table->require; |
28
|
0
|
0
|
|
|
|
|
if ($@) { |
29
|
0
|
|
|
|
|
|
FormValidator::Simple::Exception->throw( |
30
|
|
|
|
|
|
|
qq/Validation CDBI_UNIQUE: faild to require $table. "$@"/ |
31
|
|
|
|
|
|
|
); |
32
|
|
|
|
|
|
|
} |
33
|
0
|
0
|
|
|
|
|
unless ( UNIVERSAL::isa( $table => 'Class::DBI' ) ) { |
34
|
0
|
|
|
|
|
|
FormValidator::Simple::Exception->throw( |
35
|
|
|
|
|
|
|
qq/Validation CDBI_UNIQUE: set CDBI table class as first argument./ |
36
|
|
|
|
|
|
|
); |
37
|
|
|
|
|
|
|
} |
38
|
0
|
|
|
|
|
|
my %criteria = (); |
39
|
0
|
|
|
|
|
|
for ( my $i = 0; $i < scalar(@$args); $i++ ) { |
40
|
0
|
|
|
|
|
|
my $key = $args->[$i]; |
41
|
0
|
|
|
|
|
|
my $value = $params->[$i]; |
42
|
0
|
0
|
|
|
|
|
if ( $key =~ /^!(.+)$/ ) { |
43
|
0
|
|
|
|
|
|
$criteria{$1} = { '!=' => $value }; |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
else { |
46
|
0
|
|
0
|
|
|
|
$criteria{$key} = $value || ''; |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
} |
49
|
0
|
|
|
|
|
|
foreach my $column ( keys %criteria ) { |
50
|
0
|
0
|
|
|
|
|
$table->find_column($column) |
51
|
|
|
|
|
|
|
or FormValidator::Simple::Exception->throw( |
52
|
|
|
|
|
|
|
qq/Validation CDBI_UNIQUE: $column is not a column of $table./ |
53
|
|
|
|
|
|
|
); |
54
|
|
|
|
|
|
|
} |
55
|
0
|
|
|
|
|
|
my($stmt, @bind) = |
56
|
|
|
|
|
|
|
SQL::Abstract->new->select($table->table, "COUNT(*)", \%criteria); |
57
|
|
|
|
|
|
|
|
58
|
0
|
|
|
|
|
|
my $count; |
59
|
0
|
|
|
|
|
|
eval{ |
60
|
0
|
|
|
|
|
|
my $sth = $table->db_Main->prepare($stmt); |
61
|
0
|
|
|
|
|
|
$sth->execute(@bind); |
62
|
0
|
|
|
|
|
|
$sth->bind_columns(\$count); |
63
|
0
|
|
|
|
|
|
$sth->fetch; |
64
|
|
|
|
|
|
|
}; |
65
|
0
|
0
|
|
|
|
|
if($@){ |
66
|
0
|
|
|
|
|
|
FormValidator::Simple::Exception->throw( |
67
|
|
|
|
|
|
|
qq/Validation CDBI_UNIQUE: "$@"./ |
68
|
|
|
|
|
|
|
); |
69
|
|
|
|
|
|
|
} |
70
|
0
|
0
|
|
|
|
|
return $count > 0 ? FALSE : TRUE; |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
1; |
74
|
|
|
|
|
|
|
__END__ |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
=head1 NAME |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
FormValidator::Simple::Plugin::CDBI::Unique - unique check for CDBI |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
=head1 SYNOPSIS |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
use FormValidator::Simple qw/CDBI::Unique/; |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
# check single column |
85
|
|
|
|
|
|
|
FormValidator::Simple->check( $q => [ |
86
|
|
|
|
|
|
|
name => [ [qw/CDBI_UNIQUE TableClass name/] ], |
87
|
|
|
|
|
|
|
] ); |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
# check multiple columns |
90
|
|
|
|
|
|
|
FormValidator::Simple->check( $q => [ |
91
|
|
|
|
|
|
|
{ unique => [qw/name email/] } => [ [qw/CDBI_UNIQUE TableClass name mail/] ], |
92
|
|
|
|
|
|
|
] ); |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
# check multiple columns including '!=' check |
95
|
|
|
|
|
|
|
FormValidator::Simple->check( $q => [ |
96
|
|
|
|
|
|
|
{ unique => [qw/id name email/] } => [ [qw/CDBI_UNIQUE Table !id name mail/] ] |
97
|
|
|
|
|
|
|
] ); |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
# when the class name is too long... |
101
|
|
|
|
|
|
|
FormValidator::Simple->check( $q => [ |
102
|
|
|
|
|
|
|
name => [ [qw/CDBI_UNIQUE MyProj::Model::User name/] ], |
103
|
|
|
|
|
|
|
] ); |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
# you can set cdbi_base_class in option. |
106
|
|
|
|
|
|
|
my $valid = FormValidator::Simple->new( { cdbi_base_class => 'MyProj::Model' } ); |
107
|
|
|
|
|
|
|
$valid->check( $q => [ |
108
|
|
|
|
|
|
|
name => [ [qw/CDBI_UNIQUE User name/] ], |
109
|
|
|
|
|
|
|
] ); |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
=head1 DESCRIPTION |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
This module is a plugin for FormValidator::Simple. This provides you a validation for unique check with CDBI table class. |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
=head1 SEE ALSO |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
L<FormValidator::Simple> |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
=head1 AUTHOR |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
Lyo Kato E<lt>lyo.kato@gmail.comE<gt> |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
Basic Idea: Masahiro Nagano E<lt>kazeburo@gmail.comE<gt> |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
Copyright (C) 2005 by Lyo Kato |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify |
130
|
|
|
|
|
|
|
it under the same terms as Perl itself. |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
=cut |
133
|
|
|
|
|
|
|
|