Skip to content

Commit

Permalink
Merge pull request #499 from magento-troll/MAGETWO-39333
Browse files Browse the repository at this point in the history
[Troll & Dragons] Unify connection interface
  • Loading branch information
Ganin, Roman(rganin) committed Aug 3, 2015
2 parents 883975a + e31f83b commit 8313278
Show file tree
Hide file tree
Showing 581 changed files with 4,861 additions and 5,194 deletions.
20 changes: 10 additions & 10 deletions app/code/Magento/AdminNotification/Model/Resource/Inbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ protected function _construct()
*/
public function loadLatestNotice(\Magento\AdminNotification\Model\Inbox $object)
{
$adapter = $this->_getReadAdapter();
$select = $adapter->select()->from(
$connection = $this->getConnection();
$select = $connection->select()->from(
$this->getMainTable()
)->order(
$this->getIdFieldName() . ' DESC'
Expand All @@ -42,7 +42,7 @@ public function loadLatestNotice(\Magento\AdminNotification\Model\Inbox $object)
)->limit(
1
);
$data = $adapter->fetchRow($select);
$data = $connection->fetchRow($select);

if ($data) {
$object->setData($data);
Expand All @@ -62,8 +62,8 @@ public function loadLatestNotice(\Magento\AdminNotification\Model\Inbox $object)
*/
public function getNoticeStatus(\Magento\AdminNotification\Model\Inbox $object)
{
$adapter = $this->_getReadAdapter();
$select = $adapter->select()->from(
$connection = $this->getConnection();
$select = $connection->select()->from(
$this->getMainTable(),
[
'severity' => 'severity',
Expand All @@ -78,7 +78,7 @@ public function getNoticeStatus(\Magento\AdminNotification\Model\Inbox $object)
'is_read=?',
0
);
$return = $adapter->fetchPairs($select);
$return = $connection->fetchPairs($select);
return $return;
}

Expand All @@ -92,9 +92,9 @@ public function getNoticeStatus(\Magento\AdminNotification\Model\Inbox $object)
*/
public function parse(\Magento\AdminNotification\Model\Inbox $object, array $data)
{
$adapter = $this->_getWriteAdapter();
$connection = $this->getConnection();
foreach ($data as $item) {
$select = $adapter->select()->from($this->getMainTable())->where('title = ?', $item['title']);
$select = $connection->select()->from($this->getMainTable())->where('title = ?', $item['title']);

if (empty($item['url'])) {
$select->where('url IS NULL');
Expand All @@ -106,11 +106,11 @@ public function parse(\Magento\AdminNotification\Model\Inbox $object, array $dat
$row = false;
unset($item['internal']);
} else {
$row = $adapter->fetchRow($select);
$row = $connection->fetchRow($select);
}

if (!$row) {
$adapter->insert($this->getMainTable(), $item);
$connection->insert($this->getMainTable(), $item);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function __construct(
\Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
\Magento\Framework\Event\ManagerInterface $eventManager,
\Magento\Framework\Notification\MessageList $messageList,
$connection = null,
\Magento\Framework\DB\Adapter\AdapterInterface $connection = null,
\Magento\Framework\Model\Resource\Db\AbstractDb $resource = null
) {
$this->_messageList = $messageList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

use Magento\CatalogImportExport\Model\Import\Product as ImportProduct;
use Magento\CatalogImportExport\Model\Import\Product\RowValidatorInterface as ValidatorInterface;
use Magento\Framework\App\Resource;

/**
* Class AdvancedPricing
Expand Down Expand Up @@ -176,7 +177,7 @@ public function __construct(
$this->_importExportData = $importExportData;
$this->_resourceHelper = $resourceHelper;
$this->_dataSourceModel = $importData;
$this->_connection = $resource->getConnection('write');
$this->_connection = $resource->getConnection();
$this->_resourceFactory = $resourceFactory;
$this->_productModel = $productModel;
$this->_catalogData = $catalogData;
Expand Down
7 changes: 4 additions & 3 deletions app/code/Magento/Authorization/Model/Acl/Loader/Role.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

use Magento\Authorization\Model\Acl\Role\Group as RoleGroup;
use Magento\Authorization\Model\Acl\Role\User as RoleUser;
use Magento\Framework\App\Resource;

class Role implements \Magento\Framework\Acl\LoaderInterface
{
Expand Down Expand Up @@ -49,11 +50,11 @@ public function __construct(
public function populateAcl(\Magento\Framework\Acl $acl)
{
$roleTableName = $this->_resource->getTableName('authorization_role');
$adapter = $this->_resource->getConnection('core_read');
$connection = $this->_resource->getConnection();

$select = $adapter->select()->from($roleTableName)->order('tree_level');
$select = $connection->select()->from($roleTableName)->order('tree_level');

foreach ($adapter->fetchAll($select) as $role) {
foreach ($connection->fetchAll($select) as $role) {
$parent = $role['parent_id'] > 0 ? $role['parent_id'] : null;
switch ($role['role_type']) {
case RoleGroup::ROLE_TYPE:
Expand Down
8 changes: 5 additions & 3 deletions app/code/Magento/Authorization/Model/Acl/Loader/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
*/
namespace Magento\Authorization\Model\Acl\Loader;

use Magento\Framework\App\Resource;

class Rule implements \Magento\Framework\Acl\LoaderInterface
{
/**
Expand Down Expand Up @@ -37,11 +39,11 @@ public function populateAcl(\Magento\Framework\Acl $acl)
{
$ruleTable = $this->_resource->getTableName("authorization_rule");

$adapter = $this->_resource->getConnection('core_read');
$connection = $this->_resource->getConnection();

$select = $adapter->select()->from(['r' => $ruleTable]);
$select = $connection->select()->from(['r' => $ruleTable]);

$rulesArr = $adapter->fetchAll($select);
$rulesArr = $connection->fetchAll($select);

foreach ($rulesArr as $rule) {
$role = $rule['role_id'];
Expand Down
22 changes: 11 additions & 11 deletions app/code/Magento/Authorization/Model/Resource/Role.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,16 @@ class Role extends \Magento\Framework\Model\Resource\Db\AbstractDb
* @param \Magento\Framework\Model\Resource\Db\Context $context
* @param \Magento\Framework\App\CacheInterface $cache
* @param \Magento\Framework\Stdlib\DateTime $dateTime
* @param string|null $resourcePrefix
* @param string $connectionName
*/
public function __construct(
\Magento\Framework\Model\Resource\Db\Context $context,
\Magento\Framework\App\CacheInterface $cache,
\Magento\Framework\Stdlib\DateTime $dateTime,
$resourcePrefix = null
$connectionName = null
) {
$this->dateTime = $dateTime;
parent::__construct($context, $resourcePrefix);
parent::__construct($context, $connectionName);
$this->_cache = $cache->getFrontend();
}

Expand Down Expand Up @@ -82,7 +82,7 @@ protected function _beforeSave(\Magento\Framework\Model\AbstractModel $role)

if (!$role->getTreeLevel()) {
if ($role->getPid() > 0) {
$select = $this->_getReadAdapter()->select()->from(
$select = $this->getConnection()->select()->from(
$this->getMainTable(),
['tree_level']
)->where(
Expand All @@ -91,7 +91,7 @@ protected function _beforeSave(\Magento\Framework\Model\AbstractModel $role)

$binds = ['pid' => (int)$role->getPid()];

$treeLevel = $this->_getReadAdapter()->fetchOne($select, $binds);
$treeLevel = $this->getConnection()->fetchOne($select, $binds);
} else {
$treeLevel = 0;
}
Expand Down Expand Up @@ -126,11 +126,11 @@ protected function _afterSave(\Magento\Framework\Model\AbstractModel $role)
*/
protected function _afterDelete(\Magento\Framework\Model\AbstractModel $role)
{
$adapter = $this->_getWriteAdapter();
$connection = $this->getConnection();

$adapter->delete($this->getMainTable(), ['parent_id = ?' => (int)$role->getId()]);
$connection->delete($this->getMainTable(), ['parent_id = ?' => (int)$role->getId()]);

$adapter->delete($this->_ruleTable, ['role_id = ?' => (int)$role->getId()]);
$connection->delete($this->_ruleTable, ['role_id = ?' => (int)$role->getId()]);

return $this;
}
Expand All @@ -143,16 +143,16 @@ protected function _afterDelete(\Magento\Framework\Model\AbstractModel $role)
*/
public function getRoleUsers(\Magento\Authorization\Model\Role $role)
{
$read = $this->_getReadAdapter();
$connection = $this->getConnection();

$binds = ['role_id' => $role->getId(), 'role_type' => RoleUser::ROLE_TYPE];

$select = $read->select()
$select = $connection->select()
->from($this->getMainTable(), ['user_id'])
->where('parent_id = :role_id')
->where('role_type = :role_type')
->where('user_id > 0');

return $read->fetchCol($select, $binds);
return $connection->fetchCol($select, $binds);
}
}
22 changes: 11 additions & 11 deletions app/code/Magento/Authorization/Model/Resource/Rules.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,18 @@ class Rules extends \Magento\Framework\Model\Resource\Db\AbstractDb
* @param \Psr\Log\LoggerInterface $logger
* @param \Magento\Framework\Acl\RootResource $rootResource
* @param \Magento\Framework\Acl\CacheInterface $aclCache
* @param string|null $resourcePrefix
* @param string $connectionName
*/
public function __construct(
\Magento\Framework\Model\Resource\Db\Context $context,
\Magento\Framework\Acl\Builder $aclBuilder,
\Psr\Log\LoggerInterface $logger,
\Magento\Framework\Acl\RootResource $rootResource,
\Magento\Framework\Acl\CacheInterface $aclCache,
$resourcePrefix = null
$connectionName = null
) {
$this->_aclBuilder = $aclBuilder;
parent::__construct($context, $resourcePrefix);
parent::__construct($context, $connectionName);
$this->_rootResource = $rootResource;
$this->_aclCache = $aclCache;
$this->_logger = $logger;
Expand All @@ -80,13 +80,13 @@ protected function _construct()
public function saveRel(\Magento\Authorization\Model\Rules $rule)
{
try {
$adapter = $this->_getWriteAdapter();
$adapter->beginTransaction();
$connection = $this->getConnection();
$connection->beginTransaction();
$roleId = $rule->getRoleId();

$condition = ['role_id = ?' => (int)$roleId];

$adapter->delete($this->getMainTable(), $condition);
$connection->delete($this->getMainTable(), $condition);

$postedResources = $rule->getResources();
if ($postedResources) {
Expand All @@ -101,7 +101,7 @@ public function saveRel(\Magento\Authorization\Model\Rules $rule)
if ($postedResources === [$this->_rootResource->getId()]) {
$insertData = $this->_prepareDataForTable(new \Magento\Framework\Object($row), $this->getMainTable());

$adapter->insert($this->getMainTable(), $insertData);
$connection->insert($this->getMainTable(), $insertData);
} else {
$acl = $this->_aclBuilder->getAcl();
/** @var $resource \Magento\Framework\Acl\Resource */
Expand All @@ -110,18 +110,18 @@ public function saveRel(\Magento\Authorization\Model\Rules $rule)
$row['resource_id'] = $resourceId;

$insertData = $this->_prepareDataForTable(new \Magento\Framework\Object($row), $this->getMainTable());
$adapter->insert($this->getMainTable(), $insertData);
$connection->insert($this->getMainTable(), $insertData);
}
}
}

$adapter->commit();
$connection->commit();
$this->_aclCache->clean();
} catch (\Magento\Framework\Exception\LocalizedException $e) {
$adapter->rollBack();
$connection->rollBack();
throw $e;
} catch (\Exception $e) {
$adapter->rollBack();
$connection->rollBack();
$this->_logger->critical($e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function addSortByLength()
{
$length = $this->getConnection()->getLengthSql('{{resource_id}}');
$this->addExpressionFieldToSelect('length', $length, 'resource_id');
$this->getSelect()->order('length ' . \Zend_Db_Select::SQL_DESC);
$this->getSelect()->order('length ' . \Magento\Framework\DB\Select::SQL_DESC);

return $this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ public function testPopulateAcl()
$selectMock = $this->getMock('Magento\Framework\DB\Select', [], [], '', false);
$selectMock->expects($this->any())->method('from')->will($this->returnValue($selectMock));

$adapterMock = $this->getMock('Magento\Framework\DB\Adapter\Pdo\Mysql', [], [], '', false);
$adapterMock->expects($this->once())->method('select')->will($this->returnValue($selectMock));
$adapterMock->expects(
$connectionMock = $this->getMock('Magento\Framework\DB\Adapter\Pdo\Mysql', [], [], '', false);
$connectionMock->expects($this->once())->method('select')->will($this->returnValue($selectMock));
$connectionMock->expects(
$this->once()
)->method(
'fetchAll'
Expand All @@ -55,7 +55,9 @@ public function testPopulateAcl()
)
);

$this->_resourceMock->expects($this->once())->method('getConnection')->will($this->returnValue($adapterMock));
$this->_resourceMock->expects($this->once())
->method('getConnection')
->will($this->returnValue($connectionMock));

$aclMock = $this->getMock('Magento\Framework\Acl');
$aclMock->expects($this->any())->method('has')->will($this->returnValue(true));
Expand Down
Loading

0 comments on commit 8313278

Please sign in to comment.