*** opmsql	Fri Aug 29 14:27:48 1997

--- pmsql	Sat Aug 30 01:05:26 1997

***************

*** 76,81 ****

--- 76,86 ----

  

  my $database = $ARGV[0] || "";

  

+ # fancy output/msqlexport functionality

+ 

+ $fancy_output = 1;

+ $sepchar = ":";

+ $quote = "";

  

  #

  # Greetings

***************

*** 181,186 ****

--- 186,226 ----

  	# RELSHOW

  

  	print relshow(@arg);

+     } elsif ($command =~ /^f(a(n(c(y)?)?)?)?$/i) {

+ 	$arg[0] = '' if (!$arg[0]);

+ 	if ($arg[0] eq 'off') {

+ 	    $fancy_output = 0;

+ 	} elsif ($arg[0] eq 'on') {

+ 	    $fancy_output = 1;

+ 	} else {

+ 	    print "Fancy output is ", $fancy_output?"on":"off", "\n";

+ 	}

+     } elsif ($command =~ /^sep(a(r(a(t(o(r)?)?)?)?)?)?$/i) {

+ 	$arg[0] = '' if (!$arg[0]);

+ 	if ($arg[0] eq 'space') {

+ 	    $sepchar = " ";

+ 	} elsif ($arg[0] eq 'tab') {

+ 	    $sepchar = "\t";

+ 	} elsif ($arg[0] eq 'null') {

+ 	    undef($sepchar);

+ 	} elsif (!$arg[0]) {

+ 	    print "Separator: \"$sepchar\"\n";

+ 	} else {

+ 	    $sepchar = join('',@arg);

+ 	}

+     } elsif ($command =~ /^quo(t(e)?)?$/i) {

+ 	$arg[0] = '' if (!$arg[0]);

+ 	if ($arg[0] eq 'space') {

+ 	    $quote = " ";

+ 	} elsif ($arg[0] eq 'tab') {

+ 	    $quote = "\t";

+ 	} elsif (!$arg[0]) {

+ 	    print "Quote string \"$quote\"\n";

+ 	} elsif ($arg[0] eq 'null') {

+ 	    undef($quote);

+ 	} else {

+ 	    $quote = join('',@arg);

+ 	}

      } elsif ($command =~ /^q(u(i(t)?)?)?$/i) {

  

  	# QUIT

***************

*** 198,212 ****

  	my $Db = Msql->connect($host,$database) or next;

  	s/\\[qgp]$//;

  	$::Q = $Db->query($_) or next;

! 	print "Query ok\n";

  	if (ref $::Q) {

  	    $::Q->optimize(1);

! 	    if ($Less) {

  		open OUT, "| $Less";

  	    } else {

  		open OUT, ">&STDOUT";

  	    }

! 	    print OUT $::Q->as_string;

  	    close OUT;

  	}

      }

--- 238,256 ----

  	my $Db = Msql->connect($host,$database) or next;

  	s/\\[qgp]$//;

  	$::Q = $Db->query($_) or next;

! 	print "Query ok\n" if ($fancy_output);

  	if (ref $::Q) {

  	    $::Q->optimize(1);

! 	    if ($Less && (lc($Less) ne 'stdout')) {

  		open OUT, "| $Less";

  	    } else {

  		open OUT, ">&STDOUT";

  	    }

! 	    if ($fancy_output) {

! 		print OUT $::Q->as_string;

! 	    } else {

! 		print OUT sep_out($sepchar);

! 	    }

  	    close OUT;

  	}

      }

***************

*** 226,240 ****

      print STDERR "complete line[$line] pos[$pos]" if $Debug & 2;

      $line =~ s/^\s*//;

      return

! 	$pos == 0 ? grep /^$word/i, ('!','?','create','database','delete from','drop table','host','insert into','quit','relshow','select','update') :

! 	$line =~ /^[\!\?qch]/i ? () :                                  # quit, create, host

  	$line =~ /^da/i ? complete_database($word) :		       # database

  	$line =~ /^de/i ? complete_table_or_field($word,$line) :       # delete

  	$line =~ /^dr/i ? complete_table($word,$line) :		       # drop

  	$line =~ /^in/i ? complete_table_or_field($word,$line) :       # insert

  	$line =~ /^re/i ? complete_for_relshow($word,$line) :          # relshow

! 	$line =~ /^se/i ? complete_table_or_field($word,$line) :       # select

! 	$line =~ /^up/i ? complete_table_or_field($word,$line) : ();   # update

  }

  

  sub complete_database {

--- 270,290 ----

      print STDERR "complete line[$line] pos[$pos]" if $Debug & 2;

      $line =~ s/^\s*//;

      return

! 	$pos == 0 ? grep /^$word/i, ('!','?','create','database','delete from',

! 				     'drop table','host','insert into','quit',

! 				     'relshow','select','update','fancy',

! 				     'separator','quote') :

! 	$line =~ /^[\!\?ch]/i ? () :                                  # quit, create, host

  	$line =~ /^da/i ? complete_database($word) :		       # database

  	$line =~ /^de/i ? complete_table_or_field($word,$line) :       # delete

  	$line =~ /^dr/i ? complete_table($word,$line) :		       # drop

  	$line =~ /^in/i ? complete_table_or_field($word,$line) :       # insert

  	$line =~ /^re/i ? complete_for_relshow($word,$line) :          # relshow

! 	$line =~ /^sel/i ? complete_table_or_field($word,$line) :      # select

! 	$line =~ /^up/i ? complete_table_or_field($word,$line) :       # update

! 	$line =~ /^sep/i ? complete_option($word,$line) :	       # separator

! 	$line =~ /^quo/i ? complete_option($word,$line) :	       # quote

! 	$line =~ /^f/i ? complete_option($word,$line): ();	       # fancy output

  }

  

  sub complete_database {

***************

*** 242,247 ****

--- 292,312 ----

      grep /^\Q$word/, Msql->connect($host)->listdbs;

  }

  

+ sub complete_option {

+     my($word,$line) = @_;

+     if ($line =~ /^fancy/) {

+ 	if ($fancy_output) {

+ 	    return "off";

+ 	} else {

+ 	    return "on";

+ 	}

+     } elsif ($line =~ /^separator/) {

+ 	grep /^\Q$word/, qw(space tab null);

+     } elsif ($line =~ /^quote/) {

+ 	grep /^\Q$word/, qw(space tab null);

+     }

+ }

+ 

  sub complete_for_relshow {

      my($word,$line) = @_;

      my @t = split " ", $line;

***************

*** 441,446 ****

--- 506,526 ----

  	}

      }

      return join "", @m;

+ }

+ 

+ sub sep_out {

+     my($sep)=$_[0];

+     my(@arr);

+     my(@res);

+     while (@arr = $::Q->fetchrow()) {

+ 	if ($quote) {

+ 	    for (@arr) {

+ 		$_ = "$quote$_$quote";

+ 	    }

+ 	}

+ 	push(@res, join($sepchar, @arr), "\n");

+     }

+     return (@res);

  }

  

  sub usage () {"Usage: $0 [-h host] database";}

