Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
TemporaryTableService.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Framework\DB;
7 
9 
18 {
19  const INDEX_METHOD_HASH = 'HASH';
20  const DB_ENGINE_INNODB = 'INNODB';
21 
25  private $allowedIndexMethods;
26 
30  private $allowedEngines;
31 
35  private $random;
36 
40  private $createdTableAdapters = [];
41 
47  public function __construct(
48  \Magento\Framework\Math\Random $random,
49  $allowedIndexMethods = [],
50  $allowedEngines = []
51  ) {
52  $this->random = $random;
53  $this->allowedIndexMethods = $allowedIndexMethods;
54  $this->allowedEngines = $allowedEngines;
55  }
56 
83  public function createFromSelect(
86  array $indexes = [],
87  $indexMethod = self::INDEX_METHOD_HASH,
88  $dbEngine = self::DB_ENGINE_INNODB
89  ) {
90  if (!in_array($indexMethod, $this->allowedIndexMethods)) {
91  throw new \InvalidArgumentException(
92  sprintf('indexMethod must be one of %s', implode(',', $this->allowedIndexMethods))
93  );
94  }
95 
96  if (!in_array($dbEngine, $this->allowedEngines)) {
97  throw new \InvalidArgumentException(
98  sprintf('dbEngine must be one of %s', implode(',', $this->allowedEngines))
99  );
100  }
101 
102  $name = $this->random->getUniqueHash('tmp_select_');
103 
104  $indexStatements = [];
105  foreach ($indexes as $indexName => $columns) {
106  $renderedColumns = implode(',', array_map([$adapter, 'quoteIdentifier'], $columns));
107 
108  $indexType = sprintf(
109  'INDEX %s USING %s',
110  $adapter->quoteIdentifier($indexName),
111  $indexMethod
112  );
113 
114  if ($indexName === 'PRIMARY') {
115  $indexType = 'PRIMARY KEY';
116  } elseif (strpos($indexName, 'UNQ_') === 0) {
117  $indexType = sprintf('UNIQUE %s', $adapter->quoteIdentifier($indexName));
118  }
119 
120  $indexStatements[] = sprintf('%s(%s)', $indexType, $renderedColumns);
121  }
122 
123  $statement = sprintf(
124  'CREATE TEMPORARY TABLE %s %s ENGINE=%s IGNORE (%s)',
125  $adapter->quoteIdentifier($name),
126  $indexStatements ? '(' . implode(',', $indexStatements) . ')' : '',
127  $adapter->quoteIdentifier($dbEngine),
128  "{$select}"
129  );
130 
131  $adapter->query(
132  $statement,
133  $select->getBind()
134  );
135 
136  $this->createdTableAdapters[$name] = $adapter;
137 
138  return $name;
139  }
140 
154  public function dropTable($name)
155  {
156  if (!empty($this->createdTableAdapters)) {
157  if (isset($this->createdTableAdapters[$name]) && !empty($name)) {
158  $adapter = $this->createdTableAdapters[$name];
159  $adapter->dropTemporaryTable($name);
160  unset($this->createdTableAdapters[$name]);
161  return true;
162  }
163  }
164  return false;
165  }
166 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
createFromSelect(Select $select, AdapterInterface $adapter, array $indexes=[], $indexMethod=self::INDEX_METHOD_HASH, $dbEngine=self::DB_ENGINE_INNODB)
$adapter
Definition: webapi_user.php:16
$columns
Definition: default.phtml:15
__construct(\Magento\Framework\Math\Random $random, $allowedIndexMethods=[], $allowedEngines=[])
if(!isset($_GET['name'])) $name
Definition: log.php:14