/** * 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; }
/** * 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); }