line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package DataWarehouse::Aggregate; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
2119
|
use warnings; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
40
|
|
4
|
1
|
|
|
1
|
|
6
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
37
|
|
5
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
6
|
use Carp; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
77
|
|
7
|
1
|
|
|
1
|
|
608
|
use DBI; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
sub new { |
10
|
|
|
|
|
|
|
my ( $class, %params ) = @_; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
croak "Error: One of 'dbh' or 'dsn' parameters is required" if !($params{dbh} xor $params{dsn}); |
13
|
|
|
|
|
|
|
croak "Error: missing base_table" if !$params{base_table}; |
14
|
|
|
|
|
|
|
croak "Error: missing dimension" if !$params{dimension}; |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
if ( $params{dsn} ) { |
17
|
|
|
|
|
|
|
$params{dbh} = DBI->connect( $params{dsn}, $params{db_user}, $params{db_password} ),; |
18
|
|
|
|
|
|
|
} |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
bless {%params}, $class; |
21
|
|
|
|
|
|
|
} |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
sub dimension { |
24
|
|
|
|
|
|
|
my ($self) = @_; |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
my $dimension = $self->{dimension}; |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
if ( ref $dimension eq 'ARRAY' ) { |
29
|
|
|
|
|
|
|
return @{$dimension}; |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
return $dimension; |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
sub name { |
36
|
|
|
|
|
|
|
my ($self) = @_; |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
my $base_table = $self->{base_table}; |
39
|
|
|
|
|
|
|
my @dimensions = $self->dimension; |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
my $name = "aggr_" . $self->{base_table} . "_" . join( '_', sort @dimensions ); |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
return $name; |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
sub create { |
47
|
|
|
|
|
|
|
my ($self) = @_; |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
my $base_table = $self->{base_table}; |
50
|
|
|
|
|
|
|
my @dimensions = $self->dimension; |
51
|
|
|
|
|
|
|
my $aggr_name = $self->name; |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
my $sql = <<"SQL"; |
54
|
|
|
|
|
|
|
CREATE TABLE IF NOT EXISTS $aggr_name AS |
55
|
|
|
|
|
|
|
SELECT |
56
|
|
|
|
|
|
|
@{[ join(', ', @dimensions ) ]}, |
57
|
|
|
|
|
|
|
SUM(n) AS n |
58
|
|
|
|
|
|
|
FROM |
59
|
|
|
|
|
|
|
$base_table |
60
|
|
|
|
|
|
|
JOIN |
61
|
|
|
|
|
|
|
@{[ join(",\n", map { $self->_join_str($_) } @dimensions) ]} |
62
|
|
|
|
|
|
|
GROUP BY |
63
|
|
|
|
|
|
|
@{[ join(", ", @dimensions) ]} |
64
|
|
|
|
|
|
|
SQL |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
warn "Creating aggregate...\n"; |
67
|
|
|
|
|
|
|
warn $sql; |
68
|
|
|
|
|
|
|
my $dbh = $self->{dbh}; |
69
|
|
|
|
|
|
|
$dbh->do($sql); |
70
|
|
|
|
|
|
|
warn "done\n"; |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
return $self; |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
sub list { |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
sub _join_str { |
79
|
|
|
|
|
|
|
my ( $self, $dim_table ) = @_; |
80
|
|
|
|
|
|
|
my $fact_table = $self->{base_table}; |
81
|
|
|
|
|
|
|
return " $dim_table ON $fact_table.$dim_table = $dim_table.id"; |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
1; |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
__END__ |