CodeIgniter Forums
Sleightly enhanced profiler for queries - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Sleightly enhanced profiler for queries (/showthread.php?tid=6100)



Sleightly enhanced profiler for queries - El Forum - 02-14-2008

[eluser]kandmcreative[/eluser]
I tried to add this to the "Ignited Code" forum, but it won't submit. Sorry if this is in the wrong place.

In libraries/Profiler.php, we have modified it to output breaks before highlighted keywords for even nicer sql profiler output. Perhaps this would be beneficial to others, or worthy of adding to SVN.

Code:
$highlight = array('SELECT', 'FROM', 'WHERE', 'AND', 'ORDER BY', 'LEFT JOIN', ' OR', 'LIMIT', 'INSERT', 'INTO', 'VALUES', 'UPDATE', 'UNION' , 'INNER JOIN','GROUP BY');
                
                foreach ($this->CI->db->queries as $key => $val)
                {
                    $val = htmlspecialchars($val, ENT_QUOTES);
                    $time = number_format($this->CI->db->query_times[$key], 4);
                    $i=0;
                    foreach ($highlight as $bold)
                    {
                        $break = $i==0 ? '' : '<br/>';
                        if (stristr($bold,'JOIN')) $break .= '&nbsp;&nbsp;&nbsp;&nbsp;';
                        $val = str_replace($bold, $break.'<strong>'.$bold.'</strong>', $val);    
                        $i++;
                    }
                    
                    $output .= "<tr><td width='1%' valign='top' style='color:#990000;font-weight:normal;background-color:#ddd;'>".$time."&nbsp;&nbsp;</td><td style='color:#000;font-weight:normal;background-color:#ddd;'>".$val."</td></tr>\n";
                }
            }

Outputs like this (with keywords bolded):
Code:
SELECT tblCredits.CreditID, CreditDate, CreditType, tblCredits.CreditAmount, Notes, Reference, SUM(tblItemCredits.CreditAmount) AS ItemCredit
FROM tblCredits
    INNER JOIN tblItemCredits ON tblItemCredits.CreditID = tblCredits.CreditID
    INNER JOIN tblInvoiceItems ON tblInvoiceItems.InvoiceItemID = tblItemCredits.InvoiceItemID
    INNER JOIN tblCreditType ON tblCreditType.CreditTypeID = tblCredits.CreditTypeID
WHERE tblInvoiceItems.InvoiceID = 136
GROUP BY tblCredits.CreditID, CreditDate, CreditType, Notes, Reference, tblCredits.CreditAmount