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
<?php
/**
* Base Resource Setup Model
*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\Module;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\SetupInterface;
class Setup implements SetupInterface
{
/**
* Setup Connection
*
* @var \Magento\Framework\DB\Adapter\AdapterInterface
*/
private $connection = null;
/**
* Tables cache array
*
* @var array
*/
private $tables = [];
/**
* Modules configuration
*
* @var \Magento\Framework\App\ResourceConnection
*/
private $resourceModel;
/**
* Connection instance name
*
* @var string
*/
private $connectionName;
/**
* Init
*
* @param \Magento\Framework\App\ResourceConnection $resource
* @param string $connectionName
*/
public function __construct(
\Magento\Framework\App\ResourceConnection $resource,
$connectionName = ModuleDataSetupInterface::DEFAULT_SETUP_CONNECTION
) {
$this->resourceModel = $resource;
$this->connectionName = $connectionName;
}
/**
* Get connection object
*
* @param string|null $connectionName
* @return \Magento\Framework\DB\Adapter\AdapterInterface
*/
public function getConnection($connectionName = null)
{
if ($connectionName !== null) {
try {
return $this->resourceModel->getConnectionByName($connectionName);
} catch (\DomainException $exception) {
//Fallback to default connection
}
}
return $this->getDefaultConnection();
}
/**
* Returns default setup connection instance
*
* @return \Magento\Framework\DB\Adapter\AdapterInterface
*/
private function getDefaultConnection()
{
if (null === $this->connection) {
$this->connection = $this->resourceModel->getConnection($this->connectionName);
}
return $this->connection;
}
/**
* Add table placeholder/table name relation
*
* @param string $tableName
* @param string $realTableName
* @return $this
*/
public function setTable($tableName, $realTableName)
{
$this->tables[$tableName] = $realTableName;
return $this;
}
/**
* Gets table placeholder by table name
*
* @param string $tableName
* @return string
*/
public function getTablePlaceholder($tableName)
{
return $this->resourceModel->getTablePlaceholder($tableName);
}
/**
* Get table name (validated by db adapter) by table placeholder
*
* @param string|array $tableName
* @param string $connectionName
* @return string
*/
public function getTable($tableName, $connectionName = ResourceConnection::DEFAULT_CONNECTION)
{
$cacheKey = $this->_getTableCacheName($tableName);
if (!isset($this->tables[$cacheKey])) {
$this->tables[$cacheKey] = $this->resourceModel->getTableName($tableName, $connectionName);
}
return $this->tables[$cacheKey];
}
/**
* Retrieve table name for cache
*
* @param string|array $tableName
* @return string
*/
private function _getTableCacheName($tableName)
{
if (is_array($tableName)) {
return join('_', $tableName);
}
return $tableName;
}
/**
* Check is table exists
*
* @param string $table
* @param string $connectionName
* @return bool
*/
public function tableExists($table, $connectionName = ResourceConnection::DEFAULT_CONNECTION)
{
$table = $this->getTable($table, $connectionName);
return $this->getConnection($connectionName)->isTableExists($table);
}
/**
* Run plain SQL query(ies)
*
* @param string $sql
* @return $this
*/
public function run($sql)
{
$this->getConnection()->query($sql);
return $this;
}
/**
* Prepare database before install/upgrade
*
* @return $this
*/
public function startSetup()
{
$this->getConnection()->startSetup();
return $this;
}
/**
* Prepare database after install/upgrade
*
* @return $this
*/
public function endSetup()
{
$this->getConnection()->endSetup();
return $this;
}
}