AddColumn.php
5 years ago
AddColumns.php
5 years ago
AddIndex.php
5 years ago
AddPrimaryKey.php
5 years ago
AddUniqueKey.php
5 years ago
BatchInsert.php
5 years ago
BoundSql.php
5 years ago
ChangeColumn.php
5 years ago
ChangeColumnType.php
5 years ago
ChangeColumnTypes.php
5 years ago
CreateTable.php
5 years ago
DropColumn.php
5 years ago
DropColumns.php
5 years ago
DropIndex.php
5 years ago
DropPrimaryKey.php
5 years ago
DropTable.php
5 years ago
Factory.php
5 years ago
Insert.php
5 years ago
Sql.php
5 years ago
DropColumns.php
44 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Matomo - free/libre analytics platform |
| 4 | * |
| 5 | * @link https://matomo.org |
| 6 | * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 7 | */ |
| 8 | namespace Piwik\Updater\Migration\Db; |
| 9 | |
| 10 | use Piwik\Db; |
| 11 | use Piwik\DbHelper; |
| 12 | |
| 13 | /** |
| 14 | * @see Factory::dropColumn() |
| 15 | * @ignore |
| 16 | */ |
| 17 | class DropColumns extends Sql |
| 18 | { |
| 19 | public function __construct($tableName, $columnNames) |
| 20 | { |
| 21 | $table = DbHelper::getTableColumns($tableName); |
| 22 | |
| 23 | // we need to remove all not existing columns. Otherwise if only one of the columns doesn't exist, all of |
| 24 | // the columns wouldn't be removed |
| 25 | $columnNames = array_filter($columnNames, function ($columnName) use ($table) { |
| 26 | return isset($table[$columnName]); |
| 27 | }); |
| 28 | |
| 29 | if (empty($columnNames)) { |
| 30 | parent::__construct('', static::ERROR_CODE_COLUMN_NOT_EXISTS); |
| 31 | } else { |
| 32 | $columnNames = array_unique($columnNames); |
| 33 | $dropColumns = array_map(function ($columnName) { |
| 34 | return sprintf('DROP COLUMN `%s`', $columnName); |
| 35 | }, $columnNames); |
| 36 | |
| 37 | $sql = sprintf("ALTER TABLE `%s` %s", $tableName, implode(', ', $dropColumns)); |
| 38 | parent::__construct($sql, static::ERROR_CODE_COLUMN_NOT_EXISTS); |
| 39 | } |
| 40 | |
| 41 | } |
| 42 | |
| 43 | } |
| 44 |