Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Can DBI modify queries on the fly?

by jfroebe (Vicar)
on Feb 17, 2009 at 22:58 UTC ( #744586=note: print w/ replies, xml ) Need Help??


in reply to Can DBI modify queries on the fly?

Check out SQL Library. It will allow you to easily keep your SQL code in another file that you can modify at will.

Another option is to use the Perl debugger to replace the query strings.

Jason L. Froebe

Blog, Tech Blog


Comment on Re: Can DBI modify queries on the fly?
Re^2: Can DBI modify queries on the fly?
by kyle (Abbot) on Feb 18, 2009 at 01:51 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. 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.

      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.

      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);

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (11)
As of 2014-12-17 20:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (32 votes), past polls