lib/Pg/Loader/Misc_2.pm

filter: Statementstmt 48.9% 48 / 98 Branchbran 51.8% 28 / 54 Conditioncond 37.3% 31 / 83 MC/DCmcdc 37.5% 18 / 48 Subroutinesub 69.2% 9 / 13 Podpod 0.0% 0 / 3 totaltotal 44.8% 134 / 299 CC 64 SCAR 56.6
CC
64
Cov
46%
add_defaults
756.0
show_sections
56.0
_merge_with_template
30.0
CRAP 286.2
↑ Summary lib/Pg/Loader/Log.pm »
1 # Copyright (C) 2008 Ioannis Tambouras <ioannis@cpan.org>. All rights reserved.
2 # LICENSE: GPLv3, eead licensing terms at http://www.fsf.org .
3  
4 package Pg::Loader::Misc_2;
5  
6 5 ▶use 5.010000;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 #use Config::Format::Ini;
11 5 ▶use Log::Log4perl ':easy';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 #use Text::CSV;
13 #use Pg::Loader::Columns;
14 #use Pg::Loader::Query;
15 5 ▶use Pg::Loader::Log;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 #use List::MoreUtils qw( firstidx );
17 5 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
18 #use Quantum::Superpositions ;
19 5 ▶use Text::Table;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21  
22 5 our $VERSION = '0.01';
23  
24 5 our @EXPORT = qw(
25 sample_config show_sections add_defaults
26 );
27  
28 0 ▶sub sample_config {
Subroutine sample_config: not called CC=1 SCAR=6.9
Pod undocumented
29 0 (my $tmp = <<EOM ) =~ s/^\t//gmo ;
30 [pgsql]
31 base = people
32 pass = apple
33 #host = localhost
34 #pgsysconfdir=.
35 #service=
36  
37 [exam]
38 filename = exam.dat
39 table = public.exam
40 #copy = *
41 #copy_columns = id, name
42 #only_cols = 1-2,4,5
43 #use_template = cvs1
44 #copy_every=10000
45  
46 [cvs1]
47 #template=true
48 #format=cvs
49 #doublequote=false
50 #escapechar=|
51 #quotechar="
52 #skipinitialspace=true
53 #reject_log=rej_log
54 #reject_data=rej_data
55 #reformat= fn:John::Util::jupper, score:John::Util::changed
56 #null=\\NA
57 #trailing_sep=true
58 #datestyle=euro
59 #client_encoding=
60 #lc_messages=C
61 #lc_numeric=C
62 #lc_monetary=en_US
63 #lc_type=C
64 #lc_time=POSIX
65  
66 EOM
67 0 $tmp;
68 }
69  
70  
71  
72  
73 0 ▶sub _merge_with_template {
Subroutine _merge_with_template: not called CC=5 SCAR=34.0
74 ## Output: add columns into $s
75 0 my ( $s, $ini, $template) = @_;
76 0 ▶ return unless $template;
Branch: 1 branch
BranchTrueFalseTotal
unless $template 0 0 0
77 0 ▶ LOGEXIT "Missing template [$template]" unless $ini->{$template};
Branch: 1 branch
BranchTrueFalseTotal
unless $ini->{$template} 0 0 0
78 0 ▶ $s->{$_} //= $ini->{$template}{$_} for keys %{$ini->{$template}};
Condition0%0/3
$s->{$_} //= $ini->{$template}{$_}
l!l&&r!l&&!r
000
MC/DC0%0/2
$s->{$_} //= $ini->{$template}{$_}
$s->{$_} $ini->{$template}{$_}
Truth table0%0/3
A //= B
A= $s->{$_}B= $ini->{$template}{$_}
ABresult
1X1
011
000
79 }
80  
81 0 ▶sub add_defaults {
Subroutine add_defaults: not called CC=27 SCAR=66.3
Pod undocumented
82 0 my ( $ini, $section) = @_ ;
83 0 ▶ LOGEXIT "invalid section name" unless $section ;
Branch: 1 branch
BranchTrueFalseTotal
unless $section 0 0 0
84 0 my $s = $ini->{$section} ;
85 0 ▶ LOGEXIT "Missing section [$section]" unless $s ;
Branch: 1 branch
BranchTrueFalseTotal
unless $s 0 0 0
86 0 _merge_with_template( $s, $ini, $s->{use_template} ) ;
87 0 _switch_2_update( $s );
88  
89 0 ▶ $s->{ format } //= 'text' ;
Condition0%0/2
$s->{"format"} //= "text"
l!l
00
MC/DC0%0/1
$s->{"format"} //= "text"
$s->{"format"}
Truth table0%0/2
A
A= $s->{"format"}
Aresult
11
00
90 0 ▶ $s->{ copy } //= '*' ;
Condition0%0/2
$s->{"copy"} //= "*"
l!l
00
MC/DC0%0/1
$s->{"copy"} //= "*"
$s->{"copy"}
Truth table0%0/2
A
A= $s->{"copy"}
Aresult
11
00
91 0 ▶ $s->{ copy_every } //= 10_000 ;
Condition0%0/2
$s->{"copy_every"} //= 10000
l!l
00
MC/DC0%0/1
$s->{"copy_every"} //= 10000
$s->{"copy_every"}
Truth table0%0/2
A
A= $s->{"copy_every"}
Aresult
11
00
92 0 ▶ $s->{ filename } //= 'STDIN' ;
Condition0%0/2
$s->{"filename"} //= "STDIN"
l!l
00
MC/DC0%0/1
$s->{"filename"} //= "STDIN"
$s->{"filename"}
Truth table0%0/2
A
A= $s->{"filename"}
Aresult
11
00
93 0 ▶ $s->{ table } //= $section ;
Condition0%0/3
$s->{"table"} //= $section
l!l&&r!l&&!r
000
MC/DC0%0/2
$s->{"table"} //= $section
$s->{"table"} $section
Truth table0%0/3
A //= B
A= $s->{"table"}B= $section
ABresult
1X1
011
000
94 0 ▶ $s->{ quotechar } //= '"' ;
Condition0%0/2
$s->{"quotechar"} //= "\""
l!l
00
MC/DC0%0/1
$s->{"quotechar"} //= "\""
$s->{"quotechar"}
Truth table0%0/2
A
A= $s->{"quotechar"}
Aresult
11
00
95 0 ▶ $s->{ reject_data } //= '' ;
Condition0%0/2
$s->{"reject_data"} //= ""
l!l
00
MC/DC0%0/1
$s->{"reject_data"} //= ""
$s->{"reject_data"}
Truth table0%0/2
A
A= $s->{"reject_data"}
Aresult
11
00
96 0 ▶ $s->{ reject_log } //= '' ;
Condition0%0/2
$s->{"reject_log"} //= ""
l!l
00
MC/DC0%0/1
$s->{"reject_log"} //= ""
$s->{"reject_log"}
Truth table0%0/2
A
A= $s->{"reject_log"}
Aresult
11
00
97 0 ▶ $s->{ lc_messages } //= '' ;
Condition0%0/2
$s->{"lc_messages"} //= ""
l!l
00
MC/DC0%0/1
$s->{"lc_messages"} //= ""
$s->{"lc_messages"}
Truth table0%0/2
A
A= $s->{"lc_messages"}
Aresult
11
00
98 0 ▶ $s->{ lc_numeric } //= '' ;
Condition0%0/2
$s->{"lc_numeric"} //= ""
l!l
00
MC/DC0%0/1
$s->{"lc_numeric"} //= ""
$s->{"lc_numeric"}
Truth table0%0/2
A
A= $s->{"lc_numeric"}
Aresult
11
00
99 0 ▶ $s->{ lc_monetary } //= '' ;
Condition0%0/2
$s->{"lc_monetary"} //= ""
l!l
00
MC/DC0%0/1
$s->{"lc_monetary"} //= ""
$s->{"lc_monetary"}
Truth table0%0/2
A
A= $s->{"lc_monetary"}
Aresult
11
00
100 0 ▶ $s->{ lc_type } //= '' ;
Condition0%0/2
$s->{"lc_type"} //= ""
l!l
00
MC/DC0%0/1
$s->{"lc_type"} //= ""
$s->{"lc_type"}
Truth table0%0/2
A
A= $s->{"lc_type"}
Aresult
11
00
101 0 ▶ $s->{ lc_time } //= '' ;
Condition0%0/2
$s->{"lc_time"} //= ""
l!l
00
MC/DC0%0/1
$s->{"lc_time"} //= ""
$s->{"lc_time"}
Truth table0%0/2
A
A= $s->{"lc_time"}
Aresult
11
00
102 0 ▶ $s->{ datestyle } //= '' ;
Condition0%0/2
$s->{"datestyle"} //= ""
l!l
00
MC/DC0%0/1
$s->{"datestyle"} //= ""
$s->{"datestyle"}
Truth table0%0/2
A
A= $s->{"datestyle"}
Aresult
11
00
103 0 ▶ $s->{ client_encoding } //= '' ;
Condition0%0/2
$s->{"client_encoding"} //= ""
l!l
00
MC/DC0%0/1
$s->{"client_encoding"} //= ""
$s->{"client_encoding"}
Truth table0%0/2
A
A= $s->{"client_encoding"}
Aresult
11
00
104  
105 $s->{copy_columns} = $s->{copy}
106 0 ▶ unless ($s->{only_cols}||$s->{copy_columns});
Branch: 1 branch
BranchTrueFalseTotal
unless $s->{"only_cols"} or $s->{"copy_columns"} 0 0 0
Condition0%0/3
$s->{"only_cols"} or $s->{"copy_columns"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$s->{"only_cols"} or $s->{"copy_columns"}
$s->{"only_cols"} $s->{"copy_columns"}
Truth table0%0/3
A or B
A= $s->{"only_cols"}B= $s->{"copy_columns"}
ABresult
1X1
011
000
107  
108 0 ▶ if ( $s->{ format } =~ /^ '? text '?$/ox ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s->{'format'} =~ /^ '? text '?$/ox) { } 0 0 0
109 # format is 'text'
110 0 ▶ $s->{null} //= '$$\NA$$' ;
Condition0%0/2
$s->{"null"} //= "\$\$\\NA\$\$"
l!l
00
MC/DC0%0/1
$s->{"null"} //= "\$\$\\NA\$\$"
$s->{"null"}
Truth table0%0/2
A
A= $s->{"null"}
Aresult
11
00
111 0 ▶ $s->{null} = '$$'.$s->{null}.'$$' if $s->{null} ne '$$\NA$$';
Branch: 1 branch
BranchTrueFalseTotal
if $s->{"null"} ne "\$\$\\NA\$\$" 0 0 0
112 0 ▶ $s->{ field_sep } //= "\t" ;
Condition0%0/2
$s->{"field_sep"} //= "\t"
l!l
00
MC/DC0%0/1
$s->{"field_sep"} //= "\t"
$s->{"field_sep"}
Truth table0%0/2
A
A= $s->{"field_sep"}
Aresult
11
00
113 }else{
114 # format is 'csv'
115 0 ▶ $s->{null} //= '$$$$' ;
Condition0%0/2
$s->{"null"} //= "\$\$\$\$"
l!l
00
MC/DC0%0/1
$s->{"null"} //= "\$\$\$\$"
$s->{"null"}
Truth table0%0/2
A
A= $s->{"null"}
Aresult
11
00
116 0 ▶ $s->{null} = '$$'.$s->{null}.'$$' if $s->{null} ne '$$$$';
Branch: 1 branch
BranchTrueFalseTotal
if $s->{"null"} ne "\$\$\$\$" 0 0 0
117 0 ▶ $s->{ field_sep } //= ',' ;
Condition0%0/2
$s->{"field_sep"} //= ","
l!l
00
MC/DC0%0/1
$s->{"field_sep"} //= ","
$s->{"field_sep"}
Truth table0%0/2
A
A= $s->{"field_sep"}
Aresult
11
00
118 }
119 }
120  
121  
122  
123 17 ▶sub _switch_2_update {
Subroutine _switch_2_update: called CC=28 SCAR=33.4
124 17 my $s = shift;;
125 # First, some error checking
126 17 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $s->{"copy"} and $s->{"update"} and die; $s->{"copy"} and $s->{"update_columns"} and die; $s->{"copy"} and $s->{"update_only"} and die; $s->{"copy_columns"} and $s->{"update"} and die; $s->{"copy_columns"} and $s->{"update_columns"} and die; $s->{"copy_columns"} and $s->{"update_only"} and die; $s->{"copy_only"} and $s->{"update"} and die; $s->{"copy_only"} and $s->{"update_columns"} and die; $s->{"copy_only"} and $s->{"update_only"} and die; 1 } } or LOGDIE("\tCannot mix \"copy\" with \"update\" columns") 8 9 17
127 17 ▶ $s->{ copy } && $s->{ update } and die;
Branch: 1 branch
BranchTrueFalseTotal
$s->{"copy"} and $s->{"update"} and die 1 16 17
Condition100%3/3
$s->{"copy"} and $s->{"update"}
!ll&&!rl&&r
1331
MC/DC100%2/2
$s->{"copy"} and $s->{"update"}
$s->{"copy"} $s->{"update"}
Truth table100%3/3
A and B
A= $s->{"copy"}B= $s->{"update"}
ABresult
0X0
100
111
128 16 ▶ $s->{ copy } && $s->{ update_columns } and die;
Branch: 1 branch
BranchTrueFalseTotal
$s->{"copy"} and $s->{"update_columns"} and die 1 15 16
Condition100%3/3
$s->{"copy"} and $s->{"update_columns"}
!ll&&!rl&&r
1321
MC/DC100%2/2
$s->{"copy"} and $s->{"update_columns"}
$s->{"copy"} $s->{"update_columns"}
Truth table100%3/3
A and B
A= $s->{"copy"}B= $s->{"update_columns"}
ABresult
0X0
100
111
129 15 ▶ $s->{ copy } && $s->{ update_only } and die;
Branch: 1 branch
BranchTrueFalseTotal
$s->{"copy"} and $s->{"update_only"} and die 1 14 15
Condition100%3/3
$s->{"copy"} and $s->{"update_only"}
!ll&&!rl&&r
1311
MC/DC100%2/2
$s->{"copy"} and $s->{"update_only"}
$s->{"copy"} $s->{"update_only"}
Truth table100%3/3
A and B
A= $s->{"copy"}B= $s->{"update_only"}
ABresult
0X0
100
111
130 14 ▶ $s->{ copy_columns } && $s->{ update } and die;
Branch: 1 branch
BranchTrueFalseTotal
$s->{"copy_columns"} and $s->{"update"} and die 1 13 14
Condition100%3/3
$s->{"copy_columns"} and $s->{"update"}
!ll&&!rl&&r
1121
MC/DC100%2/2
$s->{"copy_columns"} and $s->{"update"}
$s->{"copy_columns"} $s->{"update"}
Truth table100%3/3
A and B
A= $s->{"copy_columns"}B= $s->{"update"}
ABresult
0X0
100
111
131 13 ▶ $s->{ copy_columns } && $s->{ update_columns } and die;
Branch: 1 branch
BranchTrueFalseTotal
$s->{"copy_columns"} and $s->{"update_columns"} and die 1 12 13
Condition100%3/3
$s->{"copy_columns"} and $s->{"update_columns"}
!ll&&!rl&&r
1111
MC/DC100%2/2
$s->{"copy_columns"} and $s->{"update_columns"}
$s->{"copy_columns"} $s->{"update_columns"}
Truth table100%3/3
A and B
A= $s->{"copy_columns"}B= $s->{"update_columns"}
ABresult
0X0
100
111
132 12 ▶ $s->{ copy_columns } && $s->{ update_only } and die;
Branch: 1 branch
BranchTrueFalseTotal
$s->{"copy_columns"} and $s->{"update_only"} and die 1 11 12
Condition66%2/3
$s->{"copy_columns"} and $s->{"update_only"}
!ll&&!rl&&r
1101
MC/DC50%1/2
$s->{"copy_columns"} and $s->{"update_only"}
$s->{"copy_columns"} $s->{"update_only"}
Truth table66%2/3
A and B
A= $s->{"copy_columns"}B= $s->{"update_only"}
ABresult
0X0
100
111
133 11 ▶ $s->{ copy_only } && $s->{ update } and die;
Branch: 1 branch
BranchTrueFalseTotal
$s->{"copy_only"} and $s->{"update"} and die 1 10 11
Condition100%3/3
$s->{"copy_only"} and $s->{"update"}
!ll&&!rl&&r
821
MC/DC100%2/2
$s->{"copy_only"} and $s->{"update"}
$s->{"copy_only"} $s->{"update"}
Truth table100%3/3
A and B
A= $s->{"copy_only"}B= $s->{"update"}
ABresult
0X0
100
111
134 10 ▶ $s->{ copy_only } && $s->{ update_columns } and die;
Branch: 1 branch
BranchTrueFalseTotal
$s->{"copy_only"} and $s->{"update_columns"} and die 1 9 10
Condition100%3/3
$s->{"copy_only"} and $s->{"update_columns"}
!ll&&!rl&&r
811
MC/DC100%2/2
$s->{"copy_only"} and $s->{"update_columns"}
$s->{"copy_only"} $s->{"update_columns"}
Truth table100%3/3
A and B
A= $s->{"copy_only"}B= $s->{"update_columns"}
ABresult
0X0
100
111
135 9 ▶ $s->{ copy_only } && $s->{ update_only } and die;
Branch: 1 branch
BranchTrueFalseTotal
$s->{"copy_only"} and $s->{"update_only"} and die 1 8 9
Condition66%2/3
$s->{"copy_only"} and $s->{"update_only"}
!ll&&!rl&&r
801
MC/DC50%1/2
$s->{"copy_only"} and $s->{"update_only"}
$s->{"copy_only"} $s->{"update_only"}
Truth table66%2/3
A and B
A= $s->{"copy_only"}B= $s->{"update_only"}
ABresult
0X0
100
111
136 8 1;
137 } or LOGDIE qq(\tCannot mix "copy" with "update" columns) ;
138 #TODO "update_only" should populate "update_columns"
139 8 ▶ $s->{ update_only } and LOGDIE qq(\t"update_only" not implemeted");
Branch: 1 branch
BranchTrueFalseTotal
$s->{"update_only"} and LOGDIE("\t\"update_only\" not implemeted\"") 0 8 8
140 # Should we switch to update mode?
141 8 ▶ exists $s->{ update_columns } and $s->{mode}='update';
Branch: 1 branch
BranchTrueFalseTotal
exists $s->{"update_columns"} and $s->{"mode"} = "update" 3 5 8
142 8 ▶ exists $s->{ update_only } and $s->{mode}='update';
Branch: 1 branch
BranchTrueFalseTotal
exists $s->{"update_only"} and $s->{"mode"} = "update" 0 8 8
143 8 ▶ exists $s->{ update } and $s->{mode}='update';
Branch: 1 branch
BranchTrueFalseTotal
exists $s->{"update"} and $s->{"mode"} = "update" 3 5 8
144 ## key statement
145 8 ▶ $s->{mode} //= 'copy' ;
Condition100%2/2
$s->{"mode"} //= "copy"
l!l
53
MC/DC100%1/1
$s->{"mode"} //= "copy"
$s->{"mode"}
Truth table100%2/2
A
A= $s->{"mode"}
Aresult
11
00
146 8 ▶ if ($s->{mode} eq 'update') {
Branch: 1 branch
BranchTrueFalseTotal
if ($s->{"mode"} eq "update") 5 3 8
147 5 ▶ $s->{copy_columns} = $s->{update_columns}//$s->{update};
Condition66%2/3
$s->{"update_columns"} // $s->{"update"}
l!l&&r!l&&!r
320
MC/DC0%0/2
$s->{"update_columns"} // $s->{"update"}
$s->{"update_columns"} $s->{"update"}
Truth table66%2/3
A // B
A= $s->{"update_columns"}B= $s->{"update"}
ABresult
1X1
011
000
148 5 ▶ $s->{copy_only} = $s->{update_columns}//'';
Condition100%2/2
$s->{"update_columns"} // ""
l!l
32
MC/DC100%1/1
$s->{"update_columns"} // ""
$s->{"update_columns"}
Truth table100%2/2
A
A= $s->{"update_columns"}
Aresult
11
00
149 5 $s->{copy} = $s->{update};
150 }
151 8 $s->{mode};
152 }
153 0 ▶sub show_sections {
Subroutine show_sections: not called CC=7 SCAR=40.3
Pod undocumented
154 0 my ($conf, $ini) = @_;
155 0 ▶ my $port = ':'. ($ini->{port}||5432) ;
Condition0%0/2
$ini->{'port'} || 5432
l!l
00
MC/DC0%0/1
$ini->{'port'} || 5432
$ini->{'port'}
Truth table0%0/2
A
A= $ini->{'port'}
Aresult
11
00
156 0 my $t = new Text::Table 'SECTION ' , 'TABLE ',
157 'FILE ' , 'OPERATION' ;
158 0 DEBUG "$ini->{pgsql}{base}\@$ini->{pgsql}{host}$port" ;
159 0 ▶ while (my ($k,$v) = each %$ini) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$ini) 0 0 0
160 0 my $s = $ini->{$k};
161 0 ▶ next if $k eq 'pgsql';
Branch: 1 branch
BranchTrueFalseTotal
if $k eq "pgsql" 0 0 0
162 0 ▶ next if exists $s->{template};
Branch: 1 branch
BranchTrueFalseTotal
if exists $s->{"template"} 0 0 0
163 0 ▶ next if $s->{template};
Branch: 1 branch
BranchTrueFalseTotal
if $s->{"template"} 0 0 0
164 0 add_defaults $ini, $k ;
165 0 _switch_2_update $s ;
166 0 ▶ my $file = $s->{filename}||'STDIN';
Condition0%0/2
$s->{'filename'} || 'STDIN'
l!l
00
MC/DC0%0/1
$s->{'filename'} || 'STDIN'
$s->{'filename'}
Truth table0%0/2
A
A= $s->{'filename'}
Aresult
11
00
167 #say sprintf '%-18s %-20s', "[$k]", $file ;
168 0 $t->load( [ "[$k]", $s->{table}, $file, $s->{mode} ] );
169 }
170 0 print $t;
171 }
172  
173 5 1;
174 __END__
↑ Summary lib/Pg/Loader/Log.pm »