10
10
11
11
use Magento \Framework \App \ResourceConnection ;
12
12
use Magento \Framework \DB \Ddl \Trigger ;
13
+ use Magento \Framework \DB \Ddl \TriggerFactory ;
14
+ use Magento \Framework \Mview \ViewInterface ;
13
15
14
16
class Subscription implements SubscriptionInterface
15
17
{
@@ -21,12 +23,12 @@ class Subscription implements SubscriptionInterface
21
23
protected $ connection ;
22
24
23
25
/**
24
- * @var \Magento\Framework\DB\Ddl\ TriggerFactory
26
+ * @var TriggerFactory
25
27
*/
26
28
protected $ triggerFactory ;
27
29
28
30
/**
29
- * @var \Magento\Framework\Mview\View\ CollectionInterface
31
+ * @var CollectionInterface
30
32
*/
31
33
protected $ viewCollection ;
32
34
@@ -57,21 +59,31 @@ class Subscription implements SubscriptionInterface
57
59
*/
58
60
protected $ resource ;
59
61
62
+ /**
63
+ * List of columns that can be updated in a subscribed table
64
+ * without creating a new change log entry
65
+ *
66
+ * @var array
67
+ */
68
+ private $ ignoredUpdateColumns = [];
69
+
60
70
/**
61
71
* @param ResourceConnection $resource
62
- * @param \Magento\Framework\DB\Ddl\ TriggerFactory $triggerFactory
63
- * @param \Magento\Framework\Mview\View\ CollectionInterface $viewCollection
64
- * @param \Magento\Framework\Mview\ ViewInterface $view
72
+ * @param TriggerFactory $triggerFactory
73
+ * @param CollectionInterface $viewCollection
74
+ * @param ViewInterface $view
65
75
* @param string $tableName
66
76
* @param string $columnName
77
+ * @param array $ignoredUpdateColumns
67
78
*/
68
79
public function __construct (
69
80
ResourceConnection $ resource ,
70
- \ Magento \ Framework \ DB \ Ddl \ TriggerFactory $ triggerFactory ,
71
- \ Magento \ Framework \ Mview \ View \ CollectionInterface $ viewCollection ,
72
- \ Magento \ Framework \ Mview \ ViewInterface $ view ,
81
+ TriggerFactory $ triggerFactory ,
82
+ CollectionInterface $ viewCollection ,
83
+ ViewInterface $ view ,
73
84
$ tableName ,
74
- $ columnName
85
+ $ columnName ,
86
+ array $ ignoredUpdateColumns = []
75
87
) {
76
88
$ this ->connection = $ resource ->getConnection ();
77
89
$ this ->triggerFactory = $ triggerFactory ;
@@ -80,12 +92,13 @@ public function __construct(
80
92
$ this ->tableName = $ tableName ;
81
93
$ this ->columnName = $ columnName ;
82
94
$ this ->resource = $ resource ;
95
+ $ this ->ignoredUpdateColumns = $ ignoredUpdateColumns ;
83
96
}
84
97
85
98
/**
86
- * Create subsciption
99
+ * Create subscription
87
100
*
88
- * @return \Magento\Framework\Mview\View\ SubscriptionInterface
101
+ * @return SubscriptionInterface
89
102
*/
90
103
public function create ()
91
104
{
@@ -102,7 +115,7 @@ public function create()
102
115
103
116
// Add statements for linked views
104
117
foreach ($ this ->getLinkedViews () as $ view ) {
105
- /** @var \Magento\Framework\Mview\ ViewInterface $view */
118
+ /** @var ViewInterface $view */
106
119
$ trigger ->addStatement ($ this ->buildStatement ($ event , $ view ->getChangelog ()));
107
120
}
108
121
@@ -116,7 +129,7 @@ public function create()
116
129
/**
117
130
* Remove subscription
118
131
*
119
- * @return \Magento\Framework\Mview\View\ SubscriptionInterface
132
+ * @return SubscriptionInterface
120
133
*/
121
134
public function remove ()
122
135
{
@@ -131,7 +144,7 @@ public function remove()
131
144
132
145
// Add statements for linked views
133
146
foreach ($ this ->getLinkedViews () as $ view ) {
134
- /** @var \Magento\Framework\Mview\ ViewInterface $view */
147
+ /** @var ViewInterface $view */
135
148
$ trigger ->addStatement ($ this ->buildStatement ($ event , $ view ->getChangelog ()));
136
149
}
137
150
@@ -154,10 +167,10 @@ public function remove()
154
167
protected function getLinkedViews ()
155
168
{
156
169
if (!$ this ->linkedViews ) {
157
- $ viewList = $ this ->viewCollection ->getViewsByStateMode (\ Magento \ Framework \ Mview \ View \ StateInterface::MODE_ENABLED );
170
+ $ viewList = $ this ->viewCollection ->getViewsByStateMode (StateInterface::MODE_ENABLED );
158
171
159
172
foreach ($ viewList as $ view ) {
160
- /** @var \Magento\Framework\Mview\ ViewInterface $view */
173
+ /** @var ViewInterface $view */
161
174
// Skip the current view
162
175
if ($ view ->getId () == $ this ->getView ()->getId ()) {
163
176
continue ;
@@ -175,35 +188,58 @@ protected function getLinkedViews()
175
188
}
176
189
177
190
/**
178
- * Build trigger statement for INSER , UPDATE, DELETE events
191
+ * Build trigger statement for INSERT , UPDATE, DELETE events
179
192
*
180
193
* @param string $event
181
- * @param \Magento\Framework\Mview\View\ ChangelogInterface $changelog
194
+ * @param ChangelogInterface $changelog
182
195
* @return string
183
196
*/
184
197
protected function buildStatement ($ event , $ changelog )
185
198
{
186
199
switch ($ event ) {
187
200
case Trigger::EVENT_INSERT :
201
+ $ trigger = "INSERT IGNORE INTO %s (%s) VALUES (NEW.%s); " ;
202
+ break ;
203
+
188
204
case Trigger::EVENT_UPDATE :
189
- return sprintf (
190
- "INSERT IGNORE INTO %s (%s) VALUES (NEW.%s); " ,
191
- $ this ->connection ->quoteIdentifier ($ this ->resource ->getTableName ($ changelog ->getName ())),
192
- $ this ->connection ->quoteIdentifier ($ changelog ->getColumnName ()),
193
- $ this ->connection ->quoteIdentifier ($ this ->getColumnName ())
194
- );
205
+ $ trigger = "INSERT IGNORE INTO %s (%s) VALUES (NEW.%s); " ;
206
+
207
+ if ($ this ->connection ->isTableExists ($ this ->getTableName ())
208
+ && $ describe = $ this ->connection ->describeTable ($ this ->getTableName ())
209
+ ) {
210
+ $ columnNames = array_column ($ describe , 'COLUMN_NAME ' );
211
+ $ columnNames = array_diff ($ columnNames , $ this ->ignoredUpdateColumns );
212
+ if ($ columnNames ) {
213
+ $ columns = [];
214
+ foreach ($ columnNames as $ columnName ) {
215
+ $ columns [] = sprintf (
216
+ 'NEW.%1$s != OLD.%1$s ' ,
217
+ $ this ->connection ->quoteIdentifier ($ columnName )
218
+ );
219
+ }
220
+ $ trigger = sprintf (
221
+ "IF (%s) THEN %s END IF; " ,
222
+ implode (' OR ' , $ columns ),
223
+ $ trigger
224
+ );
225
+ }
226
+ }
227
+ break ;
195
228
196
229
case Trigger::EVENT_DELETE :
197
- return sprintf (
198
- "INSERT IGNORE INTO %s (%s) VALUES (OLD.%s); " ,
199
- $ this ->connection ->quoteIdentifier ($ this ->resource ->getTableName ($ changelog ->getName ())),
200
- $ this ->connection ->quoteIdentifier ($ changelog ->getColumnName ()),
201
- $ this ->connection ->quoteIdentifier ($ this ->getColumnName ())
202
- );
230
+ $ trigger = "INSERT IGNORE INTO %s (%s) VALUES (OLD.%s); " ;
231
+ break ;
203
232
204
233
default :
205
234
return '' ;
206
235
}
236
+
237
+ return sprintf (
238
+ $ trigger ,
239
+ $ this ->connection ->quoteIdentifier ($ this ->resource ->getTableName ($ changelog ->getName ())),
240
+ $ this ->connection ->quoteIdentifier ($ changelog ->getColumnName ()),
241
+ $ this ->connection ->quoteIdentifier ($ this ->getColumnName ())
242
+ );
207
243
}
208
244
209
245
/**
@@ -225,7 +261,7 @@ private function getAfterEventTriggerName($event)
225
261
/**
226
262
* Retrieve View related to subscription
227
263
*
228
- * @return \Magento\Framework\Mview\ ViewInterface
264
+ * @return ViewInterface
229
265
* @codeCoverageIgnore
230
266
*/
231
267
public function getView ()
0 commit comments