line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Email::Archive::Storage::DBI; |
2
|
3
|
|
|
3
|
|
21
|
use Moo; |
|
3
|
|
|
|
|
10
|
|
|
3
|
|
|
|
|
19
|
|
3
|
3
|
|
|
3
|
|
966
|
use Carp; |
|
3
|
|
|
|
|
8
|
|
|
3
|
|
|
|
|
228
|
|
4
|
3
|
|
|
3
|
|
8421
|
use DBI; |
|
3
|
|
|
|
|
64299
|
|
|
3
|
|
|
|
|
273
|
|
5
|
3
|
|
|
3
|
|
10324
|
use File::ShareDir 'module_file'; |
|
3
|
|
|
|
|
19295
|
|
|
3
|
|
|
|
|
333
|
|
6
|
3
|
|
|
3
|
|
3239
|
use File::Slurp 'read_file'; |
|
3
|
|
|
|
|
51708
|
|
|
3
|
|
|
|
|
253
|
|
7
|
3
|
|
|
3
|
|
85
|
use Email::MIME; |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
69
|
|
8
|
3
|
|
|
3
|
|
2812
|
use Email::Abstract; |
|
3
|
|
|
|
|
92084
|
|
|
3
|
|
|
|
|
137
|
|
9
|
3
|
|
|
3
|
|
3828
|
use SQL::Abstract; |
|
3
|
|
|
|
|
32843
|
|
|
3
|
|
|
|
|
160
|
|
10
|
3
|
|
|
3
|
|
43
|
use Scalar::Util qw(looks_like_number); |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
236
|
|
11
|
3
|
|
|
3
|
|
3088
|
use autodie; |
|
3
|
|
|
|
|
55456
|
|
|
3
|
|
|
|
|
21
|
|
12
|
|
|
|
|
|
|
with q/Email::Archive::Storage/; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
has sqla => ( |
15
|
|
|
|
|
|
|
is => 'ro', |
16
|
|
|
|
|
|
|
isa => sub { |
17
|
|
|
|
|
|
|
ref $_[0] eq 'SQL::Abstract' or die "sqla must be a SQL::Abstract object" |
18
|
|
|
|
|
|
|
}, |
19
|
|
|
|
|
|
|
lazy => 1, |
20
|
|
|
|
|
|
|
default => sub { SQL::Abstract->new }, |
21
|
|
|
|
|
|
|
handles => [qw/ |
22
|
|
|
|
|
|
|
select |
23
|
|
|
|
|
|
|
insert |
24
|
|
|
|
|
|
|
/], |
25
|
|
|
|
|
|
|
); |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
has dbh => ( |
28
|
|
|
|
|
|
|
is => 'rw', |
29
|
|
|
|
|
|
|
isa => sub { |
30
|
|
|
|
|
|
|
ref $_[0] eq 'DBI::db' or die "dbh must be a DBI handle", |
31
|
|
|
|
|
|
|
}, |
32
|
|
|
|
|
|
|
handles => [qw/ |
33
|
|
|
|
|
|
|
prepare |
34
|
|
|
|
|
|
|
do |
35
|
|
|
|
|
|
|
/], |
36
|
|
|
|
|
|
|
); |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
has deployed_schema_version => ( |
39
|
|
|
|
|
|
|
is => 'rw', |
40
|
|
|
|
|
|
|
isa => sub { |
41
|
|
|
|
|
|
|
looks_like_number($_[0]) or die "deployed_schema_version must be integer" |
42
|
|
|
|
|
|
|
}, |
43
|
|
|
|
|
|
|
default => sub { 0 }, |
44
|
|
|
|
|
|
|
); |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
my $SCHEMA_VERSION = 1; |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
sub store { |
50
|
0
|
|
|
0
|
0
|
|
my ($self, $email) = @_; |
51
|
|
|
|
|
|
|
# passing an E::A to E::A->new is perfectly valid |
52
|
0
|
|
|
|
|
|
$email = Email::Abstract->new($email); |
53
|
0
|
|
|
|
|
|
my $fields = { |
54
|
|
|
|
|
|
|
from_addr => $email->get_header('From'), |
55
|
|
|
|
|
|
|
to_addr => $email->get_header('To'), |
56
|
|
|
|
|
|
|
date => $email->get_header('Date'), |
57
|
|
|
|
|
|
|
subject => $email->get_header('Subject'), |
58
|
|
|
|
|
|
|
message_id => $email->get_header('Message-ID'), |
59
|
|
|
|
|
|
|
body => $email->get_body, |
60
|
|
|
|
|
|
|
}; |
61
|
0
|
|
|
|
|
|
my ($sql, @bind) = $self->insert('messages', $fields); |
62
|
0
|
|
|
|
|
|
my $sth = $self->prepare($sql); |
63
|
0
|
|
|
|
|
|
$sth->execute(@bind); |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
sub search { |
67
|
0
|
|
|
0
|
0
|
|
my ($self, $attribs) = shift; |
68
|
0
|
|
|
|
|
|
my ($sql, @bind) = $self->select('messages', [qw/message_id from_addr to_addr date subject body/], $attribs); |
69
|
0
|
|
|
|
|
|
my $sth = $self->prepare($sql); |
70
|
0
|
|
|
|
|
|
$sth->execute(@bind); |
71
|
0
|
|
|
|
|
|
my ($message) = $sth->fetchrow_hashref; |
72
|
0
|
|
|
|
|
|
return Email::MIME->create( |
73
|
|
|
|
|
|
|
header => [ |
74
|
|
|
|
|
|
|
From => $message->{from_addr}, |
75
|
|
|
|
|
|
|
To => $message->{to_addr}, |
76
|
|
|
|
|
|
|
Subject => $message->{subject}, |
77
|
|
|
|
|
|
|
], |
78
|
|
|
|
|
|
|
body => $message->{body}, |
79
|
|
|
|
|
|
|
); |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
sub retrieve { |
83
|
0
|
|
|
0
|
0
|
|
my ($self, $message_id) = shift; |
84
|
0
|
|
|
|
|
|
$self->search({message_id => $message_id}); |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
sub _deploy { |
88
|
0
|
|
|
0
|
|
|
my ($self) = @_; |
89
|
0
|
|
|
|
|
|
my $schema = module_file('Email::Archive::Storage::DBI', 'latest_schema.txt'); |
90
|
0
|
|
|
|
|
|
my $sql = read_file($schema); |
91
|
0
|
|
|
|
|
|
$self->do($sql); |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
sub _deployed { |
95
|
0
|
|
|
0
|
|
|
my ($self) = @_; |
96
|
0
|
|
|
|
|
|
my $schema_version = eval { $self->selectcol_array('SELECT schema_version FROM metadata') }; |
|
0
|
|
|
|
|
|
|
97
|
0
|
0
|
0
|
|
|
|
if(defined $schema_version and $schema_version =~ /^\d+$/) { |
98
|
0
|
|
|
|
|
|
$self->deployed_schema_version($schema_version); |
99
|
0
|
|
|
|
|
|
return $schema_version =~ /^\d+$/; |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
sub storage_connect { |
104
|
0
|
|
|
0
|
0
|
|
my ($self, $dsn) = @_; |
105
|
0
|
|
|
|
|
|
$self->dbh(DBI->connect($dsn)); |
106
|
0
|
0
|
|
|
|
|
if(!$self->_deployed) { |
|
|
0
|
|
|
|
|
|
107
|
0
|
|
|
|
|
|
$self->_deploy; |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
elsif(!$self->_is_latest_schema) { |
110
|
0
|
|
|
|
|
|
croak sprintf "Schema version %d not supported; we support version " . |
111
|
|
|
|
|
|
|
"$SCHEMA_VERSION. Please upgrade your schema before " . |
112
|
|
|
|
|
|
|
"continuing.", $self->_deployed_schema_version; |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
1; |