ilt - list tables from a DBI database source
#!/usr/bin/perl

#
# Name         : ilt - [db]independent list tables
#
# Synopsis     : ilt [OPTIONS] 
#
# Description  : This utility allows you to list the tables 
#                or the columns of a particular table in a database 
#
#                -h HOST
#
#                   Database is located on HOST
#
#                -d DATABASE
#
#                   Which DATABASE to use
#
#                -u USER
#
#	  	    Which USER to log in as
#
#                -p PASSWD 
#
#                   Which PASSWD to log in with
#
#                -t TABLE
#
#                   If TABLE is given as an argument, list
#                   the columns of that table.
#
# REPORTING BUGS
#
#		Report bugs to Graeme Burnett 
#
# See Also     : DBI::FAQ
#
#
# Owner        : Graeme Burnet
#
# Perms        : 0755 
#
# Status       : DEVEL 
#
# Sccs Id      : %W% 
#
# Dated        : %D% %T% 
#

use strict;
use DBI;
use strict;
use CGI::Carp;
use Getopt::Std;
use vars qw($opt_d $opt_h $opt_p $opt_u $opt_t);

my $program = $ARGV[0]; 
my $driver = "mysql";
my $port=3306;
my $debug = 0;

getopt('dupht');
 
$opt_d = "intranet" if (!$opt_d);
$opt_u = "graeme" if (!$opt_u);
$opt_p = "tigger1" if (!$opt_p);
$opt_h = `hostname` if (!$opt_h);

if ($debug)
{
  print" 
   Parameters
   [ \$opt_d = $opt_d ]
   [ \$opt_u = $opt_u ]
   [ \$opt_p = $opt_p ]
   [ \$opt_h = $opt_h ]
   [ \%opt_t = $opt_t ]\n";
   
  my @drivernames = DBI->available_drivers; 

  print "Available DB Drivers\n";

  foreach my $driver (@drivernames)
  {
    print "[$driver]\n";
  }
}

my $dsn="dbi:mysql:$opt_d";

my $dbh = DBI->connect($dsn, $opt_u, $opt_p, { RaiseError => 1, AutoCommit => 1 }) || die DBI::errstr;

if ($opt_t)
{
  my $statement = "select * from $opt_t";

  my $sth = $dbh->prepare($statement);

  $sth->execute();

  my $array_ref = $sth->{NAME};

  my $numcols = scalar(@$array_ref);

  print STDERR "Listing Columns in $opt_d\-\>$opt_t\n";

  for(my $i = 0; $i < $numcols; $i++)
  {
    print "$sth->{NAME}->[$i],\n";
  }
}
else
{
  print STDERR "Listing tables in database [ $opt_d ]\n";

  my $sth = $dbh->table_info; 
  $sth->execute();
 
  while (my @row = $sth->fetchrow_array ) 
  { 
    print "$row[2]\n";
  }
}

$dbh->disconnect();

1;