lib/Pg/Loader/Update.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/Copy.pm ↑ Summary lib/Pg/Loader/Columns.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::Update;
6  
7 2 ▶use 5.010000;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Fatal qw(open);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Time::HiRes qw( gettimeofday tv_interval );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 0 ▶use Pg::Loader::Query qw/ update_string create_tmp_table/;
Subroutine BEGIN: called CC=1 SCAR=2.6
15 use Pg::Loader::Misc qw( error_check filter_ini pk4updates
16 reformat_values add_modules update_semantic_check
17 );
18 use Pg::Loader::Copy qw/ _stop_input _no_pk_found load_2table/;
19 use Pg::Loader::Columns;
20 use Pg::Loader::Log;
21 use Log::Log4perl qw( :easy );
22 use base 'Exporter';
23 use SQL::Abstract;
24 use Storable qw( dclone );
25 #create_tmp_table
26  
27 our $VERSION = '0.17';
28  
29 our @EXPORT = qw( update_loader ) ;
30  
31 sub update_loader {
32 my ( $conf, $ini, $dh, $section ) = @_ ;
33 my $s = $ini->{$section} ;
34 my $dry = $conf->{dry} ;
35  
36 INFO("UPDATE, as per [$section]") ;
37 $ini->{$section}{table} = $conf->{relation} if $conf->{relation} ;
38 $ini->{$section}{copy_every} = 1 if $conf->{every} ;
39 error_check( $ini, $section ) ;
40 filter_ini( $ini->{$section}, $dh ) ;
41 pk4updates($dh, $ini->{$section}) or return _no_pk_found($ini,$section);
42 eval { update_semantic_check( $ini->{$section} ) ; 1
43 } or return { name=>$section, elapsed=>0,
44 rows=>0, errors=>'config', size=>$s->{copy_every} };
45 reformat_values( $ini->{$section}, $dh ) ;
46 add_modules( $ini->{$section}, $dh ) ;
47  
48 # Prepare for internal COPY
49 my $c_conf = dclone( $conf );
50 my $c_ini = dclone( $ini );
51 my $c_s = $c_ini->{$section} ;
52 @{$c_conf}{'indexes','disable_triggers','vacuum'} = qw( 0 0 0) ;
53 push @{$c_s->{copy_columns}}, $ _ for @{$s->{pk}};
54 @{$c_s}{'table',} = qw( d ) ;
55  
56 # Load to internal table
57 create_tmp_table( $dh, $s->{table} );
58 my $ret = load_2table ( $c_conf, $c_ini, $dh, $section ) ;
59 return $ret if $ret->{errors};
60  
61 DEBUG "\tUpdating " . $s->{table} ;
62 my $t0 = [gettimeofday];
63 my $sql = update_string('d', @{$s}{'table','copy_columns','pk'} );
64 DEBUG "\t\t$sql";
65 unless ($dry) {
66 $dh->begin_work;
67 if ( my $total = $dh->do($sql) ) {
68 $dh->commit ;
69 return
70 { name=> $section,
71 elapsed => $ret->{elapsed}+tv_interval($t0),
72 rows=> $total//0, errors => $ret->{errors}//0,
73 size=> $s->{copy_every},
74 }
75 }else{
76 $dh->rollback;
77 return
78 { name=> $section, elapsed => $ret->{elapsed}//0,
79 rows=> 0, errors => $ret->{errors}//0,
80 size=> $s->{copy_every},
81 }
82 }
83 }
84 }
85  
86  
87 1;
88 __END__
« lib/Pg/Loader/Copy.pm ↑ Summary lib/Pg/Loader/Columns.pm »