<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package DBI::Shell;

=head1 NAME

DBI::Shell - Interactive command shell for the DBI

=head1 SYNOPSIS

  perl -MDBI::Shell -e shell [&lt;DBI data source&gt; [&lt;user&gt; [&lt;password&gt;]]]

or

  dbish [&lt;DBI data source&gt; [&lt;user&gt; [&lt;password&gt;]]]

=head1 DESCRIPTION

The DBI::Shell module (and dbish command, if installed) provide a
simple but effective command line interface for the Perl DBI module.

DBI::Shell is very new, very experimental and very subject to change.
Your milage I&lt;will&gt; vary. Interfaces I&lt;will&gt; change with each release.

=cut

###
###	See TO DO section in the docs at the end.
###


BEGIN { require 5.004 }
BEGIN { $^W = 1 }

use strict;
use vars qw(@ISA @EXPORT $VERSION $SHELL);
use Exporter ();
use Carp;

@ISA = qw(Exporter);
@EXPORT = qw(shell);
$VERSION = sprintf "%d.%02d", '$Revision: 11.2 $ ' =~ /(\d+)\.(\d+)/;

my $warning = &lt;&lt;'EOM';

WARNING: The DBI::Shell interface and functionality are
=======  very likely to change in subsequent versions!

EOM

sub shell {
    my @args = @_ ? @_ : @ARGV;
    $SHELL = DBI::Shell::Std-&gt;new(@args);
    $SHELL-&gt;load_plugins;
    $SHELL-&gt;run;
}


# -------------------------------------------------------------
package DBI::Shell::Std;

use vars qw(@ISA);
@ISA = qw(DBI::Shell::Base);

# XXX this package might be used to override commands etc.


# -------------------------------------------------------------
package DBI::Shell::Base;

use Carp;
use Text::Abbrev ();
use Term::ReadLine;
use Getopt::Long 2.17;	# upgrade from CPAN if needed: http://www.perl.com/CPAN

use DBI 1.00 qw(:sql_types :utils);
use DBI::Format;

my $haveTermReadKey;


sub usage {
    warn &lt;&lt;USAGE;
Usage: perl -MDBI::Shell -e shell [&lt;DBI data source&gt; [&lt;user&gt; [&lt;password&gt;]]]
USAGE
}

sub log {
    my $sh = shift;
    ($sh-&gt;{batch}) ? warn @_,"\n" : print @_,"\n";	# XXX maybe
}

sub alert {	# XXX not quite sure how alert and err relate
    # for msgs that would pop-up an alert dialog if this was a Tk app
    my $sh = shift;
    warn @_,"\n";
}

sub err {	# XXX not quite sure how alert and err relate
    my ($sh, $msg, $die) = @_;
    $msg = "DBI::Shell: $msg\n";
    die $msg if $die;
    $sh-&gt;alert($msg);
}



sub add_option {
    my ($sh, $opt, $default) = @_;
    (my $opt_name = $opt) =~ s/[|=].*//;
    croak "Can't add_option '$opt_name', already defined"
	if exists $sh-&gt;{$opt_name};
    $sh-&gt;{options}-&gt;{$opt_name} = $opt;
    $sh-&gt;{$opt_name} = $default;
}


