/
ResourceConnection.php
251 lines (224 loc) · 6.85 KB
/
ResourceConnection.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<?php
/**
* Resources and connections registry and factory
*
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\App;
use Magento\Framework\App\DeploymentConfig;
use Magento\Framework\App\ResourceConnection\ConfigInterface as ResourceConfigInterface;
use Magento\Framework\Model\ResourceModel\Type\Db\ConnectionFactoryInterface;
use Magento\Framework\Config\ConfigOptionsListConstants;
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 $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);
return $this->getConnectionByName($connectionName);
}
/**
* Retrieve connection by $connectionName
*
* @param string $connectionName
* @return \Magento\Framework\DB\Adapter\AdapterInterface
* @throws \DomainException
*/
public function getConnectionByName($connectionName)
{
if (isset($this->connections[$connectionName])) {
return $this->connections[$connectionName];
}
$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[$connectionName] = $connection;
return $connection;
}
/**
* 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);
}
/**
* 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
);
}
/**
* Get table prefix
*
* @return string
*/
private function getTablePrefix()
{
if (null === $this->tablePrefix) {
$this->tablePrefix = (string)$this->deploymentConfig->get(
ConfigOptionsListConstants::CONFIG_PATH_DB_PREFIX
);
}
return $this->tablePrefix;
}
}