From 9dc075ca39468cc5703945c396de21d847e145c4 Mon Sep 17 00:00:00 2001 From: Damien Metzger Date: Fri, 29 Nov 2013 14:21:06 +0100 Subject: [PATCH] [*] FO : Code cleaning, optimizations, new callstack available in the profiling tools --- classes/Currency.php | 31 ++++++++--------- classes/Manufacturer.php | 61 ++++++++++++---------------------- classes/Supplier.php | 6 +--- tools/profiling/Controller.php | 19 ++++++++--- tools/profiling/Db.php | 22 ++++++------ 5 files changed, 63 insertions(+), 76 deletions(-) diff --git a/classes/Currency.php b/classes/Currency.php index d92660542..ecba2e84b 100644 --- a/classes/Currency.php +++ b/classes/Currency.php @@ -207,16 +207,15 @@ class CurrencyCore extends ObjectModel * * @return array Currencies */ - public static function getCurrencies($object = false, $active = 1) + public static function getCurrencies($object = false, $active = true) { - $sql = 'SELECT * - FROM `'._DB_PREFIX_.'currency` c - '.Shop::addSqlAssociation('currency', 'c').' - WHERE `deleted` = 0' - .($active == 1 ? ' AND c.`active` = 1' : '').' - GROUP BY c.id_currency - ORDER BY `name` ASC'; - $tab = Db::getInstance()->executeS($sql); + $tab = Db::getInstance()->executeS(' + SELECT * + FROM `'._DB_PREFIX_.'currency` c + '.Shop::addSqlAssociation('currency', 'c').' + WHERE `deleted` = 0 + '.($active ? ' AND c.`active` = 1' : '').' + ORDER BY `name` ASC'); if ($object) foreach ($tab as $key => $currency) $tab[$key] = Currency::getCurrencyInstance($currency['id_currency']); @@ -225,14 +224,12 @@ class CurrencyCore extends ObjectModel public static function getCurrenciesByIdShop($id_shop = 0) { - $sql = 'SELECT * - FROM `'._DB_PREFIX_.'currency` c - LEFT JOIN `'._DB_PREFIX_.'currency_shop` cs ON (cs.`id_currency` = c.`id_currency`) - '.($id_shop != 0 ? ' WHERE cs.`id_shop` = '.(int)$id_shop : '').' - GROUP BY c.id_currency - ORDER BY `name` ASC'; - - return Db::getInstance()->executeS($sql); + return Db::getInstance()->executeS(' + SELECT * + FROM `'._DB_PREFIX_.'currency` c + LEFT JOIN `'._DB_PREFIX_.'currency_shop` cs ON (cs.`id_currency` = c.`id_currency`) + '.($id_shop ? ' WHERE cs.`id_shop` = '.(int)$id_shop : '').' + ORDER BY `name` ASC'); } diff --git a/classes/Manufacturer.php b/classes/Manufacturer.php index ae15e4ecb..eab4a667e 100644 --- a/classes/Manufacturer.php +++ b/classes/Manufacturer.php @@ -162,22 +162,14 @@ class ManufacturerCore extends ObjectModel if (!Group::isFeatureActive()) $all_group = true; - $sql = 'SELECT m.*, ml.`description`, ml.`short_description` - FROM `'._DB_PREFIX_.'manufacturer` m - LEFT JOIN `'._DB_PREFIX_.'manufacturer_lang` ml ON ( - m.`id_manufacturer` = ml.`id_manufacturer` - AND ml.`id_lang` = '.(int)$id_lang.' - ) - '.Shop::addSqlAssociation('manufacturer', 'm'); - if ($active) - $sql .= ' - WHERE m.`active` = 1'; - $sql .= ' - GROUP BY m.id_manufacturer - ORDER BY m.`name` ASC'. - ($p ? ' LIMIT '.(((int)$p - 1) * (int)$n).','.(int)$n : ''); - - $manufacturers = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql); + $manufacturers = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS(' + SELECT m.*, ml.`description`, ml.`short_description` + FROM `'._DB_PREFIX_.'manufacturer` m + '.Shop::addSqlAssociation('manufacturer', 'm').' + INNER JOIN `'._DB_PREFIX_.'manufacturer_lang` ml ON (m.`id_manufacturer` = ml.`id_manufacturer` AND ml.`id_lang` = '.(int)$id_lang.') + '.($active ? 'WHERE m.`active` = 1' : '').' + ORDER BY m.`name` ASC + '.($p ? ' LIMIT '.(((int)$p - 1) * (int)$n).','.(int)$n : '')); if ($manufacturers === false) return false; @@ -192,35 +184,26 @@ class ManufacturerCore extends ObjectModel foreach ($manufacturers as $key => $manufacturer) { - $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS( - 'SELECT p.`id_product` - FROM `'._DB_PREFIX_.'product` p - '.Shop::addSqlAssociation('product', 'p').' - LEFT JOIN `'._DB_PREFIX_.'manufacturer` as m ON (m.`id_manufacturer`= p.`id_manufacturer`) - WHERE m.`id_manufacturer` = '.(int)$manufacturer['id_manufacturer']. - ($active ? ' AND product_shop.`active` = 1 ' : ''). - ' AND product_shop.`visibility` NOT IN ("none")'. - ($all_group ? '' : ' AND p.`id_product` IN ( - SELECT cp.`id_product` - FROM `'._DB_PREFIX_.'category_group` cg - LEFT JOIN `'._DB_PREFIX_.'category_product` cp ON (cp.`id_category` = cg.`id_category`) - WHERE cg.`id_group` '.$sql_groups.' - )') - ); - - $manufacturers[$key]['nb_products'] = count($result); + $manufacturers[$key]['nb_products'] = Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue(' + SELECT COUNT(DISTINCT p.`id_product`) + FROM `'._DB_PREFIX_.'product` p + '.Shop::addSqlAssociation('product', 'p').' + WHERE p.`id_manufacturer` = '.(int)$manufacturer['id_manufacturer'].' + AND product_shop.`visibility` NOT IN ("none") + '.($active ? ' AND product_shop.`active` = 1 ' : '').' + '.($all_group ? '' : ' AND p.`id_product` IN ( + SELECT cp.`id_product` + FROM `'._DB_PREFIX_.'category_group` cg + LEFT JOIN `'._DB_PREFIX_.'category_product` cp ON (cp.`id_category` = cg.`id_category`) + WHERE cg.`id_group` '.$sql_groups.' + )')); } } $total_manufacturers = count($manufacturers); $rewrite_settings = (int)Configuration::get('PS_REWRITING_SETTINGS'); - for ($i = 0; $i < $total_manufacturers; $i++) - if ($rewrite_settings) - $manufacturers[$i]['link_rewrite'] = Tools::link_rewrite($manufacturers[$i]['name']); - else - $manufacturers[$i]['link_rewrite'] = 0; - + $manufacturers[$i]['link_rewrite'] = ($rewrite_settings ? Tools::link_rewrite($manufacturers[$i]['name']) : 0); return $manufacturers; } diff --git a/classes/Supplier.php b/classes/Supplier.php index 7b5e23bed..ed0f70559 100644 --- a/classes/Supplier.php +++ b/classes/Supplier.php @@ -119,7 +119,6 @@ class SupplierCore extends ObjectModel $query->where('s.`active` = 1'); $query->orderBy(' s.`name` ASC'); $query->limit($n, ($p - 1) * $n); - $query->groupBy('s.id_supplier'); $suppliers = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($query); if ($suppliers === false) @@ -159,10 +158,7 @@ class SupplierCore extends ObjectModel $nb_suppliers = count($suppliers); $rewrite_settings = (int)Configuration::get('PS_REWRITING_SETTINGS'); for ($i = 0; $i < $nb_suppliers; $i++) - if ($rewrite_settings) - $suppliers[$i]['link_rewrite'] = Tools::link_rewrite($suppliers[$i]['name']); - else - $suppliers[$i]['link_rewrite'] = 0; + $suppliers[$i]['link_rewrite'] = ($rewrite_settings ? Tools::link_rewrite($suppliers[$i]['name']) : 0); return $suppliers; } diff --git a/tools/profiling/Controller.php b/tools/profiling/Controller.php index a6d8f3108..0902718d0 100644 --- a/tools/profiling/Controller.php +++ b/tools/profiling/Controller.php @@ -414,10 +414,11 @@ abstract class Controller extends ControllerCore $query_row = array( 'time' => $data['time'], 'query' => $data['query'], - 'location' => $data['file'].':'.$data['line'], + 'location' => $data['stack'][0]['file'].':'.$data['stack'][0]['line'], 'filesort' => false, 'rows' => 1, - 'group_by' => false + 'group_by' => false, + 'stack' => $data['stack'] ); if (preg_match('/^\s*select\s+/i', $data['query'])) { @@ -445,7 +446,7 @@ abstract class Controller extends ControllerCore