The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.
#! /usr/bin/env perl

# dbs_printtab - creates file set with SQL table data

# This script queries a SQL database and creates one file for each
# table in the database containing the data of the table.

# Copyright (C) 2000-2012 Stefan Hornburg

# Author: Stefan Hornburg (Racke) <racke@linuxia.de>
# Maintainer: Stefan Hornburg (Racke) <racke@linuxia.de>
# Version: 0.19

# This file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2, or (at your option) any
# later version.

# This file is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied warranty
# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this file; see the file COPYING.  If not, write to the Free
# Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.

use strict;
use warnings;

use DBIx::Easy;
use Getopt::Long;
use Term::ReadKey;

# process commandline parameters
my %opts;
my $whandler = $SIG{__WARN__};
$SIG{__WARN__} = sub {print STDERR "$0: @_";};
unless (GetOptions (\%opts, 'columns|c=s',
					'sort=s')) {
    exit 1;
}
$SIG{__WARN__} = $whandler;

my $USAGE = <<EOT;
Usage: $0 DRIVER DATABASE TABLE [USER]
EOT

# sanity check on commandline parameters
if (@ARGV < 3) {
    warn ($USAGE);
    exit 1;
}

my $dbif;
my $pwdused = 0;

my ($driver, $database, $table, $user) = @ARGV;
$dbif = new DBIx::Easy ($driver, $database, $user);

# handler for DBI error messages and missing password
$dbif -> install_handler (\&fatal);

# we need to explicitly establish the connection
# for the case that a password is needed
$dbif -> connect;

# query records in table
print $dbif -> view ($table, columns => $opts{'columns'}, order => $opts{'sort'});

# Destroy database object as written in manpage
undef $dbif;

# -----------------------------------
# FUNCTION: fatal
#
# Error handler called by DBIx::Easy.
# -----------------------------------

sub fatal {
  my ($statement, $err, $msg) = @_;
  my $pwd;

  if ($dbif->is_auth_error ($err)) {
    unless ($pwdused) {
      print "We need a password.\n";
      $pwd = querypwd();
      $pwdused = 1;
    
      # retry the connection
      if (length ($pwd)) {
        $dbif = new DBIx::Easy ($driver, $database, $user, $pwd);
        $dbif -> install_handler (\&fatal);
        $dbif -> connect ();
        return;
      } else {
        die ("$statement.\n");
      }
    }
  }
  die ("$statement.\n");
}

# ----------------------------
# FUNCTION: querypwd
#
# Queries user for a password.
# ----------------------------

sub querypwd () {
  my $pwd;

  print "Password: ";
  ReadMode ('noecho');  # turn echo off
  $pwd = ReadLine (0);
  ReadMode ('restore'); # restore terminal
  print "\n";
  chomp ($pwd);
  $pwd;
}

=head1 NAME

dbs_printtab - Prints SQL table data on standard output

=head1 DESCRIPTION

dbs_printtab is an utility to print SQL table data on
standard output. dbs_printtab asks for a password if necessary.

=head1 COMMAND LINE PARAMETERS

Required command line parameters are the DBI driver
(C<Pg> for Postgres or C<mysql> for MySQL), the database name
and the table name. The fourth parameter is optionally
and specifies the database user and/or the host where the
database resides (C<racke>, C<racke@linuxia.de> or C<@linuxia.de>).

=head1 COMMAND LINE OPTIONS

=head2 B<-c>=COLUMN[,COLUMN], B<--columns>=COLUMN[,COLUMN]

The output is restricted to the given COLUMN arguments.

=head2 B<--sort>=KEY[,KEY]

The output is sorted by the KEY arguments.

=head1 BUGS

msql is not fully supported.

=head1 AUTHOR

Stefan Hornburg (Racke), racke@linuxia.de

=head1 SEE ALSO

perl(1), DBIx::Easy(3)

=cut