# file    : getparsetime.pl
# last    : 9-Jul-2012
# author  : Craig Shallahamer, craig@orapub.com
# warrenty: Absolutely no warrenty or implied usefullness. Use at your own risk.

$sqlid=@ARGV[0];  	# this is simply a marker and can be filled with anything.
$tracefile=@ARGV[1];	# this is an Oracle sql trace file with its full path

#$sqlid=100;
#$tracefile='/home/oracle/base/diag/rdbms/prod23/prod23/trace/prod23_ora_15921.trc';

open(OUTFILE, ">>./op_results.sql");

open(INPUTFILE, "<$tracefile"); 
@lines = <INPUTFILE>; 

$count  = 0;
$select = "N" ;

foreach $line (@lines) { 
  if  ( $line =~ m/select distinct/ ) {
    $select = "Y" ;
  }

  if  ( ( $select eq "Y" ) && ( $line =~ m/PARSE/ ) ) {
    $count++;

    @splitline = split(/,/, $line);

    @csplit = split(/=/, @splitline[0]);
    $ctime  = @csplit[1];

    @esplit = split(/=/, @splitline[1]);
    $etime  = @esplit[1];
    
    $select = "N" ;

    $output = "insert into op\_results values ($sqlid,$count,$ctime,$etime,\'" . $tracefile . "\');\n";

    #print "$line";
    #print $output ;
    print OUTFILE $output ;
  }
  else {
    #print "NO($select)  : $line";
  }
}

close(OUTFILE);
close(INPUTFILE);

