line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# the contents of this file are Copyright (c) 2009 Daniel Norman |
2
|
|
|
|
|
|
|
# This program is free software; you can redistribute it and/or |
3
|
|
|
|
|
|
|
# modify it under the terms of the GNU General Public License as |
4
|
|
|
|
|
|
|
# published by the Free Software Foundation. |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
package DBR::Query::Part::Value; |
7
|
|
|
|
|
|
|
|
8
|
18
|
|
|
18
|
|
124
|
use strict; |
|
18
|
|
|
|
|
32
|
|
|
18
|
|
|
|
|
700
|
|
9
|
18
|
|
|
18
|
|
91
|
use base 'DBR::Common'; |
|
18
|
|
|
|
|
41
|
|
|
18
|
|
|
|
|
1641
|
|
10
|
18
|
|
|
18
|
|
106
|
use Scalar::Util 'looks_like_number'; |
|
18
|
|
|
|
|
37
|
|
|
18
|
|
|
|
|
829
|
|
11
|
18
|
|
|
18
|
|
97
|
use Carp; |
|
18
|
|
|
|
|
31
|
|
|
18
|
|
|
|
|
26399
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
#### Constructors ############################################### |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
sub new{ |
16
|
2920
|
|
|
2920
|
0
|
6117
|
my( $package ) = shift; |
17
|
2920
|
|
|
|
|
12037
|
my %params = @_; |
18
|
|
|
|
|
|
|
|
19
|
2920
|
|
|
|
|
5574
|
my $field = $params{field}; # optional |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
|
22
|
2920
|
|
|
|
|
11868
|
my $self = { |
23
|
|
|
|
|
|
|
session => $params{session}, |
24
|
|
|
|
|
|
|
field => $field |
25
|
|
|
|
|
|
|
}; |
26
|
|
|
|
|
|
|
|
27
|
2920
|
|
|
|
|
37316
|
bless( $self, $package ); |
28
|
|
|
|
|
|
|
|
29
|
2920
|
100
|
|
|
|
13916
|
if (defined $field){ #field object is optional |
30
|
150
|
50
|
|
|
|
591
|
ref($field) eq 'DBR::Config::Field' or croak 'invalid field object'; |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
|
33
|
2920
|
50
|
|
|
|
8778
|
exists($params{value}) || croak 'value must be specified'; # undef and 0 are both legal, so cannot check for defined or truth |
34
|
2920
|
|
|
|
|
5973
|
my $value = $params{value}; |
35
|
|
|
|
|
|
|
|
36
|
2920
|
100
|
|
|
|
12674
|
if ( ref($value) eq 'DBR::Util::Operator' ) { |
37
|
15
|
|
|
|
|
30
|
my $wrapper = $value; |
38
|
|
|
|
|
|
|
|
39
|
15
|
|
|
|
|
73
|
$value = $wrapper->value; |
40
|
15
|
|
|
|
|
51
|
$self->{op_hint} = $wrapper->operator; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
2920
|
100
|
|
|
|
10313
|
$value = [$value] unless ref($value) eq 'ARRAY'; |
44
|
|
|
|
|
|
|
|
45
|
2920
|
100
|
|
|
|
9532
|
if(ref($field) eq 'DBR::Config::Field'){ # No Anon |
46
|
|
|
|
|
|
|
|
47
|
150
|
|
|
|
|
591
|
my $trans = $field->translator; |
48
|
150
|
100
|
|
|
|
452
|
if($trans){ |
49
|
|
|
|
|
|
|
|
50
|
37
|
|
|
|
|
62
|
my @translated; |
51
|
37
|
|
|
|
|
96
|
foreach (@$value){ |
52
|
37
|
|
|
|
|
477
|
my @tv = $trans->backward($_); |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
# undef is ok... but we Must have at least one element, or we are bailing |
55
|
37
|
0
|
|
|
|
133
|
scalar(@tv) or croak 'invalid value ' . (defined($_)?"'$_'":'undef') . ' for field ' . $field->name . ' (translator)'; |
|
|
50
|
|
|
|
|
|
56
|
37
|
|
|
|
|
140
|
push @translated, @tv; |
57
|
|
|
|
|
|
|
} |
58
|
37
|
|
|
|
|
105
|
$value = \@translated; |
59
|
|
|
|
|
|
|
} |
60
|
150
|
|
|
|
|
545
|
$self->{is_number} = $field->is_numeric; |
61
|
|
|
|
|
|
|
|
62
|
150
|
50
|
|
|
|
585
|
my $testsub = $field->testsub or confess 'failed to retrieve testsub'; |
63
|
|
|
|
|
|
|
|
64
|
150
|
|
|
|
|
434
|
foreach (@$value){ |
65
|
161
|
100
|
|
|
|
11756
|
$testsub->($_) or croak 'invalid value ' . (defined($_)?"'$_'":'undef') . ' for field ' . $field->name; |
|
|
100
|
|
|
|
|
|
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
}else{ |
69
|
2770
|
50
|
|
|
|
8651
|
defined($params{is_number}) or croak 'is_number must be specified'; |
70
|
|
|
|
|
|
|
|
71
|
2770
|
100
|
|
|
|
9580
|
$self->{is_number} = $params{is_number}? 1 : 0; |
72
|
|
|
|
|
|
|
|
73
|
2770
|
100
|
|
|
|
7760
|
if( $self->{is_number} ){ |
74
|
2011
|
|
|
|
|
2732
|
foreach my $val ( @{$value}) { |
|
2011
|
|
|
|
|
53313
|
|
75
|
2231
|
50
|
|
|
|
5429
|
$val = '' unless defined $val; |
76
|
2231
|
50
|
|
|
|
13957
|
looks_like_number($val) or croak "value '$val' is not a legal number"; |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
2918
|
|
|
|
|
7240
|
$self->{value} = $value; |
82
|
|
|
|
|
|
|
|
83
|
2918
|
|
|
|
|
23840
|
return $self; |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
1; |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
## Methods ################################################# |
91
|
830
|
|
|
830
|
0
|
7020
|
sub op_hint { return $_[0]->{op_hint} } |
92
|
2917
|
|
|
2917
|
0
|
11394
|
sub is_number{ return $_[0]->{is_number} } |
93
|
1600
|
|
|
1600
|
0
|
2266
|
sub count { return scalar( @{ $_[0]->{value} } ) } |
|
1600
|
|
|
|
|
11986
|
|
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
sub sql { |
96
|
2917
|
|
|
2917
|
0
|
5702
|
my $self = shift; |
97
|
2917
|
50
|
|
|
|
9281
|
my $conn = shift or croak 'conn is required'; |
98
|
|
|
|
|
|
|
|
99
|
2917
|
|
|
|
|
6007
|
my $sql; |
100
|
|
|
|
|
|
|
|
101
|
2917
|
|
|
|
|
8752
|
my $values = $self->quoted($conn); |
102
|
|
|
|
|
|
|
|
103
|
2917
|
100
|
|
|
|
12321
|
if (@$values != 1) { |
|
|
50
|
|
|
|
|
|
104
|
85
|
|
|
|
|
189
|
$sql .= '(' . join(',',@{$values}) . ')'; |
|
85
|
|
|
|
|
505
|
|
105
|
|
|
|
|
|
|
} elsif(@$values == 1){ |
106
|
2832
|
|
|
|
|
6458
|
$sql = $values->[0]; |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
|
109
|
2917
|
|
|
|
|
16466
|
return $sql; |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
sub is_null{ |
114
|
745
|
|
|
745
|
0
|
1432
|
my $self = shift; |
115
|
|
|
|
|
|
|
|
116
|
745
|
100
|
66
|
|
|
2467
|
return 1 if $self->count == 1 and !defined( $self->{value}->[0] ); |
117
|
720
|
|
|
|
|
2901
|
return 0; |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
|
120
|
13
|
|
|
13
|
0
|
65
|
sub is_emptyset{ $_[0]->count == 0 } |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
sub quoted{ |
123
|
2917
|
|
|
2917
|
0
|
3781
|
my $self = shift; |
124
|
2917
|
50
|
|
|
|
7352
|
my $conn = shift or croak('conn is required'); |
125
|
|
|
|
|
|
|
|
126
|
2917
|
100
|
|
|
|
7529
|
if ($self->is_number){ |
127
|
2132
|
100
|
|
|
|
4295
|
return [ map { defined($_)?$_:'NULL' } @{$self->{value}} ]; |
|
2363
|
|
|
|
|
14200
|
|
|
2132
|
|
|
|
|
5943
|
|
128
|
|
|
|
|
|
|
}else{ |
129
|
785
|
50
|
|
|
|
1677
|
return [ map { defined($_)?$_:'NULL' } map { $conn->quote($_) } @{$self->{value}} ]; |
|
785
|
|
|
|
|
14184
|
|
|
785
|
|
|
|
|
3866
|
|
|
785
|
|
|
|
|
2486
|
|
130
|
|
|
|
|
|
|
} |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
} |
133
|
|
|
|
|
|
|
|
134
|
12
|
50
|
|
12
|
0
|
133
|
sub raw{ wantarray?@{ $_[0]->{value} } : $_[0]->{value} } |
|
0
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
|
136
|
0
|
|
|
0
|
|
|
sub _session { $_[0]->{session} } |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
|