| 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; | ||
| 5 | |||
| 6 | 9 | ▶ | use 5.010000; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 9 | ▶ | use Data::Dumper; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 9 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 9 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 9 | ▶ | use Config::Format::Ini qw/ read_ini /; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 9 | ▶ | use Log::Log4perl ':easy'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | #use Text::CSV; | ||
| 13 | #use Pg::Loader::Columns; | ||
| 14 | 0 | ▶ | use Pg::Loader::Query qw/ primary_keys /; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 15 | use List::MoreUtils qw( firstidx ); | ||
| 16 | use base 'Exporter'; | ||
| 17 | use Quantum::Superpositions ; | ||
| 18 | #use Text::Table; | ||
| 19 | |||
| 20 | *get_columns_names = \&Pg::Loader::Query::get_columns_names; | ||
| 21 | |||
| 22 | |||
| 23 | our $VERSION = '0.12'; | ||
| 24 | |||
| 25 | our @EXPORT = qw( | ||
| 26 | ini_conf error_check | ||
| 27 | usage | ||
| 28 | print_results add_defaults subset | ||
| 29 | error_check_pgsql filter_ini reformat_values | ||
| 30 | add_modules pk4updates gen | ||
| 31 | insert_semantic_check update_semantic_check | ||
| 32 | ); | ||
| 33 | |||
| 34 | |||
| 35 | |||
| 36 | sub ini_conf { | ||
| 37 | $Config::Format::Ini::SIMPLIFY = 1 ; | ||
| 38 | my $file = shift || 'pgloader.conf'; | ||
| 39 | INFO( "Configuring from $file" ) ; | ||
| 40 | my $ini = read_ini $file ; | ||
| 41 | } | ||
| 42 | |||
| 43 | |||
| 44 | |||
| 45 | sub print_results { | ||
| 46 | my @stats = @_ or return; | ||
| 47 | printf "%-17s | %11s | %7s | %10s | %10s\n", | ||
| 48 | 'section name', 'duration', 'size', 'affected rows', 'errors'; | ||
| 49 | say '='x 70; | ||
| 50 | printf "%-17s | %10.3fs | %7s | %13d | %10s\n" , | ||
| 51 | @{$_}{qw( name elapsed size rows errors)} for @stats; | ||
| 52 | } | ||
| 53 | |||
| 54 | |||
| 55 | |||
| 56 | sub error_check_pgsql { | ||
| 57 | my ($conf, $ini) = @_ ; | ||
| 58 | my $s = $ini->{pgsql} || LOGEXIT(qq(Missing pgsql section )); | ||
| 59 | if ($s->{pgsysconfdir} || $ENV{ PGSYSCONFDIR } ) { | ||
| 60 | my $msg = 'Expected service parameter in pgsql section'; | ||
| 61 | $s->{service} or LOGEXIT ( $msg ) ; | ||
| 62 | } | ||
| 63 | $conf->{dry_run} //= 0; | ||
| 64 | } | ||
| 65 | |||
| 66 | sub error_check { | ||
| 67 | my ( $ini, $section) = @_; | ||
| 68 | die unless $section; | ||
| 69 | my $s = $ini->{$section}|| LOGEXIT(qq(No config for [$section])); | ||
| 70 | my $msg01 = q("copy_columns" and "only_cols" are mutually exclusive); | ||
| 71 | $s->{copy_columns} and $s->{only_cols} and LOGEXIT( $msg01 ) ; | ||
| 72 | |||
| 73 | $s->{filename} or LOGEXIT(qq(No filename specified for [$section])); | ||
| 74 | $s->{table} or LOGEXIT(qq(No table specified for [$section])); | ||
| 75 | $s->{format} =~ s/^ \s*'|'\s* $//xog; | ||
| 76 | $s->{format} or LOGEXIT(qq(No format specified for [$section])); | ||
| 77 | given ($s->{format} ) { | ||
| 78 | when (/^(text|csv)$/) {} ; | ||
| 79 | default { LOGEXIT( q(Set format to either 'text' or 'csv'))}; | ||
| 80 | }; | ||
| 81 | _check_copy_grammar( $s->{copy} ); | ||
| 82 | DEBUG("\tPassed grammar check"); | ||
| 83 | } | ||
| 84 | sub pk4updates { | ||
| 85 | ## ensure that table has pk | ||
| 86 | ## Output: the pk as an arrayref | ||
| 87 | my ($dh, $s) = @_ ; | ||
| 88 | my $table = $s->{table}; | ||
| 89 | $s->{pk} = Pg::Loader::Query::primary_keys($dh, $table); | ||
| 90 | @{$s->{pk}} ; | ||
| 91 | } | ||
| 92 | sub update_semantic_check { | ||
| 93 | my $s = shift; | ||
| 94 | # ensure "copy_columns" is an arrayref | ||
| 95 | my $msg = qq(\t"update_columns" cannot contain primary keys); | ||
| 96 | for my $pk (@{$s->{pk}}) { | ||
| 97 | grep { /^$pk$/ } @{$s->{copy_columns}} and LOGDIE( $msg ); | ||
| 98 | } | ||
| 99 | $msg = qq(\t"update" must include the primary keys); | ||
| 100 | subset( $s->{copy}, $s->{pk} ) or LOGDIE( $msg ); | ||
| 101 | $msg = qq(\tConfig implies that no columns should be updated); | ||
| 102 | ( @{$s->{copy}} == @{$s->{pk}} ) and LOGDIE( $msg ); | ||
| 103 | #TODO: what is updated must be in the "copy" list | ||
| 104 | } | ||
| 105 | sub _check_copy_grammar { | ||
| 106 | my $values = shift||return; | ||
| 107 | # $s->copy should be either a '*' string, or an array of | ||
| 108 | # string in the form of \w(:\d+) . Whitespaces are trimed. | ||
| 109 | my $err = 'Invadid value for param "copy"' ; | ||
| 110 | |||
| 111 | if (ref $values eq 'ARRAY') { | ||
| 112 | # array of arrayref | ||
| 113 | my $max = $#{$values}; | ||
| 114 | my $pat = qr/^\s*\w+(?:\s*[:]\s*\d+)?/ ; | ||
| 115 | ($max+1) == grep { LOGEXIT $err unless $_; | ||
| 116 | LOGEXIT $err unless $_=~ $pat; | ||
| 117 | } @$values or LOGEXIT $err; | ||
| 118 | }else{ | ||
| 119 | # assume it is string, big assumption | ||
| 120 | my $_ = $values ; | ||
| 121 | my $pat = qr/^ \s* \w+ (?:[:]1)? \s* $/xo; | ||
| 122 | LOGEXIT $err unless (/^ \s* [*] \s* $/xo or $_=~ $pat ); | ||
| 123 | } | ||
| 124 | # passed | ||
| 125 | } | ||
| 126 | |||
| 127 | sub subset { | ||
| 128 | my ($h,$n) = @_ ; | ||
| 129 | # True if $n is subset of $h; | ||
| 130 | my @intersection = eigenstates(all( any(@$h), any(@$n) )); | ||
| 131 | (@intersection == @$n); | ||
| 132 | } | ||
| 133 | sub _copy_param { | ||
| 134 | my $values = shift; | ||
| 135 | # receives a array of strings like [qw(a:1 b c:4 d:3)] and returns | ||
| 136 | # an arrayref of ordered columns: [q( a b d c )] | ||
| 137 | return if $values =~ /^ \s* [*] \s* $/xo; | ||
| 138 | |||
| 139 | (ref $values eq 'ARRAY') or $values = [$values]; | ||
| 140 | |||
| 141 | my ($max, $last, @ret) = ($#{$values}, 0); | ||
| 142 | for (@$values) { | ||
| 143 | s/^\s*|\s*$//og; | ||
| 144 | my ($name, $num) = split /\s*:\s*/, $_; | ||
| 145 | $num //= $last+1; | ||
| 146 | $last = $num; | ||
| 147 | $ret[$num-1] = $name ; | ||
| 148 | } | ||
| 149 | LOGEXIT "invalid values for copy param" unless $#ret == $max; | ||
| 150 | \@ret; | ||
| 151 | } | ||
| 152 | |||
| 153 | |||
| 154 | sub filter_ini { | ||
| 155 | # Check if configuration values are sensible. | ||
| 156 | # Assumption: The configuration syntax obeys grammar | ||
| 157 | # Output: records real table attributes to $s->{attributes} | ||
| 158 | # Output: "copy" and "copy_columns" become arrayrefs | ||
| 159 | #TODO: parameters for "copy" should match those of actual table | ||
| 160 | #TODO: parameters for "copy_only" should match those of actual table | ||
| 161 | my ($s, $dh) = @_ ; | ||
| 162 | |||
| 163 | $s->{$_} =~ s/ \\ (?=,) //gox for keys %$s; | ||
| 164 | |||
| 165 | my $attributes = [ get_columns_names( $dh, $s->{table}, $s ) ]; | ||
| 166 | $s->{attributes} = $attributes; | ||
| 167 | LOGEXIT("Could not fetch column names from db for table $s->{table}") | ||
| 168 | unless @$attributes; | ||
| 169 | |||
| 170 | $s->{copy} = ($s->{copy}=~/^\s*[*]\s*$/ox) ? $attributes | ||
| 171 | : _copy_param $s->{copy}; | ||
| 172 | ($s->{copy_columns}||'') =~/^\s*[*]\s*$/ox | ||
| 173 | and $s->{copy_columns}=$s->{copy}; | ||
| 174 | # Ensure that "copy" and "copy_columns" are always arrayref | ||
| 175 | ref $s->{copy} or $s->{copy} = [$s->{copy}]; | ||
| 176 | ref $s->{copy_columns} or $s->{copy_columns} = [$s->{copy_columns}]; | ||
| 177 | DEBUG("\tPassed semantic check"); | ||
| 178 | } | ||
| 179 | |||
| 180 | sub insert_semantic_check { | ||
| 181 | my $s = shift; | ||
| 182 | # Check semantics for these things: | ||
| 183 | # 1. "copy" is a subset of the real attribute names | ||
| 184 | # 2. "copy" is a subset of the real attribute names | ||
| 185 | # 3. "copy_only" is a subset of "copy" | ||
| 186 | my $cmsg = q(names in "copy" are not a subset of actual table names); | ||
| 187 | subset $s->{attributes}, $s->{copy} or LOGEXIT( $cmsg ); | ||
| 188 | $cmsg= q(names in "copy_columns" are not a subset for actual names); | ||
| 189 | subset( $s->{attributes}, $s->{copy_columns}) or LOGEXIT( $cmsg ); | ||
| 190 | $cmsg= q(names in "copy_columns" are not a subset of "copy"); | ||
| 191 | subset( $s->{copy}, $s->{copy_columns}) or LOGEXIT( $cmsg ); | ||
| 192 | #TODO: what is copied must be in the "copy" list | ||
| 193 | } | ||
| 194 | |||
| 195 | sub reformat_values { | ||
| 196 | # Adjusts values as needed. | ||
| 197 | # Assumption: The configuration syntax obay grammar | ||
| 198 | # Output: TODO | ||
| 199 | my ($s, $dh) = @_ ; | ||
| 200 | return unless $s->{ reformat} ; | ||
| 201 | (ref $s->{reformat} eq 'ARRAY') or $s->{reformat} = [$s->{reformat} ]; | ||
| 202 | for ( @{$s->{reformat}} ) { | ||
| 203 | next unless $_; | ||
| 204 | my ($col, $mod, $fun ) = m/^(\w+): (.*)::(\w+) $/gxo; | ||
| 205 | next unless defined $fun; | ||
| 206 | $s->{rfm}{$col} = { col=>$col, pack=>$mod, fun=>$fun }; | ||
| 207 | } | ||
| 208 | DEBUG("\tPassed reformat"); | ||
| 209 | } | ||
| 210 | |||
| 211 | sub add_modules { | ||
| 212 | my $s = shift ; | ||
| 213 | return unless $s->{rfm} ; | ||
| 214 | for ( keys %{$s->{rfm}}) { | ||
| 215 | my $h = $s->{rfm}{$_}; | ||
| 216 | my ($pack, $fun) = @{$h}{'pack','fun'}; | ||
| 217 | (my $module = $pack) =~ s{::}{\/}o ; | ||
| 218 | $module .= '.pm'; | ||
| 219 | require $module ; | ||
| 220 | #say "${pack}::$fun"; | ||
| 221 | LOGEXIT qq(could not find "${pack}::$fun") | ||
| 222 | unless UNIVERSAL::can( $pack, $fun ); | ||
| 223 | $h->{ref} = 1 ;# cludge fix | ||
| 224 | } | ||
| 225 | } | ||
| 226 | |||
| 227 | |||
| 228 | 1; | ||
| 229 | __END__ | ||