Hi, I am attempting to write a perfmon log parser for windows using ActiveState perl v5.8.8, using DBI since the final goal is to allow the users write a query and retrieve the data. For example something like:


perflogquery "SELECT AVG([\\server\Processor(_Total)\% User Time] FROM perfdata.csv WHERE [(PDH-CSV 4.0) (Central Standard Time) (300)] BETWEEN '1/20/2008 12:03:58 AM' AND '1/20/2008 01:27:42 AM"

The problem I am facing is with the column names. I instantiate a new db handle, connect to the csv file and bind(if that is the right word) the file to a table name. After this, I prepare the sql query and I have errors. After a little googling and perldoc reading, I found that due to the SQL standard, field names cannot contain special characters like a dot (.). So, all these tokens are translated to an underscore (_) when reading the first line of the CSV file, so all field names are `sanitized'. I also found that if we do not want this to happen, we need to set the raw_header to a true value. But, even setting the raw_header to 1 did not help.</p.

Could someone, please point me in the right direction on how to parse the query string automatically to reflect the sanitization that is done with the headings?


Below is a partial code block that I used for testing:

use strict; use DBI; my $pc_csv_query_string = shift or die "usage perflogquery <queryS +tring>"; my $pc_csv_file_name = "perfdata.csv"; my $pc_csv_db_handle; my $pc_query_stmt_handle; $@ = ""; eval{ #Connect to CSV file or database if you will. $pc_csv_db_handle = DBI->connect( "DBI:CSV:f_dir=.", { PrintError => 1, RaiseError => 1, AutoCommit => 0 } ); #Turn on tracing with trace_level 2 DBI->trace( 2, 'parse_csv_trace.log' ); #We will be working with individual files for now. Maybe i +n the #future, we can move to using multiple files. Here we are +binding #the perfmon table to a csv file. $pc_csv_db_handle -> {csv_tables} -> {perfmon} = { file => $pc_csv_file_name, }; #$pc_csv_db_handle -> {raw_header} = 1; not much use even +if we use raw_header. #Prepare statement for exection. $pc_query_stmt_handle = $pc_csv_db_handle -> prepare("$pc_csv_query_string +"); #Execute the statement $pc_query_stmt_handle -> execute(); # Retrieve the returned rows of data.Need to work on this. while ( my @row = $pc_query_stmt_handle->fetchrow_array() +) { print ("@row \n"); } #Clean up $pc_query_stmt_handle -> finish(); $pc_csv_db_handle -> di +sconnect(); }; $@ and die "SQL database error: $@";
Hope is a Heuristic Search.

In reply to Perfmon log parser. Problem with column names having special chars. by hemanth.damecharla

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.