in reply to Perfmon log parser. Problem with column names having special chars.

I think, I have found a work around for this. After looking at here I found that SQL::Tokenizer was suitable for my case and used regex used by the Tokenizer module to sanitize my query. It needs a little working on but, for now works for most of my test cases.

use strict; my $insane_sql = shift or die "usage sanitize_sql_query.pl <insane que +ry>"; print "\n\n$insane_sql\n\n"; my $sane_sql = ""; #Match anything inside single quotes. #Got it from SQL::Tokenizer. #Author:Igor Sutton Lopes while ($insane_sql =~ m/'.*?(?:(?:''){1,}'|(?<!['\\])'(?!')|\\'{2})/sm +xg) { my $pre_match = $`; my $match = $&; my $post_match = $'; print "Match: $match\n"; $match =~ s/'//g; #replace the single quotes $match =~ s/\W/_/g; #sanitize the match $insane_sql = $pre_match . $match . $post_match; } print "\n\n$insane_sql\n\n"; __END__ perl sanitize_sql_query.pl "SELECT TO_DATE(['(PDH-CSV 4.0) (Central St +andard Time)(360)']) AS CAPTUREDATE, AVG(TO_REAL(['\\Server\LogicalDi +sk(N:)\Avg. Disk sec/Read'])) AS AVG_LOG_SEC_READ FROM 'C:\PerfCSV\Se +rver_01200549.csv' GROUP BY TO_DATE(['(PDH-CSV 4.0) (Central Standard + Time)(360)'])" SELECT TO_DATE(['(PDH-CSV 4.0) (Central Standard Time)(360)']) AS CAPT +UREDATE, AVG(TO_REAL(['\\Server\LogicalDisk(N:)\Avg. Disk sec/Read']) +) AS AVG_LOG_SEC_READ FROM 'C:\PerfCSV\Server_01200549.csv' GROUP BY +TO_DATE(['(PDH-CSV 4.0) (Central Standard Time)(360)']) Match: '(PDH-CSV 4.0) (Central Standard Time)(360)' Match: '\\Server\LogicalDisk(N:)\Avg. Disk sec/Read' Match: 'C:\PerfCSV\Server_01200549.csv' Match: '(PDH-CSV 4.0) (Central Standard Time)(360)' SELECT TO_DATE([_PDH_CSV_4_0_Central_Standard_Time_360_]) AS CAPTUREDA +TE, AVG(TO_REAL([_Server_LogicalDisk_N_Avg_Disk_sec_Read])) AS AVG_LO +G_SEC_READFROM C_PerfCSV_Server_01200549_csv GROUP BY TO_DATE([_PDH_C +SV_4_0_Central_Standard_Time_360_])
Edit: $match =~ s/\W+/_/g;, this should have actually been $match =~ s/\W/_/g; since we need all special characters to be converted to an underscore.
Hope is a Heuristic Search.
  • Comment on Re: Perfmon log parser. Problem with column names having special chars.
  • Download Code