Flourish PHP Unframework
This is an archived copy of the forum for reference purposes

Easy way to retrieve SQL debugging info

posted by earth 9 years ago

I'd like an easy way to retrieve SQL debugging information, such as the syntax of executed queries and their execution time. Currently,

$db->enableDebugging(true);

only shows this info at the top and bottom of the page, which is impractical when in production mode.

You are going to want to use fCore::registerDebugCallback(), which you can see in action at fCore#Debugging.

posted by wbond 9 years ago

Oh great. I missed that part in the code!

posted by anonymous 9 years ago

Is there no way to retrieve database query debugging info as an array or so? I'd like to use that info to create a custom logging feature and distinct it from other debugging messages.

posted by earth 9 years ago

No, there currently is not such a method. If you only want database messages I would recommend extending fDatabase and adding your special logging needs to an overridden method.

posted by wbond 9 years ago

Extending the fDatabase class won't be sufficient for what I'm trying to accomplish. I got used to ZFW's DB profiler because I am able to use the debugging info in any way I like while flourishlib just passes the messages which I can only log or display on the web page in an ugly way, and if I display it on the web page with a custom callback function then debugging messages from destructors aren't displayed.

Any chance of having those features for flourish any time in the near future?

posted by earth 9 years ago

The only further plans I have is to add a hook or two to fDatabase to allow for modifying a SQL query before it is run. I suppose it would also be possible to add one after it is run that includes the sql and the time it took to run. I don't have a set date when I will get to it, it really depends on what other work I have on my plate.

posted by wbond 9 years ago

I'll just use the DB module from ZFW then because it's the only thing that I'm missing in Flourishlib. It's quite against my will because it consequently includes a bunch of dependencies.

posted by earth 9 years ago

As of r885, there is now a nice clean hook available with fDatabase::registerHookCallback() that should allow for whatever logging you want to do for queries. In your case you probably want to use the 'run' hook and access the second and third parameters.

function log_sql($db, $statement, $query_time, $result) {
    // This handles prepared statements since the statement and values are separate
    if (is_array($statement)) {
        $sql = '"' . $statement[0]->getSQL() . '" with the values: ' . join(", ", array_map('fCore::dump', $values));
    } else {
        $sql = '"' . $statement . '"';
    }
    echo 'The following query took ' . $query_time . " seconds: \\n" . $sql;
}
$db->registerHookCallback('run', 'log_sql');

I opted for this route because some other requests have come through looking to do similar things, but a lot of the internals of fDatabase is kind of messy due to all of the supported database extensions.

I'll be adding some more in-depth documentation about this soon on the fDatabase page.

posted by wbond 9 years ago

This looks interesting :)

posted by earth 9 years ago