02.05 php saved
grigri
Tags add more
 
Note
Overriding the datasource for fun and profit.
  1. <?php
  2.  
  3. require_once (LIBS . 'model' . DS . 'datasources' . DS . 'dbo' . DS . 'dbo_mysql.php');
  4.  
  5. class DboMysqlEx extends DboMysql {
  6.     var $description = "MySQL DBO Driver - Extended";
  7.    
  8.     // Override resultSet to allow for Model__field type aliases
  9.     function resultSet(&$results) {
  10.         $this->results =& $results;
  11.         $this->map = array();
  12.         $num_fields = mysql_num_fields($results);
  13.         $index = 0;
  14.         $j = 0;
  15.  
  16.         while ($j < $num_fields) {
  17.             $column = mysql_fetch_field($results,$j);
  18.             if (!empty($column->table)) {
  19.                 $this->map[$index++] = array($column->table, $column->name);
  20.             } else {
  21.                 if (strpos($column->name, '__')) {
  22.                     $parts = explode('__', $column->name);
  23.                     $this->map[$index++] = array($parts[0], $parts[1]);
  24.                 } else {
  25.                     $this->map[$index++] = array(0, $column->name);
  26.                 }
  27.             }
  28.             $j++;
  29.         }
  30.     }
  31.    
  32.     // Override logQuery to provide backtrace information
  33.     function logQuery($sql) {
  34.         $trace = debug_backtrace();  
  35.        
  36.         $logEntry = false;
  37.         if (!preg_match('#^DESCRIBE #i', $sql)) {
  38.             foreach ($trace as $step) {
  39.                 if (!empty($step['object']) && !empty($step['file']) && !empty($step['type'])) {
  40.                     if (preg_match('#' . preg_quote('cake' . DS . 'dispatcher.php') . '$#', $step['file'])) {
  41.                         break;
  42.                     }
  43.                     $file = implode(DS, array_slice(explode(DS, $step['file']), -3));
  44.                     //if (preg_match('#_controller.php$#', $file)) {
  45.                     if ($logEntry) {
  46.                        
  47.                         $function = $step['function'];
  48.                        
  49.                         if ($step['class'] == 'Model' && $step['type'] == '->' && $step['function'] == 'find') {
  50.                             if (isset($step['args'][0]) && is_string($step['args'][0])) {
  51.                                 $function .= '(\'' . $step['args'][0] . '\')';
  52.                             }
  53.                         }
  54.                        
  55.                         $sql .= "\n-- ".sprintf(
  56.                             '%s - %s , [%s] %s%s%s',
  57.                             $file,
  58.                             $step['line'],
  59.                             $step['class'],
  60.                             get_class($step['object']),
  61.                             $step['type'],
  62.                             $function
  63.                         );
  64.                        
  65.                     }
  66.                     if (preg_match('#' . preg_quote('model' . DS . 'model.php') . '$#', $step['file'])) {
  67.                         $logEntry = true;
  68.                     }
  69.                 }
  70.             }
  71.         }
  72.        
  73.         $ret = parent::logQuery($sql);
  74.        
  75.         return $ret;
  76.     }
  77.  
  78.     // Override showLog to display syntax-highlighted SQL in the log.
  79.     // Uses geshi - ensure geshi is in app/vendors/geshi.php
  80.     function showLog($sorted = false) {
  81.         if ($sorted) {
  82.             $log = sortByKey($this->_queriesLog, 'took', 'desc', SORT_NUMERIC);
  83.         } else {
  84.             $log = $this->_queriesLog;
  85.         }
  86.  
  87.         if ($this->_queriesCnt > 1) {
  88.             $text = 'queries';
  89.         } else {
  90.             $text = 'query';
  91.         }
  92.  
  93.         if (php_sapi_name() != 'cli') {
  94.  
  95.             require_once APP . 'vendors' . DS . 'geshi.php';
  96.  
  97.             $geshi = new GeSHi('', 'mysql');
  98.             $geshi->set_header_type(GESHI_HEADER_NONE);
  99.            
  100.             print ("<table class=\"cake-sql-log\" id=\"cakeSqlLog_" . preg_replace('/[^A-Za-z0-9_]/', '_', uniqid(time(), true)) . "\" summary=\"Cake SQL Log\" cellspacing=\"0\" border = \"0\">\n<caption>{$this->_queriesCnt} {$text} took {$this->_queriesTime} ms</caption>\n");
  101.             print ("<thead>\n<tr><th>Nr</th><th>Query</th><th>Error</th><th>Affected</th><th>Num. rows</th><th>Took (ms)</th></tr>\n</thead>\n<tbody>\n");
  102.  
  103.             foreach ($log as $k => $i) {
  104.                 $geshi->set_source($i['query']);
  105.                 $query = $geshi->parse_code();
  106.                 print ("<tr><td>" . ($k + 1) . "</td><td>" . $query . "</td><td>{$i['error']}</td><td style = \"text-align: right\">{$i['affected']}</td><td style = \"text-align: right\">{$i['numRows']}</td><td style = \"text-align: right\">{$i['took']}</td></tr>\n");
  107.             }
  108.             print ("</tbody></table>\n");
  109.         } else {
  110.             foreach ($log as $k => $i) {
  111.                 print (($k + 1) . ". {$i['query']} {$i['error']}\n");
  112.             }
  113.         }
  114.     }
  115. }
  116.  
  117. ?>
Parsed in 0.245 seconds, using GeSHi 1.0.7.14

Modify this Paste