Skip to content

Commit fb1d6b4

Browse files
tmotyledannenberg
authored andcommitted
[bug-OpenMage#936] Missing type cast in Varien_Db_Select::where()
Fixes SQL query quoting/casting when type is passed to where function The $type variable can be both string or int, so before comparing it to 'TYPE_CONDITION' string it has to be casted to avoid comparing integer zero with string (0 == 'TYPE_CONDITION') which will wrongly return true, and remove the information about type. Pass type provided to where function down the chain to allow automatic casting of arrays of values e.g. to int. This fixes following cases: 1) ->where('attr_table.store_id IN (?)', $storeIds, Zend_Db::INT_TYPE); 2) ->where('attr_table.store_id = ?', $storeId, Zend_Db::INT_TYPE); In both cases now passed value is correctly casted to int (either single value, or each value from array) Passing Zend_Db::INT_TYPE to where condition will prevent mysql performance issues which might occur when mixed types are passed in "in()" condition. Also fixes type hints along the way.
1 parent cead290 commit fb1d6b4

File tree

6 files changed

+23
-22
lines changed

6 files changed

+23
-22
lines changed

lib/Magento/Db/Adapter/Pdo/Mysql.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,9 @@ protected function _quote($value)
124124
* If an array is passed as the value, the array values are quote
125125
* and then returned as a comma-separated string.
126126
*
127-
* @param mixed $value The value to quote.
128-
* @param null $type OPTIONAL the SQL datatype name, or constant, or null.
129-
* @return mixed|string An SQL-safe quoted value (or string of separated values).
127+
* @param Zend_Db_Select|Zend_Db_Expr|array|null|int|string|float $value OPTIONAL A single value to quote into the condition.
128+
* @param null|string|int $type OPTIONAL The type of the given value e.g. Zend_Db::INT_TYPE, "INT"
129+
* @return string An SQL-safe quoted value (or string of separated values).
130130
*/
131131
public function quote($value, $type = null)
132132
{

lib/Varien/Db/Adapter/Interface.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -566,9 +566,9 @@ public function fetchOne($sql, $bind = array());
566566
* If an array is passed as the value, the array values are quoted
567567
* and then returned as a comma-separated string.
568568
*
569-
* @param mixed $value The value to quote.
570-
* @param mixed $type OPTIONAL the SQL datatype name, or constant, or null.
571-
* @return mixed An SQL-safe quoted value (or string of separated values).
569+
* @param Zend_Db_Select|Zend_Db_Expr|array|null|int|string|float $value OPTIONAL A single value to quote into the condition.
570+
* @param null|string|int $type OPTIONAL The type of the given value e.g. Zend_Db::INT_TYPE, "INT"
571+
* @return string An SQL-safe quoted value (or string of separated values).
572572
*/
573573
public function quote($value, $type = null);
574574

@@ -586,8 +586,8 @@ public function quote($value, $type = null);
586586
* </code>
587587
*
588588
* @param string $text The text with a placeholder.
589-
* @param mixed $value The value to quote.
590-
* @param string $type OPTIONAL SQL datatype
589+
* @param Zend_Db_Select|Zend_Db_Expr|array|null|int|string|float $value OPTIONAL A single value to quote into the condition.
590+
* @param null|string|int $type OPTIONAL The type of the given value e.g. Zend_Db::INT_TYPE, "INT"
591591
* @param integer $count OPTIONAL count of placeholders to replace
592592
* @return string An SQL-safe quoted value placed into the original text.
593593
*/

lib/Varien/Db/Adapter/Pdo/Mysql.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1506,8 +1506,8 @@ protected function _debugWriteToFile($str)
15061506
* Method revrited for handle empty arrays in value param
15071507
*
15081508
* @param string $text The text with a placeholder.
1509-
* @param mixed $value The value to quote.
1510-
* @param string $type OPTIONAL SQL datatype
1509+
* @param Zend_Db_Select|Zend_Db_Expr|array|null|int|string|float $value OPTIONAL A single value to quote into the condition.
1510+
* @param null|string|int $type OPTIONAL The type of the given value e.g. Zend_Db::INT_TYPE, "INT"
15111511
* @param integer $count OPTIONAL count of placeholders to replace
15121512
* @return string An SQL-safe quoted value placed into the orignal text.
15131513
*/

lib/Varien/Db/Select.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ public function __construct(Zend_Db_Adapter_Abstract $adapter)
101101
* </code>
102102
*
103103
* @param string $cond The WHERE condition.
104-
* @param string $value OPTIONAL A single value to quote into the condition.
105-
* @param constant $type OPTIONAL The type of the given value
104+
* @param Zend_Db_Select|Zend_Db_Expr|array|null|int|string|float $value OPTIONAL A single value to quote into the condition.
105+
* @param null|string|int $type OPTIONAL The type of the given value e.g. Zend_Db::INT_TYPE, "INT"
106106
* @return Varien_Db_Select This Zend_Db_Select object.
107107
*/
108108
public function where($cond, $value = null, $type = null)
@@ -112,12 +112,13 @@ public function where($cond, $value = null, $type = null)
112112
}
113113
/**
114114
* Additional internal type used for really null value
115+
* cast to string, to prevent false matching 0 == "TYPE_CONDITION"
115116
*/
116-
if ($type == self::TYPE_CONDITION) {
117+
if ((string)$type === self::TYPE_CONDITION) {
117118
$type = null;
118119
}
119120
if (is_array($value)) {
120-
$cond = $this->_adapter->quoteInto($cond, $value);
121+
$cond = $this->_adapter->quoteInto($cond, $value, $type);
121122
$value = null;
122123
}
123124
return parent::where($cond, $value, $type);

lib/Zend/Db/Adapter/Abstract.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -852,9 +852,9 @@ protected function _quote($value)
852852
* If an array is passed as the value, the array values are quoted
853853
* and then returned as a comma-separated string.
854854
*
855-
* @param mixed $value The value to quote.
856-
* @param mixed $type OPTIONAL the SQL datatype name, or constant, or null.
857-
* @return mixed An SQL-safe quoted value (or string of separated values).
855+
* @param Zend_Db_Select|Zend_Db_Expr|array|null|int|string|float $value OPTIONAL A single value to quote into the condition.
856+
* @param null|string|int $type OPTIONAL The type of the given value e.g. Zend_Db::INT_TYPE, "INT"
857+
* @return string An SQL-safe quoted value (or string of separated values).
858858
*/
859859
public function quote($value, $type = null)
860860
{
@@ -920,8 +920,8 @@ public function quote($value, $type = null)
920920
* </code>
921921
*
922922
* @param string $text The text with a placeholder.
923-
* @param mixed $value The value to quote.
924-
* @param string $type OPTIONAL SQL datatype
923+
* @param Zend_Db_Select|Zend_Db_Expr|array|null|int|string|float $value OPTIONAL A single value to quote into the condition.
924+
* @param null|string|int $type OPTIONAL The type of the given value e.g. Zend_Db::INT_TYPE, "INT"
925925
* @param integer $count OPTIONAL count of placeholders to replace
926926
* @return string An SQL-safe quoted value placed into the original text.
927927
*/

lib/Zend/Db/Select.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ public function joinNatural($name, $cols = self::SQL_WILDCARD, $schema = null)
468468
* </code>
469469
*
470470
* @param string $cond The WHERE condition.
471-
* @param mixed $value OPTIONAL The value to quote into the condition.
471+
* @param Zend_Db_Select|Zend_Db_Expr|array|null|int|string|float $value OPTIONAL The value to quote into the condition.
472472
* @param int $type OPTIONAL The type of the given value
473473
* @return Zend_Db_Select This Zend_Db_Select object.
474474
*/
@@ -485,7 +485,7 @@ public function where($cond, $value = null, $type = null)
485485
* Otherwise identical to where().
486486
*
487487
* @param string $cond The WHERE condition.
488-
* @param mixed $value OPTIONAL The value to quote into the condition.
488+
* @param Zend_Db_Select|Zend_Db_Expr|array|null|int|string|float $value OPTIONAL The value to quote into the condition.
489489
* @param int $type OPTIONAL The type of the given value
490490
* @return Zend_Db_Select This Zend_Db_Select object.
491491
*
@@ -991,7 +991,7 @@ protected function _tableCols($correlationName, $cols, $afterCorrelationName = n
991991
* Internal function for creating the where clause
992992
*
993993
* @param string $condition
994-
* @param mixed $value optional
994+
* @param Zend_Db_Select|Zend_Db_Expr|array|null|int|string|float $value optional
995995
* @param string $type optional
996996
* @param boolean $bool true = AND, false = OR
997997
* @return string clause

0 commit comments

Comments
 (0)