1
|
|
|
<?php |
2
|
|
|
namespace Rentgen\Schema\Manipulation; |
3
|
|
|
|
4
|
|
|
use Rentgen\Schema\Command; |
5
|
|
|
use Rentgen\Database\Column; |
6
|
|
|
use Rentgen\Database\Column\CustomColumn; |
7
|
|
|
use Rentgen\Event\ColumnEvent; |
8
|
|
|
use Rentgen\Schema\ColumnTypeMapper; |
9
|
|
|
|
10
|
|
|
class AddColumnCommand extends Command |
11
|
|
|
{ |
12
|
|
|
private $column; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Sets a column. |
16
|
|
|
* |
17
|
|
|
* @param Column $column The column instance. |
18
|
|
|
* |
19
|
|
|
* @return AddColumnCommand |
20
|
|
|
*/ |
21
|
|
|
public function setColumn(Column $column) |
22
|
|
|
{ |
23
|
|
|
$this->column = $column; |
24
|
|
|
|
25
|
|
|
return $this; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* {@inheritdoc} |
30
|
|
|
*/ |
31
|
|
|
public function getSql() |
32
|
|
|
{ |
33
|
|
|
if ($this->column instanceof CustomColumn) { |
34
|
|
|
$columnType = $this->column->getType(); |
35
|
|
|
} else { |
36
|
|
|
$columnTypeMapper = new ColumnTypeMapper(); |
37
|
|
|
$columnType = $columnTypeMapper->getNative($this->column->getType()); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
$sql = sprintf('ALTER TABLE %s ADD COLUMN %s %s;' |
41
|
|
|
, $this->column->getTable()->getQualifiedName() |
42
|
|
|
, $this->column->getName() |
43
|
|
|
, $columnType |
44
|
|
|
); |
45
|
|
|
|
46
|
|
|
$columnDescription = $this->column->getDescription(); |
47
|
|
|
if (!empty($tableDescription)) { |
|
|
|
|
48
|
|
|
$sql .= sprintf("COMMENT ON COLUMN %s.%s IS '%s';", |
49
|
|
|
$this->column->getTable()->getQualifiedName(), |
50
|
|
|
$this->column->getName(), |
51
|
|
|
$columnDescription); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return $sql; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritdoc} |
59
|
|
|
*/ |
60
|
|
|
protected function postExecute() |
61
|
|
|
{ |
62
|
|
|
$this->dispatcher->dispatch('column.add', new ColumnEvent($this->column, $this->getSql())); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
This check looks for calls to
isset(...)
orempty()
on variables that are yet undefined. These calls will always produce the same result and can be removed.This is most likely caused by the renaming of a variable or the removal of a function/method parameter.