line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package DataWarehouse::ETL; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
1615
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
31
|
|
4
|
1
|
|
|
1
|
|
14
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
180
|
|
5
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
6
|
use Carp; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
79
|
|
7
|
1
|
|
|
1
|
|
6
|
use Data::Dumper; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
45
|
|
8
|
1
|
|
|
1
|
|
441
|
use DBI; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
sub new { |
11
|
|
|
|
|
|
|
my ( $class, %param ) = @_; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
croak "Error: One of 'dbh' or 'dsn' parameters is required" if !($param{dbh} xor $param{dsn}); |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
if ( $param{dsn} ) { |
16
|
|
|
|
|
|
|
$param{dbh} = DBI->connect( $param{dsn}, $param{db_user}, $param{db_password} ); |
17
|
|
|
|
|
|
|
} |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
bless \%param, $class; |
20
|
|
|
|
|
|
|
} |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
sub dbh { |
23
|
|
|
|
|
|
|
my $self = shift; |
24
|
|
|
|
|
|
|
return $self->{dbh}; |
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
sub initialize_dimension { |
28
|
|
|
|
|
|
|
my ( $self, %param ) = @_; |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
# mandatory |
31
|
|
|
|
|
|
|
my $TABLE = delete $param{TABLE} || croak "Missing parameter TABLE"; |
32
|
|
|
|
|
|
|
my $NATURAL_KEY = delete $param{NATURAL_KEY} || croak "Missing parameter NATURAL_KEY"; |
33
|
|
|
|
|
|
|
my $ATTRIBUTES = delete $param{ATTRIBUTES} || croak "Missing parameter ATTRIBUTES"; |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
$self->{$TABLE}->{NATURAL_KEY} = $NATURAL_KEY; |
36
|
|
|
|
|
|
|
$self->{$TABLE}->{ATTRIBUTES} = $ATTRIBUTES; |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
# optional |
39
|
|
|
|
|
|
|
$self->{$TABLE}->{TRANSFORM} = delete $param{TRANSFORM}; |
40
|
|
|
|
|
|
|
$self->{$TABLE}->{KEEP_HISTORY} = delete $param{KEEP_HISTORY}; |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
# build a (natural key => surrogate key) cache |
43
|
|
|
|
|
|
|
my $rows = $self->dbh->selectall_arrayref("SELECT $NATURAL_KEY, id FROM $TABLE"); |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
my %table_cache = map { $_->[0] => $_->[1] } @{$rows}; |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
$self->{$TABLE}{CACHE} = \%table_cache; |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
sub populate_fact { |
51
|
|
|
|
|
|
|
my ( $self, %param ) = @_; |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
my $TABLE = delete $param{TABLE} || croak "Missing parameter TABLE"; |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
my $sql = qq{ |
56
|
|
|
|
|
|
|
INSERT INTO $TABLE ( |
57
|
|
|
|
|
|
|
@{[ join(',', keys %param) ]} |
58
|
|
|
|
|
|
|
) VALUES ( |
59
|
|
|
|
|
|
|
@{[ join(',', map { qq{'$param{$_}'} } keys %param) ]} |
60
|
|
|
|
|
|
|
); |
61
|
|
|
|
|
|
|
}; |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
my $rv = $self->dbh->do($sql); |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
# we don't need to keep a cache of fact ids |
66
|
|
|
|
|
|
|
return 1; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
sub populate_dimension { |
70
|
|
|
|
|
|
|
my ( $self, %param ) = @_; |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
my $TABLE = delete $param{TABLE} || croak "Missing parameter TABLE"; |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
my $TRANSFORM = delete $param{TRANSFORM} || $self->{$TABLE}{TRANSFORM}; # optional |
75
|
|
|
|
|
|
|
if ( ref $TRANSFORM eq 'CODE' ) { |
76
|
|
|
|
|
|
|
%param = $TRANSFORM->(%param); |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
my $NATURAL_KEY = |
80
|
|
|
|
|
|
|
delete $param{NATURAL_KEY} |
81
|
|
|
|
|
|
|
|| $self->{$TABLE}{NATURAL_KEY} |
82
|
|
|
|
|
|
|
|| croak "Missing parameter NATURAL_KEY"; |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
my $SOURCE_SYSTEM_ID = $param{$NATURAL_KEY} || croak "Attribute '$NATURAL_KEY' can't be NULL"; |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
if ( my $cached = $self->{$TABLE}{CACHE}{$SOURCE_SYSTEM_ID} ) { |
87
|
|
|
|
|
|
|
return $cached; |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
my $sql = qq{ |
91
|
|
|
|
|
|
|
INSERT INTO $TABLE ( |
92
|
|
|
|
|
|
|
@{[ join(',', keys %param) ]} |
93
|
|
|
|
|
|
|
) VALUES ( |
94
|
|
|
|
|
|
|
@{[ join(',', map { qq{'$param{$_}'} } keys %param) ]} |
95
|
|
|
|
|
|
|
); |
96
|
|
|
|
|
|
|
}; |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
$self->dbh->do($sql); |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
my $last_insert_id = $self->dbh->last_insert_id( undef, undef, $TABLE, 'id' ); |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
$self->{$TABLE}{CACHE}{$SOURCE_SYSTEM_ID} = $last_insert_id; |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
return $last_insert_id; |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
sub drop_indexes { |
108
|
|
|
|
|
|
|
my ($self) = @_; |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
# drop indexes, if exist |
111
|
|
|
|
|
|
|
# |
112
|
|
|
|
|
|
|
# DROP INDEX request_day; |
113
|
|
|
|
|
|
|
# DROP INDEX request_method; |
114
|
|
|
|
|
|
|
# DROP INDEX request_user; |
115
|
|
|
|
|
|
|
# and so on... |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
sub create_indexes { |
120
|
|
|
|
|
|
|
my ($self) = @_; |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
# create indexes on fact table foreign keys: |
123
|
|
|
|
|
|
|
# |
124
|
|
|
|
|
|
|
# CREATE INDEX request_day ON request(day); |
125
|
|
|
|
|
|
|
# CREATE INDEX request_method ON request(method); |
126
|
|
|
|
|
|
|
# CREATE INDEX request_user ON request(user); |
127
|
|
|
|
|
|
|
# and so on... |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
} |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
1; |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
__END__ |