|
| 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 | +} |
0 commit comments