lib/Pg/Loader/Copy.pm

filter: Statementstmt 91.3% 21 / 23 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 8 / 8 Podpod n/a totaltotal 93.5% 29 / 31 CC 1 SCAR 0.4
CC
1
Cov
91%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« lib/Pg/Loader/Query.pm ↑ Summary lib/Pg/Loader/Update.pm »
1  
2 # Copyright (C) 2008 Ioannis Tambouras <ioannis@cpan.org>. All rights reserved.
3 # LICENSE: GPLv3, eead licensing terms at http://www.fsf.org .
4  
5 package Pg::Loader::Copy;
6  
7 3 ▶use 5.010000;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Fatal qw(open);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Time::HiRes qw( gettimeofday tv_interval );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 0 ▶use Pg::Loader::Query qw/ _truncate _disable_triggers _disable_indexes
Subroutine BEGIN: called CC=1 SCAR=2.6
15 0 vacuum_analyze pgoptions/;
16 use Pg::Loader::Misc qw/ error_check filter_ini insert_semantic_check
17 reformat_values add_modules /;
18 use Pg::Loader::Columns qw/ requested_cols init_csv combine/;
19 use Pg::Loader::Log qw/ log_rejected_data log_reject_errors/;
20 use Log::Log4perl qw( :easy );
21 use SQL::Abstract;
22 use Storable qw( dclone );
23 use base 'Exporter';
24  
25 our $VERSION = '0.17';
26  
27 our @EXPORT = qw( copy_loader load_2table _stop_input _no_pk_found) ;
28  
29  
30  
31  
32 sub _stop_input {
33 my ($conf, $rows) = @_ ;
34 ($rows//0) >= ($conf->{count} // '1E10') ;
35 }
36 sub _no_pk_found {
37 my ($ini,$section) = @_ ;
38 my $s = $ini->{$section};
39 my $msg = qq(\tTable $s->{table} )
40 . q(does not contain pk. Skipping...);
41 WARN( $msg ) ;
42 { name => $section, elapsed => 0,
43 rows => 0, errors => 'no pk',
44 size => $s->{copy_every},
45 }
46 }
47 sub copy_loader {
48 my ( $conf, $ini, $dh, $section ) = @_ ;
49 my $s = $ini->{$section} ;
50 my $dry = $conf->{dry} ;
51 INFO("COPY, as per [$section]") ;
52 $ini->{$section}{table} = $conf->{relation} if $conf->{relation} ;
53 $ini->{$section}{copy_every} = 1 if $conf->{every} ;
54 error_check( $ini, $section ) ;
55 filter_ini( $ini->{$section}, $dh ) ;
56 insert_semantic_check( $ini->{section} ) ;
57 reformat_values( $ini->{$section}, $dh ) ;
58 add_modules( $ini->{$section}, $dh ) ;
59  
60 load_2table ( dclone($conf), dclone($ini), $dh, $section ) ;
61 }
62  
63 sub load_2table {
64 my ( $conf, $ini, $dh, $section ) = @_ ;
65 my $s = $ini->{$section} ;
66 my ($dry , $r )= ( $conf->{dry}, $s->{copy_every} ) ;
67 my ($file, $format, $null, $table) =
68 @{$s}{'filename','format','null','table'} ;
69 my ($col, @col) = requested_cols( $s ) ;
70 INFO("\tData from $file") ;
71 my $fd = \* STDIN ;
72 open $fd, $file unless $file=~/^STDIN$/i ;
73 my $csv = init_csv( $s ) ;
74 $csv->column_names( @{$s->{copy}} ) ;
75  
76 local $dh->{ AutoCommit } = 0 ;
77 $dh->begin_work;
78 pgoptions( $dh, $s );
79 _truncate( $dh, $table, $dry ) if $conf->{truncate};
80 _disable_triggers( $dh, $table, $dry) if $conf->{disable_triggers};
81  
82 my ($t0, $data) = ([gettimeofday], 'true') ;
83 my ( $rows, $total, $errors);
84  
85 while ( $data ) {
86 ($rows,$data) = _insert($s, $dh, $col, $csv,
87 $fd, $section, $conf, @col );
88 $rows > 0 ? ($total+=$rows) : ($errors+=-$rows) ;
89 last unless $data ;
90 last if _stop_input($conf, $rows//0) ;
91 }
92 $total ? $dh->commit : $dh->rollback;
93 vacuum_analyze( $dh, $table, $dry ) if $conf->{vacuum};
94  
95 { name => $section, elapsed => tv_interval($t0),
96 rows => $total//0, errors => $errors//0,
97 size => $s->{copy_every},
98 }
99 }
100  
101  
102 sub _insert {
103 my ($s, $dh, $col, $csv, $fd, $section, $conf, @col ) = @_;
104 my ( $format, $null, $table) = @{$s}{'format','null','table'};
105 my ($dry , $r )= ( $conf->{dry}, $s->{copy_every} );
106  
107 $dh->pg_savepoint('every_block');
108 my $defs = _disable_indexes( $dh, $table) if $conf->{indexes};
109 my $sql = qq( COPY $table $col FROM STDIN )
110 . ($s->{format} eq 'csv' ? ' CSV ' : "" )
111 . qq( DELIMITER '$s->{field_sep}' NULL $s->{null} );
112  
113 DEBUG( "\t\t$sql" ) ;
114 $dh->do( $sql ) if (! $dry) ;
115  
116 ## Initialize $dh, and NDC
117 local $dh->{ PrintError } = 0 ;
118 local $dh->{ RaiseError } = 1 ;
119 $Log::Log4perl::NDC = $s->{copy_every};
120 Log::Log4perl::NDC->remove ;
121  
122 my ($rows, $data) ;
123 while ( $r -- ) {
124 $data = $csv->getline_hr ($fd) ;
125 last unless $data;
126 last if _stop_input($conf, $rows//0) ;
127 $_ = combine( $s, $csv, $data, @col ) ;
128 Log::Log4perl::NDC->push( "$_\n" ) if $_;
129 DEBUG( "\t\t$_" ) ;
130 $rows += $dh->pg_putcopydata("$_\n") unless $dry ;
131 }
132  
133 eval {
134 $dh->pg_putcopyend ;
135 enable_indexes( $dh,$table,$defs) if $conf->{indexes};
136 Log::Log4perl::NDC->remove;
137 1;
138 } || do {
139 log_rejected_data( $s, $section );
140 log_reject_errors( $s, $dh->errstr, $section );
141 $dh->pg_rollback_to("every_block");
142 return (-$rows//0, $data);
143 };
144 return ($rows//0, $data);
145 }
146  
147  
148 1;
149 __END__
« lib/Pg/Loader/Query.pm ↑ Summary lib/Pg/Loader/Update.pm »