Example #1
0
 public function testQuery()
 {
     $db = $this->getDb();
     $select = new Select($db);
     $db->setEnableProfiler(true);
     $stmt = $select->from('product')->limit(1)->query();
     $this->assertInstanceOf('PDOStatement', $stmt);
     $profiler = $db->getProfiler();
     $lastQuery = end($profiler);
     $this->assertEquals('SELECT product.* FROM product LIMIT 1', $lastQuery['query']);
 }
Example #2
0
 /**
  * @param Cond  $cond
  * @param mixed $entity
  *
  * @throws \Model\Db\Exception\ErrorException
  * @throws \Model\Exception\ErrorException
  * @internal param \Model\Cond\AbstractCond $opts
  * @return Select
  */
 private function prepareSelect(Cond $cond = null, $entity = null)
 {
     $cond = $cond ?: $this->getCond($entity);
     $select = new Select($this->getDb());
     /**********************************************************************
      * FROM
      *********************************************************************/
     $from = $cond->getCond(Cond::FROM);
     if (!$from) {
         $from = $this->getRawName();
     }
     if (!is_array($from)) {
         $from = $this->getDb()->quoteTableAs($from);
     }
     $select->from($from);
     /**********************************************************************
      * DISTINCT
      *********************************************************************/
     $distinct = $cond->getCond(Cond::DISTINCT);
     if ($distinct) {
         $select->distinct($cond->getCond(Cond::DISTINCT));
     }
     /**********************************************************************
      * COLUMNS
      *********************************************************************/
     if ($cond->checkCond(Cond::COLUMNS)) {
         $select->reset(Cond::COLUMNS)->columns($cond->getCond(Cond::COLUMNS));
     }
     /**********************************************************************
      * JOIN
      *********************************************************************/
     if ($cond->checkAnyJoin()) {
         $joinRules = $cond->getJoin();
         $relation = $this->getRelation();
         foreach ($joinRules as $join) {
             $joinFunc = Cond::$_joinTypes[$join->getJoinType()];
             if (!$join->issetRule() && isset($relation[$join->getEntity()])) {
                 $rel = $relation[$join->getEntity()];
                 $joinFunc = Cond::$_joinTypes[$join->getJoinType()];
                 if (isset($rel['link_table']) && !empty($rel['link_table'])) {
                     $select->{$joinFunc}($rel['link_table'], "`{$rel['local_table']}`.`{$rel['local_column']}` = `{$rel['link_table']}`.`{$rel['link_table_local_column']}`", '');
                     $select->{$joinFunc}($rel['foreign_table'], "`{$rel['foreign_table']}`.`{$rel['foreign_column']}` = `{$rel['link_table']}`.`{$rel['link_table_foreign_column']}`", '');
                 } else {
                     $select->{$joinFunc}($rel['foreign_table'], "`{$rel['local_table']}`.`{$rel['local_column']}` = `{$rel['foreign_table']}`.`{$rel['foreign_column']}`", '');
                 }
             } else {
                 $select->{$joinFunc}($join->getTable(), $join->getCondition(), $join->getColumns());
             }
         }
     }
     /**********************************************************************
      * WHERE
      *********************************************************************/
     $where = $cond->getCond(Cond::WHERE);
     if (is_array($where) && !empty($where)) {
         foreach ($where as $whereCond) {
             if (!is_array($whereCond)) {
                 continue;
             }
             $_cond = $whereCond['cond'];
             $_bind = $whereCond['bind'];
             if (is_array($_cond)) {
                 foreach ($_cond as $k => $value) {
                     if (is_array($value) && count($value) == 1) {
                         $value = reset($value);
                     }
                     if (is_null($value)) {
                         $_cond = $k . ' IS NULL ';
                     } elseif (is_array($value)) {
                         foreach ($value as &$v) {
                             if (is_scalar($v) && !is_int($v)) {
                                 $v = $this->getDb()->_quote($v);
                             }
                         }
                         $_v = implode(',', $value);
                         $_cond = $k . ' IN (' . $_v . ') ';
                     } elseif ($value instanceof Expr) {
                         if (is_string($k)) {
                             $_cond = $k . ' =  ' . $value;
                         } else {
                             $_cond = $value->__toString();
                         }
                     } elseif (is_int($value)) {
                         $_cond = $k . ' = ' . $value;
                     } elseif (is_scalar($value)) {
                         $_v = $this->getDb()->_quote($value);
                         $_cond = $k . ' =  ' . $_v;
                     }
                     $select->where(trim($_cond));
                 }
             } else {
                 $select->where($_cond, $_bind);
             }
         }
     }
     /**********************************************************************
      * GROUP
      *********************************************************************/
     $group = $cond->getCond(Cond::GROUP);
     if (is_array($group) && !empty($group)) {
         foreach ($group as $_group) {
             $select->group($_group);
         }
     }
     /**********************************************************************
      * ORDER
      *********************************************************************/
     $order = $cond->getCond(Cond::ORDER);
     if (is_array($order) && !empty($order)) {
         foreach ($order as $_order) {
             if ($_order instanceof Select) {
                 $_order = str_replace("`", '', $_order->renderOrder());
             }
             $select->order($_order);
         }
     }
     /**********************************************************************
         *
        /*********************************************************************/
     if (!$cond->checkCond(Cond::PAGE)) {
         if ($cond->checkCond(Cond::LIMIT)) {
             if ($cond->checkCond(Cond::OFFSET)) {
                 $select->limit($cond->getCond(Cond::LIMIT), $cond->getCond(Cond::OFFSET));
             } else {
                 $select->limit($cond->getCond(Cond::LIMIT));
             }
         } else {
             if ($cond->checkCond(Cond::OFFSET)) {
                 $select->limit(0, $cond->getCond(Cond::OFFSET));
             }
         }
     } else {
         $select->limitPage($cond->getCond(Cond::PAGE), $cond->getCond(Cond::ITEMS_PER_PAGE));
     }
     return $select;
 }