/**
  * Create the SQL of a case expression
  * @param zibo\library\database\manipulation\expression\CaseExpression $case
  * @return string SQL of the case expression
  */
 protected function parseCaseExpression(CaseExpression $case, $useAlias = true)
 {
     $sql = 'CASE ';
     $when = $case->getWhen();
     foreach ($when as $w) {
         $whenCondition = $this->parseCondition($w->getCondition(), $useAlias);
         $whenExpression = $this->parseExpression($w->getExpression(), $useAlias);
         $sql .= 'WHEN ' . $whenCondition . ' ';
         $sql .= 'THEN ' . $whenExpression . ' ';
     }
     $defaultExpression = $case->getDefaultExpression();
     if ($defaultExpression) {
         $sql .= 'ELSE ' . $this->parseExpression($defaultExpression, $useAlias) . ' ';
     }
     $sql = '(' . $sql . 'END)';
     $alias = $case->getAlias();
     if ($useAlias && $alias) {
         $sql .= ' AS ' . $this->connection->quoteIdentifier($alias);
     }
     return $sql;
 }
Пример #2
0
 /**
  * Parses the provided case into a database case expression
  * @param string $case String of a case expression
  * @return boolean|zibo\library\database\manipulation\expression\CaseExpression false if the
  *         provided case expression could not be parsed, the case expression otherwise
  * @todo implement this function
  */
 private function parseCase($case)
 {
     if (!preg_match(self::REGEX_CASE, $case)) {
         return false;
     }
     if (strpos($case, ' AS ') !== false) {
         list($case, $alias) = explode(' AS ', $case);
     } else {
         $alias = null;
     }
     if (substr($case, 0, 1) == '(' && substr($case, -1) == ')') {
         $case = substr($case, 1, -1);
     }
     $case = substr($case, 5, -4);
     if (strpos($case, ' ELSE ') !== false) {
         list($conditions, $else) = explode(' ELSE ', $case);
     } else {
         $condition = $case;
         $else = null;
     }
     $conditions = explode('WHEN', $conditions);
     if (!$conditions) {
         return false;
     }
     $case = new CaseExpression();
     if ($alias) {
         $case->setAlias($alias);
     }
     if ($else) {
         $case->setDefaultExpression($this->parseExpression($else));
     }
     foreach ($conditions as $condition) {
         $condition = trim($condition);
         if (!$condition) {
             continue;
         }
         list($condition, $value) = explode(' THEN ', $condition);
         $condition = $this->parseCondition(new ModelExpression($condition));
         $value = $this->parseExpression($value);
         $case->addWhen($condition, $value);
     }
     return $case;
 }
Пример #3
0
 /**
  * Adds the join to localize the data of the model to the statement
  * @return null
  */
 private function addLocalizeJoin()
 {
     $joinCondition = $this->createLocalizeCondition($this->tables[self::ALIAS_SELF], $this->tables[self::ALIAS_SELF_LOCALIZED]);
     if ($this->includeUnlocalizedData) {
         $joinType = JoinExpression::TYPE_LEFT;
         $localeField = new FieldExpression(LocalizedModel::FIELD_LOCALE, $this->tables[self::ALIAS_SELF_LOCALIZED], self::ALIAS_SELF . self::ALIAS_SEPARATOR . LocalizedModel::FIELD_LOCALE);
         $this->statement->addField($localeField);
         $isLocalizedCondition = new SimpleCondition($localeField, new ScalarExpression(null), Condition::OPERATOR_IS);
         $isLocalizedCase = new CaseExpression();
         $isLocalizedCase->setAlias(self::ALIAS_IS_LOCALIZED);
         $isLocalizedCase->setDefaultExpression(new ScalarExpression(1));
         $isLocalizedCase->addWhen($isLocalizedCondition, new ScalarExpression(0));
         $this->statement->addField($isLocalizedCase);
         if ($this->addIsLocalizedOrder) {
             $order = new OrderExpression(new FieldExpression(self::ALIAS_IS_LOCALIZED), OrderExpression::DIRECTION_DESC);
             $this->statement->addOrderBy($order);
         }
     } else {
         $joinType = JoinExpression::TYPE_INNER;
     }
     $join = new JoinExpression($joinType, $this->tables[self::ALIAS_SELF_LOCALIZED], $joinCondition);
     $this->tables[self::ALIAS_SELF]->addJoin($join);
 }