#! /usr/bin/perl

require 'getopts.pl' ;

my $allIndexes = 0 ;
my $allTriggers = 0 ;
my $table = "" ;
my $index = "" ;
my $notreally = 0 ;
Getopts('ITt:i:n') ;
$allIndexes = $opt_I if $opt_I ;
$allTriggers = $opt_T if $opt_T ;
$table = $opt_t if $opt_t ;
$index = $opt_i if $opt_i ;
$notreally = $opt_n if $opt_n ;

use strict ;

my $USAGE = <<USAGE ;
$0 - Postgresql table tool

  $0 -t <table> [-i <index>] [-I] [-T] <database>

	Options:
		-t <table>	Table to work on
		-i <index>	Index to rebuild
		-I		Rebuild all triggers
		-T		Do all triggers
	You must choose at least one of -i, -I and -T
USAGE

die $USAGE if ($table eq "") ;
die "You can only use one of '-I' and '-i' options"
	if ($index ne "" && $allIndexes) ;
die $USAGE if ($index eq "" && ! $allIndexes && ! $allTriggers) ;

use DBI ;

my $dbname = shift @ARGV ;
$$$$$$$$$ DELETE THIS LINE AND FIX NEXT THREE VALUES $$$$$$$$$
my $user = 'USERNAME' ;
my $passwd = 'PASSWORD' ;
my $hostname = 'HOSTNAME' ;

my $dbh = DBI->connect("dbi:Pg:dbname=$dbname;host=$hostname",$user,$passwd) ;

my %indexes ;
my @triggers ;
my %unique ;

open PGDUMP, "pg_dump -s -t $table $dbname |" ;
while (<PGDUMP>)
{
  chomp ;
  next if ! m/^CREATE/ ;

  if (m/^CREATE\s+(.*)\s*INDEX\s+"([^"]*)".*(\(.*\))/)
  {
    $indexes{$2} = $3 ;
    $indexes{$2 => "UNIQUE"} = ($1 ne "") ;

    next ;
  }
  if (! defined $unique{$_})
  {
    $unique{$_} = 1 ;
    if (m/^CREATE\s+CONSTRAINT\s+TRIGGER\s+"([^"]*)"/)
    {
      push (@triggers, $_) ;
      next ;
    }
  }
  else
  {
    next ;
  }
}
close PGDUMP ;

if ($allTriggers)
{
  my $cmd = "SELECT oid from pg_class where relname = '$table'" ;
  my $relid ;
  if (my $sth = $dbh->prepare($cmd))
  {
    if ($sth->execute)
    {
      if (my $row = $sth->fetchrow_arrayref)
      {
	$relid = $$row[0] ;
      }
    }
  }

  $cmd = "SELECT tgname FROM pg_trigger where tgrelid = $relid" ;
  if (my $sth = $dbh->prepare($cmd))
  {
    if ($sth->execute)
    {
      while (my $row = $sth->fetchrow_arrayref)
      {
	$relid = $$row[0] ;
	print "DROP TRIGGER $$row[0] ON $table;\n" ;
      }
    }
  }

  for my $trigger (@triggers)
  {
    print $trigger . "\n" ;
  }
}

die "Index $index does not exist on table $table"
      if ($index ne "" && ! defined $indexes{$index}) ;

if ($index)
{
  my $cmd = "CREATE" ;
  $cmd.= " UNIQUE" if $indexes{$index => "UNIQUE"} == 1 ;
  $cmd.= " INDEX $index ON $table $indexes{$index};" ;
  print "$cmd\n" ;
  my $cmd = "DROP INDEX $index;" ;
  print "$cmd\n" ;
  $cmd = "ALTER TABLE ".$index."_new" ;
  $cmd.= " RENAME TO $index;" ;
  print "$cmd\n" ;
}
elsif ($allIndexes)
{
  for my $index (keys %indexes)
  {
    next if ! defined $indexes{$index => "UNIQUE"} ;

    my $cmd = "CREATE" ;
    $cmd.= " UNIQUE" if $indexes{$index => "UNIQUE"} == 1 ;
    $cmd.= " INDEX ".$index."_new ON $table $indexes{$index};" ;
    print "$cmd\n" ;
    $cmd = "DROP INDEX $index;" ;
    print "$cmd\n" ;
    $cmd = "ALTER TABLE ".$index."_new" ;
    $cmd.= " RENAME TO $index;" ;
    print "$cmd\n" ;
  }
}

$dbh->disconnect ;

__END__
