line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package SQL::Load; |
2
|
|
|
|
|
|
|
|
3
|
5
|
|
|
5
|
|
271932
|
use strict; |
|
5
|
|
|
|
|
50
|
|
|
5
|
|
|
|
|
145
|
|
4
|
5
|
|
|
5
|
|
25
|
use warnings; |
|
5
|
|
|
|
|
9
|
|
|
5
|
|
|
|
|
137
|
|
5
|
5
|
|
|
5
|
|
25
|
use Carp; |
|
5
|
|
|
|
|
10
|
|
|
5
|
|
|
|
|
302
|
|
6
|
5
|
|
|
|
|
287
|
use SQL::Load::Util qw/ |
7
|
|
|
|
|
|
|
name_list |
8
|
|
|
|
|
|
|
remove_extension |
9
|
5
|
|
|
5
|
|
2018
|
/; |
|
5
|
|
|
|
|
16
|
|
10
|
5
|
|
|
5
|
|
1974
|
use SQL::Load::Method; |
|
5
|
|
|
|
|
13
|
|
|
5
|
|
|
|
|
5342
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our $VERSION = '0.04'; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
sub new { |
15
|
4
|
|
|
4
|
1
|
1302
|
my ($class, $path) = @_; |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
# valid if path exists |
18
|
4
|
50
|
|
|
|
20
|
croak "Path not defined!" unless $path; |
19
|
4
|
50
|
|
|
|
81
|
croak "The '$path' path does not exist!" unless -d $path; |
20
|
|
|
|
|
|
|
|
21
|
4
|
|
|
|
|
28
|
my $self = { |
22
|
|
|
|
|
|
|
_path => $path, |
23
|
|
|
|
|
|
|
_data => {}, |
24
|
|
|
|
|
|
|
_keys => {} |
25
|
|
|
|
|
|
|
}; |
26
|
|
|
|
|
|
|
|
27
|
4
|
|
|
|
|
19
|
return bless $self, $class; |
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
sub load { |
31
|
27
|
|
|
27
|
1
|
1666
|
my ($self, $file_name, $reload) = @_; |
32
|
|
|
|
|
|
|
|
33
|
27
|
|
|
|
|
36
|
my $name; |
34
|
|
|
|
|
|
|
|
35
|
27
|
100
|
|
|
|
148
|
if ($file_name =~ /^([\w\-\/\.]+)\#([\w\-]+)$/) { |
36
|
18
|
|
|
|
|
58
|
$file_name = remove_extension($1); |
37
|
18
|
|
|
|
|
40
|
$name = $2; |
38
|
|
|
|
|
|
|
} else { |
39
|
9
|
|
|
|
|
32
|
$file_name = remove_extension($file_name); |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
27
|
50
|
33
|
|
|
174
|
if ($file_name && $file_name =~ /^[\w\-\/]+$/) { |
43
|
|
|
|
|
|
|
# if true not get tmp |
44
|
27
|
100
|
|
|
|
61
|
unless ($reload) { |
45
|
|
|
|
|
|
|
# check if exist the key to get tmp |
46
|
25
|
|
|
|
|
60
|
my $key = $self->_key($file_name); |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
# check if tmp exists, if true return |
49
|
25
|
100
|
|
|
|
58
|
if ($key) { |
50
|
19
|
100
|
|
|
|
57
|
return $name |
51
|
|
|
|
|
|
|
? $self->_get_tmp($key)->name($name) |
52
|
|
|
|
|
|
|
: $self->_get_tmp($key); |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
# get name list |
57
|
8
|
|
|
|
|
24
|
my $name_list = name_list($file_name); |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
# get file from name list |
60
|
8
|
|
|
|
|
24
|
my $file = $self->_find_file($name_list); |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
# get content |
63
|
8
|
|
|
|
|
28
|
my $content = $self->_file_content($file); |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
# set tmp |
66
|
8
|
|
|
|
|
38
|
$self->_set_tmp($content, $file, $name_list); |
67
|
|
|
|
|
|
|
|
68
|
8
|
50
|
|
|
|
53
|
return $name |
69
|
|
|
|
|
|
|
? SQL::Load::Method->new($content)->name($name) |
70
|
|
|
|
|
|
|
: SQL::Load::Method->new($content); |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
0
|
|
|
|
|
0
|
croak "the name '$file_name' is invalid!"; |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
sub reload { |
77
|
1
|
|
|
1
|
1
|
702
|
my ($self, $file_name) = @_; |
78
|
|
|
|
|
|
|
|
79
|
1
|
|
|
|
|
4
|
return $self->load($file_name, 1); |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
sub _find_file { |
83
|
8
|
|
|
8
|
|
54
|
my ($self, $name_list) = @_; |
84
|
|
|
|
|
|
|
|
85
|
8
|
|
|
|
|
23
|
my $file; |
86
|
|
|
|
|
|
|
|
87
|
8
|
|
|
|
|
22
|
for my $name (@$name_list) { |
88
|
16
|
|
|
|
|
45
|
my $is_file = $self->{_path} . '/' . $name . '.sql'; |
89
|
|
|
|
|
|
|
|
90
|
16
|
100
|
|
|
|
332
|
if (-e $is_file) { |
91
|
8
|
|
|
|
|
24
|
$file = $is_file; |
92
|
|
|
|
|
|
|
|
93
|
8
|
|
|
|
|
51
|
last; |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
8
|
50
|
|
|
|
62
|
last if $file; |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
|
99
|
8
|
50
|
|
|
|
30
|
return $file if $file; |
100
|
|
|
|
|
|
|
|
101
|
0
|
|
|
|
|
0
|
croak "The file does not exist!"; |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
sub _file_content { |
105
|
8
|
|
|
8
|
|
21
|
my ($self, $file) = @_; |
106
|
|
|
|
|
|
|
|
107
|
8
|
|
|
|
|
12
|
my $content = ''; |
108
|
|
|
|
|
|
|
|
109
|
8
|
50
|
|
|
|
256
|
open FH, '<', $file or croak $!; |
110
|
8
|
|
|
|
|
289
|
while () { |
111
|
86
|
|
|
|
|
214
|
$content .= $_; |
112
|
|
|
|
|
|
|
} |
113
|
8
|
|
|
|
|
81
|
close FH; |
114
|
|
|
|
|
|
|
|
115
|
8
|
|
|
|
|
299
|
$content =~ s/^\s+|\s+$//g; |
116
|
|
|
|
|
|
|
|
117
|
8
|
|
|
|
|
32
|
return $content; |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
sub _key { |
121
|
25
|
|
|
25
|
|
50
|
my ($self, $name) = @_; |
122
|
|
|
|
|
|
|
|
123
|
25
|
100
|
|
|
|
99
|
return $self->{_keys}->{$name} if exists $self->{_keys}->{$name}; |
124
|
|
|
|
|
|
|
|
125
|
6
|
|
|
|
|
18
|
return; |
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
sub _generate_key { |
129
|
8
|
|
|
8
|
|
99
|
my @characters = ('0'..'9', 'A'..'Z', 'a'..'z'); |
130
|
8
|
|
|
|
|
26
|
my $x = int scalar @characters; |
131
|
8
|
|
|
|
|
226
|
my $result = join '', map $characters[rand $x], 1..16; |
132
|
|
|
|
|
|
|
|
133
|
8
|
|
|
|
|
49
|
return $result; |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
sub _get_tmp { |
137
|
19
|
|
|
19
|
|
33
|
my ($self, $key) = @_; |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
return SQL::Load::Method->new($self->{_data}->{$key}->{content}) |
140
|
19
|
50
|
|
|
|
79
|
if exists $self->{_data}->{$key}->{content}; |
141
|
|
|
|
|
|
|
|
142
|
0
|
|
|
|
|
0
|
return; |
143
|
|
|
|
|
|
|
} |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
sub _set_tmp { |
146
|
8
|
|
|
8
|
|
28
|
my ($self, $content, $file, $name_list) = @_; |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
# generate new key |
149
|
8
|
|
|
|
|
17
|
my $key = $self->_generate_key; |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
# save name => key in tmp keys |
152
|
8
|
|
|
|
|
89
|
$self->{_keys}->{$_} = $key for @$name_list; |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
# save data in tmp data |
155
|
8
|
|
|
|
|
31
|
$self->{_data}->{$key}->{content} = $content; |
156
|
|
|
|
|
|
|
} |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
1; |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
__END__ |