Here's a Cool Hack
I first saw it a while ago in in this rails ActiveRecord tutorial – as the guy plays with the object relational mapper in one terminal window, all the SQL it’s running comes out in another terminal window. I saw that he had typed tail -f logs/development.log in the second window, so I investigated: tail displays the “tail” end of a specified file; -f keeps it going, spitting out new changes as the file is modified. In the case of the video, the file was a log file that rails writes all the SQL it runs to.
So I did this with braindump. Now if you set log_queries to true in app/config.php, the system saves every SQL query it runs to a log file at core/query-log.txt. So now I can type tail -f query-log.txt in the terminal, and the next time I go to a page on braindump, it spits out the requested URL and then all of the SQL that was run in the course of creating that page, like so:
tail -f core/query-log.txt # go to a page, and viola: /pages/index SELECT * FROM pages ORDER BY name DESC
It’s very useful for debugging, to see what’s going on behind the scenes. It’s also just cool to browse around and watch the terminal spit out SQL. It’s as if my program, braindump, is in the space between the browser window and the terminal window—you can see the cycle from HTTP request to SQL query and all the way back to HTML output.
It’s also cool to have the BQL console running in one terminal window and the SQL tail running in another. I type a BQL query into the console like so:
python xmlrpc_console.py braindump> get creator of braindump
and immediately the other window spits out the SQL queries that my BQL parser needed to get the answer:
SELECT id FROM pages WHERE name = 'creator' SELECT id FROM pages WHERE name = 'braindump' SELECT object_id FROM triples WHERE predicate_id = 1 AND subject_id = 2 SELECT name FROM pages WHERE id = 3
and then the answer comes back to the first window:
python xmlrpc_console.py braindump> get creator of braindump pedro
Anyway, yeah. If you didn’t follow that, oh well. This is a show-off post, not an informative one :)
May 11th @ 7:34 PM | 0 Comments | Trackback