Advertisement
samv

Extract SQLS from PowerBuilder DBTrace

May 16th, 2014
720
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 3.02 KB | None | 0 0
  1. # pb_sqls.pl - Script to extract SQLs from dbtrace file generated by PB application
  2. # Usage: pb_sqls.pl -f <dbtrace file name> > <output-file>
  3. #           Note: dbtrace.log needs to be saved to a ANSI format first,.
  4.  
  5. # Author: Sam Varadarajan
  6. # Date: 10/04/2010
  7. # Purpose: This script extracts the SQLs from a dbtrace file. dbtrace.log file is created when a PB application is
  8. #           run with TRACE on for the DBMS.
  9.  
  10. # Modification History:
  11. # Copyright, Sam Varadarajan, 2010 - 2014
  12. #use warnings;
  13. #use strict;
  14.  
  15. #globals
  16. use vars qw/ %opt /;
  17.  
  18. my $file = "c:\\windows\\dbtrace.log";
  19. #main()
  20.  
  21. init(); # get command line options
  22.  
  23. if ($opt{'f'}) {
  24.    $file = $opt{'f'};
  25. }
  26. process_file($file, 'SELECT,INSERT,DELETE,UPDATE');
  27.  
  28. #end main
  29.  
  30.  
  31. #
  32. # Command line options processing
  33. #
  34. sub init()
  35. {
  36.     use Getopt::Std;
  37.     my $opt_string = 'f:';
  38.     getopts( "$opt_string", \%opt ) or usage();
  39.     usage() if $opt{h};
  40. }
  41.  
  42.  
  43. #
  44. # Message about this program and how to use it
  45. #
  46. sub usage()
  47. {
  48.   print STDERR << "EOF";
  49.  
  50.     This program extracts SQLs from a DBTrace file generated by PB application
  51.  
  52.     usage: $0 [-h] [-f <file>]
  53.  
  54.     -h          : this (help) message
  55.    -f          : File name to extract from
  56.     example: $0 -f <filename>
  57.  
  58. EOF
  59.     exit;
  60. }
  61.  
  62. sub process_file()
  63. {
  64.  my $path = @_[0];
  65.  my $SQLOP= ();
  66.  @SQLOP = split(',', @_[1]); # SELECT/INSERT/UPDATE/DELETE
  67.  #print join('|', @SQLOP);
  68.  my $sql_block_found = 0;
  69.  my @lines = ();
  70.  my $fcount = 0;
  71.  my $line = '';
  72.  my $result = '';
  73.  #open (LOGFILE, $path)  or die "$0: Cannot open $path: $!\n";
  74.  open LOGFILE, "<:encoding(UCS-2LE)", $path or die "Can't open $path $!\n";
  75.  print "--".$path."\n";
  76.  while (<LOGFILE>) {
  77.   chomp;
  78.   my $str = $_;
  79.   #print $str;
  80.  
  81.    if ($str =~ /PREPARE:/) {
  82.       #print $str;
  83.       $sql_block_found = 1;
  84.       next;
  85.    }
  86.    elsif (($sql_block_found == 2) &&
  87.                ($str =~ /GET AFFECTED ROWS:/ || /BIND SELECT OUTPUT BUFFER/ || /DESCRIBE:/ || /EXECUTE:/)) {
  88.       #print $str;
  89.       $result = join("\n", @lines);
  90.  
  91.       $result =~ s/FROM/\nFROM/;
  92.       $result =~ s/VALUES/\nVALUES/;
  93.       $result =~ s/SET/\nSET/;
  94.       $result =~ s/WHERE/\nWHERE/;
  95.       $result =~ s/AND/\nAND/;
  96.       $result =~ s/ORDER BY/\nORDER BY/;
  97.       $result =~ s/GROUP BY/\nGROUP BY/;
  98.       $result =~ s/\([0-9.]+ MS \/ [0-9.]+ MS\)//;
  99.       if ($result && $result !~ /FN_SYSDATE/ && $result !~ /NAV_/)
  100.       {
  101.           print $result.";\n\n";
  102.       }
  103.       $sql_block_found = 0;
  104.       @lines = ();
  105.       $fcount = 0;
  106.       next;
  107.    }
  108.    elsif ($str =~ "^\(.*\):")
  109.    {
  110.       $str =~ s/^\(.*\):[ ]*//;
  111.       OP: foreach $op(@SQLOP) {
  112.          if ($str =~ /^$op/i) {
  113.             $sql_block_found = 2;
  114.             last OP;
  115.          }
  116.       }
  117.    }
  118.    
  119.    if ($sql_block_found == 2)
  120.    {
  121.       #print $str."\n";
  122.       push(@lines, $str);
  123.    }
  124.    elsif ($str =~ /\/\*[ ]*[0-9]{2}.*\*\//)
  125.    {
  126.       #remove /* */
  127.       $str =~ s/\/\*[ ]*/-- /;
  128.       $str =~ s/\*\///;
  129.       push(@lines, $str);
  130.    }
  131.  }
  132.  close(LOGFILE);
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement