| 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::Query; | ||
| 6 | |||
| 7 | 16 | ▶ | use 5.010000; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 16 | ▶ | use DBI; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 16 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 16 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 16 | ▶ | use Config::Format::Ini; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | 16 | ▶ | use Log::Log4perl qw( :easy ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 13 | 0 | ▶ | use Pg::Loader::Columns; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 14 | use Pg::Loader::Misc; | ||
| 15 | use Data::Dumper; | ||
| 16 | use Text::CSV; | ||
| 17 | use base 'Exporter'; | ||
| 18 | |||
| 19 | our $VERSION = '0.11'; | ||
| 20 | |||
| 21 | our @EXPORT = qw( | ||
| 22 | connect_db get_columns_names primary_keys | ||
| 23 | disable_indexes enable_indexes vacuum_analyze | ||
| 24 | update_string pgoptions create_tmp_table | ||
| 25 | _truncate _disable_triggers _disable_indexes | ||
| 26 | |||
| 27 | ); | ||
| 28 | sub _truncate { | ||
| 29 | my ($dh, $table, $dry) = @_ ; | ||
| 30 | INFO("\tTruncating $table") ; | ||
| 31 | $dh->do("truncate $table") unless $dry ; | ||
| 32 | } | ||
| 33 | sub _disable_triggers { | ||
| 34 | my ($dh, $table, $dry) = @_ ; | ||
| 35 | DEBUG( "\tDisabling triggers") ; | ||
| 36 | $dh->do( <<"") unless $dry ; | ||
| 37 | ALTER TABLE $table DISABLE TRIGGER ALL | ||
| 38 | |||
| 39 | } | ||
| 40 | sub _disable_indexes { | ||
| 41 | my ($dh, $table, $dry) = @_ ; | ||
| 42 | disable_indexes( $dh, $table ) unless $dry | ||
| 43 | } | ||
| 44 | |||
| 45 | sub create_tmp_table { | ||
| 46 | my ($dh, $like) = @_ ; | ||
| 47 | my $sql = <<""; | ||
| 48 | DROP TABLE IF EXISTS d; | ||
| 49 | CREATE TEMP TABLE d (like $like including indexes) | ||
| 50 | |||
| 51 | $dh->do( $sql ) or LOGDIE( $dh->errstr ); | ||
| 52 | #TODO delete next line | ||
| 53 | DEBUG( 'created tmp table d' ); | ||
| 54 | } | ||
| 55 | |||
| 56 | |||
| 57 | sub pgoptions { | ||
| 58 | my ($dh, $s) = @_ ; | ||
| 59 | for ( qw( datestyle client_encoding | ||
| 60 | lc_messages lc_numeric lc_monetary lc_time)) { | ||
| 61 | next unless $s->{$_}; | ||
| 62 | $dh->do( "set $_ to ". $dh->quote($s->{$_} )); | ||
| 63 | } | ||
| 64 | } | ||
| 65 | |||
| 66 | sub connect_db { | ||
| 67 | my $pgsql = shift ; | ||
| 68 | my ($port, $host, $base) = @{$pgsql}{'port','host','base'} ; | ||
| 69 | $port //= 5432 ; | ||
| 70 | $host //= 'localhost' ; | ||
| 71 | $base || usage() ; | ||
| 72 | my ($user, $pass) = @{$pgsql}{'user','pass'} ; | ||
| 73 | my $dsn = "dbi:Pg:dbname=$base;host=$host;port=$port" ; | ||
| 74 | $dsn .=';options=--client_min_messages=WARNING' ; | ||
| 75 | $ENV{ PGSYSCONFDIR } //= $pgsql->{pgsysconfdir} //'' ; | ||
| 76 | if ( -f "$ENV{ PGSYSCONFDIR }/pg_service.conf") { | ||
| 77 | DEBUG( "Using PGSYSCONFIGDIR ") ; | ||
| 78 | $dsn = "dbi:Pg:service=$pgsql->{service}" ; | ||
| 79 | $user = $pass = '' ; | ||
| 80 | } | ||
| 81 | my $att = { AutoCommit => 0 , pg_server_prepare => 1, | ||
| 82 | PrintError => 0 , Profile => 0, | ||
| 83 | }; | ||
| 84 | DBI->connect( $dsn, $user//getlogin,$pass,$att) or die "$DBI::errstr\n"; | ||
| 85 | } | ||
| 86 | |||
| 87 | sub vacuum_analyze { | ||
| 88 | my ($dh, $table, $dry) = @_ ; | ||
| 89 | local $dh->{ AutoCommit } = 1; | ||
| 90 | local $dh->{ RaiseError } = 0; | ||
| 91 | local $dh->{ PrintError } = 0; | ||
| 92 | my ($msg, $rv) = ("\tVacuum analyze $table", 1); | ||
| 93 | unless ($dry) { | ||
| 94 | $rv = $dh->do("VACUUM ANALYZE $table") ; | ||
| 95 | } | ||
| 96 | INFO $rv//'' ? $msg : $msg . '.....FAILED' ; | ||
| 97 | $rv; | ||
| 98 | } | ||
| 99 | |||
| 100 | sub disable_indexes { | ||
| 101 | my ( $dh, $schema, $table) = ($_[0], schema_name( $_[1] )); | ||
| 102 | (my $st = $dh->prepare(<<""))->execute() ; | ||
| 103 | SELECT indexrelid::regclass::text AS name, | ||
| 104 | indisprimary AS pk, | ||
| 105 | pg_get_indexdef(indexrelid) AS def | ||
| 106 | FROM pg_index I | ||
| 107 | join pg_class C ON ( C.oid = I.indrelid ) | ||
| 108 | join pg_namespace N ON ( N.oid = C.relnamespace ) | ||
| 109 | WHERE relname = @{[ $dh->quote($table) ]} | ||
| 110 | and nspname = @{[ $dh->quote($schema) ]} | ||
| 111 | |||
| 112 | |||
| 113 | my @definitions; | ||
| 114 | #while ( my $idx = $st->fetchrow_hashref ) { | ||
| 115 | while ( my $idx = $st->fetchrow_arrayref ) { | ||
| 116 | my $sql = $idx->[1] | ||
| 117 | ? "ALTER table $table drop constraint ".$idx->[0] | ||
| 118 | : "DROP INDEX ".$idx->[0]; | ||
| 119 | DEBUG( "\t\t$sql" ) ; | ||
| 120 | $dh->do( $sql ) and INFO( "\t\tDisabled ".$idx->[0]) ; | ||
| 121 | push @definitions, | ||
| 122 | { name =>$idx->[0],def =>$idx->[2], pk=>$idx->[1] }; | ||
| 123 | } | ||
| 124 | \@definitions; | ||
| 125 | } | ||
| 126 | sub enable_indexes { | ||
| 127 | my ( $dh, $schema, $table) = ($_[0], schema_name($_[1])); | ||
| 128 | my @defs = @{$_[2]}; | ||
| 129 | for (@defs) { | ||
| 130 | my ($col) = $_->{def} =~ / (\( [,\w\s]+? \)) $/xo ; | ||
| 131 | $col //= ''; | ||
| 132 | my $sql = $_->{pk} ? "ALTER TABLE $table add PRIMARY KEY $col" | ||
| 133 | : $_->{def}; | ||
| 134 | DEBUG( "\t\t$sql" ) ; | ||
| 135 | $dh->do( $sql) and INFO( "\t\tCreated index $_->{name}" ) ; | ||
| 136 | } | ||
| 137 | } | ||
| 138 | |||
| 139 | sub schema_name { | ||
| 140 | my ($canonical, $search) = @_ ; | ||
| 141 | my ($schema, $table) = split /\./, $canonical, 2 ; | ||
| 142 | unless ($table ) { | ||
| 143 | $table = $schema; | ||
| 144 | $schema = $search || 'public'; | ||
| 145 | } | ||
| 146 | ( $schema, $table ); | ||
| 147 | } | ||
| 148 | |||
| 149 | |||
| 150 | sub primary_keys { | ||
| 151 | # Input: name of table | ||
| 152 | # Output: names of its columns that form primary key | ||
| 153 | my ( $dh, $schema, $table) = ($_[0], schema_name( $_[1] )); | ||
| 154 | my $h = $dh->selectall_arrayref(<<"",{}, $schema, $table ); | ||
| 155 | SELECT column_name | ||
| 156 | FROM information_schema.constraint_table_usage T | ||
| 157 | join information_schema.constraint_column_usage using (constraint_name) WHERE T.table_schema = ? | ||
| 158 | and T.table_name = ? | ||
| 159 | |||
| 160 | return unless $h; | ||
| 161 | [ map { $_->[0] } @$h ] ; | ||
| 162 | } | ||
| 163 | |||
| 164 | |||
| 165 | sub get_columns_names { | ||
| 166 | # return ordered list of culumn names | ||
| 167 | my ( $dh, $schema, $table) = ($_[0], schema_name( $_[1] )); | ||
| 168 | (my $st = $dh->prepare(<<""))->execute( $table, $schema) ; | ||
| 169 | select column_name, ordinal_position | ||
| 170 | from information_schema.columns | ||
| 171 | where table_name = ? | ||
| 172 | and table_schema = ? | ||
| 173 | order by 2; | ||
| 174 | |||
| 175 | my $h = $st->fetchall_arrayref; | ||
| 176 | map { ${$_}[0] } @$h ; | ||
| 177 | } | ||
| 178 | sub _where_clause { | ||
| 179 | my ($pk , $target, $from) = @_ ; | ||
| 180 | my $sql = 'WHERE ' ; | ||
| 181 | $sql .= "$target.$_=$from.$_ and " for @$pk; | ||
| 182 | $sql =~ s/and\s*$//o , $sql; | ||
| 183 | } | ||
| 184 | sub _set_clause { | ||
| 185 | my ($cols, $from) = @_ ; | ||
| 186 | return unless @$cols; | ||
| 187 | my $sql = 'SET ' ; | ||
| 188 | $sql .= "$_=$from.$_, " for @$cols; | ||
| 189 | $sql =~ s/,\s*$/ /o; | ||
| 190 | $sql ; | ||
| 191 | } | ||
| 192 | sub update_string { | ||
| 193 | my ( $from, $target, $set_cols, $where_cols ) = @_ ; | ||
| 194 | my $sql = "UPDATE $target "; | ||
| 195 | $sql .= _set_clause( $set_cols, $from ) ; | ||
| 196 | $sql .= "FROM $from " ; | ||
| 197 | $sql .= _where_clause( $where_cols, $target, 'd'); | ||
| 198 | } | ||
| 199 | |||
| 200 | |||
| 201 | |||
| 202 | 1; | ||
| 203 | __END__ | ||