line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
################################################################################ |
2
|
|
|
|
|
|
|
# |
3
|
|
|
|
|
|
|
# Mojolicious::Plugin::AnyData 1.20 |
4
|
|
|
|
|
|
|
# |
5
|
|
|
|
|
|
|
# uses perl data in the memory like a database source. |
6
|
|
|
|
|
|
|
# author: Alexander Ponomarev |
7
|
|
|
|
|
|
|
# |
8
|
|
|
|
|
|
|
################################################################################ |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
package Mojolicious::Plugin::AnyData; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
BEGIN { |
13
|
1
|
|
|
1
|
|
45998
|
$Mojolicious::Plugin::AnyData::VERSION = '1.20'; |
14
|
|
|
|
|
|
|
} |
15
|
|
|
|
|
|
|
|
16
|
1
|
|
|
1
|
|
1124
|
use Mojo::Base 'Mojolicious::Plugin'; |
|
1
|
|
|
|
|
13601
|
|
|
1
|
|
|
|
|
212
|
|
17
|
1
|
|
|
1
|
|
6265
|
use DBI; |
|
1
|
|
|
|
|
24197
|
|
|
1
|
|
|
|
|
74
|
|
18
|
1
|
|
|
1
|
|
26
|
use v5.10; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
697
|
|
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
has 'dbh'; |
21
|
|
|
|
|
|
|
has 'app'; |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
# Run on startup |
24
|
|
|
|
|
|
|
sub register { |
25
|
0
|
|
|
0
|
1
|
|
my ($self, $app, $param) = @_; |
26
|
|
|
|
|
|
|
|
27
|
0
|
0
|
0
|
|
|
|
return if $app->mode && $app->mode eq 'production'; |
28
|
|
|
|
|
|
|
|
29
|
0
|
|
|
|
|
|
my ($data, $data_file); |
30
|
0
|
|
|
|
|
|
my $func = $param->{func}; |
31
|
0
|
|
|
|
|
|
my $helper = $param->{helper}; |
32
|
0
|
|
0
|
|
|
|
$helper ||= 'db'; |
33
|
0
|
|
|
|
|
|
my $dbh = DBI->connect('dbi:AnyData:(RaiseError=>1)'); |
34
|
|
|
|
|
|
|
|
35
|
0
|
|
|
|
|
|
$self->dbh($dbh); |
36
|
0
|
|
|
|
|
|
$self->app($app); |
37
|
|
|
|
|
|
|
|
38
|
0
|
0
|
|
|
|
|
if ( $param->{load_data} ) { |
39
|
0
|
|
|
|
|
|
$self->load_data( $param->{load_data} ); |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
0
|
0
|
0
|
|
|
|
if ( $func && ref $func eq 'ARRAY' && scalar @$func > 0 ) { |
|
|
|
0
|
|
|
|
|
43
|
|
|
|
|
|
|
#$dbh->func(@$func); |
44
|
0
|
|
|
|
|
|
$self->func(@$func); |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
0
|
|
|
0
|
|
|
$app->helper( $param->{helper} => sub { return $dbh } ); |
|
0
|
|
|
|
|
|
|
48
|
0
|
|
|
0
|
|
|
$app->helper( any_data => sub { return $self } ); |
|
0
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
# Load data into the memory using different ways |
52
|
|
|
|
|
|
|
# in: arrayref (data structure) or scalar (filename) |
53
|
|
|
|
|
|
|
sub load_data { |
54
|
0
|
|
|
0
|
1
|
|
my ($self, $data) = @_; |
55
|
|
|
|
|
|
|
|
56
|
0
|
0
|
0
|
|
|
|
return unless $self->dbh && $self->app; |
57
|
|
|
|
|
|
|
|
58
|
0
|
0
|
|
|
|
|
if ( ref $data ) { |
59
|
0
|
|
|
|
|
|
$self->ad_import($data); |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
else { |
62
|
0
|
|
|
|
|
|
my $data_file = $data; |
63
|
0
|
|
|
|
|
|
$data = $self->app->plugin(config => { |
64
|
|
|
|
|
|
|
file => $data_file, |
65
|
|
|
|
|
|
|
stash_key => 'any_data', |
66
|
|
|
|
|
|
|
}); |
67
|
0
|
|
|
|
|
|
$self->ad_import($data); |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
# Provide the wrapper method for DBD::AnyData::func method |
72
|
|
|
|
|
|
|
sub func { |
73
|
0
|
|
|
0
|
1
|
|
my ($self, $table_name, $table_type, $table_data, $table_method) = @_; |
74
|
|
|
|
|
|
|
|
75
|
0
|
0
|
|
|
|
|
return unless $self->dbh; |
76
|
|
|
|
|
|
|
|
77
|
0
|
|
|
|
|
|
$self->dbh->func($table_name, 'ad_clear'); |
78
|
0
|
|
|
|
|
|
$self->dbh->func( $table_name, $table_type, $table_data, $table_method ); |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
# Execute DBD::AnyData::func method in order to load data into the memory |
82
|
|
|
|
|
|
|
sub ad_import { |
83
|
0
|
|
|
0
|
0
|
|
my ($self, $data) = @_; |
84
|
|
|
|
|
|
|
|
85
|
0
|
0
|
|
|
|
|
return unless $self->dbh; |
86
|
|
|
|
|
|
|
|
87
|
0
|
0
|
0
|
|
|
|
if ( $data && ref $data eq 'HASH' && keys %$data > 0 ) { |
|
|
|
0
|
|
|
|
|
88
|
|
|
|
|
|
|
TABLE: |
89
|
0
|
|
|
|
|
|
for my $table_name ( keys %$data ) { |
90
|
0
|
0
|
|
|
|
|
next TABLE unless ref $data->{$table_name} eq 'ARRAY'; |
91
|
|
|
|
|
|
|
|
92
|
0
|
|
|
|
|
|
$self->dbh->func($table_name, 'ad_clear'); |
93
|
0
|
|
|
|
|
|
$self->dbh->func($table_name, 'ARRAY', $data->{$table_name}, 'ad_import'); |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
sub version { |
99
|
0
|
|
|
0
|
0
|
|
my ($self) = @_; |
100
|
|
|
|
|
|
|
|
101
|
0
|
|
|
|
|
|
return $Mojolicious::Plugin::AnyData::VERSION; |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
1; |
105
|
|
|
|
|
|
|
__END__ |