sub load_plugins {
    my ($sh) = @_;
    my @pi;
    foreach my $where (qw(DBI/Shell DBI_Shell)) {
	my $mod = $where; $mod =~ s!/!::!g; #/ so vim see the syn correctly
	my @dir = map { -d "$_/$where" ? ("$_/$where") : () } @INC;
	foreach my $dir (@dir) {
	    opendir DIR, $dir or warn "Unable to read $dir: $!\n";
	    push @pi, map { s/\.pm$//; "${mod}::$_" } grep { /\.pm$/ }
	        readdir DIR;
	    closedir DIR;
	}
    }
    foreach my $pi (@pi) {
	local $DBI::Shell::SHELL = $sh; # publish the current shell
	$sh-&gt;log("Loading $pi");
	eval qq{ use $pi };
	$sh-&gt;alert("Unable to load $pi: $@") if $@;
    }
    # plug-ins should remove options they recognise from (localized) @ARGV
    # by calling Getopt::Long::GetOptions (which is already in pass_through mode).
    foreach my $pi (@pi) {
	local *ARGV = $sh-&gt;{unhandled_options};
	$pi-&gt;init($sh);
    }
}


sub new {
    my ($class, @args) = @_;
    my $sh = bless {}, $class;

    #
    # Set default configuration options
    #
    foreach my $opt_ref (
	 [ 'command_prefix=s'	=&gt; '/' ],
	 [ 'chistory_size=i'	=&gt; 50 ],
	 [ 'rhistory_size=i'	=&gt; 50 ],
	 [ 'rhistory_head=i'	=&gt;  5 ],
	 [ 'rhistory_tail=i'	=&gt;  5 ],
	 [ 'editor|ed=s'	=&gt; ($ENV{VISUAL} || $ENV{EDITOR} || 'vi') ],
	 [ 'batch'		=&gt; 0 ],
	 [ 'displaymode|display'=&gt; 'neat' ],
	 [ 'columnseparator=s' =&gt; ',' ],
	# defaults for each new database connect:
	 [ 'init_trace|trace=i' =&gt; 0 ],
	 [ 'init_autocommit|autocommit=i' =&gt; 1 ],
	 [ 'debug|d=i'		=&gt; ($ENV{DBISH_DEBUG} || 0) ],
    ) {
	$sh-&gt;add_option(@$opt_ref);
    }


    #
    # Install default commands
    #
    # The sub is passed a reference to the shell and the @ARGV-style
    # args it was invoked with.
    #
    $sh-&gt;{commands} = {

    'help' =&gt; {
	    hint =&gt; "display this list of commands",
    },
    'quit' =&gt; {
	    hint =&gt; "exit",
    },
    'exit' =&gt; {
	    hint =&gt; "exit",
    },
    'trace' =&gt; {
	    hint =&gt; "set DBI trace level for current database",
    },
    'connect' =&gt; {
	    hint =&gt; "connect to another data source/DSN",
    },

    # --- execute commands
    'go' =&gt; {
	    hint =&gt; "execute the current statement",
    },
    'do' =&gt; {
	    hint =&gt; "execute the current (non-select) statement",
    },
    'perl' =&gt; {
	    hint =&gt; "evaluate the current statement as perl code",
    },
    'commit' =&gt; {
	    hint =&gt; "commit changes to the database",
    },
    'rollback' =&gt; {
	    hint =&gt; "rollback changes to the database",
    },
    # --- information commands
    'table_info' =&gt; {
	    hint =&gt; "display tables that exist in current database",
    },
    'type_info' =&gt; {
	    hint =&gt; "display data types supported by current server",
    },
    'drivers' =&gt; {
	    hint =&gt; "display available DBI drivers",
    },

    # --- statement/history management commands
    'clear' =&gt; {
	    hint =&gt; "erase the current statement",
    },
    'redo' =&gt; {
	    hint =&gt; "re-execute the previously executed statement",
    },
    'get' =&gt; {
	    hint =&gt; "make a previous statement current again",
    },
    'current' =&gt; {
	    hint =&gt; "display current statement",
    },
    'edit' =&gt; {
	    hint =&gt; "edit current statement in an external editor",
    },
    'chistory' =&gt; {
	    hint =&gt; "display command history",
    },
    'rhistory' =&gt; {
	    hint =&gt; "display result history",
    },
    'format' =&gt; {
	    hint =&gt; "set display format for selected data (Neat|Box)",
    },
    'history' =&gt; {
	    hint =&gt; "display combined command and result history",
    },
    'option' =&gt; {
	    hint =&gt; "display or set an option value",
    },
    'describe' =&gt; {
	    hint =&gt; "display information about a table",
    },

    };


    # Source config file which may override the defaults.
    # Default is $ENV{HOME}/.dbish_config.
    # Can be overridden with $ENV{DBISH_CONFIG}.
    # Make $ENV{DBISH_CONFIG} empty to prevent sourcing config file.
    # XXX all this will change
    my $homedir = $ENV{HOME}				# unix
		|| "$ENV{HOMEDRIVE}$ENV{HOMEPATH}";	# NT
    $sh-&gt;{config_file} = $ENV{DBISH_CONFIG} || "$homedir/.dbish_config";
    if ($sh-&gt;{config_file} &amp;&amp; -f $sh-&gt;{config_file}) {
	require $sh-&gt;{config_file};
    }
    
    #
    # Handle command line parameters
    #
    # data_source and user command line parameters overrides both 
    # environment and config settings.
    #
    local (@ARGV) = @args;
    my @options = values %{ $sh-&gt;{options} };
    Getopt::Long::config('pass_through');	# for plug-ins
    unless (GetOptions($sh, 'help|h', @options)) {
	$class-&gt;usage;
	croak "DBI::Shell aborted.\n";
    }
    if ($sh-&gt;{help}) {
	$class-&gt;usage;
	return;
    }
    $sh-&gt;{unhandled_options} = [];
    @args = ();
    foreach my $arg (@ARGV) {
	if ($arg =~ /^-/) {	# expected to be in "--opt=value" format
	    push @{$sh-&gt;{unhandled_options}}, $arg;
	}
	else {
	    push @args, $arg;
	}
    }

    $sh-&gt;do_format($sh-&gt;{displaymode});

    $sh-&gt;{data_source}	= shift(@args) || $ENV{DBI_DSN}  || '';
    $sh-&gt;{user}		= shift(@args) || $ENV{DBI_USER} || '';
    $sh-&gt;{password}	= shift(@args) || $ENV{DBI_PASS} || undef;

    $sh-&gt;{chistory} = [];	# command history
    $sh-&gt;{rhistory} = [];	# result  history

    #
    # Setup Term
    #
    my $mode;
    if ($sh-&gt;{batch} || ! -t STDIN) {
	$sh-&gt;{batch} = 1;
	$mode = "in batch mode";
    }
    else {
	$sh-&gt;{term} = new Term::ReadLine($class);
	$mode = "";
    }

    $sh-&gt;log("DBI::Shell $DBI::Shell::VERSION using DBI $DBI::VERSION $mode");
    $sh-&gt;log("DBI::Shell loaded from $INC{'DBI/Shell.pm'}") if $sh-&gt;{debug};

    return $sh;
}


sub run {
    my $sh = shift;

    die "Unrecognised options: @{$sh-&gt;{unhandled_options}}\n"
	if @{$sh-&gt;{unhandled_options}};

    $sh-&gt;log($warning) unless $sh-&gt;{batch};

    # Use valid "dbi:driver:..." to connect with source.
    $sh-&gt;do_connect( $sh-&gt;{data_source} );

    #
    # Main loop
    #
    $sh-&gt;{abbrev} = undef;
    $sh-&gt;{abbrev} = Text::Abbrev::abbrev(keys %{$sh-&gt;{commands}})
	unless $sh-&gt;{batch};
    $sh-&gt;{current_buffer} = '';
    my $current_line = '';

    while (1) {
	my $prefix = $sh-&gt;{command_prefix};

	$current_line = $sh-&gt;readline($sh-&gt;prompt());
	$current_line = "${prefix}quit" unless defined $current_line;

	if ( $current_line =~ /
		^(.*?)
		$prefix
		(?:(\w*)([^\|&gt;]*))?
		((?:\||&gt;&gt;?).+)?
		$
	/x) {
	    my ($stmt, $cmd, $args_string, $output) = ($1, $2, $3, $4||''); 

	    $sh-&gt;{current_buffer} .= "$stmt\n" if length $stmt;

	    $cmd = 'go' if $cmd eq '';
	    my @args = split ' ', $args_string||'';

	    warn("command='$cmd' args='$args_string' output='$output'") 
		    if $sh-&gt;{debug};

	    my $command;
	    if ($sh-&gt;{abbrev}) {
		$command = $sh-&gt;{abbrev}-&gt;{$cmd};
	    }
	    else {
		$command = ($sh-&gt;{commands}-&gt;{$cmd}) ? $cmd : undef;
	    }
	    if ($command) {
		$sh-&gt;run_command($command, $output, @args);
	    }
	    else {
		if ($sh-&gt;{batch}) {
		    die "Command '$cmd' not recognised";
		}
		$sh-&gt;alert("Command '$cmd' not recognised ",
		    "(enter ${prefix}help for help).");
	    }
	}
	elsif ($current_line ne "") {
	    $sh-&gt;{current_buffer} .= $current_line . "\n";
	    # print whole buffer here so user can see it as
	    # it grows (and new users might guess that unrecognised
	    # inputs are treated as commands)
	    $sh-&gt;run_command('current', undef,
		"(enter '$prefix' to execute or '${prefix}help' for help)");
	}
    }
}
	



#
# Internal methods
#

sub readline {
    my ($sh, $prompt) = @_;
    my $rv;
    if ($sh-&gt;{term}) {
	$rv = $sh-&gt;{term}-&gt;readline($prompt);
    }
    else {
	chop($rv = &lt;STDIN&gt;);
    }
    return $rv;
}


sub run_command {
    my ($sh, $command, $output, @args) = @_;
    return unless $command;
    local(*STDOUT) if $output;
    local(*OUTPUT) if $output;
    if ($output) {
	if (open(OUTPUT, $output)) {
	    *STDOUT = *OUTPUT;
	} else {
	    $sh-&gt;err("Couldn't open output '$output'");
	    $sh-&gt;run_command('current', undef, '');
	}
    }
    eval {
	my $code = "do_$command";
	$sh-&gt;$code(@args);
    };
    close OUTPUT if $output;
    $sh-&gt;err("$command failed: $@") if $@;
}


sub print_list {
    my ($sh, $list_ref) = @_;
    for(my $i = 0; $i &lt; @$list_ref; $i++) {
	print $i+1,":  $$list_ref[$i]\n";
    }
}


sub print_buffer {
    my ($sh, $buffer) = @_;
    print $sh-&gt;prompt(), $buffer, "\n";
}


sub get_data_source {
    my ($sh, $dsn, @args) = @_;
    my $driver;

    if ($dsn) {
	if ($dsn =~ m/^dbi:.*:/i) {	# has second colon
	    return $dsn;		# assumed to be full DSN
	}
	elsif ($dsn =~ m/^dbi:([^:]*)/i) {
	    $driver = $1		# use DriverName part
	}
	else {
	    print "Ignored unrecognised DBI DSN '$dsn'.\n";
	}
    }

    if ($sh-&gt;{batch}) {
	die "Missing or unrecognised DBI DSN.";
    }

    print "\n";
    while (!$driver) {
	print "Available DBI drivers:\n";
	my @drivers = DBI-&gt;available_drivers;
	for( my $cnt = 0; $cnt &lt;= $#drivers; $cnt++ ) {
	    printf "%2d: dbi:%s\n", $cnt+1, $drivers[$cnt];
	} 
	$driver = $sh-&gt;readline(
		"Enter driver name or number, or full 'dbi:...:...' DSN: ");
	exit unless defined $driver;	# detect ^D / EOF
	print "\n";

	return $driver if $driver =~ /^dbi:.*:/i; # second colon entered

	if ( $driver =~ /^\s*(\d+)/ ) {
	    $driver = $drivers[$1-1];
	} else {
	    $driver = $1;
	    $driver =~ s/^dbi://i if $driver # incase they entered 'dbi:Name'
	}
	# XXX try to install $driver (if true)
	# unset $driver if install fails.
    }

    my $source;
    while (!defined $source) {
	my $prompt;
	my @data_sources = DBI-&gt;data_sources($driver);
	if (@data_sources) {
	    print "Enter data source to connect to: \n";
	    for( my $cnt = 0; $cnt &lt;= $#data_sources; $cnt++ ) {
		printf "%2d: %s\n", $cnt+1, $data_sources[$cnt];
	    } 
	    $prompt = "Enter data source or number,";
	}
	else {
	    print "(The data_sources method returned nothing.)\n";
	    $prompt = "Enter data source";
	}
	$source = $sh-&gt;readline(
		"$prompt or full 'dbi:...:...' DSN: ");
	return if !defined $source;	# detect ^D / EOF
	if ($source =~ /^\s*(\d+)/) {
	    $source = $data_sources[$1-1]
	}
	elsif ($source =~ /^dbi:([^:]+)$/) { # no second colon
	    $driver = $1;		     # possibly new driver
	    $source = undef;
	}
	print "\n";
    }

    return $source;
}


sub prompt_for_password {
    my ($sh) = @_;
    if (!defined($haveTermReadKey)) {
	$haveTermReadKey = eval { require Term::ReadKey } ? 1 : 0;
    }
    local $| = 1;
    print "Password for $sh-&gt;{user} (",
	($haveTermReadKey ? "not " : "Warning: "),
	"echoed to screen): ";
    if ($haveTermReadKey) {
        Term::ReadKey::ReadMode('noecho');
	$sh-&gt;{password} = Term::ReadKey::ReadLine(0);
	Term::ReadKey::ReadMode('restore');
    } else {
	$sh-&gt;{password} = &lt;STDIN&gt;;
    }
    chomp $sh-&gt;{password};
    print "\n";
}

sub prompt {
    my ($sh) = @_;
    return "" if $sh-&gt;{batch};
    return "(not connected)&gt; " unless $sh-&gt;{dbh};
    return "$sh-&gt;{user}\@$sh-&gt;{data_source}&gt; ";
}


sub push_chistory {
    my ($sh, $cmd) = @_;
    $cmd = $sh-&gt;{current_buffer} unless defined $cmd;
    $sh-&gt;{prev_buffer} = $cmd;
    my $chist = $sh-&gt;{chistory};
    shift @$chist if @$chist &gt;= $sh-&gt;{chistory_size};
    push @$chist, $cmd;
}


#
# Command methods
#

sub do_help {
    my ($sh, @args) = @_;
    my $prefix = $sh-&gt;{command_prefix};
    my $commands = $sh-&gt;{commands};
    print "Defined commands, in alphabetical order:\n";
    foreach my $cmd (sort keys %$commands) {
	my $hint = $commands-&gt;{$cmd}-&gt;{hint} || '';
	printf "  %s%-10s %s\n", $prefix, $cmd, $hint;
    }
    print "Commands can be abbreviated.\n" if $sh-&gt;{abbrev};
}


sub do_format {
    my ($sh, @args) = @_;
    my $mode = $args[0] || '';
    my $col_sep = $args[1];
    my $class = eval { DBI::Format-&gt;formatter($mode) };
    unless ($class) {
	$sh-&gt;alert("Unable to select '$mode': $@");
	return;
    }
    $sh-&gt;log("Using formatter class '$class'") if $sh-&gt;{debug};
    $sh-&gt;{display} = $class-&gt;new($sh);
    $sh-&gt;do_option("columnseparator=$col_sep") if $col_sep;
}


sub do_go {
    my ($sh, @args) = @_;

    return if $sh-&gt;{current_buffer} eq '';

    $sh-&gt;{prev_buffer} = $sh-&gt;{current_buffer};

    $sh-&gt;push_chistory;
    
    eval {
	my $sth = $sh-&gt;{dbh}-&gt;prepare($sh-&gt;{current_buffer});

	$sh-&gt;sth_go($sth, 1);
    };
    if ($@) {
	my $err = $@;
	$err =~ s: at \S*DBI/Shell.pm line \d+(,.*?chunk \d+)?::
		if !$sh-&gt;{debug} &amp;&amp; $err =~ /^DBD::\w+::\w+ \w+/;
	print "$err";
    }

    # There need to be a better way, maybe clearing the
    # buffer when the next non command is typed.
    # Or sprinkle &lt;$sh-&gt;{current_buffer} ||= $sh-&gt;{prev_buffer};&gt;
    # around in the code.
    $sh-&gt;{current_buffer} = '';
}


sub sth_go {
    my ($sh, $sth, $execute) = @_;

    my $rv;
    if ($execute || !$sth-&gt;{Active}) {
	my @params;
	my $params = $sth-&gt;{NUM_OF_PARAMS} || 0;
	print "Statement has $params parameters:\n" if $params;
	foreach(1..$params) {
	    my $val = $sh-&gt;readline("Parameter $_ value: ");
	    push @params, $val;
	}
	$rv = $sth-&gt;execute(@params);
    }
	
    if (!$sth-&gt;{'NUM_OF_FIELDS'}) { # not a select statement
	local $^W=0;
	$rv = "undefined number of" unless defined $rv;
	$rv = "unknown number of"   if $rv == -1;
	print "[$rv row" . ($rv==1 ? "" : "s") . " affected]\n";
	return;
    }

    $sh-&gt;{sth} = $sth;

    #
    # Remove oldest result from history if reached limit
    #
    my $rhist = $sh-&gt;{rhistory};
    shift @$rhist if @$rhist &gt;= $sh-&gt;{rhistory_size};
    push @$rhist, [];

    #
    # Keep a buffer of $sh-&gt;{rhistory_tail} many rows,
    # when done with result add those to rhistory buffer.
    # Could use $sth-&gt;rows(), but not all DBD's support it.
    #
    my @rtail;
    my $i = 0;
    my $display = $sh-&gt;{display} || die "panic: no display set";
    $display-&gt;header($sth, \*STDOUT, $sh-&gt;{columnseparator});
    while (my $rowref = $sth-&gt;fetchrow_arrayref()) {
	$i++;

	$display-&gt;row($rowref);

	if ($i &lt;= $sh-&gt;{rhistory_head}) {
	    push @{$rhist-&gt;[-1]}, [@$rowref];
	}
	else {
	    shift @rtail if @rtail == $sh-&gt;{rhistory_tail};
	    push @rtail, [@$rowref];
	}

    }
    $display-&gt;trailer($i);

    if (@rtail) {
	my $rows = $i;
	my $ommitted = $i - $sh-&gt;{rhistory_head} - @rtail;
	    push(@{$rhist-&gt;[-1]},
		 [ "[...$ommitted rows out of $rows ommitted...]"]);
	foreach my $rowref (@rtail) {
	    push @{$rhist-&gt;[-1]}, $rowref;
	}
    }

    #$sh-&gt;{sth} = undef;
    #$sth-&gt;finish();	# drivers which need this are broken
}


sub do_do {
    my ($sh, @args) = @_;
    $sh-&gt;push_chistory;
    my $rv = $sh-&gt;{dbh}-&gt;do($sh-&gt;{current_buffer});
    print "[$rv row" . ($rv==1 ? "" : "s") . " affected]\n"
	if defined $rv;

    # XXX I question setting the buffer to '' here.
    # I may want to edit my line without having to scroll back.
    $sh-&gt;{current_buffer} = '';
}


sub do_disconnect {
    my ($sh, @args) = @_;
    return unless $sh-&gt;{dbh};
    $sh-&gt;log("Disconnecting from $sh-&gt;{data_source}.");
    eval {
	$sh-&gt;{sth}-&gt;finish if $sh-&gt;{sth};
	$sh-&gt;{dbh}-&gt;rollback unless $sh-&gt;{dbh}-&gt;{AutoCommit};
	$sh-&gt;{dbh}-&gt;disconnect;
    };
    $sh-&gt;alert("Error during disconnect: $@") if $@;
    $sh-&gt;{sth} = undef;
    $sh-&gt;{dbh} = undef;
}


sub do_connect {
    my ($sh, $dsn, $user, $pass) = @_;

    $dsn = $sh-&gt;get_data_source($dsn);
    return unless $dsn;

    $sh-&gt;do_disconnect if $sh-&gt;{dbh};

    $sh-&gt;{data_source} = $dsn;
    if (defined $user and length $user) {
	$sh-&gt;{user}     = $user;
	$sh-&gt;{password} = undef;	# force prompt below
    }

    $sh-&gt;log("Connecting to '$sh-&gt;{data_source}' as '$sh-&gt;{user}'...");
    if ($sh-&gt;{user} and !defined $sh-&gt;{password}) {
	$sh-&gt;prompt_for_password();
    }
    $sh-&gt;{dbh} = DBI-&gt;connect(
	$sh-&gt;{data_source}, $sh-&gt;{user}, $sh-&gt;{password}, {
	    AutoCommit =&gt; $sh-&gt;{init_autocommit},
	    PrintError =&gt; 0,
	    RaiseError =&gt; 1,
	    LongTruncOk =&gt; 1,	# XXX
    });
    $sh-&gt;{dbh}-&gt;trace($sh-&gt;{init_trace}) if $sh-&gt;{init_trace};
}


sub do_current {
    my ($sh, $msg, @args) = @_;
    $msg = $msg ? " $msg" : "";
    $sh-&gt;log("Current statement buffer$msg:\n" . $sh-&gt;{current_buffer});
}


sub do_trace {
    shift-&gt;{dbh}-&gt;trace(@_);
}

sub do_commit {
    shift-&gt;{dbh}-&gt;commit(@_);
}

sub do_rollback {
    shift-&gt;{dbh}-&gt;rollback(@_);
}


sub do_quit {
    my ($sh, @args) = @_;
    $sh-&gt;do_disconnect if $sh-&gt;{dbh};
    undef $sh-&gt;{term};
    exit 0;
}

# Until the alias command is working each command requires definition.
sub do_exit { shift-&gt;do_quit(@_); }

sub do_clear {
    my ($sh, @args) = @_;
    $sh-&gt;{current_buffer} = '';
}


sub do_redo {
    my ($sh, @args) = @_;
    $sh-&gt;{current_buffer} = $sh-&gt;{prev_buffer} || '';
    $sh-&gt;run_command('go') if $sh-&gt;{current_buffer};
}


sub do_chistory {
    my ($sh, @args) = @_;
    $sh-&gt;print_list($sh-&gt;{chistory});
}

sub do_history {
    my ($sh, @args) = @_;
    for(my $i = 0; $i &lt; @{$sh-&gt;{chistory}}; $i++) {
	print $i+1, ":\n", $sh-&gt;{chistory}-&gt;[$i], "--------\n";
	foreach my $rowref (@{$sh-&gt;{rhistory}[$i]}) {
	    print "    ", join(", ", @$rowref), "\n";
	}
    }
}

sub do_rhistory {
    my ($sh, @args) = @_;
    for(my $i = 0; $i &lt; @{$sh-&gt;{rhistory}}; $i++) {
	print $i+1, ":\n";
	foreach my $rowref (@{$sh-&gt;{rhistory}[$i]}) {
	    print "    ", join(", ", @$rowref), "\n";
	}
    }
}


sub do_get {
    my ($sh, $num, @args) = @_;
    if (!$num || $num !~ /^\d+$/ || !defined($sh-&gt;{chistory}-&gt;[$num-1])) {
	$sh-&gt;err("No such command number '$num'. Use /chistory to list previous commands.");
	return;
    }
    $sh-&gt;{current_buffer} = $sh-&gt;{chistory}-&gt;[$num-1];
    $sh-&gt;print_buffer($sh-&gt;{current_buffer});
}


sub do_perl {
    my ($sh, @args) = @_;
	$DBI::Shell::eval::dbh = $sh-&gt;{dbh};
    eval "package DBI::Shell::eval; $sh-&gt;{current_buffer}";
    if ($@) { $sh-&gt;err("Perl failed: $@") }
    $sh-&gt;run_command('clear');
}


sub do_edit {
    my ($sh, @args) = @_;

    $sh-&gt;run_command('get', '', $&amp;) if @args and $args[0] =~ /^\d+$/;
    $sh-&gt;{current_buffer} ||= $sh-&gt;{prev_buffer};
	    
    # Find an area to write a temp file into.
    my $tmp_dir = $ENV{DBISH_TMP} || # Give people the choice.
	    $ENV{TMP}  ||            # Is TMP set?
	    $ENV{TEMP} ||            # How about TEMP?
	    $ENV{HOME} ||            # Look for HOME?
	    $ENV{HOMEDRIVE} . $ENV{HOMEPATH} || # Last env checked.
	    ".";       # fallback: try to write in current directory.
    my $tmp_file = "$tmp_dir/dbish$$.sql";

    local (*FH);
    open(FH, "&gt;$tmp_file") ||
	    $sh-&gt;err("Can't create $tmp_file: $!\n", 1);
    print FH $sh-&gt;{current_buffer} if defined $sh-&gt;{current_buffer};
    close(FH) || $sh-&gt;err("Can't write $tmp_file: $!\n", 1);

    my $command = "$sh-&gt;{editor} $tmp_file";
    system($command);

    # Read changes back in (editor may have deleted and rewritten file)
    open(FH, "&lt;$tmp_file") || $sh-&gt;err("Can't open $tmp_file: $!\n");
    $sh-&gt;{current_buffer} = join "", &lt;FH&gt;;
    close(FH);
    unlink $tmp_file;

    $sh-&gt;run_command('current');
}


sub do_drivers {
    my ($sh, @args) = @_;
    $sh-&gt;log("Available drivers:");
    my @drivers = DBI-&gt;available_drivers;
    foreach my $driver (sort @drivers) {
	$sh-&gt;log("\t$driver");
    }
}


sub do_type_info {
    my ($sh, @args) = @_;
    my $dbh = $sh-&gt;{dbh};
    my $ti = $dbh-&gt;type_info_all;
    my $ti_cols = shift @$ti;
    my @names = sort { $ti_cols-&gt;{$a} &lt;=&gt; $ti_cols-&gt;{$b} } keys %$ti_cols;
    my $sth = $sh-&gt;prepare_from_data("type_info", $ti, \@names);
    $sh-&gt;sth_go($sth, 0);
}

sub do_describe {
    my ($sh, $tab, @argv) = @_;
	$sh-&gt;log( "Describle: $tab" );
	my $dbh = $sh-&gt;{dbh};
	my $sql = qq{select * from $tab where 1 = 0};
	my $sth = $dbh-&gt;prepare( $sql );
	$sth-&gt;execute;
	my $cnt = $#{$sth-&gt;{NAME}};  #
    	my @names = qw{NAME TYPE NULLABLE};
	my @ti;
	#push( @j, join( "\t", qw{NAME TYPE PRECISION SCALE NULLABLE}));
	for ( my $c = 0; $c &lt;= $cnt; $c++ ) {
		push( my @j, $sth-&gt;{NAME}-&gt;[$c] || 0 );
		my $m = $dbh-&gt;type_info($sth-&gt;{TYPE}-&gt;[$c]);
		my $s;
		if (ref $m eq 'HASH') {
			$s = $m-&gt;{TYPE_NAME};
		} elsif (not defined $m) {
			 $s = q{undef } . $sth-&gt;{TYPE}-&gt;[$c];
		} else {
			warn "describe: can't parse data ($m) from type_info!";
		}

		if (defined $sth-&gt;{PRECISION}-&gt;[$c]) {
			$s .= "(" . $sth-&gt;{PRECISION}-&gt;[$c] || '';
			$s .= "," . $sth-&gt;{SCALE}-&gt;[$c] 
			if ( defined $sth-&gt;{SCALE}-&gt;[$c] 
				and $sth-&gt;{SCALE}-&gt;[$c] ne 0);
			$s .= ")";
		}
		push(@j, $s,
			 $sth-&gt;{NULLABLE}-&gt;[$c] ne 1? qq{N}: qq{Y} );
		push(@ti,\@j);
	}
	$sth-&gt;finish;
	$sth = $sh-&gt;prepare_from_data("describe", \@ti, \@names);
	$sh-&gt;sth_go($sth, 0);
}


sub prepare_from_data {
    my ($sh, $statement, $data, $names, %attr) = @_;
    my $sponge = DBI-&gt;connect("dbi:Sponge:","","",{ RaiseError =&gt; 1 });
    my $sth = $sponge-&gt;prepare($statement, { rows=&gt;$data, NAME=&gt;$names, %attr });
    return $sth;
}


# Do option: sets or gets an option
sub do_option {
    my ($sh, @args) = @_;

    unless (@args) {
	foreach my $opt (sort keys %{ $sh-&gt;{options}}) {
	    my $value = (defined $sh-&gt;{$opt}) ? $sh-&gt;{$opt} : 'undef';
	    $sh-&gt;log(sprintf("%20s: %s", $opt, $value));
	}
	return;
    }

    my $options = Text::Abbrev::abbrev(keys %{$sh-&gt;{options}});

    # Expecting the form [option=value] [option=] [option]
    foreach my $opt (@args) {
	my ($opt_name, $value) = $opt =~ /^\s*(\w+)(?:=(.*))?/;
	$opt_name = $options-&gt;{$opt_name} || $opt_name if $opt_name;
	if (!$opt_name || !$sh-&gt;{options}-&gt;{$opt_name}) {
	    $sh-&gt;log("Unknown or ambiguous option name '$opt_name' (use name=value format)");
	    next;
	}
	my $crnt = (defined $sh-&gt;{$opt_name}) ? $sh-&gt;{$opt_name} : 'undef';
	my $log;
	if (not defined $value) {
	    $log = "$opt_name=$crnt";
	}
	else {
	    $log = "/option $opt_name=$value  (was $crnt)";
	    $sh-&gt;{$opt_name} = ($value eq 'undef') ? undef : $value;
	}
	$sh-&gt;log($sh-&gt;{command_prefix}."option $log");
    }
}


sub do_table_info {
    my ($sh, @args) = @_;
    my $dbh = $sh-&gt;{dbh};
    my $sth = $dbh-&gt;table_info(@args);
    unless(ref $sth) {
	print "Driver has not implemented the table_info() method, ",
		"trying tables()\n";
	my @tables = $dbh-&gt;tables(@args); # else try list context
	unless (@tables) {
	    print "No tables exist ",
		  "(or driver hasn't implemented the tables method)\n";
	    return;
	}
	$sth = $sh-&gt;prepare_from_data("tables",
		[ map { [ $_ ] } @tables ],
		[ "TABLE_NAME" ]
	);
    }
    $sh-&gt;sth_go($sth, 0);
}



1;
__END__

=head1 TO DO

Proper docs - but not yet, too much is changing.

"/source file" command to read command file.
Allow to nest via stack of command file handles.
Add command log facility to create batch files.

Commands:
	load (query?) from file
	save (query?) to file

Use Data::ShowTable if available.

Define DBI::Shell plug-in semantics.
	Implement import/export as plug-in module

Clarify meaning of batch mode

Completion hooks

Set/Get DBI handle attributes

Portability

Emulate popular command shell modes (Oracle, Ingres etc)?

=head1 COMMANDS

Many commands - few documented, yet!

=over 4

=item help

  /help

=item chistory

  /chistory          (display history of all commands entered)
  /chistory | YourPager (display history with paging)

=item clear

  /clear             (Clears the current command buffer)

=item commit

  /commit            (commit changes to the database)

=item connect

  /connect           (pick from available drivers and sources)
  /connect dbi:Oracle (pick source from based on driver)
  /connect dbi:YourDriver:YourSource i.e. dbi:Oracle:mysid

Use this option to change userid or password.

=item current

  /current            (Display current statement in the buffer)

=item do

  /do                 (execute the current (non-select) statement)

	dbish&gt; create table foo ( mykey integer )
	dbish&gt; /do

	dbish&gt; truncate table OldTable /do (Oracle truncate)

=item drivers

  /drivers            (Display available DBI drivers)

=item edit

  /edit               (Edit current statement in an external editor)

Editor is defined using the enviroment variable $VISUAL or
$EDITOR or default is vi.  Use /option editor=new editor to change
in the current session.

To read a file from the operating system invoke the editor (/edit)
and read the file into the editor buffer.

=item exit

  /exit              (Exits the shell)

=item get

  /get               (Retrieve a previous command to the current buffer)

=item go

  /go                (Execute the current statement)

Run (execute) the statement in the current buffer.  This is the default
action if the statement ends with /

	dbish&gt; select * from user_views/

	dbish&gt; select table_name from user_tables
	dbish&gt; where table_name like 'DSP%'
	dbish&gt; /

	dbish&gt; select table_name from all_tables/ | more

=item history

  /history            (Display combined command and result history)
  /history | more

=item option

  /option [option1[=value]] [option2 ...]
  /option            (Displays the current options)
  /option   MyOption (Displays the value, if exists, of MyOption)
  /option   MyOption=4 (defines and/or sets value for MyOption)

=item perl

  /perl               (Evaluate the current statement as perl code)

=item quit

  /quit               (Leaves shell.  Same as exit)

=item redo

  /redo               (Re-execute the previously executed statement)

=item rhistory

  /rhistory           (Display result history)

=item rollback

  /rollback           (rollback changes to the database)

For this to be useful, turn the autocommit off. /option autocommit=0

=item table_info

  /table_info         (display all tables that exist in current database)
  /table_info | more  (for paging)

=item trace

  /trace              (set DBI trace level for current database)

Adjust the trace level for DBI 0 - 4.  0 off.  4 is lots of information.
Useful for determining what is really happening in DBI.  See DBI.

=item type_info

  /type_info          (display data types supported by current server)

=back

=head1 AUTHORS and ACKNOWLEDGEMENTS

The DBI::Shell has a long lineage.

It started life around 1994-1997 as the pmsql script written by Andreas
König. Jochen Wiedmann picked it up and ran with it (adding much along
the way) as I&lt;dbimon&gt;, bundled with his DBD::mSQL driver modules. In
1998, around the time I wanted to bundle a shell with the DBI, Adam
Marks was working on a dbish modeled after the Sybase sqsh utility.

Wanting to start from a cleaner slate than the feature-full but complex
dbimon, I worked with Adam to create a fairly open modular and very
configurable DBI::Shell module. Along the way Tom Lowery chipped in
ideas and patches. As we go further along more useful code and concepts
from Jochen's dbimon is bound to find it's way back in.

=head1 COPYRIGHT

The DBI::Shell module is Copyright (c) 1998 Tim Bunce. England.
All rights reserved. Portions are Copyright by Jochen Wiedmann,
Adam Marks and Tom Lowery.

You may distribute under the terms of either the GNU General Public
License or the Artistic License, as specified in the Perl README file.

=cut
</pre></body></html>