line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Query::Abstract::Driver::SQL; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
our $VERSION = '0.01'; |
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
977
|
use v5.10; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
57
|
|
6
|
1
|
|
|
1
|
|
7
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
42
|
|
7
|
1
|
|
|
1
|
|
7
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
39
|
|
8
|
|
|
|
|
|
|
|
9
|
1
|
|
|
1
|
|
6
|
use Data::Dumper; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
84
|
|
10
|
1
|
|
|
1
|
|
6
|
use Carp; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
82
|
|
11
|
|
|
|
|
|
|
|
12
|
1
|
|
|
1
|
|
6
|
use base 'Query::Abstract::Driver::Base'; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
656
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
my %CONVERTORS = ( |
15
|
|
|
|
|
|
|
'eq' => sub {"$_[0] = ?" }, |
16
|
|
|
|
|
|
|
'ne' => sub {"$_[0] <> ?" }, |
17
|
|
|
|
|
|
|
'lt' => sub {"$_[0] < ?" }, |
18
|
|
|
|
|
|
|
'le' => sub {"$_[0] <= ?" }, |
19
|
|
|
|
|
|
|
'gt' => sub {"$_[0] > ?" }, |
20
|
|
|
|
|
|
|
'ge' => sub {"$_[0] >= ?" }, |
21
|
|
|
|
|
|
|
'<' => sub {"$_[0] < ?" }, |
22
|
|
|
|
|
|
|
'>' => sub {"$_[0] > ?" }, |
23
|
|
|
|
|
|
|
'<=' => sub {"$_[0] <= ?" }, |
24
|
|
|
|
|
|
|
'>=' => sub {"$_[0] >= ?" }, |
25
|
|
|
|
|
|
|
'in' => sub {"$_[0] IN (" . join(', ', ('?') x @{$_[1]} ) . ")"}, |
26
|
|
|
|
|
|
|
'like' => sub {"$_[0] LIKE ?" }, |
27
|
|
|
|
|
|
|
); |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
sub new { |
30
|
1
|
|
|
1
|
0
|
4
|
my ( $class, %args ) = @_; |
31
|
1
|
|
|
|
|
8
|
my $self = $class->SUPER::new(%args); |
32
|
1
|
50
|
|
|
|
14
|
croak "Table name is required!" unless $self->{table}; |
33
|
|
|
|
|
|
|
|
34
|
1
|
|
|
|
|
8
|
return $self; |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
sub convert_query { |
38
|
15
|
|
|
15
|
0
|
26
|
my ($self, %query) = @_; |
39
|
|
|
|
|
|
|
|
40
|
15
|
|
|
|
|
28
|
my $select_sql = "SELECT * FROM $self->{table}"; |
41
|
|
|
|
|
|
|
|
42
|
15
|
|
|
|
|
29
|
my ($where_sql, $bind_values) = $self->convert_filter( $query{where} ); |
43
|
15
|
50
|
|
|
|
49
|
$select_sql .= " $where_sql" if $where_sql; |
44
|
|
|
|
|
|
|
|
45
|
15
|
|
|
|
|
35
|
my $sort_sql = $self->convert_sort( $query{sort_by} ); |
46
|
15
|
100
|
|
|
|
33
|
$select_sql .= " $sort_sql" if $sort_sql; |
47
|
|
|
|
|
|
|
|
48
|
15
|
|
|
|
|
74
|
return( $select_sql, $bind_values ); |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
sub convert_filter { |
53
|
15
|
|
|
15
|
0
|
18
|
my ( $self, $where ) = @_; |
54
|
|
|
|
|
|
|
|
55
|
15
|
|
|
|
|
13
|
my @rules; |
56
|
|
|
|
|
|
|
my @bind_values; |
57
|
15
|
|
|
|
|
38
|
for ( my $i = 0; $i < @$where; $i += 2 ) { |
58
|
19
|
|
|
|
|
21
|
my $field = $where->[$i]; |
59
|
19
|
|
|
|
|
20
|
my $condition = $where->[$i+1]; |
60
|
19
|
|
|
|
|
32
|
my ($oper, $values) = %$condition; |
61
|
|
|
|
|
|
|
|
62
|
19
|
|
|
|
|
51
|
push @rules, $CONVERTORS{$oper}->($field, $values); |
63
|
19
|
100
|
|
|
|
26
|
push @bind_values, @{ ref($values) ? $values : [$values] }; |
|
19
|
|
|
|
|
77
|
|
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
15
|
50
|
|
|
|
31
|
if (@rules) { |
67
|
15
|
|
|
|
|
27
|
my $where_str = 'WHERE ' . join(' AND ', @rules); |
68
|
15
|
|
|
|
|
64
|
return( $where_str, \@bind_values ); |
69
|
|
|
|
|
|
|
} else { |
70
|
0
|
|
|
|
|
0
|
return ''; |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
sub convert_sort { |
75
|
15
|
|
|
15
|
0
|
26
|
my ( $self, $sort_by ) = @_; |
76
|
15
|
|
|
|
|
14
|
my @rules; |
77
|
|
|
|
|
|
|
|
78
|
15
|
|
|
|
|
29
|
foreach my $sort_rule ( @$sort_by ) { |
79
|
3
|
|
|
|
|
8
|
my ($field, $order) = split(/\s+/, $sort_rule, 2); |
80
|
3
|
|
100
|
|
|
9
|
$order ||='ASC'; |
81
|
|
|
|
|
|
|
|
82
|
3
|
|
|
|
|
9
|
push @rules, "$field \U$order\E"; |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
|
85
|
15
|
100
|
|
|
|
24
|
if (@rules) { |
86
|
2
|
50
|
|
|
|
7
|
return @rules ? 'ORDER BY ' . join(' ,', @rules) : ''; |
87
|
|
|
|
|
|
|
} else { |
88
|
13
|
|
|
|
|
23
|
return ''; |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
1; |