1- <?php
1+ <?php declare (strict_types= 1 );
22/**
33 * Copyright © Magento, Inc. All rights reserved.
44 * See COPYING.txt for license details.
55 */
66
7- namespace Magento \Eav \Model \Mview ;
7+ namespace Magento \Eav \Model \Mview \ ChangeLogBatchWalker ;
88
99use Magento \Framework \App \ResourceConnection ;
10+ use Magento \Framework \DB \Select ;
1011use Magento \Framework \DB \Sql \Expression ;
11- use Magento \Framework \Mview \View \ChangeLogBatchWalkerInterface ;
12+ use Magento \Framework \Mview \View \ChangeLogBatchWalker \ IdsSelectBuilderInterface ;
1213use Magento \Framework \Mview \View \ChangelogInterface ;
1314
14- /**
15- * Class BatchIterator
16- */
17- class ChangeLogBatchWalker implements ChangeLogBatchWalkerInterface
15+ class IdsSelectBuilder implements IdsSelectBuilderInterface
1816{
1917 private const GROUP_CONCAT_MAX_VARIABLE = 'group_concat_max_len ' ;
2018 /** ID is defined as small int. Default size of it is 5 */
2119 private const DEFAULT_ID_SIZE = 5 ;
22-
2320 /**
24- * @var ResourceConnection
21+ * @var \Magento\Framework\App\ ResourceConnection
2522 */
26- private $ resourceConnection ;
27-
23+ private ResourceConnection $ resourceConnection ;
2824 /**
2925 * @var array
3026 */
31- private $ entityTypeCodes ;
27+ private array $ entityTypeCodes ;
3228
3329 /**
34- * @param ResourceConnection $resourceConnection
30+ * @param \Magento\Framework\App\ ResourceConnection $resourceConnection
3531 * @param array $entityTypeCodes
3632 */
3733 public function __construct (
3834 ResourceConnection $ resourceConnection ,
39- array $ entityTypeCodes = []
35+ array $ entityTypeCodes = []
4036 ) {
4137 $ this ->resourceConnection = $ resourceConnection ;
4238 $ this ->entityTypeCodes = $ entityTypeCodes ;
4339 }
4440
41+ /**
42+ * @inheritdoc
43+ */
44+ public function build (ChangelogInterface $ changelog ): Select
45+ {
46+ $ numberOfAttributes = $ this ->calculateEavAttributeSize ($ changelog );
47+ $ this ->setGroupConcatMax ($ numberOfAttributes );
48+
49+ $ changelogTableName = $ this ->resourceConnection ->getTableName ($ changelog ->getName ());
50+
51+ $ connection = $ this ->resourceConnection ->getConnection ();
52+
53+ $ columns = [
54+ $ changelog ->getColumnName (),
55+ 'attribute_ids ' => new Expression ('GROUP_CONCAT(attribute_id) ' ),
56+ 'store_id '
57+ ];
58+
59+ return $ connection ->select ()
60+ ->from ($ changelogTableName , $ columns )
61+ ->group ([$ changelog ->getColumnName (), 'store_id ' ]);
62+ }
63+
4564 /**
4665 * Calculate EAV attributes size
4766 *
@@ -54,7 +73,7 @@ private function calculateEavAttributeSize(ChangelogInterface $changelog): int
5473 $ connection = $ this ->resourceConnection ->getConnection ();
5574
5675 if (!isset ($ this ->entityTypeCodes [$ changelog ->getViewId ()])) {
57- throw new \Exception ('Entity type for view was not defined ' );
76+ throw new \InvalidArgumentException ('Entity type for view was not defined ' );
5877 }
5978
6079 $ select = $ connection ->select ();
@@ -63,12 +82,12 @@ private function calculateEavAttributeSize(ChangelogInterface $changelog): int
6382 new Expression ('COUNT(*) ' )
6483 )
6584 ->joinInner (
66- ['type ' => $ connection ->getTableName ('eav_entity_type ' )],
85+ ['type ' => $ connection ->getTableName ('eav_entity_type ' )],
6786 'type.entity_type_id=eav_attribute.entity_type_id '
6887 )
6988 ->where ('type.entity_type_code = ? ' , $ this ->entityTypeCodes [$ changelog ->getViewId ()]);
7089
71- return (int ) $ connection ->fetchOne ($ select );
90+ return (int )$ connection ->fetchOne ($ select );
7291 }
7392
7493 /**
@@ -87,34 +106,4 @@ private function setGroupConcatMax(int $numberOfAttributes): void
87106 $ numberOfAttributes * (self ::DEFAULT_ID_SIZE + 1 )
88107 ));
89108 }
90-
91- /**
92- * @inheritdoc
93- * @throws \Exception
94- */
95- public function walk (ChangelogInterface $ changelog , int $ fromVersionId , int $ toVersion , int $ batchSize )
96- {
97- $ connection = $ this ->resourceConnection ->getConnection ();
98- $ numberOfAttributes = $ this ->calculateEavAttributeSize ($ changelog );
99- $ this ->setGroupConcatMax ($ numberOfAttributes );
100- $ select = $ connection ->select ()->distinct (true )
101- ->where (
102- 'version_id > ? ' ,
103- (int ) $ fromVersionId
104- )
105- ->where (
106- 'version_id <= ? ' ,
107- $ toVersion
108- )
109- ->group ([$ changelog ->getColumnName (), 'store_id ' ])
110- ->limit ($ batchSize );
111-
112- $ columns = [
113- $ changelog ->getColumnName (),
114- 'attribute_ids ' => new Expression ('GROUP_CONCAT(attribute_id) ' ),
115- 'store_id '
116- ];
117- $ select ->from ($ changelog ->getName (), $ columns );
118- return $ connection ->fetchAll ($ select );
119- }
120109}
0 commit comments