Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^2: Can DBI modify queries on the fly?

by kyle (Abbot)
on Feb 18, 2009 at 01:51 UTC ( [id://744612]=note: print w/replies, xml ) Need Help??


in reply to Re: Can DBI modify queries on the fly?
in thread Can DBI modify queries on the fly?

I want to modify the queries of a running application so that what's passed to the database includes a comment that tells where in the Perl code the request originated. SQL::Library (or Data::Phrasebook) is a nice idea, but it solves a completely different problem.

Likewise, I don't want to step through my code in the debugger and modify queries by hand. Rather, this is a tedious task that I think the computer should do for me. More to the point, I can't sit at the debugger in production.

  • Comment on Re^2: Can DBI modify queries on the fly?

Replies are listed 'Best First'.
Re^3: Can DBI modify queries on the fly?
by doom (Deacon) on Feb 18, 2009 at 04:45 UTC

    I want to modify the queries of a running application so that what's passed to the database includes a comment that tells where in the Perl code the request originated.
    Well, there is such a thing as an SQL comment, and for at least some databases (e.g. MySQL), I believe the comments get logged along with the SQL. (There was one occasion where I solved a problem like this with screwed-up capitalization, e.g. changing "SELECT" to "SeLECT", "SElECT", "SELeCT"...).

    Have you looked at the "TRACING" features listed in the DBI docs? That at least can be turned on dynamically, though if the docs are up-to-date, I infer that it doesn't have SQL logging yet:

    Curently the DBI only defines two trace flags: ALL - turn on all DBI and driver flags (not recommended) SQL - trace SQL statements executed (not yet implemented)

    Update: actually, I bet that turning on trace level 1 is pretty close to what you're asking for. It will echo your prepare statements (including the SQL), and label them with line number and file of your code:

    $dbh->trace(1, $trace_log_file);

Re^3: Can DBI modify queries on the fly?
by jfroebe (Parson) on Feb 18, 2009 at 02:59 UTC

    Understood. I would advise strongly against doing this in production though. Most DBMSs will have a performance penalty for writing to the errorlog (or any external file). But... it's your code/server.

    Jason L. Froebe

    Blog, Tech Blog

      We're not planning to write everything to a log, but there's a "slow queries" log for when something takes too long, and I'd like those entries to have a little more information in them. Unfortunately, I can't always tell ahead of time which queries those will be, so I need to add my special bonus info to everything.

        Instead of writing to the DBMS errorlog, how about writing to a table? It would probably have a smaller performance impact. It would also allow you to determine which queries are most often used.. allowing you to focus your energy on the worst.

        Which DBMS are you using? There might be tools that would identify the slow queries that you could use in conjunction with your code line => query.

        Jason L. Froebe

        Blog, Tech Blog

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://744612]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2024-04-24 04:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found