AddColumn.php
2 years ago
AddColumns.php
2 years ago
AddIndex.php
2 years ago
AddPrimaryKey.php
2 years ago
AddUniqueKey.php
2 years ago
BatchInsert.php
1 year ago
BoundSql.php
2 years ago
ChangeColumn.php
2 years ago
ChangeColumnType.php
2 years ago
ChangeColumnTypes.php
2 years ago
CreateTable.php
2 months ago
DropColumn.php
2 years ago
DropColumns.php
2 years ago
DropIndex.php
2 years ago
DropPrimaryKey.php
2 years ago
DropTable.php
2 years ago
Factory.php
5 months ago
Insert.php
2 years ago
Sql.php
1 year ago
AddIndex.php
51 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Matomo - free/libre analytics platform |
| 5 | * |
| 6 | * @link https://matomo.org |
| 7 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 8 | */ |
| 9 | namespace Piwik\Updater\Migration\Db; |
| 10 | |
| 11 | /** |
| 12 | * @see Factory::addIndex() |
| 13 | * @ignore |
| 14 | */ |
| 15 | class AddIndex extends \Piwik\Updater\Migration\Db\Sql |
| 16 | { |
| 17 | protected $indexType = 'INDEX'; |
| 18 | protected $indexNamePrefix = 'index'; |
| 19 | /** |
| 20 | * AddIndex constructor. |
| 21 | * @param string $table |
| 22 | * @param array $columnNames |
| 23 | * @param string $indexName |
| 24 | */ |
| 25 | public function __construct($table, $columnNames, $indexName) |
| 26 | { |
| 27 | $columns = array(); |
| 28 | $columnNamesOnly = array(); |
| 29 | foreach ($columnNames as $columnName) { |
| 30 | $columnName = str_replace(' ', '', $columnName); |
| 31 | // eg "column_name (10)" => "column_name(10)" |
| 32 | preg_match('/^([\\w]+)(\\(?\\d*\\)?)$/', $columnName, $matches); |
| 33 | // match "column_name" and "column_name(10)" |
| 34 | $nameOnly = $matches[1]; |
| 35 | // eg "column_name" |
| 36 | $columnNamesOnly[] = $nameOnly; |
| 37 | $column = "`{$nameOnly}`"; |
| 38 | if (!empty($matches[2])) { |
| 39 | $column .= ' ' . $matches[2]; |
| 40 | // eg "(10)" |
| 41 | } |
| 42 | $columns[] = $column; |
| 43 | } |
| 44 | if (empty($indexName)) { |
| 45 | $indexName = $this->indexNamePrefix . '_' . implode('_', $columnNamesOnly); |
| 46 | } |
| 47 | $sql = sprintf("ALTER TABLE `%s` ADD %s %s (%s)", $table, $this->indexType, $indexName, implode(', ', $columns)); |
| 48 | parent::__construct($sql, array(static::ERROR_CODE_DUPLICATE_KEY, static::ERROR_CODE_KEY_COLUMN_NOT_EXISTS)); |
| 49 | } |
| 50 | } |
| 51 |