Browse Source

Removing inheritance on the Comparison expression

The inheritance was not needed at all and only created confusion,
specially since the operator was hacked into _conjunction and
the getter/setter for it made no sense at all.
Jose Lorenzo Rodriguez 11 years ago
parent
commit
b9c28f0082

+ 3 - 3
src/Database/Dialect/TupleComparisonTranslatorTrait.php

@@ -53,7 +53,7 @@ trait TupleComparisonTranslatorTrait {
 		}
 
 		$value = $expression->getValue();
-		$op = $expression->type();
+		$op = $expression->getOperator();
 		$true = new QueryExpression('1');
 
 		if ($value instanceof Query) {
@@ -63,7 +63,7 @@ trait TupleComparisonTranslatorTrait {
 			}
 			$value->select($true, true);
 			$expression->field($true);
-			$expression->type('=');
+			$expression->setOperator('=');
 			return;
 		}
 
@@ -86,7 +86,7 @@ trait TupleComparisonTranslatorTrait {
 
 		$expression->field($true);
 		$expression->value($surrogate);
-		$expression->type('=');
+		$expression->setOperator('=');
 	}
 
 }

+ 31 - 14
src/Database/Expression/Comparison.php

@@ -24,7 +24,7 @@ use Cake\Database\ValueBinder;
  *
  * @internal
  */
-class Comparison extends QueryExpression {
+class Comparison implements ExpressionInterface {
 
 /**
  * The field name or expression to be used in the left hand side of the operator
@@ -48,17 +48,24 @@ class Comparison extends QueryExpression {
 	protected $_type;
 
 /**
+ * The operator used for comparing field and value
+ *
+ * @var string
+ */
+	protected $_operator;
+
+/**
  * Constructor
  *
  * @param string $field the field name to compare to a value
  * @param mixed $value The value to be used in comparison
  * @param string $type the type name used to cast the value
- * @param string $conjunction the operator used for comparing field and value
+ * @param string $operator the operator used for comparing field and value
  */
-	public function __construct($field, $value, $type, $conjunction) {
+	public function __construct($field, $value, $type, $operator) {
 		$this->field($field);
 		$this->value($value);
-		$this->type($conjunction);
+		$this->_operator = $operator;
 
 		if (is_string($type)) {
 			$this->_type = $type;
@@ -104,6 +111,25 @@ class Comparison extends QueryExpression {
 	}
 
 /**
+ * Returns the operator used for comparison
+ *
+ * @return string
+ */
+	public function getOperator() {
+		return $this->_operator;
+	}
+
+/**
+ * Sets the operator to use for the comparison
+ *
+ * @param string $operator The operator to be used for the comparison.
+ * @return void
+ */
+	public function setOperator($operator) {
+		$this->_operator = $operator;
+	}
+
+/**
  * Convert the expression into a SQL fragment.
  *
  * @param \Cake\Database\ValueBinder $generator Placeholder generator object
@@ -123,7 +149,7 @@ class Comparison extends QueryExpression {
 			list($template, $value) = $this->_stringExpression($generator);
 		}
 
-		return sprintf($template, $field, $this->_conjunction, $value);
+		return sprintf($template, $field, $this->_operator, $value);
 	}
 
 /**
@@ -206,13 +232,4 @@ class Comparison extends QueryExpression {
 		return implode(',', $parts);
 	}
 
-/**
- * Returns the number of expression this class represents
- *
- * @return int
- */
-	public function count() {
-		return 1;
-	}
-
 }

+ 2 - 2
src/Database/Expression/TupleComparison.php

@@ -62,7 +62,7 @@ class TupleComparison extends Comparison {
 		$values = $this->_stringifyValues($generator);
 
 		$field = implode(', ', $fields);
-		return sprintf($template, $field, $this->_conjunction, $values);
+		return sprintf($template, $field, $this->_operator, $values);
 	}
 
 /**
@@ -179,7 +179,7 @@ class TupleComparison extends Comparison {
  * @return bool
  */
 	public function isMulti() {
-		return in_array(strtolower($this->_conjunction), ['in', 'not in']);
+		return in_array(strtolower($this->_operator), ['in', 'not in']);
 	}
 
 }