SimpleConditionBuilder.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\db\conditions;
  8. use yii\db\ExpressionBuilderInterface;
  9. use yii\db\ExpressionBuilderTrait;
  10. use yii\db\ExpressionInterface;
  11. /**
  12. * Class NotConditionBuilder builds objects of [[SimpleCondition]]
  13. *
  14. * @author Dmytro Naumenko <d.naumenko.a@gmail.com>
  15. * @since 2.0.14
  16. */
  17. class SimpleConditionBuilder implements ExpressionBuilderInterface
  18. {
  19. use ExpressionBuilderTrait;
  20. /**
  21. * Method builds the raw SQL from the $expression that will not be additionally
  22. * escaped or quoted.
  23. *
  24. * @param ExpressionInterface|SimpleCondition $expression the expression to be built.
  25. * @param array $params the binding parameters.
  26. * @return string the raw SQL that will not be additionally escaped or quoted.
  27. */
  28. public function build(ExpressionInterface $expression, array &$params = [])
  29. {
  30. $operator = $expression->getOperator();
  31. $column = $expression->getColumn();
  32. $value = $expression->getValue();
  33. if (is_string($column) && strpos($column, '(') === false) {
  34. $column = $this->queryBuilder->db->quoteColumnName($column);
  35. }
  36. if ($value === null) {
  37. return "$column $operator NULL";
  38. }
  39. if ($value instanceof ExpressionInterface) {
  40. return "$column $operator {$this->queryBuilder->buildExpression($value, $params)}";
  41. }
  42. $phName = $this->queryBuilder->bindParam($value, $params);
  43. return "$column $operator $phName";
  44. }
  45. }