ResourceConnection.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\App;
  7. use Magento\Framework\App\ResourceConnection\ConfigInterface as ResourceConfigInterface;
  8. use Magento\Framework\Config\ConfigOptionsListConstants;
  9. use Magento\Framework\Model\ResourceModel\Type\Db\ConnectionFactoryInterface;
  10. /**
  11. * Application provides ability to configure multiple connections to persistent storage.
  12. *
  13. * This class provides access to all these connections.
  14. *
  15. * @api
  16. * @since 100.0.2
  17. */
  18. class ResourceConnection
  19. {
  20. const AUTO_UPDATE_ONCE = 0;
  21. const AUTO_UPDATE_NEVER = -1;
  22. const AUTO_UPDATE_ALWAYS = 1;
  23. const DEFAULT_CONNECTION = 'default';
  24. /**
  25. * Instances of actual connections.
  26. *
  27. * @var \Magento\Framework\DB\Adapter\AdapterInterface[]
  28. */
  29. protected $connections = [];
  30. /**
  31. * Mapped tables cache array.
  32. *
  33. * @var array
  34. */
  35. protected $mappedTableNames;
  36. /**
  37. * Resource config.
  38. *
  39. * @var ResourceConfigInterface
  40. */
  41. protected $config;
  42. /**
  43. * Resource connection adapter factory.
  44. *
  45. * @var ConnectionFactoryInterface
  46. */
  47. protected $connectionFactory;
  48. /**
  49. * @var DeploymentConfig
  50. */
  51. private $deploymentConfig;
  52. /**
  53. * @var string
  54. */
  55. protected $tablePrefix;
  56. /**
  57. * @param ResourceConfigInterface $resourceConfig
  58. * @param ConnectionFactoryInterface $connectionFactory
  59. * @param DeploymentConfig $deploymentConfig
  60. * @param string $tablePrefix
  61. */
  62. public function __construct(
  63. ResourceConfigInterface $resourceConfig,
  64. ConnectionFactoryInterface $connectionFactory,
  65. DeploymentConfig $deploymentConfig,
  66. $tablePrefix = ''
  67. ) {
  68. $this->config = $resourceConfig;
  69. $this->connectionFactory = $connectionFactory;
  70. $this->deploymentConfig = $deploymentConfig;
  71. $this->tablePrefix = $tablePrefix ?: null;
  72. }
  73. /**
  74. * Retrieve connection to resource specified by $resourceName.
  75. *
  76. * @param string $resourceName
  77. * @return \Magento\Framework\DB\Adapter\AdapterInterface
  78. * @throws \DomainException
  79. * @codeCoverageIgnore
  80. */
  81. public function getConnection($resourceName = self::DEFAULT_CONNECTION)
  82. {
  83. $connectionName = $this->config->getConnectionName($resourceName);
  84. $connection = $this->getConnectionByName($connectionName);
  85. return $connection;
  86. }
  87. /**
  88. * Close connection.
  89. *
  90. * @param string $resourceName
  91. * @return void
  92. * @since 100.1.3
  93. */
  94. public function closeConnection($resourceName = self::DEFAULT_CONNECTION)
  95. {
  96. if ($resourceName === null) {
  97. foreach ($this->connections as $processConnection) {
  98. if ($processConnection !== null) {
  99. $processConnection->closeConnection();
  100. }
  101. }
  102. $this->connections = [];
  103. } else {
  104. $processConnectionName = $this->getProcessConnectionName($this->config->getConnectionName($resourceName));
  105. if (isset($this->connections[$processConnectionName])) {
  106. if ($this->connections[$processConnectionName] !== null) {
  107. $this->connections[$processConnectionName]->closeConnection();
  108. }
  109. $this->connections[$processConnectionName] = null;
  110. }
  111. }
  112. }
  113. /**
  114. * Retrieve connection by $connectionName.
  115. *
  116. * @param string $connectionName
  117. * @return \Magento\Framework\DB\Adapter\AdapterInterface
  118. * @throws \DomainException
  119. */
  120. public function getConnectionByName($connectionName)
  121. {
  122. $processConnectionName = $this->getProcessConnectionName($connectionName);
  123. if (isset($this->connections[$processConnectionName])) {
  124. return $this->connections[$processConnectionName];
  125. }
  126. $connectionConfig = $this->deploymentConfig->get(
  127. ConfigOptionsListConstants::CONFIG_PATH_DB_CONNECTIONS . '/' . $connectionName
  128. );
  129. if ($connectionConfig) {
  130. $connection = $this->connectionFactory->create($connectionConfig);
  131. } else {
  132. throw new \DomainException('Connection "' . $connectionName . '" is not defined');
  133. }
  134. $this->connections[$processConnectionName] = $connection;
  135. return $connection;
  136. }
  137. /**
  138. * Get conneciton name for process.
  139. *
  140. * @param string $connectionName
  141. * @return string
  142. */
  143. private function getProcessConnectionName($connectionName)
  144. {
  145. return $connectionName . '_process_' . getmypid();
  146. }
  147. /**
  148. * Get resource table name, validated by db adapter.
  149. *
  150. * @param string|string[] $modelEntity
  151. * @param string $connectionName
  152. * @return string
  153. * @api
  154. */
  155. public function getTableName($modelEntity, $connectionName = self::DEFAULT_CONNECTION)
  156. {
  157. $tableSuffix = null;
  158. if (is_array($modelEntity)) {
  159. list($modelEntity, $tableSuffix) = $modelEntity;
  160. }
  161. $tableName = $modelEntity;
  162. $mappedTableName = $this->getMappedTableName($tableName);
  163. if ($mappedTableName) {
  164. $tableName = $mappedTableName;
  165. } else {
  166. $tablePrefix = $this->getTablePrefix();
  167. if ($tablePrefix && strpos($tableName, $tablePrefix) !== 0) {
  168. $tableName = $tablePrefix . $tableName;
  169. }
  170. }
  171. if ($tableSuffix) {
  172. $tableName .= '_' . $tableSuffix;
  173. }
  174. return $this->getConnection($connectionName)->getTableName($tableName);
  175. }
  176. /**
  177. * Gets table placeholder by table name.
  178. *
  179. * @param string $tableName
  180. * @return string
  181. * @since 100.1.0
  182. */
  183. public function getTablePlaceholder($tableName)
  184. {
  185. $tableName = preg_replace('/^' . preg_quote($this->getTablePrefix()) . '_/', '', $tableName);
  186. return $tableName;
  187. }
  188. /**
  189. * Build a trigger name.
  190. *
  191. * @param string $tableName The table that is the subject of the trigger
  192. * @param string $time Either "before" or "after"
  193. * @param string $event The DB level event which activates the trigger, i.e. "update" or "insert"
  194. * @return string
  195. */
  196. public function getTriggerName($tableName, $time, $event)
  197. {
  198. return $this->getConnection()->getTriggerName($tableName, $time, $event);
  199. }
  200. /**
  201. * Set mapped table name.
  202. *
  203. * @param string $tableName
  204. * @param string $mappedName
  205. * @return $this
  206. * @codeCoverageIgnore
  207. */
  208. public function setMappedTableName($tableName, $mappedName)
  209. {
  210. $this->mappedTableNames[$tableName] = $mappedName;
  211. return $this;
  212. }
  213. /**
  214. * Get mapped table name.
  215. *
  216. * @param string $tableName
  217. * @return bool|string
  218. */
  219. public function getMappedTableName($tableName)
  220. {
  221. if (isset($this->mappedTableNames[$tableName])) {
  222. return $this->mappedTableNames[$tableName];
  223. } else {
  224. return false;
  225. }
  226. }
  227. /**
  228. * Retrieve 32bit UNIQUE HASH for a Table index.
  229. *
  230. * @param string $tableName
  231. * @param string|string[] $fields
  232. * @param string $indexType
  233. * @return string
  234. */
  235. public function getIdxName(
  236. $tableName,
  237. $fields,
  238. $indexType = \Magento\Framework\DB\Adapter\AdapterInterface::INDEX_TYPE_INDEX
  239. ) {
  240. return $this->getConnection()
  241. ->getIndexName(
  242. $this->getTableName($tableName),
  243. $fields,
  244. $indexType
  245. );
  246. }
  247. /**
  248. * Retrieve 32bit UNIQUE HASH for a Table foreign key.
  249. *
  250. * @param string $priTableName the target table name
  251. * @param string $priColumnName the target table column name
  252. * @param string $refTableName the reference table name
  253. * @param string $refColumnName the reference table column name
  254. * @return string
  255. */
  256. public function getFkName($priTableName, $priColumnName, $refTableName, $refColumnName)
  257. {
  258. return $this->getConnection()->getForeignKeyName(
  259. $this->getTableName($priTableName),
  260. $priColumnName,
  261. $this->getTableName($refTableName),
  262. $refColumnName
  263. );
  264. }
  265. /**
  266. * Retrieve db name.
  267. *
  268. * That name can be needed, when we do request in information_schema to identify db.
  269. *
  270. * @param string $resourceName
  271. * @return string
  272. * @since 102.0.0
  273. */
  274. public function getSchemaName($resourceName)
  275. {
  276. return $this->deploymentConfig->get(
  277. ConfigOptionsListConstants::CONFIG_PATH_DB_CONNECTIONS .
  278. '/' .
  279. $resourceName .
  280. '/dbname'
  281. );
  282. }
  283. /**
  284. * Get table prefix.
  285. *
  286. * @return string
  287. */
  288. public function getTablePrefix()
  289. {
  290. if ($this->tablePrefix !== null) {
  291. return $this->tablePrefix;
  292. }
  293. return (string) $this->deploymentConfig->get(
  294. ConfigOptionsListConstants::CONFIG_PATH_DB_PREFIX
  295. );
  296. }
  297. }