Skip to content

Commit 79847fe

Browse files
committed
Merge remote-tracking branch 'magento-l3/ACP2E-63' into L3_PR_21-10-19
2 parents cb53994 + 44c5b46 commit 79847fe

File tree

2 files changed

+129
-16
lines changed

2 files changed

+129
-16
lines changed
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Sales\Model\ResourceModel\Provider\Query;
9+
10+
use Magento\Framework\App\ResourceConnection;
11+
use Magento\Framework\DB\Adapter\AdapterInterface;
12+
use Magento\Framework\DB\Select;
13+
14+
/**
15+
* Query builder for retrieving list of updated order ids that was not synced to grid table.
16+
*/
17+
class IdListBuilder
18+
{
19+
/**
20+
* @var array
21+
*/
22+
private $additionalGridTables = [];
23+
24+
/**
25+
* @var ResourceConnection
26+
*/
27+
private $resourceConnection;
28+
29+
/**
30+
* @var AdapterInterface
31+
*/
32+
private $connection;
33+
34+
/**
35+
* IdListBuilder. Builds query for getting updated id list.
36+
*
37+
* @param ResourceConnection $resourceConnection
38+
*/
39+
public function __construct(ResourceConnection $resourceConnection)
40+
{
41+
$this->resourceConnection = $resourceConnection;
42+
}
43+
44+
/**
45+
* Adding additional grid table where entities may already exist.
46+
*
47+
* @param string $table
48+
* @return $this
49+
*/
50+
public function addAdditionalGridTable(string $table): IdListBuilder
51+
{
52+
$this->additionalGridTables[] = $table;
53+
54+
return $this;
55+
}
56+
57+
/**
58+
* Returns connection.
59+
*
60+
* @return AdapterInterface
61+
*/
62+
private function getConnection(): AdapterInterface
63+
{
64+
if (!$this->connection) {
65+
$this->connection = $this->resourceConnection->getConnection('sales');
66+
}
67+
68+
return $this->connection;
69+
}
70+
71+
/**
72+
* Returns update time of the last row in the grid.
73+
*
74+
* @param string $gridTableName
75+
* @return string
76+
*/
77+
private function getLastUpdatedAtValue(string $gridTableName): string
78+
{
79+
$select = $this->getConnection()->select()
80+
->from($this->getConnection()->getTableName($gridTableName), ['updated_at'])
81+
->order('updated_at DESC')
82+
->limit(1);
83+
$row = $this->getConnection()->fetchRow($select);
84+
85+
return $row['updated_at'] ?? '0000-00-00 00:00:00';
86+
}
87+
88+
/**
89+
* Builds select object.
90+
*
91+
* @param string $mainTableName
92+
* @param string $gridTableName
93+
* @return Select
94+
*/
95+
public function build(string $mainTableName, string $gridTableName): Select
96+
{
97+
$select = $this->getConnection()->select()
98+
->from($mainTableName, [$mainTableName . '.entity_id']);
99+
$lastUpdateTime = $this->getLastUpdatedAtValue($gridTableName);
100+
$select->where($mainTableName . '.updated_at >= ?', $lastUpdateTime);
101+
foreach ($this->additionalGridTables as $table) {
102+
$select->joinLeft(
103+
[$table => $table],
104+
sprintf(
105+
'%s.%s = %s.%s',
106+
$mainTableName,
107+
'entity_id',
108+
$table,
109+
'entity_id'
110+
),
111+
[]
112+
)
113+
->where($table . '.entity_id IS NULL');
114+
}
115+
return $select;
116+
}
117+
}

app/code/Magento/Sales/Model/ResourceModel/Provider/UpdatedIdListProvider.php

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
*/
66
namespace Magento\Sales\Model\ResourceModel\Provider;
77

8+
use Magento\Framework\App\ObjectManager;
89
use Magento\Framework\App\ResourceConnection;
910
use Magento\Framework\DB\Adapter\AdapterInterface;
11+
use Magento\Sales\Model\ResourceModel\Provider\Query\IdListBuilder;
1012

1113
/**
1214
* Provides latest updated entities ids list
@@ -23,14 +25,22 @@ class UpdatedIdListProvider implements NotSyncedDataProviderInterface
2325
*/
2426
private $connection;
2527

28+
/**
29+
* @var IdListBuilder
30+
*/
31+
private $idListQueryBuilder;
32+
2633
/**
2734
* NotSyncedDataProvider constructor.
2835
* @param ResourceConnection $resourceConnection
36+
* @param IdListBuilder|null $idListQueryBuilder
2937
*/
3038
public function __construct(
31-
ResourceConnection $resourceConnection
39+
ResourceConnection $resourceConnection,
40+
?IdListBuilder $idListQueryBuilder = null
3241
) {
3342
$this->resourceConnection = $resourceConnection;
43+
$this->idListQueryBuilder = $idListQueryBuilder ?? ObjectManager::getInstance()->get(IdListBuilder::class);
3444
}
3545

3646
/**
@@ -40,21 +50,7 @@ public function getIds($mainTableName, $gridTableName)
4050
{
4151
$mainTableName = $this->resourceConnection->getTableName($mainTableName);
4252
$gridTableName = $this->resourceConnection->getTableName($gridTableName);
43-
$select = $this->getConnection()->select()
44-
->from($mainTableName, [$mainTableName . '.entity_id'])
45-
->joinLeft(
46-
[$gridTableName => $gridTableName],
47-
sprintf(
48-
'%s.%s = %s.%s',
49-
$mainTableName,
50-
'entity_id',
51-
$gridTableName,
52-
'entity_id'
53-
),
54-
[]
55-
)
56-
->where($gridTableName . '.entity_id IS NULL');
57-
53+
$select = $this->idListQueryBuilder->build($mainTableName, $gridTableName);
5854
return $this->getConnection()->fetchAll($select, [], \Zend_Db::FETCH_COLUMN);
5955
}
6056

0 commit comments

Comments
 (0)