1
- <?php
1
+ <?php declare (strict_types= 1 );
2
2
/**
3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
6
7
- namespace Magento \Eav \Model \Mview ;
7
+ namespace Magento \Eav \Model \Mview \ ChangeLogBatchWalker ;
8
8
9
9
use Magento \Framework \App \ResourceConnection ;
10
+ use Magento \Framework \DB \Select ;
10
11
use Magento \Framework \DB \Sql \Expression ;
11
- use Magento \Framework \Mview \View \ChangeLogBatchWalkerInterface ;
12
+ use Magento \Framework \Mview \View \ChangeLogBatchWalker \ IdsSelectBuilderInterface ;
12
13
use Magento \Framework \Mview \View \ChangelogInterface ;
13
14
14
- /**
15
- * Class BatchIterator
16
- */
17
- class ChangeLogBatchWalker implements ChangeLogBatchWalkerInterface
15
+ class IdsSelectBuilder implements IdsSelectBuilderInterface
18
16
{
19
17
private const GROUP_CONCAT_MAX_VARIABLE = 'group_concat_max_len ' ;
20
18
/** ID is defined as small int. Default size of it is 5 */
21
19
private const DEFAULT_ID_SIZE = 5 ;
22
-
23
20
/**
24
- * @var ResourceConnection
21
+ * @var \Magento\Framework\App\ ResourceConnection
25
22
*/
26
- private $ resourceConnection ;
27
-
23
+ private ResourceConnection $ resourceConnection ;
28
24
/**
29
25
* @var array
30
26
*/
31
- private $ entityTypeCodes ;
27
+ private array $ entityTypeCodes ;
32
28
33
29
/**
34
- * @param ResourceConnection $resourceConnection
30
+ * @param \Magento\Framework\App\ ResourceConnection $resourceConnection
35
31
* @param array $entityTypeCodes
36
32
*/
37
33
public function __construct (
38
34
ResourceConnection $ resourceConnection ,
39
- array $ entityTypeCodes = []
35
+ array $ entityTypeCodes = []
40
36
) {
41
37
$ this ->resourceConnection = $ resourceConnection ;
42
38
$ this ->entityTypeCodes = $ entityTypeCodes ;
43
39
}
44
40
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
+
45
64
/**
46
65
* Calculate EAV attributes size
47
66
*
@@ -54,7 +73,7 @@ private function calculateEavAttributeSize(ChangelogInterface $changelog): int
54
73
$ connection = $ this ->resourceConnection ->getConnection ();
55
74
56
75
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 ' );
58
77
}
59
78
60
79
$ select = $ connection ->select ();
@@ -63,12 +82,12 @@ private function calculateEavAttributeSize(ChangelogInterface $changelog): int
63
82
new Expression ('COUNT(*) ' )
64
83
)
65
84
->joinInner (
66
- ['type ' => $ connection ->getTableName ('eav_entity_type ' )],
85
+ ['type ' => $ connection ->getTableName ('eav_entity_type ' )],
67
86
'type.entity_type_id=eav_attribute.entity_type_id '
68
87
)
69
88
->where ('type.entity_type_code = ? ' , $ this ->entityTypeCodes [$ changelog ->getViewId ()]);
70
89
71
- return (int ) $ connection ->fetchOne ($ select );
90
+ return (int )$ connection ->fetchOne ($ select );
72
91
}
73
92
74
93
/**
@@ -87,34 +106,4 @@ private function setGroupConcatMax(int $numberOfAttributes): void
87
106
$ numberOfAttributes * (self ::DEFAULT_ID_SIZE + 1 )
88
107
));
89
108
}
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
- }
120
109
}
0 commit comments