Skip to content

Commit

Permalink
Merge pull request #1356 from magento-engcom/develop-prs
Browse files Browse the repository at this point in the history
Public Pull Requests

#10306
#4893
  • Loading branch information
Oleksii Korshenko authored Jul 24, 2017
2 parents 8d0a729 + 1b2fbfc commit c955c32
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 21 deletions.
7 changes: 7 additions & 0 deletions app/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1330,4 +1330,11 @@
<argument name="configSource" xsi:type="object">Magento\Config\App\Config\Source\EnvironmentConfigSource</argument>
</arguments>
</type>
<type name="Magento\Framework\Mview\View\Subscription">
<arguments>
<argument name="ignoredUpdateColumns" xsi:type="array">
<item name="updated_at" xsi:type="string">updated_at</item>
</argument>
</arguments>
</type>
</config>
10 changes: 6 additions & 4 deletions lib/internal/Magento/Framework/DataObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -325,19 +325,21 @@ public function convertToXml(
* Convert object data to JSON
*
* @param array $keys array of required keys
* @return string
* @return bool|string
* @throws \InvalidArgumentException
*/
public function toJson(array $keys = [])
{
$data = $this->toArray($keys);
return \Zend_Json::encode($data);
return \Magento\Framework\Serialize\JsonConverter::convert($data);
}

/**
* The "__" style wrapper for toJson
*
* @param array $keys
* @return string
* @param array $keys
* @return bool|string
* @throws \InvalidArgumentException
*/
public function convertToJson(array $keys = [])
{
Expand Down
67 changes: 50 additions & 17 deletions lib/internal/Magento/Framework/Mview/View/Subscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use Magento\Framework\App\ResourceConnection;
use Magento\Framework\DB\Ddl\Trigger;
use Magento\Framework\Mview\View\StateInterface;

class Subscription implements SubscriptionInterface
{
Expand Down Expand Up @@ -52,6 +53,14 @@ class Subscription implements SubscriptionInterface
*/
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
*/
Expand All @@ -64,14 +73,16 @@ class Subscription implements SubscriptionInterface
* @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
$columnName,
$ignoredUpdateColumns = []
) {
$this->connection = $resource->getConnection();
$this->triggerFactory = $triggerFactory;
Expand All @@ -80,6 +91,7 @@ public function __construct(
$this->tableName = $tableName;
$this->columnName = $columnName;
$this->resource = $resource;
$this->ignoredUpdateColumns = $ignoredUpdateColumns;
}

/**
Expand Down Expand Up @@ -154,7 +166,7 @@ public function remove()
protected function getLinkedViews()
{
if (!$this->linkedViews) {
$viewList = $this->viewCollection->getViewsByStateMode(\Magento\Framework\Mview\View\StateInterface::MODE_ENABLED);
$viewList = $this->viewCollection->getViewsByStateMode(StateInterface::MODE_ENABLED);

foreach ($viewList as $view) {
/** @var \Magento\Framework\Mview\ViewInterface $view */
Expand All @@ -175,35 +187,56 @@ protected function getLinkedViews()
}

/**
* Build trigger statement for INSER, UPDATE, DELETE events
* 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)
{
$columns = [];
if ($this->connection->isTableExists($this->getTableName())
&& $describe = $this->connection->describeTable($this->getTableName())
) {
foreach ($describe as $column) {
if (in_array($column['COLUMN_NAME'], $this->ignoredUpdateColumns)) {
continue;
}
$columns[] = sprintf(
'NEW.%1$s != OLD.%1$s',
$this->connection->quoteIdentifier($column['COLUMN_NAME'])
);
}
}

switch ($event) {
case Trigger::EVENT_INSERT:
$trigger = "INSERT IGNORE INTO %s (%s) VALUES (NEW.%s);";
break;
case Trigger::EVENT_UPDATE:
return sprintf(
"INSERT IGNORE INTO %s (%s) VALUES (NEW.%s);",
$this->connection->quoteIdentifier($this->resource->getTableName($changelog->getName())),
$this->connection->quoteIdentifier($changelog->getColumnName()),
$this->connection->quoteIdentifier($this->getColumnName())
);

$trigger = "INSERT IGNORE INTO %s (%s) VALUES (NEW.%s);";
if ($columns) {
$trigger = sprintf(
"IF (%s) THEN %s END IF;",
implode(' OR ', $columns),
$trigger
);
}
break;
case Trigger::EVENT_DELETE:
return sprintf(
"INSERT IGNORE INTO %s (%s) VALUES (OLD.%s);",
$this->connection->quoteIdentifier($this->resource->getTableName($changelog->getName())),
$this->connection->quoteIdentifier($changelog->getColumnName()),
$this->connection->quoteIdentifier($this->getColumnName())
);

$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())
);
}

/**
Expand Down
28 changes: 28 additions & 0 deletions lib/internal/Magento/Framework/Serialize/JsonConverter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\Serialize;

/**
* This class was introducted only for usage in the \Magento\Framework\DataObject::toJson method.
* It should not be used in other cases and instead \Magento\Framework\Serialize\Serializer\Json::serialize
* should be used.
*/
class JsonConverter
{
/**
* This method should only be used by \Magento\Framework\DataObject::toJson
* All other cases should use \Magento\Framework\Serialize\Serializer\Json::serialize directly
*
* @param string|int|float|bool|array|null $data
* @return bool|string
* @throws \InvalidArgumentException
*/
public static function convert($data)
{
$serializer = new \Magento\Framework\Serialize\Serializer\Json();
return $serializer->serialize($data);
}
}

0 comments on commit c955c32

Please sign in to comment.