Exemple #1
0
 /**
  * @return Schema
  */
 public function init()
 {
     $this->getDb()->query('USE ' . $this->getName());
     $sql = 'show tables';
     $tableNames = array();
     $_tableNames = $this->_db->fetchAll($sql);
     uasort($_tableNames, function ($a, $b) {
         $v = reset($a);
         $vv = reset($b);
         if (substr($v, -5) == '_link' && substr($vv, -5) == '_link') {
             if (substr_count($v, '_') == substr_count($vv, '_')) {
                 return strcmp($v, $vv);
             } else {
                 return substr_count($v, '_') > substr_count($vv, '_');
             }
         } elseif (substr($v, -5) == '_link') {
             return true;
         } else {
             if (substr_count($v, '_') == substr_count($vv, '_')) {
                 return strcmp($v, $vv);
             } else {
                 return substr_count($v, '_') > substr_count($vv, '_');
             }
         }
     });
     foreach ($_tableNames as $name) {
         $tableNames[] = reset($name);
     }
     if (!empty($tableNames)) {
         foreach ($tableNames as $tableName) {
             if ($tableName[0] == '_') {
                 continue;
             }
             $table = new Table($tableName, $this);
             $this->addTable($table->init());
         }
     }
     $this->initIndex();
     /** @var Schema|Table[] $this */
     foreach ($this as $table) {
         /** @var Table|Column[] $table */
         foreach ($table as $column) {
             $column->init();
         }
     }
     $this->initTableLinks();
     return $this;
 }
Exemple #2
0
 /**
  * @param \Model\Cond\AbstractCond $cond
  *
  * @throws \Model\Db\Exception\ErrorException
  * @throws \Model\Exception\ErrorException
  * @return mixed
  */
 public function fetchOne(Cond $cond = null)
 {
     $entity = $cond->getEntityName() ? $cond->getEntityName() : $this->getRawName();
     $select = $this->prepareSelect($cond->limit(1), $entity);
     if ($cond->checkCond(Cond::SHOW_QUERY) || $cond->checkCond(Cond::SHOW_QUERY_EXTENDED)) {
         echo '<!--' . $select . "-->\n";
     }
     return $this->db->fetchOne($select->__toString(), $select->getBind());
 }
Exemple #3
0
 /**
  * Render ORDER clause
  *
  * @param string   $sql SQL query
  * @return string
  */
 protected function _renderOrder($sql)
 {
     if ($this->_parts[self::ORDER]) {
         $order = array();
         foreach ($this->_parts[self::ORDER] as $term) {
             if (is_array($term)) {
                 if (is_numeric($term[0]) && strval(intval($term[0])) == $term[0]) {
                     $order[] = (int) trim($term[0]) . ' ' . $term[1];
                 } else {
                     $order[] = $this->_adapter->quoteIdentifier($term[0], true) . ' ' . $term[1];
                 }
             } else {
                 if (is_numeric($term) && strval(intval($term)) == $term) {
                     $order[] = (int) trim($term);
                 } else {
                     $order[] = $this->_adapter->quoteIdentifier($term, true);
                 }
             }
         }
         $sql .= ' ' . self::SQL_ORDER_BY . ' ' . implode(', ', $order);
     }
     return $sql;
 }