123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Framework\App;
- use Magento\Framework\App\ResourceConnection\ConfigInterface as ResourceConfigInterface;
- use Magento\Framework\Config\ConfigOptionsListConstants;
- use Magento\Framework\Model\ResourceModel\Type\Db\ConnectionFactoryInterface;
- /**
- * Application provides ability to configure multiple connections to persistent storage.
- *
- * This class provides access to all these connections.
- *
- * @api
- * @since 100.0.2
- */
- class ResourceConnection
- {
- const AUTO_UPDATE_ONCE = 0;
- const AUTO_UPDATE_NEVER = -1;
- const AUTO_UPDATE_ALWAYS = 1;
- const DEFAULT_CONNECTION = 'default';
- /**
- * Instances of actual connections.
- *
- * @var \Magento\Framework\DB\Adapter\AdapterInterface[]
- */
- protected $connections = [];
- /**
- * Mapped tables cache array.
- *
- * @var array
- */
- protected $mappedTableNames;
- /**
- * Resource config.
- *
- * @var ResourceConfigInterface
- */
- protected $config;
- /**
- * Resource connection adapter factory.
- *
- * @var ConnectionFactoryInterface
- */
- protected $connectionFactory;
- /**
- * @var DeploymentConfig
- */
- private $deploymentConfig;
- /**
- * @var string
- */
- protected $tablePrefix;
- /**
- * @param ResourceConfigInterface $resourceConfig
- * @param ConnectionFactoryInterface $connectionFactory
- * @param DeploymentConfig $deploymentConfig
- * @param string $tablePrefix
- */
- public function __construct(
- ResourceConfigInterface $resourceConfig,
- ConnectionFactoryInterface $connectionFactory,
- DeploymentConfig $deploymentConfig,
- $tablePrefix = ''
- ) {
- $this->config = $resourceConfig;
- $this->connectionFactory = $connectionFactory;
- $this->deploymentConfig = $deploymentConfig;
- $this->tablePrefix = $tablePrefix ?: null;
- }
- /**
- * Retrieve connection to resource specified by $resourceName.
- *
- * @param string $resourceName
- * @return \Magento\Framework\DB\Adapter\AdapterInterface
- * @throws \DomainException
- * @codeCoverageIgnore
- */
- public function getConnection($resourceName = self::DEFAULT_CONNECTION)
- {
- $connectionName = $this->config->getConnectionName($resourceName);
- $connection = $this->getConnectionByName($connectionName);
- return $connection;
- }
- /**
- * Close connection.
- *
- * @param string $resourceName
- * @return void
- * @since 100.1.3
- */
- public function closeConnection($resourceName = self::DEFAULT_CONNECTION)
- {
- if ($resourceName === null) {
- foreach ($this->connections as $processConnection) {
- if ($processConnection !== null) {
- $processConnection->closeConnection();
- }
- }
- $this->connections = [];
- } else {
- $processConnectionName = $this->getProcessConnectionName($this->config->getConnectionName($resourceName));
- if (isset($this->connections[$processConnectionName])) {
- if ($this->connections[$processConnectionName] !== null) {
- $this->connections[$processConnectionName]->closeConnection();
- }
- $this->connections[$processConnectionName] = null;
- }
- }
- }
- /**
- * Retrieve connection by $connectionName.
- *
- * @param string $connectionName
- * @return \Magento\Framework\DB\Adapter\AdapterInterface
- * @throws \DomainException
- */
- public function getConnectionByName($connectionName)
- {
- $processConnectionName = $this->getProcessConnectionName($connectionName);
- if (isset($this->connections[$processConnectionName])) {
- return $this->connections[$processConnectionName];
- }
- $connectionConfig = $this->deploymentConfig->get(
- ConfigOptionsListConstants::CONFIG_PATH_DB_CONNECTIONS . '/' . $connectionName
- );
- if ($connectionConfig) {
- $connection = $this->connectionFactory->create($connectionConfig);
- } else {
- throw new \DomainException('Connection "' . $connectionName . '" is not defined');
- }
- $this->connections[$processConnectionName] = $connection;
- return $connection;
- }
- /**
- * Get conneciton name for process.
- *
- * @param string $connectionName
- * @return string
- */
- private function getProcessConnectionName($connectionName)
- {
- return $connectionName . '_process_' . getmypid();
- }
- /**
- * Get resource table name, validated by db adapter.
- *
- * @param string|string[] $modelEntity
- * @param string $connectionName
- * @return string
- * @api
- */
- public function getTableName($modelEntity, $connectionName = self::DEFAULT_CONNECTION)
- {
- $tableSuffix = null;
- if (is_array($modelEntity)) {
- list($modelEntity, $tableSuffix) = $modelEntity;
- }
- $tableName = $modelEntity;
- $mappedTableName = $this->getMappedTableName($tableName);
- if ($mappedTableName) {
- $tableName = $mappedTableName;
- } else {
- $tablePrefix = $this->getTablePrefix();
- if ($tablePrefix && strpos($tableName, $tablePrefix) !== 0) {
- $tableName = $tablePrefix . $tableName;
- }
- }
- if ($tableSuffix) {
- $tableName .= '_' . $tableSuffix;
- }
- return $this->getConnection($connectionName)->getTableName($tableName);
- }
- /**
- * Gets table placeholder by table name.
- *
- * @param string $tableName
- * @return string
- * @since 100.1.0
- */
- public function getTablePlaceholder($tableName)
- {
- $tableName = preg_replace('/^' . preg_quote($this->getTablePrefix()) . '_/', '', $tableName);
- return $tableName;
- }
- /**
- * Build a trigger name.
- *
- * @param string $tableName The table that is the subject of the trigger
- * @param string $time Either "before" or "after"
- * @param string $event The DB level event which activates the trigger, i.e. "update" or "insert"
- * @return string
- */
- public function getTriggerName($tableName, $time, $event)
- {
- return $this->getConnection()->getTriggerName($tableName, $time, $event);
- }
- /**
- * Set mapped table name.
- *
- * @param string $tableName
- * @param string $mappedName
- * @return $this
- * @codeCoverageIgnore
- */
- public function setMappedTableName($tableName, $mappedName)
- {
- $this->mappedTableNames[$tableName] = $mappedName;
- return $this;
- }
- /**
- * Get mapped table name.
- *
- * @param string $tableName
- * @return bool|string
- */
- public function getMappedTableName($tableName)
- {
- if (isset($this->mappedTableNames[$tableName])) {
- return $this->mappedTableNames[$tableName];
- } else {
- return false;
- }
- }
- /**
- * Retrieve 32bit UNIQUE HASH for a Table index.
- *
- * @param string $tableName
- * @param string|string[] $fields
- * @param string $indexType
- * @return string
- */
- public function getIdxName(
- $tableName,
- $fields,
- $indexType = \Magento\Framework\DB\Adapter\AdapterInterface::INDEX_TYPE_INDEX
- ) {
- return $this->getConnection()
- ->getIndexName(
- $this->getTableName($tableName),
- $fields,
- $indexType
- );
- }
- /**
- * Retrieve 32bit UNIQUE HASH for a Table foreign key.
- *
- * @param string $priTableName the target table name
- * @param string $priColumnName the target table column name
- * @param string $refTableName the reference table name
- * @param string $refColumnName the reference table column name
- * @return string
- */
- public function getFkName($priTableName, $priColumnName, $refTableName, $refColumnName)
- {
- return $this->getConnection()->getForeignKeyName(
- $this->getTableName($priTableName),
- $priColumnName,
- $this->getTableName($refTableName),
- $refColumnName
- );
- }
- /**
- * Retrieve db name.
- *
- * That name can be needed, when we do request in information_schema to identify db.
- *
- * @param string $resourceName
- * @return string
- * @since 102.0.0
- */
- public function getSchemaName($resourceName)
- {
- return $this->deploymentConfig->get(
- ConfigOptionsListConstants::CONFIG_PATH_DB_CONNECTIONS .
- '/' .
- $resourceName .
- '/dbname'
- );
- }
- /**
- * Get table prefix.
- *
- * @return string
- */
- public function getTablePrefix()
- {
- if ($this->tablePrefix !== null) {
- return $this->tablePrefix;
- }
- return (string) $this->deploymentConfig->get(
- ConfigOptionsListConstants::CONFIG_PATH_DB_PREFIX
- );
- }
- }
|