Пример #1
0
 /**
  * Fetch result
  * @param K_Db_Select/String 	$sql		SQL
  * @param Int 					$count		row count (0 - default/unlimited)
  */
 public function fetch($sql, $count = 0, &$model = null)
 {
     $initTime = K_Time::microtime_float();
     $useCache = $sql instanceof K_Db_Select && $this->usedCache instanceof K_Cache_ICache;
     $tableStructure = array();
     $lastModifyTime = 0;
     $sqlCacheIdMod;
     // get cached described data about table(s)
     if ($sql instanceof K_Db_Select && $this->structureCache) {
         $tableNames = $sql->table();
         if (count($tableNames)) {
             foreach ($tableNames as $table) {
                 $name = trim(strtolower($table['name']));
                 $tableStructure[$name] = $this->_describeTable($name);
                 $lastDataChange = $this->_getLastDataChange($name);
                 $lastModifyTime = $lastModifyTime < $lastDataChange ? $lastDataChange : $lastModifyTime;
             }
         }
         $sqlCacheIdMod = $sql->cacheId($lastModifyTime);
     }
     // Load results from cache
     if ($useCache && $this->usedCache->test($sqlCacheIdMod)) {
         if ($data = $this->usedCache->load($sqlCacheIdMod)) {
             $this->lastQueryDuration = K_Time::microtime_float() - $initTime;
             return $data;
         }
     }
     // Run sql query
     $return = $this->fetchReal($sql, $count, $model, false);
     // Save results to cache
     if ($useCache) {
         $this->usedCache->save($sqlCacheIdMod, $return);
     }
     $this->lastQueryDuration = K_Time::microtime_float() - $initTime;
     return $return;
 }
Пример #2
0
 public function printAll()
 {
     if (!$this->enabled) {
         return;
     }
     echo '<html><body><br/><div style="font-size:12px; font-family:arial">';
     if (count($this->errorList)) {
         echo '<strong style="color:red">Errors:</strong><br/>';
         foreach ($this->errorList as &$string) {
             echo $string;
         }
         echo '<br/>';
     }
     if (count($this->messageList)) {
         echo '<strong style="color:green">Messages:</strong><br/>';
         foreach ($this->messageList as &$string) {
             echo $string;
         }
         echo '<br/>';
     }
     if (count($this->sqlList)) {
         $strongList = array('SELECT', 'WHERE', 'ORDER BY', 'FROM', 'GROUP BY', 'HAVING', 'LIMIT', 'OFFSET', 'AND', 'OR', 'NOT', 'UPDATE', 'INSERT INTO', 'DUPLICATE KEY', 'ON', 'VALUES');
         echo '<strong style="color:black">SQL:</strong><br/><table style="font-size:12px">' . '<tr><th>Sql query</th><th>Return rows</th><th>Elapse</th></tr>';
         foreach ($this->sqlList as &$string) {
             foreach ($strongList as $strongText) {
                 $string = str_replace($strongText . ' ', '<strong>' . $strongText . ' </strong>', $string);
             }
             echo $string;
         }
         echo '</table><br/>';
     }
     if (count($this->dumpList)) {
         echo '<strong style="color:blue">Data dumps:</strong><br/>';
         $i = 1;
         foreach ($this->dumpList as &$item) {
             echo $i++ . '.' . ($item['name'] ? '<strong>' . $item['name'] . '</strong>' : '') . '<br/>';
             var_dump($item['object']);
             echo '<br/>';
             echo '<br/>';
         }
         echo '<br/>';
     }
     $elapse = K_Time::microtime_float() - $this->elapse;
     echo 'Render time: ' . $elapse . ' s';
     //  $xhprof_data = xhprof_disable();
     // $xhprof_runs = new XHProfRuns_Default();
     //  $run_id = $xhprof_runs->save_run($xhprof_data, "xhprof_testing");
     // Формируем ссылку на данные профайлинга и записываем ее в консоль
     //  $link = "http://prof.loc/xhprof_html/index.php?run={$run_id}&source=xhprof_testing\n";
     //  echo '<br><a  href="'.$link.'">профайлер</a><br><br><br><br><br><br>';
     echo '</div></body></html>';
 }
Пример #3
0
 /**
  * Set from table
  */
 public function from($table, $alias = null)
 {
     unset($this->table);
     $this->table = array();
     if (is_string($table) && strlen(trim($table))) {
         $this->table[] = array('sql' => K_Db_Quote::quoteKey($table), 'name' => $table, 'type' => 'table');
     } elseif (is_array($table) && count($table)) {
         foreach ($table as $tableAlias => $tableName) {
             $asString = '';
             if (!is_numeric($tableAlias)) {
                 $asString = 'as ' . K_Db_Quote::quoteKey($tableAlias);
             }
             $this->table[] = array('sql' => K_Db_Quote::quoteKey($tableName) . $asString, 'type' => 'table', 'name' => $tableName);
         }
     } elseif ($table instanceof K_Db_Model) {
         // Set as mother model
         $this->originalModel = $table;
         if (empty($this->motherModel)) {
             $this->mother($table);
         }
         $this->table[] = array('sql' => K_Db_Quote::quoteKey($table->name), 'type' => 'table', 'name' => $table->name);
     } elseif ($table instanceof K_Db_Select) {
         if (empty($alias)) {
             $alias = md5(K_Time::microtime_float());
         }
         $this->table[] = array('sql' => $table->sql(), 'type' => 'select', 'name' => $alias);
     } else {
         throw new Exception(__CLASS__ . '->' . __METHOD__ . ': table name is not a string');
     }
     return $this;
 }