1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\Mview\View;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\DB\Ddl\Trigger;
use Magento\Framework\Mview\View\StateInterface;
/**
* Class Subscription
*
* @package Magento\Framework\Mview\View
*/
class Subscription implements SubscriptionInterface
{
/**
* Database connection
*
* @var \Magento\Framework\DB\Adapter\AdapterInterface
*/
protected $connection;
/**
* @var \Magento\Framework\DB\Ddl\TriggerFactory
*/
protected $triggerFactory;
/**
* @var \Magento\Framework\Mview\View\CollectionInterface
*/
protected $viewCollection;
/**
* @var string
*/
protected $view;
/**
* @var string
*/
protected $tableName;
/**
* @var string
*/
protected $columnName;
/**
* List of views linked to the same entity as the current view
*
* @var array
*/
protected $linkedViews = [];
/**
* List of columns that can be updated in a subscribed table
* without creating a new change log entry
*
* @var array
*/
private $ignoredUpdateColumns = [];
/**
* @var Resource
*/
protected $resource;
/**
* @param ResourceConnection $resource
* @param \Magento\Framework\DB\Ddl\TriggerFactory $triggerFactory
* @param \Magento\Framework\Mview\View\CollectionInterface $viewCollection
* @param \Magento\Framework\Mview\ViewInterface $view
* @param string $tableName
* @param string $columnName
* @param array $ignoredUpdateColumns
*/
public function __construct(
ResourceConnection $resource,
\Magento\Framework\DB\Ddl\TriggerFactory $triggerFactory,
\Magento\Framework\Mview\View\CollectionInterface $viewCollection,
\Magento\Framework\Mview\ViewInterface $view,
$tableName,
$columnName,
$ignoredUpdateColumns = []
) {
$this->connection = $resource->getConnection();
$this->triggerFactory = $triggerFactory;
$this->viewCollection = $viewCollection;
$this->view = $view;
$this->tableName = $tableName;
$this->columnName = $columnName;
$this->resource = $resource;
$this->ignoredUpdateColumns = $ignoredUpdateColumns;
}
/**
* Create subsciption
*
* @return \Magento\Framework\Mview\View\SubscriptionInterface
*/
public function create()
{
foreach (Trigger::getListOfEvents() as $event) {
$triggerName = $this->getAfterEventTriggerName($event);
/** @var Trigger $trigger */
$trigger = $this->triggerFactory->create()
->setName($triggerName)
->setTime(Trigger::TIME_AFTER)
->setEvent($event)
->setTable($this->resource->getTableName($this->tableName));
$trigger->addStatement($this->buildStatement($event, $this->getView()->getChangelog()));
// Add statements for linked views
foreach ($this->getLinkedViews() as $view) {
/** @var \Magento\Framework\Mview\ViewInterface $view */
$trigger->addStatement($this->buildStatement($event, $view->getChangelog()));
}
$this->connection->dropTrigger($trigger->getName());
$this->connection->createTrigger($trigger);
}
return $this;
}
/**
* Remove subscription
*
* @return \Magento\Framework\Mview\View\SubscriptionInterface
*/
public function remove()
{
foreach (Trigger::getListOfEvents() as $event) {
$triggerName = $this->getAfterEventTriggerName($event);
/** @var Trigger $trigger */
$trigger = $this->triggerFactory->create()
->setName($triggerName)
->setTime(Trigger::TIME_AFTER)
->setEvent($event)
->setTable($this->resource->getTableName($this->getTableName()));
// Add statements for linked views
foreach ($this->getLinkedViews() as $view) {
/** @var \Magento\Framework\Mview\ViewInterface $view */
$trigger->addStatement($this->buildStatement($event, $view->getChangelog()));
}
$this->connection->dropTrigger($trigger->getName());
// Re-create trigger if trigger used by linked views
if ($trigger->getStatements()) {
$this->connection->createTrigger($trigger);
}
}
return $this;
}
/**
* Retrieve list of linked views
*
* @return array
*/
protected function getLinkedViews()
{
if (!$this->linkedViews) {
$viewList = $this->viewCollection->getViewsByStateMode(StateInterface::MODE_ENABLED);
foreach ($viewList as $view) {
/** @var \Magento\Framework\Mview\ViewInterface $view */
// Skip the current view
if ($view->getId() == $this->getView()->getId()) {
continue;
}
// Search in view subscriptions
foreach ($view->getSubscriptions() as $subscription) {
if ($subscription['name'] != $this->getTableName()) {
continue;
}
$this->linkedViews[] = $view;
}
}
}
return $this->linkedViews;
}
/**
* Build trigger statement for INSERT, UPDATE, DELETE events
*
* @param string $event
* @param \Magento\Framework\Mview\View\ChangelogInterface $changelog
* @return string
*/
protected function buildStatement($event, $changelog)
{
switch ($event) {
case Trigger::EVENT_INSERT:
$trigger = "INSERT IGNORE INTO %s (%s) VALUES (NEW.%s);";
break;
case Trigger::EVENT_UPDATE:
$tableName = $this->resource->getTableName($this->getTableName());
$trigger = "INSERT IGNORE INTO %s (%s) VALUES (NEW.%s);";
if ($this->connection->isTableExists($tableName) &&
$describe = $this->connection->describeTable($tableName)
) {
$columnNames = array_column($describe, 'COLUMN_NAME');
$columnNames = array_diff($columnNames, $this->ignoredUpdateColumns);
if ($columnNames) {
$columns = [];
foreach ($columnNames as $columnName) {
$columns[] = sprintf(
'NEW.%1$s <=> OLD.%1$s',
$this->connection->quoteIdentifier($columnName)
);
}
$trigger = sprintf(
"IF (%s) THEN %s END IF;",
implode(' OR ', $columns),
$trigger
);
}
}
break;
case Trigger::EVENT_DELETE:
$trigger = "INSERT IGNORE INTO %s (%s) VALUES (OLD.%s);";
break;
default:
return '';
}
return sprintf(
$trigger,
$this->connection->quoteIdentifier($this->resource->getTableName($changelog->getName())),
$this->connection->quoteIdentifier($changelog->getColumnName()),
$this->connection->quoteIdentifier($this->getColumnName())
);
}
/**
* Build an "after" event for the given table and event
*
* @param string $event The DB level event, like "update" or "insert"
*
* @return string
*/
private function getAfterEventTriggerName($event)
{
return $this->resource->getTriggerName(
$this->resource->getTableName($this->getTableName()),
Trigger::TIME_AFTER,
$event
);
}
/**
* Retrieve View related to subscription
*
* @return \Magento\Framework\Mview\ViewInterface
* @codeCoverageIgnore
*/
public function getView()
{
return $this->view;
}
/**
* Retrieve table name
*
* @return string
* @codeCoverageIgnore
*/
public function getTableName()
{
return $this->tableName;
}
/**
* Retrieve table column name
*
* @return string
* @codeCoverageIgnore
*/
public function getColumnName()
{
return $this->columnName;
}
}