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