[+] Classes: add DbQuery class in order to build queries (usefull when working with chunks of queries)

git-svn-id: http://dev.prestashop.com/svn/v1/branches/1.5.x@8668 b9a71923-0436-4b27-9f14-aed3839534dd
This commit is contained in:
rMalie committed 2011-09-20 16:14:19 +00:00
1 parent 92cc465a3c
commit 95233f9df5
12 files changed
+115 -72

No files matched your search

+10 -6
View File
@@ -76,10 +76,6 @@
'Customization' => 'override/classes/Customization.php', 'Customization' => 'override/classes/Customization.php',
'DateRangeCore' => 'classes/DateRange.php', 'DateRangeCore' => 'classes/DateRange.php',
'DateRange' => 'override/classes/DateRange.php', 'DateRange' => 'override/classes/DateRange.php',
'DbCore' => 'classes/Db.php',
'Db' => 'override/classes/Db.php',
'DbMySQLiCore' => 'classes/DbMySQLi.php',
'DbMySQLi' => 'override/classes/DbMySQLi.php',
'DeliveryCore' => 'classes/Delivery.php', 'DeliveryCore' => 'classes/Delivery.php',
'Delivery' => 'override/classes/Delivery.php', 'Delivery' => 'override/classes/Delivery.php',
'DiscountCore' => 'classes/Discount.php', 'DiscountCore' => 'classes/Discount.php',
@@ -146,8 +142,6 @@
'ModuleGrid' => 'override/classes/ModuleGrid.php', 'ModuleGrid' => 'override/classes/ModuleGrid.php',
'ModuleGridEngineCore' => 'classes/ModuleGridEngine.php', 'ModuleGridEngineCore' => 'classes/ModuleGridEngine.php',
'ModuleGridEngine' => 'override/classes/ModuleGridEngine.php', 'ModuleGridEngine' => 'override/classes/ModuleGridEngine.php',
'MySQLCore' => 'classes/MySQL.php',
'MySQL' => 'override/classes/MySQL.php',
'NotificationCore' => 'classes/Notification.php', 'NotificationCore' => 'classes/Notification.php',
'Notification' => 'override/classes/Notification.php', 'Notification' => 'override/classes/Notification.php',
'ObjectModelCore' => 'classes/ObjectModel.php', 'ObjectModelCore' => 'classes/ObjectModel.php',
@@ -252,6 +246,16 @@
'WebserviceSpecificManagementSearch' => 'override/classes/WebserviceSpecificManagementSearch.php', 'WebserviceSpecificManagementSearch' => 'override/classes/WebserviceSpecificManagementSearch.php',
'ZoneCore' => 'classes/Zone.php', 'ZoneCore' => 'classes/Zone.php',
'Zone' => 'override/classes/Zone.php', 'Zone' => 'override/classes/Zone.php',
'DbCore' => 'classes/db/Db.php',
'Db' => 'override/classes/db/Db.php',
'DbMySQLiCore' => 'classes/db/DbMySQLi.php',
'DbMySQLi' => 'override/classes/db/DbMySQLi.php',
'DbPDOCore' => 'classes/db/DbPDO.php',
'DbPDO' => 'override/classes/db/DbPDO.php',
'DbQueryCore' => 'classes/db/DbQuery.php',
'DbQuery' => 'override/classes/db/DbQuery.php',
'MySQLCore' => 'classes/db/MySQL.php',
'MySQL' => 'override/classes/db/MySQL.php',
'GroupShopCore' => 'classes/shop/GroupShop.php', 'GroupShopCore' => 'classes/shop/GroupShop.php',
'GroupShop' => 'override/classes/shop/GroupShop.php', 'GroupShop' => 'override/classes/shop/GroupShop.php',
'ShopCore' => 'classes/shop/Shop.php', 'ShopCore' => 'classes/shop/Shop.php',
+45 -38
View File
@@ -366,61 +366,68 @@ class CartCore extends ObjectModel
$id_country = Context::getContext()->country->id; $id_country = Context::getContext()->country->id;
// Build query // Build query
$sql = array(); $sql = new DbQuery();
// Build SELECT // Build SELECT
$sql['select'] = 'SELECT cp.`id_product_attribute`, cp.`id_product`, cp.`quantity` AS cart_quantity, cp.id_shop, pl.`name`, $sql->select('cp.`id_product_attribute`, cp.`id_product`, cp.`quantity` AS cart_quantity, cp.id_shop, pl.`name`')
pl.`description_short`, pl.`available_now`, pl.`available_later`, p.`id_product`, p.`id_category_default`, p.`id_supplier`, p.`id_manufacturer`, p.`on_sale`, p.`ecotax`, p.`additional_shipping_cost`, p.`available_for_order`, ->select('pl.`description_short`, pl.`available_now`, pl.`available_later`, p.`id_product`, p.`id_category_default`, p.`id_supplier`, p.`id_manufacturer`')
p.`price`, p.`weight`, p.`width`, p.`height`, p.`depth`, p.`out_of_stock`, p.`active`, p.`date_add`, p.`date_upd`, ->select('p.`on_sale`, p.`ecotax`, p.`additional_shipping_cost`, p.`available_for_order`, p.`price`, p.`weight`, p.`width`, p.`height`, p.`depth`, p.`out_of_stock`')
t.`id_tax`, tl.`name` AS tax, t.`rate`, stock.quantity, pl.`link_rewrite`, cl.`link_rewrite` AS category, CONCAT(cp.`id_product`, cp.`id_product_attribute`) AS unique_id'; ->select('p.`active`, p.`date_add`, p.`date_upd`, t.`id_tax`, tl.`name` AS tax, t.`rate`, stock.quantity, pl.`link_rewrite`, cl.`link_rewrite` AS category')
->select('CONCAT(cp.`id_product`, cp.`id_product_attribute`) AS unique_id');
// Build FROM // Build FROM
$sql['from'] = 'FROM `'._DB_PREFIX_.'cart_product` cp'; $sql->from(_DB_PREFIX_.'cart_product cp');
// Build JOIN // Build JOIN
$sql['join'] = 'LEFT JOIN `'._DB_PREFIX_.'product` p ON p.`id_product` = cp.`id_product` $sql->leftJoin(_DB_PREFIX_.'product p', 'p.`id_product` = cp.`id_product`')
LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON (p.`id_product` = pl.`id_product` AND pl.`id_lang` = '.(int)$this->id_lang.Context::getContext()->shop->sqlLang('pl').') ->leftJoin(_DB_PREFIX_.'product_lang pl', 'p.`id_product` = pl.`id_product` AND pl.`id_lang` = '.(int)$this->id_lang.Context::getContext()->shop->sqlLang('pl'))
LEFT JOIN `'._DB_PREFIX_.'tax_rule` tr ON (p.`id_tax_rules_group` = tr.`id_tax_rules_group` ->leftJoin(_DB_PREFIX_.'tax_rule tr', 'p.`id_tax_rules_group` = tr.`id_tax_rules_group`
AND tr.`id_country` = '.(int)$id_country.' AND tr.`id_country` = '.(int)$id_country.'
AND tr.`id_state` = 0 AND tr.`id_state` = 0
AND tr.`zipcode_from` = 0) AND tr.`zipcode_from` = 0')
LEFT JOIN `'._DB_PREFIX_.'tax` t ON (t.`id_tax` = tr.`id_tax`) ->leftJoin(_DB_PREFIX_.'tax t', 't.`id_tax` = tr.`id_tax`')
LEFT JOIN `'._DB_PREFIX_.'tax_lang` tl ON (t.`id_tax` = tl.`id_tax` AND tl.`id_lang` = '.(int)$this->id_lang.') ->leftJoin(_DB_PREFIX_.'tax_lang tl', 't.`id_tax` = tl.`id_tax` AND tl.`id_lang` = '.(int)$this->id_lang)
LEFT JOIN `'._DB_PREFIX_.'category_lang` cl ON (p.`id_category_default` = cl.`id_category` AND cl.`id_lang` = '.(int)$this->id_lang.Context::getContext()->shop->sqlLang('cl').') ->leftJoin(_DB_PREFIX_.'category_lang cl', 'p.`id_category_default` = cl.`id_category` AND cl.`id_lang` = '.(int)$this->id_lang.Context::getContext()->shop->sqlLang('cl'));
'.Product::sqlStock('cp', 'cp');
// @todo test if everything is ok, then refactorise call of this method
Product::sqlStock('cp', 'cp', false, null, $sql);
// Build WHERE clauses // Build WHERE clauses
$sql['where'] = ' $sql->where('cp.`id_cart` = '.(int)$this->id);
WHERE cp.`id_cart` = '.(int)$this->id.' if ($id_product)
'.($id_product ? ' AND cp.`id_product` = '.(int)$id_product : '').' $sql->where('cp.`id_product` = '.(int)$id_product);
AND p.`id_product` IS NOT NULL'; $sql->where('p.`id_product` IS NOT NULL');
// Build GROUP BY // Build GROUP BY
$sql['groupby'] = 'GROUP BY unique_id'; $sql->group('unique_id');
// Build ORDER BY // Build ORDER BY
$sql['orderby'] = 'ORDER BY cp.date_add ASC'; $sql->order('cp.date_add ASC');
if (Customization::isFeatureActive()) if (Customization::isFeatureActive())
{ {
$sql['select'] .= ', cu.`id_customization`, cu.`quantity` AS customization_quantity'; $sql->select('cu.`id_customization`, cu.`quantity` AS customization_quantity');
$sql['join'] .= 'LEFT JOIN `'._DB_PREFIX_.'customization` cu ON (p.`id_product` = cu.`id_product`)'; $sql->leftJoin(_DB_PREFIX_.'customization` cu ON (p.`id_product` = cu.`id_product`)');
} }
if (Combination::isFeatureActive()) if (Combination::isFeatureActive())
{ {
$sql['select'] .= ', pa.`price` AS price_attribute, pa.`ecotax` AS ecotax_attr, $sql->select('pa.`price` AS price_attribute, pa.`ecotax` AS ecotax_attr')
IF (IFNULL(pa.`reference`, \'\') = \'\', p.`reference`, pa.`reference`) AS reference, ->select('IF (IFNULL(pa.`reference`, \'\') = \'\', p.`reference`, pa.`reference`) AS reference')
IF (IFNULL(pa.`supplier_reference`, \'\') = \'\', p.`supplier_reference`, pa.`supplier_reference`) AS supplier_reference, ->select('IF (IFNULL(pa.`supplier_reference`, \'\') = \'\', p.`supplier_reference`, pa.`supplier_reference`) AS supplier_reference')
(p.`weight`+ pa.`weight`) weight_attribute, ->select('(p.`weight`+ pa.`weight`) weight_attribute')
IF (IFNULL(pa.`ean13`, \'\') = \'\', p.`ean13`, pa.`ean13`) AS ean13, IF (IFNULL(pa.`upc`, \'\') = \'\', p.`upc`, pa.`upc`) AS upc, ->select('IF (IFNULL(pa.`ean13`, \'\') = \'\', p.`ean13`, pa.`ean13`) AS ean13, IF (IFNULL(pa.`upc`, \'\') = \'\', p.`upc`, pa.`upc`) AS upc')
pai.`id_image` as pai_id_image, IFNULL(pa.`minimal_quantity`, p.`minimal_quantity`) as minimal_quantity, pa.`ecotax` AS ecotax_attr'; ->select('pai.`id_image` as pai_id_image, IFNULL(pa.`minimal_quantity`, p.`minimal_quantity`) as minimal_quantity, pa.`ecotax` AS ecotax_attr');
$sql['join'] .= '
LEFT JOIN `'._DB_PREFIX_.'product_attribute` pa ON (pa.`id_product_attribute` = cp.`id_product_attribute`) $sql->leftJoin(_DB_PREFIX_.'product_attribute pa', 'pa.`id_product_attribute` = cp.`id_product_attribute`')
LEFT JOIN `'._DB_PREFIX_.'product_attribute_image` pai ON (pai.`id_product_attribute` = pa.`id_product_attribute`) ->leftJoin(_DB_PREFIX_.'product_attribute_image pai', 'pai.`id_product_attribute` = pa.`id_product_attribute`');
';
} }
else else
$sql['select'] .= ', p.`reference` AS reference, p.`supplier_reference` AS supplier_reference, $sql->select('p.`reference` AS reference, p.`supplier_reference` AS supplier_reference, p.`ean13`, p.`upc` AS upc, p.`minimal_quantity` AS minimal_quantity');
p.`ean13`, p.`upc` AS upc, p.`minimal_quantity` AS minimal_quantity';
$result = Db::getInstance()->ExecuteS($sql);
$result = Db::getInstance()->ExecuteS(Tools::buildQuery($sql));
// Reset the cache before the following return, or else an empty cart will add dozens of queries // Reset the cache before the following return, or else an empty cart will add dozens of queries
$productsIds = array(); $productsIds = array();
$paIds = array(); $paIds = array();
foreach ($result as $row) foreach ($result as $row)
+29 -10
View File
@@ -2056,22 +2056,41 @@ class ProductCore extends ObjectModel
* @param Context $context * @param Context $context
* @return string * @return string
*/ */
public static function sqlStock($productAlias, $productAttribute = 0, $innerJoin = false, Shop $shop = null) public static function sqlStock($productAlias, $productAttribute = 0, $innerJoin = false, Shop $shop = null, DbQuery $sql = null)
{ {
if (!$shop) if (!$shop)
$shop = Context::getContext()->shop; $shop = Context::getContext()->shop;
$sql = (($innerJoin) ? ' INNER ' : ' LEFT ').'JOIN '._DB_PREFIX_.'stock stock ON stock.id_product = '.pSQL($productAlias).'.id_product'; if ($sql)
if (!is_null($productAttribute))
{ {
if (!Combination::isFeatureActive()) // @todo remove this code when query builder is accepted or removed
$sql .= ' AND stock.id_product_attribute = 0'; $method = ($innerJoin) ? 'innerJoin' : 'leftJoin';
else if (is_numeric($productAttribute)) $sql->$method(_DB_PREFIX_.'stock stock', 'stock.id_product = '.pSQL($productAlias).'.id_product');
$sql .= ' AND stock.id_product_attribute = '.$productAttribute; if (!is_null($productAttribute))
else if (is_string($productAttribute)) {
$sql .= ' AND stock.id_product_attribute = IFNULL('.pSQL($productAttribute).'.id_product_attribute, 0)'; if (!Combination::isFeatureActive())
$sql->where('stock.id_product_attribute = 0');
else if (is_numeric($productAttribute))
$sql->where('stock.id_product_attribute = '.$productAttribute);
else if (is_string($productAttribute))
$sql->where('stock.id_product_attribute = IFNULL('.pSQL($productAttribute).'.id_product_attribute, 0)');
}
$sql->where(ltrim($shop->sqlRestriction(Shop::SHARE_STOCK, 'stock'), ' AND '));
}
else
{
$sql = (($innerJoin) ? ' INNER ' : ' LEFT ').'JOIN '._DB_PREFIX_.'stock stock ON stock.id_product = '.pSQL($productAlias).'.id_product';
if (!is_null($productAttribute))
{
if (!Combination::isFeatureActive())
$sql .= ' AND stock.id_product_attribute = 0';
else if (is_numeric($productAttribute))
$sql .= ' AND stock.id_product_attribute = '.$productAttribute;
else if (is_string($productAttribute))
$sql .= ' AND stock.id_product_attribute = IFNULL('.pSQL($productAttribute).'.id_product_attribute, 0)';
}
$sql .= $shop->sqlRestriction(Shop::SHARE_STOCK, 'stock') . ' ';
} }
$sql .= $shop->sqlRestriction(Shop::SHARE_STOCK, 'stock') . ' ';
return $sql; return $sql;
} }
+13 -8
View File
@@ -1725,8 +1725,8 @@ FileETag INode MTime Size
include_once(_PS_TOOL_DIR_.'json/json.php'); include_once(_PS_TOOL_DIR_.'json/json.php');
$pearJson = new Services_JSON(($assoc) ? SERVICES_JSON_LOOSE_TYPE : 0); $pearJson = new Services_JSON(($assoc) ? SERVICES_JSON_LOOSE_TYPE : 0);
return $pearJson->decode($json); return $pearJson->decode($json);
} }
} }
/** /**
* Convert an array to json string * Convert an array to json string
@@ -1743,21 +1743,26 @@ FileETag INode MTime Size
include_once(_PS_TOOL_DIR_.'json/json.php'); include_once(_PS_TOOL_DIR_.'json/json.php');
$pearJson = new Services_JSON(); $pearJson = new Services_JSON();
return $pearJson->encode($data); return $pearJson->encode($data);
} }
} }
/** /**
* Display a warning message indicating that the method is deprecated * Display a warning message indicating that the method is deprecated
*/ */
public static function displayAsDeprecated() public static function displayAsDeprecated($message = null)
{ {
if (_PS_DISPLAY_COMPATIBILITY_WARNING_) if (_PS_DISPLAY_COMPATIBILITY_WARNING_)
{ {
$backtrace = debug_backtrace(); $backtrace = debug_backtrace();
$callee = next($backtrace); $callee = next($backtrace);
trigger_error('Function <strong>'.$callee['function'].'()</strong> is deprecated in <strong>'.$callee['file'].'</strong> on line <strong>'.$callee['line'].'</strong><br />', E_USER_WARNING);
$message = self::displayError('The function').' '.$callee['function'].' ('.self::displayError('Line').' '.$callee['line'].') '.self::displayError('is deprecated and will be removed in the next major version.'); if ($message)
trigger_error($message, E_USER_WARNING);
else
{
trigger_error('Function <strong>'.$callee['function'].'()</strong> is deprecated in <strong>'.$callee['file'].'</strong> on line <strong>'.$callee['line'].'</strong><br />', E_USER_WARNING);
$message = self::displayError('The function').' '.$callee['function'].' ('.self::displayError('Line').' '.$callee['line'].') '.self::displayError('is deprecated and will be removed in the next major version.');
}
Logger::addLog($message, 3, $callee['class']); Logger::addLog($message, 3, $callee['class']);
} }
@@ -1771,7 +1776,7 @@ FileETag INode MTime Size
$backtrace = debug_backtrace(); $backtrace = debug_backtrace();
$callee = next($backtrace); $callee = next($backtrace);
$error = 'Parameter <strong>'.$parameter.'</strong> in function <strong>'.$callee['function'].'()</strong> is deprecated in <strong>'.$callee['file'].'</strong> on line <strong>'.$callee['Line'].'</strong><br />'; $error = 'Parameter <strong>'.$parameter.'</strong> in function <strong>'.$callee['function'].'()</strong> is deprecated in <strong>'.$callee['file'].'</strong> on line <strong>'.$callee['Line'].'</strong><br />';
$message = self::displayError('The parameter').' '.$parameter.' '.self::displayError(' in function ').' '.$callee['function'].' ('.self::displayError('Line').' '.$callee['Line'].') '.self::displayError('is deprecated and will be removed in the next major version.'); $message = self::displayError('The parameter').' '.$parameter.' '.self::displayError(' in function ').' '.$callee['function'].' ('.self::displayError('Line').' '.$callee['Line'].') '.self::displayError('is deprecated and will be removed in the next major version.');
self::throwDeprecated($error, $message, $callee['class']); self::throwDeprecated($error, $message, $callee['class']);
} }
+6
View File
@@ -284,6 +284,7 @@ abstract class DbCore
*/ */
public function query($sql) public function query($sql)
{ {
$sql = (string)$sql;
$result = $this->_query($sql); $result = $this->_query($sql);
if (_PS_DEBUG_SQL_) if (_PS_DEBUG_SQL_)
$this->displayError($sql); $this->displayError($sql);
@@ -318,6 +319,7 @@ abstract class DbCore
*/ */
public function Execute($sql, $use_cache = 1) public function Execute($sql, $use_cache = 1)
{ {
$sql = (string)$sql;
$this->_result = $this->query($sql); $this->_result = $this->query($sql);
if ($use_cache AND _PS_CACHE_ENABLED_) if ($use_cache AND _PS_CACHE_ENABLED_)
Cache::getInstance()->deleteQuery($sql); Cache::getInstance()->deleteQuery($sql);
@@ -334,6 +336,7 @@ abstract class DbCore
*/ */
public function ExecuteS($sql, $array = true, $use_cache = 1) public function ExecuteS($sql, $array = true, $use_cache = 1)
{ {
$sql = (string)$sql;
$this->_result = false; $this->_result = false;
$this->_lastQuery = $sql; $this->_lastQuery = $sql;
if ($use_cache AND _PS_CACHE_ENABLED_ && $array AND ($result = Cache::getInstance()->get(md5($sql)))) if ($use_cache AND _PS_CACHE_ENABLED_ && $array AND ($result = Cache::getInstance()->get(md5($sql))))
@@ -369,6 +372,7 @@ abstract class DbCore
*/ */
public function getRow($sql, $use_cache = 1) public function getRow($sql, $use_cache = 1)
{ {
$sql = (string)$sql;
$sql .= ' LIMIT 1'; $sql .= ' LIMIT 1';
$this->_result = false; $this->_result = false;
$this->_lastQuery = $sql; $this->_lastQuery = $sql;
@@ -398,6 +402,7 @@ abstract class DbCore
*/ */
public function getValue($sql, $use_cache = 1) public function getValue($sql, $use_cache = 1)
{ {
$sql = (string)$sql;
if (!$result = $this->getRow($sql, $use_cache)) if (!$result = $this->getRow($sql, $use_cache))
return false; return false;
return array_shift($result); return array_shift($result);
@@ -432,6 +437,7 @@ abstract class DbCore
{ {
global $webservice_call; global $webservice_call;
$sql = (string)$sql;
$this->_result = false; $this->_result = false;
$result = $this->query($sql); $result = $this->query($sql);
if ($use_cache AND _PS_CACHE_ENABLED_) if ($use_cache AND _PS_CACHE_ENABLED_)
@@ -25,6 +25,9 @@
* International Registered Trademark & Property of PrestaShop SA * International Registered Trademark & Property of PrestaShop SA
*/ */
/**
* @since 1.5.0
*/
class DbMySQLiCore extends Db class DbMySQLiCore extends Db
{ {
/** /**
File renamed without changes.
+9 -10
View File
@@ -170,19 +170,18 @@ class Tools extends ToolsCore
*/ */
public static function displayAsDeprecated() public static function displayAsDeprecated()
{ {
if (_PS_DISPLAY_COMPATIBILITY_WARNING_) if (_PS_DISPLAY_COMPATIBILITY_WARNING_)
{ {
$backtrace = debug_backtrace(); $backtrace = debug_backtrace();
$callee = next($backtrace); $callee = next($backtrace);
if(PS_USE_FIREPHP) if (PS_USE_FIREPHP)
FB::warn('Function <strong>'.$callee['function'].'()</strong> is deprecated in <strong>'.$callee['file'].'</strong> on line <strong>'.$callee['line'].'</strong><br />', 'Deprecated method'); FB::warn('Function <strong>'.$callee['function'].'()</strong> is deprecated in <strong>'.$callee['file'].'</strong> on line <strong>'.$callee['line'].'</strong><br />', 'Deprecated method');
else else
trigger_error('Function <strong>'.$callee['function'].'()</strong> is deprecated in <strong>'.$callee['file'].'</strong> on line <strong>'.$callee['line'].'</strong><br />', E_USER_WARNING); trigger_error('Function <strong>'.$callee['function'].'()</strong> is deprecated in <strong>'.$callee['file'].'</strong> on line <strong>'.$callee['line'].'</strong><br />', E_USER_WARNING);
$message = Tools::displayError('The function').' '.$callee['function'].' ('.Tools::displayError('Line').' '.$callee['line'].') '.Tools::displayError('is deprecated and will be removed in the next major version.'); $message = Tools::displayError('The function').' '.$callee['function'].' ('.Tools::displayError('Line').' '.$callee['line'].') '.Tools::displayError('is deprecated and will be removed in the next major version.');
Logger::addLog($message, 3, $callee['class']); Logger::addLog($message, 3, $callee['class']);
} }
} }
/** /**
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.