View file vendor/robmorgan/phinx/src/Phinx/Db/Adapter/TablePrefixAdapter.php

File size: 14.73Kb
  1. <?php
  2.  
  3. /**
  4. * MIT License
  5. * For full license information, please view the LICENSE file that was distributed with this source code.
  6. */
  7.  
  8. namespace Phinx\Db\Adapter;
  9.  
  10. use BadMethodCallException;
  11. use InvalidArgumentException;
  12. use Phinx\Db\Action\AddColumn;
  13. use Phinx\Db\Action\AddForeignKey;
  14. use Phinx\Db\Action\AddIndex;
  15. use Phinx\Db\Action\ChangeColumn;
  16. use Phinx\Db\Action\ChangeComment;
  17. use Phinx\Db\Action\ChangePrimaryKey;
  18. use Phinx\Db\Action\DropForeignKey;
  19. use Phinx\Db\Action\DropIndex;
  20. use Phinx\Db\Action\DropTable;
  21. use Phinx\Db\Action\RemoveColumn;
  22. use Phinx\Db\Action\RenameColumn;
  23. use Phinx\Db\Action\RenameTable;
  24. use Phinx\Db\Table\Column;
  25. use Phinx\Db\Table\ForeignKey;
  26. use Phinx\Db\Table\Index;
  27. use Phinx\Db\Table\Table;
  28.  
  29. /**
  30. * Table prefix/suffix adapter.
  31. *
  32. * Used for inserting a prefix or suffix into table names.
  33. *
  34. * @author Samuel Fisher <sam@sfisher.co>
  35. */
  36. class TablePrefixAdapter extends AdapterWrapper implements DirectActionInterface
  37. {
  38. /**
  39. * @inheritDoc
  40. */
  41. public function getAdapterType()
  42. {
  43. return 'TablePrefixAdapter';
  44. }
  45.  
  46. /**
  47. * @inheritDoc
  48. */
  49. public function hasTable($tableName)
  50. {
  51. $adapterTableName = $this->getAdapterTableName($tableName);
  52.  
  53. return parent::hasTable($adapterTableName);
  54. }
  55.  
  56. /**
  57. * @inheritDoc
  58. */
  59. public function createTable(Table $table, array $columns = [], array $indexes = [])
  60. {
  61. $adapterTable = new Table(
  62. $this->getAdapterTableName($table->getName()),
  63. $table->getOptions()
  64. );
  65. parent::createTable($adapterTable, $columns, $indexes);
  66. }
  67.  
  68. /**
  69. * {@inheritDoc}
  70. *
  71. * @throws \BadMethodCallException
  72. *
  73. * @return void
  74. */
  75. public function changePrimaryKey(Table $table, $newColumns)
  76. {
  77. $adapter = $this->getAdapter();
  78. if (!$adapter instanceof DirectActionInterface) {
  79. throw new BadMethodCallException('The underlying adapter does not implement DirectActionInterface');
  80. }
  81.  
  82. $adapterTable = new Table(
  83. $this->getAdapterTableName($table->getName()),
  84. $table->getOptions()
  85. );
  86. $adapter->changePrimaryKey($adapterTable, $newColumns);
  87. }
  88.  
  89. /**
  90. * {@inheritDoc}
  91. *
  92. * @throws \BadMethodCallException
  93. *
  94. * @return void
  95. */
  96. public function changeComment(Table $table, $newComment)
  97. {
  98. $adapter = $this->getAdapter();
  99. if (!$adapter instanceof DirectActionInterface) {
  100. throw new BadMethodCallException('The underlying adapter does not implement DirectActionInterface');
  101. }
  102.  
  103. $adapterTable = new Table(
  104. $this->getAdapterTableName($table->getName()),
  105. $table->getOptions()
  106. );
  107. $adapter->changeComment($adapterTable, $newComment);
  108. }
  109.  
  110. /**
  111. * {@inheritDoc}
  112. *
  113. * @throws \BadMethodCallException
  114. *
  115. * @return void
  116. */
  117. public function renameTable($tableName, $newTableName)
  118. {
  119. $adapter = $this->getAdapter();
  120. if (!$adapter instanceof DirectActionInterface) {
  121. throw new BadMethodCallException('The underlying adapter does not implement DirectActionInterface');
  122. }
  123.  
  124. $adapterTableName = $this->getAdapterTableName($tableName);
  125. $adapterNewTableName = $this->getAdapterTableName($newTableName);
  126. $adapter->renameTable($adapterTableName, $adapterNewTableName);
  127. }
  128.  
  129. /**
  130. * {@inheritDoc}
  131. *
  132. * @throws \BadMethodCallException
  133. *
  134. * @return void
  135. */
  136. public function dropTable($tableName)
  137. {
  138. $adapter = $this->getAdapter();
  139. if (!$adapter instanceof DirectActionInterface) {
  140. throw new BadMethodCallException('The underlying adapter does not implement DirectActionInterface');
  141. }
  142. $adapterTableName = $this->getAdapterTableName($tableName);
  143. $adapter->dropTable($adapterTableName);
  144. }
  145.  
  146. /**
  147. * @inheritDoc
  148. */
  149. public function truncateTable($tableName)
  150. {
  151. $adapterTableName = $this->getAdapterTableName($tableName);
  152. parent::truncateTable($adapterTableName);
  153. }
  154.  
  155. /**
  156. * @inheritDoc
  157. */
  158. public function getColumns($tableName)
  159. {
  160. $adapterTableName = $this->getAdapterTableName($tableName);
  161.  
  162. return parent::getColumns($adapterTableName);
  163. }
  164.  
  165. /**
  166. * @inheritDoc
  167. */
  168. public function hasColumn($tableName, $columnName)
  169. {
  170. $adapterTableName = $this->getAdapterTableName($tableName);
  171.  
  172. return parent::hasColumn($adapterTableName, $columnName);
  173. }
  174.  
  175. /**
  176. * {@inheritDoc}
  177. *
  178. * @throws \BadMethodCallException
  179. *
  180. * @return void
  181. */
  182. public function addColumn(Table $table, Column $column)
  183. {
  184. $adapter = $this->getAdapter();
  185. if (!$adapter instanceof DirectActionInterface) {
  186. throw new BadMethodCallException('The underlying adapter does not implement DirectActionInterface');
  187. }
  188. $adapterTableName = $this->getAdapterTableName($table->getName());
  189. $adapterTable = new Table($adapterTableName, $table->getOptions());
  190. $adapter->addColumn($adapterTable, $column);
  191. }
  192.  
  193. /**
  194. * {@inheritDoc}
  195. *
  196. * @throws \BadMethodCallException
  197. *
  198. * @return void
  199. */
  200. public function renameColumn($tableName, $columnName, $newColumnName)
  201. {
  202. $adapter = $this->getAdapter();
  203. if (!$adapter instanceof DirectActionInterface) {
  204. throw new BadMethodCallException('The underlying adapter does not implement DirectActionInterface');
  205. }
  206. $adapterTableName = $this->getAdapterTableName($tableName);
  207. $adapter->renameColumn($adapterTableName, $columnName, $newColumnName);
  208. }
  209.  
  210. /**
  211. * {@inheritDoc}
  212. *
  213. * @throws \BadMethodCallException
  214. *
  215. * @return void
  216. */
  217. public function changeColumn($tableName, $columnName, Column $newColumn)
  218. {
  219. $adapter = $this->getAdapter();
  220. if (!$adapter instanceof DirectActionInterface) {
  221. throw new BadMethodCallException('The underlying adapter does not implement DirectActionInterface');
  222. }
  223. $adapterTableName = $this->getAdapterTableName($tableName);
  224. $adapter->changeColumn($adapterTableName, $columnName, $newColumn);
  225. }
  226.  
  227. /**
  228. * {@inheritDoc}
  229. *
  230. * @throws \BadMethodCallException
  231. *
  232. * @return void
  233. */
  234. public function dropColumn($tableName, $columnName)
  235. {
  236. $adapter = $this->getAdapter();
  237. if (!$adapter instanceof DirectActionInterface) {
  238. throw new BadMethodCallException('The underlying adapter does not implement DirectActionInterface');
  239. }
  240. $adapterTableName = $this->getAdapterTableName($tableName);
  241. $adapter->dropColumn($adapterTableName, $columnName);
  242. }
  243.  
  244. /**
  245. * @inheritDoc
  246. */
  247. public function hasIndex($tableName, $columns)
  248. {
  249. $adapterTableName = $this->getAdapterTableName($tableName);
  250.  
  251. return parent::hasIndex($adapterTableName, $columns);
  252. }
  253.  
  254. /**
  255. * @inheritDoc
  256. */
  257. public function hasIndexByName($tableName, $indexName)
  258. {
  259. $adapterTableName = $this->getAdapterTableName($tableName);
  260.  
  261. return parent::hasIndexByName($adapterTableName, $indexName);
  262. }
  263.  
  264. /**
  265. * {@inheritDoc}
  266. *
  267. * @throws \BadMethodCallException
  268. *
  269. * @return void
  270. */
  271. public function addIndex(Table $table, Index $index)
  272. {
  273. $adapter = $this->getAdapter();
  274. if (!$adapter instanceof DirectActionInterface) {
  275. throw new BadMethodCallException('The underlying adapter does not implement DirectActionInterface');
  276. }
  277. $adapterTable = new Table($table->getName(), $table->getOptions());
  278. $adapter->addIndex($adapterTable, $index);
  279. }
  280.  
  281. /**
  282. * {@inheritDoc}
  283. *
  284. * @throws \BadMethodCallException
  285. *
  286. * @return void
  287. */
  288. public function dropIndex($tableName, $columns)
  289. {
  290. $adapter = $this->getAdapter();
  291. if (!$adapter instanceof DirectActionInterface) {
  292. throw new BadMethodCallException('The underlying adapter does not implement DirectActionInterface');
  293. }
  294. $adapterTableName = $this->getAdapterTableName($tableName);
  295. $adapter->dropIndex($adapterTableName, $columns);
  296. }
  297.  
  298. /**
  299. * {@inheritDoc}
  300. *
  301. * @throws \BadMethodCallException
  302. *
  303. * @return void
  304. */
  305. public function dropIndexByName($tableName, $indexName)
  306. {
  307. $adapter = $this->getAdapter();
  308. if (!$adapter instanceof DirectActionInterface) {
  309. throw new BadMethodCallException('The underlying adapter does not implement DirectActionInterface');
  310. }
  311. $adapterTableName = $this->getAdapterTableName($tableName);
  312. $adapter->dropIndexByName($adapterTableName, $indexName);
  313. }
  314.  
  315. /**
  316. * @inheritDoc
  317. */
  318. public function hasPrimaryKey($tableName, $columns, $constraint = null)
  319. {
  320. $adapterTableName = $this->getAdapterTableName($tableName);
  321.  
  322. return parent::hasPrimaryKey($adapterTableName, $columns, $constraint);
  323. }
  324.  
  325. /**
  326. * @inheritDoc
  327. */
  328. public function hasForeignKey($tableName, $columns, $constraint = null)
  329. {
  330. $adapterTableName = $this->getAdapterTableName($tableName);
  331.  
  332. return parent::hasForeignKey($adapterTableName, $columns, $constraint);
  333. }
  334.  
  335. /**
  336. * {@inheritDoc}
  337. *
  338. * @throws \BadMethodCallException
  339. *
  340. * @return void
  341. */
  342. public function addForeignKey(Table $table, ForeignKey $foreignKey)
  343. {
  344. $adapter = $this->getAdapter();
  345. if (!$adapter instanceof DirectActionInterface) {
  346. throw new BadMethodCallException('The underlying adapter does not implement DirectActionInterface');
  347. }
  348. $adapterTableName = $this->getAdapterTableName($table->getName());
  349. $adapterTable = new Table($adapterTableName, $table->getOptions());
  350. $adapter->addForeignKey($adapterTable, $foreignKey);
  351. }
  352.  
  353. /**
  354. * {@inheritDoc}
  355. *
  356. * @throws \BadMethodCallException
  357. *
  358. * @return void
  359. */
  360. public function dropForeignKey($tableName, $columns, $constraint = null)
  361. {
  362. $adapter = $this->getAdapter();
  363. if (!$adapter instanceof DirectActionInterface) {
  364. throw new BadMethodCallException('The underlying adapter does not implement DirectActionInterface');
  365. }
  366. $adapterTableName = $this->getAdapterTableName($tableName);
  367. $adapter->dropForeignKey($adapterTableName, $columns, $constraint);
  368. }
  369.  
  370. /**
  371. * @inheritDoc
  372. */
  373. public function insert(Table $table, $row)
  374. {
  375. $adapterTableName = $this->getAdapterTableName($table->getName());
  376. $adapterTable = new Table($adapterTableName, $table->getOptions());
  377. parent::insert($adapterTable, $row);
  378. }
  379.  
  380. /**
  381. * @inheritDoc
  382. */
  383. public function bulkinsert(Table $table, $rows)
  384. {
  385. $adapterTableName = $this->getAdapterTableName($table->getName());
  386. $adapterTable = new Table($adapterTableName, $table->getOptions());
  387. parent::bulkinsert($adapterTable, $rows);
  388. }
  389.  
  390. /**
  391. * Gets the table prefix.
  392. *
  393. * @return string
  394. */
  395. public function getPrefix()
  396. {
  397. return (string)$this->getOption('table_prefix');
  398. }
  399.  
  400. /**
  401. * Gets the table suffix.
  402. *
  403. * @return string
  404. */
  405. public function getSuffix()
  406. {
  407. return (string)$this->getOption('table_suffix');
  408. }
  409.  
  410. /**
  411. * Applies the prefix and suffix to the table name.
  412. *
  413. * @param string $tableName Table name
  414. *
  415. * @return string
  416. */
  417. public function getAdapterTableName($tableName)
  418. {
  419. return $this->getPrefix() . $tableName . $this->getSuffix();
  420. }
  421.  
  422. /**
  423. * {@inheritDoc}
  424. *
  425. * @throws \InvalidArgumentException
  426. *
  427. * @return void
  428. */
  429. public function executeActions(Table $table, array $actions)
  430. {
  431. $adapterTableName = $this->getAdapterTableName($table->getName());
  432. $adapterTable = new Table($adapterTableName, $table->getOptions());
  433.  
  434. foreach ($actions as $k => $action) {
  435. switch (true) {
  436. case ($action instanceof AddColumn):
  437. $actions[$k] = new AddColumn($adapterTable, $action->getColumn());
  438. break;
  439.  
  440. case ($action instanceof AddIndex):
  441. $actions[$k] = new AddIndex($adapterTable, $action->getIndex());
  442. break;
  443.  
  444. case ($action instanceof AddForeignKey):
  445. $foreignKey = clone $action->getForeignKey();
  446. $refTable = $foreignKey->getReferencedTable();
  447. $refTableName = $this->getAdapterTableName($refTable->getName());
  448. $foreignKey->setReferencedTable(new Table($refTableName, $refTable->getOptions()));
  449. $actions[$k] = new AddForeignKey($adapterTable, $foreignKey);
  450. break;
  451.  
  452. case ($action instanceof ChangeColumn):
  453. $actions[$k] = new ChangeColumn($adapterTable, $action->getColumnName(), $action->getColumn());
  454. break;
  455.  
  456. case ($action instanceof DropForeignKey):
  457. $actions[$k] = new DropForeignKey($adapterTable, $action->getForeignKey());
  458. break;
  459.  
  460. case ($action instanceof DropIndex):
  461. $actions[$k] = new DropIndex($adapterTable, $action->getIndex());
  462. break;
  463.  
  464. case ($action instanceof DropTable):
  465. $actions[$k] = new DropTable($adapterTable);
  466. break;
  467.  
  468. case ($action instanceof RemoveColumn):
  469. $actions[$k] = new RemoveColumn($adapterTable, $action->getColumn());
  470. break;
  471.  
  472. case ($action instanceof RenameColumn):
  473. $actions[$k] = new RenameColumn($adapterTable, $action->getColumn(), $action->getNewName());
  474. break;
  475.  
  476. case ($action instanceof RenameTable):
  477. $actions[$k] = new RenameTable($adapterTable, $action->getNewName());
  478. break;
  479.  
  480. case ($action instanceof ChangePrimaryKey):
  481. $actions[$k] = new ChangePrimaryKey($adapterTable, $action->getNewColumns());
  482. break;
  483.  
  484. case ($action instanceof ChangeComment):
  485. $actions[$k] = new ChangeComment($adapterTable, $action->getNewComment());
  486. break;
  487.  
  488. default:
  489. throw new InvalidArgumentException(
  490. sprintf("Forgot to implement table prefixing for action: '%s'", get_class($action))
  491. );
  492. }
  493. }
  494.  
  495. parent::executeActions($adapterTable, $actions);
  496. }
  497. }