| <?php |
|
|
| |
| |
| |
| |
| |
| |
|
|
| namespace Piwik\Updater\Migration\Db; |
|
|
| |
| |
| |
| |
| class AddIndex extends Sql |
| { |
| protected $indexType = 'INDEX'; |
| protected $indexNamePrefix = 'index'; |
|
|
| |
| |
| |
| |
| |
| |
| public function __construct($table, $columnNames, $indexName) |
| { |
| $columns = array(); |
| $columnNamesOnly = array(); |
| foreach ($columnNames as $columnName) { |
| $columnName = str_replace(' ', '', $columnName); |
| preg_match('/^([\w]+)(\(?\d*\)?)$/', $columnName, $matches); |
|
|
| $nameOnly = $matches[1]; |
| $columnNamesOnly[] = $nameOnly; |
| $column = "`$nameOnly`"; |
| if (!empty($matches[2])) { |
| $column .= ' ' . $matches[2]; |
| } |
|
|
| $columns[] = $column; |
| } |
|
|
| if (empty($indexName)) { |
| $indexName = $this->indexNamePrefix . '_' . implode('_', $columnNamesOnly); |
| } |
|
|
| $sql = sprintf("ALTER TABLE `%s` ADD %s %s (%s)", $table, $this->indexType, $indexName, implode(', ', $columns)); |
|
|
| parent::__construct($sql, array(static::ERROR_CODE_DUPLICATE_KEY, static::ERROR_CODE_KEY_COLUMN_NOT_EXISTS)); |
| } |
| } |
|
|