#!/usr/bin/perl # # a frontend to mysql and gnuplot, v0.01 # # features that would be nice: # 1) easy set up of plotting options # 2) default from and default limit clauses automatically overriden if included on command line # 3) readline history # use DBI; use Data::Dumper; $dbh = DBI->connect("dbi:mysql:db=primes",undef,undef) || die "Failed to connect to database"; $alive = 1; while($alive) { print "select>"; $select = <>; #$select = "SELECT n & dPrevPrime FROM numbers LIMIT 500"; $default = "from numbers limit 100"; $slc = lc $select; if($slc =~ /quit.*/) { exit(0); } elsif (($set) = $slc =~ /set (.*)/) { ($token, $value) = $set =~ /(.*?) (.*)/; print "Set $token: $value\n"; if(!$token && ($value eq "default")) { $default = ""; } if($token eq "default") { $default = $value; print "Default now $value\n"; } } elsif(($graph) = $select =~ /graph (.*)/) { $sth = $dbh->prepare($graph . " " . $default); if($sth->execute()) { graph($sth); } else { print "Error: " . $DBI::errstr . "\n"; } } else { open(FOO,"|mysql --table primes"); if($slc =~ /.*select.*/) { print FOO "$select $default\n"; } else { print FOO "$select\n"; } close(FOO); } } sub graph { my $sth = shift; $file = "/tmp/graph.$$"; open(FOO,">$file"); $a = 0; while($data = $sth->fetchrow_arrayref()) { $rows = @{$data}; print FOO $a++; for($i=0;$i<$rows;$i++) { print FOO "\t"; print FOO @{$data}[$i]; } print FOO "\n"; } close(FOO); $rc = ""; $rcb = ""; for($i=0;$i<$rows;$i++) { $rc .= ":" if(length($rc)); $rc .= $i; $rcb .= ",\\\n" if(length($rcb)); $rcb .= "'" . $file . "' using 1:" . ($i+2) ." with lines"; print "rcb: $rcb\n"; } $ofile = "/tmp/script.$$"; $pfile = "output.png"; $script = "set terminal png\n"; $script .= "set out \"$pfile\"\n"; #print SCRIPT "set linestyle 1 linetype 1 linewidth 1 pointstyle 4 pointsize 1\n"; $script .= "set ls 1\n"; #print SCRIPT "plot '$file' using $rc with $rcb\n"; $script .= "plot $rcb\n"; open(SCRIPT,">$ofile"); print SCRIPT $script; print $script; close(SCRIPT); system("gnuplot $ofile"); unlink($ofile); unlink($file); }