[*] CORE : Remove duplicate SQL queries

This commit is contained in:
gRoussac
2013-11-11 13:41:23 +01:00
parent de404706d9
commit a7869a1f06
22 changed files with 507 additions and 322 deletions
+1 -3
View File
@@ -572,7 +572,7 @@ class Blocktopmenu extends Module
switch (substr($item, 0, strlen($value[1])))
{
case 'CAT':
$this->getCategory((int)$id);
$this->getCategory($id, $id_lang, $id_shop);
break;
case 'PRD':
@@ -687,9 +687,7 @@ class Blocktopmenu extends Module
if (isset($children) && count($children))
foreach ($children as $child)
{
$this->getCategoryOption((int)$child['id_category'], (int)$id_lang, (int)$child['id_shop']);
}
}
private function getCategory($id_category, $id_lang = false, $id_shop = false)
+14 -8
View File
@@ -171,6 +171,8 @@ class WishList extends ObjectModel
*/
public static function getByIdCustomer($id_customer)
{
if (!Validate::isUnsignedId($id_customer))
die (Tools::displayError());
if (Shop::getContextShopID())
$shop_restriction = 'AND id_shop = '.(int)Shop::getContextShopID();
elseif (Shop::getContextShopGroupID())
@@ -178,14 +180,18 @@ class WishList extends ObjectModel
else
$shop_restriction = '';
if (!Validate::isUnsignedId($id_customer))
die (Tools::displayError());
return (Db::getInstance()->executeS('
SELECT w.`id_wishlist`, w.`name`, w.`token`, w.`date_add`, w.`date_upd`, w.`counter`
FROM `'._DB_PREFIX_.'wishlist` w
WHERE `id_customer` = '.(int)($id_customer).'
'.$shop_restriction.'
ORDER BY w.`name` ASC'));
$cache_id = __CLASS__.__FUNCTION__.(int)$id_customer.'-'.(int)Shop::getContextShopID().'-'.(int)Shop::getContextShopGroupID();
if (!Cache::isStored($cache_id))
{
$result = Db::getInstance()->executeS('
SELECT w.`id_wishlist`, w.`name`, w.`token`, w.`date_add`, w.`date_upd`, w.`counter`
FROM `'._DB_PREFIX_.'wishlist` w
WHERE `id_customer` = '.(int)($id_customer).'
'.$shop_restriction.'
ORDER BY w.`name` ASC');
Cache::store($cache_id, $result);
}
return Cache::retrieve($cache_id);
}
public static function refreshWishList($id_wishlist)
+45 -29
View File
@@ -97,18 +97,24 @@ class ProductComment extends ObjectModel
if ($n != null && $n <= 0)
$n = 5;
return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
SELECT pc.`id_product_comment`,
(SELECT count(*) FROM `'._DB_PREFIX_.'product_comment_usefulness` pcu WHERE pcu.`id_product_comment` = pc.`id_product_comment` AND pcu.`usefulness` = 1) as total_useful,
(SELECT count(*) FROM `'._DB_PREFIX_.'product_comment_usefulness` pcu WHERE pcu.`id_product_comment` = pc.`id_product_comment`) as total_advice, '.
((int)$id_customer ? '(SELECT count(*) FROM `'._DB_PREFIX_.'product_comment_usefulness` pcuc WHERE pcuc.`id_product_comment` = pc.`id_product_comment` AND pcuc.id_customer = '.(int)$id_customer.') as customer_advice, ' : '').
((int)$id_customer ? '(SELECT count(*) FROM `'._DB_PREFIX_.'product_comment_report` pcrc WHERE pcrc.`id_product_comment` = pc.`id_product_comment` AND pcrc.id_customer = '.(int)$id_customer.') as customer_report, ' : '').'
IF(c.id_customer, CONCAT(c.`firstname`, \' \', LEFT(c.`lastname`, 1)), pc.customer_name) customer_name, pc.`content`, pc.`grade`, pc.`date_add`, pc.title
FROM `'._DB_PREFIX_.'product_comment` pc
LEFT JOIN `'._DB_PREFIX_.'customer` c ON c.`id_customer` = pc.`id_customer`
WHERE pc.`id_product` = '.(int)($id_product).($validate == '1' ? ' AND pc.`validate` = 1' : '').'
ORDER BY pc.`date_add` DESC
'.($n ? 'LIMIT '.(int)(($p - 1) * $n).', '.(int)($n) : ''));
$cache_id = __CLASS__.__FUNCTION__.(int)$id_product.'-'.(int)$p.'-'.(int)$n.'-'.(int)$id_customer.'-'.(bool)$validate;
if (!Cache::isStored($cache_id))
{
$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
SELECT pc.`id_product_comment`,
(SELECT count(*) FROM `'._DB_PREFIX_.'product_comment_usefulness` pcu WHERE pcu.`id_product_comment` = pc.`id_product_comment` AND pcu.`usefulness` = 1) as total_useful,
(SELECT count(*) FROM `'._DB_PREFIX_.'product_comment_usefulness` pcu WHERE pcu.`id_product_comment` = pc.`id_product_comment`) as total_advice, '.
((int)$id_customer ? '(SELECT count(*) FROM `'._DB_PREFIX_.'product_comment_usefulness` pcuc WHERE pcuc.`id_product_comment` = pc.`id_product_comment` AND pcuc.id_customer = '.(int)$id_customer.') as customer_advice, ' : '').
((int)$id_customer ? '(SELECT count(*) FROM `'._DB_PREFIX_.'product_comment_report` pcrc WHERE pcrc.`id_product_comment` = pc.`id_product_comment` AND pcrc.id_customer = '.(int)$id_customer.') as customer_report, ' : '').'
IF(c.id_customer, CONCAT(c.`firstname`, \' \', LEFT(c.`lastname`, 1)), pc.customer_name) customer_name, pc.`content`, pc.`grade`, pc.`date_add`, pc.title
FROM `'._DB_PREFIX_.'product_comment` pc
LEFT JOIN `'._DB_PREFIX_.'customer` c ON c.`id_customer` = pc.`id_customer`
WHERE pc.`id_product` = '.(int)($id_product).($validate == '1' ? ' AND pc.`validate` = 1' : '').'
ORDER BY pc.`date_add` DESC
'.($n ? 'LIMIT '.(int)(($p - 1) * $n).', '.(int)($n) : ''));
Cache::store($cache_id, $result);
}
return Cache::retrieve($cache_id);
}
/**
@@ -118,18 +124,24 @@ class ProductComment extends ObjectModel
*/
public static function getByCustomer($id_product, $id_customer, $get_last = false, $id_guest = false)
{
$results = Db::getInstance()->executeS('
SELECT *
FROM `'._DB_PREFIX_.'product_comment` pc
WHERE pc.`id_product` = '.(int)$id_product.'
AND '.(!$id_guest ? 'pc.`id_customer` = '.(int)$id_customer : 'pc.`id_guest` = '.(int)$id_guest).'
ORDER BY pc.`date_add` DESC '
.($get_last ? 'LIMIT 1' : '')
);
$cache_id = __CLASS__.__FUNCTION__.(int)$id_product.'-'.(int)$id_customer.'-'.(bool)$get_last.'-'.(int)$id_guest;
if (!Cache::isStored($cache_id))
{
$results = Db::getInstance()->executeS('
SELECT *
FROM `'._DB_PREFIX_.'product_comment` pc
WHERE pc.`id_product` = '.(int)$id_product.'
AND '.(!$id_guest ? 'pc.`id_customer` = '.(int)$id_customer : 'pc.`id_guest` = '.(int)$id_guest).'
ORDER BY pc.`date_add` DESC '
.($get_last ? 'LIMIT 1' : '')
);
if ($get_last)
$results = array_shift($results);
return $results;
if ($get_last && count($results))
$results = array_shift($results);
Cache::store($cache_id, $results);
}
return Cache::retrieve($cache_id);
}
/**
@@ -202,12 +214,16 @@ class ProductComment extends ObjectModel
if (!Validate::isUnsignedId($id_product))
die(Tools::displayError());
$validate = (int)Configuration::get('PRODUCT_COMMENTS_MODERATE');
if (($result = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow('
SELECT COUNT(`id_product_comment`) AS "nbr"
FROM `'._DB_PREFIX_.'product_comment` pc
WHERE `id_product` = '.(int)($id_product).($validate == '1' ? ' AND `validate` = 1' : ''))) === false)
return false;
return (int)($result['nbr']);
$cache_id = __CLASS__.__FUNCTION__.(int)$id_product.'-'.$validate;
if (!Cache::isStored($cache_id))
{
$result = (int)Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue('
SELECT COUNT(`id_product_comment`) AS "nbr"
FROM `'._DB_PREFIX_.'product_comment` pc
WHERE `id_product` = '.(int)($id_product).($validate == '1' ? ' AND `validate` = 1' : ''));
Cache::store($cache_id, $result);
}
return Cache::retrieve($cache_id);
}
/**
@@ -159,26 +159,33 @@ class ProductCommentCriterion extends ObjectModel
$table = '_shop';
$alias = 'ps';
}
return Db::getInstance()->executeS('
SELECT pcc.`id_product_comment_criterion`, pccl.`name`
FROM `'._DB_PREFIX_.'product_comment_criterion` pcc
LEFT JOIN `'._DB_PREFIX_.'product_comment_criterion_lang` pccl
ON (pcc.id_product_comment_criterion = pccl.id_product_comment_criterion)
LEFT JOIN `'._DB_PREFIX_.'product_comment_criterion_product` pccp
ON (pcc.`id_product_comment_criterion` = pccp.`id_product_comment_criterion` AND pccp.`id_product` = '.(int)$id_product.')
LEFT JOIN `'._DB_PREFIX_.'product_comment_criterion_category` pccc
ON (pcc.`id_product_comment_criterion` = pccc.`id_product_comment_criterion`)
LEFT JOIN `'._DB_PREFIX_.'product'.$table.'` '.$alias.'
ON ('.$alias.'.id_category_default = pccc.id_category AND '.$alias.'.id_product = '.(int)$id_product.')
WHERE pccl.`id_lang` = '.(int)($id_lang).'
AND (
pccp.id_product IS NOT NULL
OR ps.id_product IS NOT NULL
OR pcc.id_product_comment_criterion_type = 1
)
AND pcc.active = 1
GROUP BY pcc.id_product_comment_criterion
');
$cache_id = __CLASS__.__FUNCTION__.(int)$id_product.'-'.(int)$id_lang;
if (!Cache::isStored($cache_id))
{
$result = Db::getInstance()->executeS('
SELECT pcc.`id_product_comment_criterion`, pccl.`name`
FROM `'._DB_PREFIX_.'product_comment_criterion` pcc
LEFT JOIN `'._DB_PREFIX_.'product_comment_criterion_lang` pccl
ON (pcc.id_product_comment_criterion = pccl.id_product_comment_criterion)
LEFT JOIN `'._DB_PREFIX_.'product_comment_criterion_product` pccp
ON (pcc.`id_product_comment_criterion` = pccp.`id_product_comment_criterion` AND pccp.`id_product` = '.(int)$id_product.')
LEFT JOIN `'._DB_PREFIX_.'product_comment_criterion_category` pccc
ON (pcc.`id_product_comment_criterion` = pccc.`id_product_comment_criterion`)
LEFT JOIN `'._DB_PREFIX_.'product'.$table.'` '.$alias.'
ON ('.$alias.'.id_category_default = pccc.id_category AND '.$alias.'.id_product = '.(int)$id_product.')
WHERE pccl.`id_lang` = '.(int)($id_lang).'
AND (
pccp.id_product IS NOT NULL
OR ps.id_product IS NOT NULL
OR pcc.id_product_comment_criterion_type = 1
)
AND pcc.active = 1
GROUP BY pcc.id_product_comment_criterion
');
Cache::store($cache_id, $result);
}
return Cache::retrieve($cache_id);
}
/**