Skip to content

Commit

Permalink
[bug-OpenMage#936] Missing type cast in Varien_Db_Select::where()
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tmotyl authored and edannenberg committed Aug 24, 2020
1 parent 23b0377 commit a3e3686
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 22 deletions.
6 changes: 3 additions & 3 deletions lib/Magento/Db/Adapter/Pdo/Mysql.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,9 @@ protected function _quote($value)
* If an array is passed as the value, the array values are quote
* and then returned as a comma-separated string.
*
* @param mixed $value The value to quote.
* @param null $type OPTIONAL the SQL datatype name, or constant, or null.
* @return mixed|string An SQL-safe quoted value (or string of separated values).
* @param Zend_Db_Select|Zend_Db_Expr|array|null|int|string|float $value OPTIONAL A single value to quote into the condition.
* @param null|string|int $type OPTIONAL The type of the given value e.g. Zend_Db::INT_TYPE, "INT"
* @return string An SQL-safe quoted value (or string of separated values).
*/
public function quote($value, $type = null)
{
Expand Down
10 changes: 5 additions & 5 deletions lib/Varien/Db/Adapter/Interface.php
Original file line number Diff line number Diff line change
Expand Up @@ -566,9 +566,9 @@ public function fetchOne($sql, $bind = array());
* If an array is passed as the value, the array values are quoted
* and then returned as a comma-separated string.
*
* @param mixed $value The value to quote.
* @param mixed $type OPTIONAL the SQL datatype name, or constant, or null.
* @return mixed An SQL-safe quoted value (or string of separated values).
* @param Zend_Db_Select|Zend_Db_Expr|array|null|int|string|float $value OPTIONAL A single value to quote into the condition.
* @param null|string|int $type OPTIONAL The type of the given value e.g. Zend_Db::INT_TYPE, "INT"
* @return string An SQL-safe quoted value (or string of separated values).
*/
public function quote($value, $type = null);

Expand All @@ -586,8 +586,8 @@ public function quote($value, $type = null);
* </code>
*
* @param string $text The text with a placeholder.
* @param mixed $value The value to quote.
* @param string $type OPTIONAL SQL datatype
* @param Zend_Db_Select|Zend_Db_Expr|array|null|int|string|float $value OPTIONAL A single value to quote into the condition.
* @param null|string|int $type OPTIONAL The type of the given value e.g. Zend_Db::INT_TYPE, "INT"
* @param integer $count OPTIONAL count of placeholders to replace
* @return string An SQL-safe quoted value placed into the original text.
*/
Expand Down
4 changes: 2 additions & 2 deletions lib/Varien/Db/Adapter/Pdo/Mysql.php
Original file line number Diff line number Diff line change
Expand Up @@ -1506,8 +1506,8 @@ protected function _debugWriteToFile($str)
* Method revrited for handle empty arrays in value param
*
* @param string $text The text with a placeholder.
* @param mixed $value The value to quote.
* @param string $type OPTIONAL SQL datatype
* @param Zend_Db_Select|Zend_Db_Expr|array|null|int|string|float $value OPTIONAL A single value to quote into the condition.
* @param null|string|int $type OPTIONAL The type of the given value e.g. Zend_Db::INT_TYPE, "INT"
* @param integer $count OPTIONAL count of placeholders to replace
* @return string An SQL-safe quoted value placed into the orignal text.
*/
Expand Down
9 changes: 5 additions & 4 deletions lib/Varien/Db/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ public function __construct(Zend_Db_Adapter_Abstract $adapter)
* </code>
*
* @param string $cond The WHERE condition.
* @param string $value OPTIONAL A single value to quote into the condition.
* @param constant $type OPTIONAL The type of the given value
* @param Zend_Db_Select|Zend_Db_Expr|array|null|int|string|float $value OPTIONAL A single value to quote into the condition.
* @param null|string|int $type OPTIONAL The type of the given value e.g. Zend_Db::INT_TYPE, "INT"
* @return Varien_Db_Select This Zend_Db_Select object.
*/
public function where($cond, $value = null, $type = null)
Expand All @@ -112,12 +112,13 @@ public function where($cond, $value = null, $type = null)
}
/**
* Additional internal type used for really null value
* cast to string, to prevent false matching 0 == "TYPE_CONDITION"
*/
if ($type == self::TYPE_CONDITION) {
if ((string)$type === self::TYPE_CONDITION) {
$type = null;
}
if (is_array($value)) {
$cond = $this->_adapter->quoteInto($cond, $value);
$cond = $this->_adapter->quoteInto($cond, $value, $type);
$value = null;
}
return parent::where($cond, $value, $type);
Expand Down
10 changes: 5 additions & 5 deletions lib/Zend/Db/Adapter/Abstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -852,9 +852,9 @@ protected function _quote($value)
* If an array is passed as the value, the array values are quoted
* and then returned as a comma-separated string.
*
* @param mixed $value The value to quote.
* @param mixed $type OPTIONAL the SQL datatype name, or constant, or null.
* @return mixed An SQL-safe quoted value (or string of separated values).
* @param Zend_Db_Select|Zend_Db_Expr|array|null|int|string|float $value OPTIONAL A single value to quote into the condition.
* @param null|string|int $type OPTIONAL The type of the given value e.g. Zend_Db::INT_TYPE, "INT"
* @return string An SQL-safe quoted value (or string of separated values).
*/
public function quote($value, $type = null)
{
Expand Down Expand Up @@ -920,8 +920,8 @@ public function quote($value, $type = null)
* </code>
*
* @param string $text The text with a placeholder.
* @param mixed $value The value to quote.
* @param string $type OPTIONAL SQL datatype
* @param Zend_Db_Select|Zend_Db_Expr|array|null|int|string|float $value OPTIONAL A single value to quote into the condition.
* @param null|string|int $type OPTIONAL The type of the given value e.g. Zend_Db::INT_TYPE, "INT"
* @param integer $count OPTIONAL count of placeholders to replace
* @return string An SQL-safe quoted value placed into the original text.
*/
Expand Down
6 changes: 3 additions & 3 deletions lib/Zend/Db/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ public function joinNatural($name, $cols = self::SQL_WILDCARD, $schema = null)
* </code>
*
* @param string $cond The WHERE condition.
* @param mixed $value OPTIONAL The value to quote into the condition.
* @param Zend_Db_Select|Zend_Db_Expr|array|null|int|string|float $value OPTIONAL The value to quote into the condition.
* @param int $type OPTIONAL The type of the given value
* @return Zend_Db_Select This Zend_Db_Select object.
*/
Expand All @@ -485,7 +485,7 @@ public function where($cond, $value = null, $type = null)
* Otherwise identical to where().
*
* @param string $cond The WHERE condition.
* @param mixed $value OPTIONAL The value to quote into the condition.
* @param Zend_Db_Select|Zend_Db_Expr|array|null|int|string|float $value OPTIONAL The value to quote into the condition.
* @param int $type OPTIONAL The type of the given value
* @return Zend_Db_Select This Zend_Db_Select object.
*
Expand Down Expand Up @@ -991,7 +991,7 @@ protected function _tableCols($correlationName, $cols, $afterCorrelationName = n
* Internal function for creating the where clause
*
* @param string $condition
* @param mixed $value optional
* @param Zend_Db_Select|Zend_Db_Expr|array|null|int|string|float $value optional
* @param string $type optional
* @param boolean $bool true = AND, false = OR
* @return string clause
Expand Down

0 comments on commit a3e3686

Please sign in to comment.