|
1
|
|
|
<?php |
|
2
|
|
|
namespace Rentgen\Schema\Manipulation; |
|
3
|
|
|
|
|
4
|
|
|
use Rentgen\Schema\Command; |
|
5
|
|
|
use Rentgen\Database\Constraint\ConstraintInterface; |
|
6
|
|
|
use Rentgen\Database\Constraint\ForeignKey; |
|
7
|
|
|
use Rentgen\Database\Constraint\Unique; |
|
8
|
|
|
use Rentgen\Database\Table; |
|
9
|
|
|
|
|
10
|
|
|
class AddConstraintCommand extends Command |
|
11
|
|
|
{ |
|
12
|
|
|
private $constraint; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Set a constraint. |
|
16
|
|
|
* |
|
17
|
|
|
* @param ConstraintInterface $constraint A constraint instance. |
|
18
|
|
|
* |
|
19
|
|
|
* @return AddConstraintCommand |
|
20
|
|
|
*/ |
|
21
|
|
|
public function setConstraint(ConstraintInterface $constraint) |
|
22
|
|
|
{ |
|
23
|
|
|
$this->constraint = $constraint; |
|
24
|
|
|
|
|
25
|
|
|
return $this; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* {@inheritdoc} |
|
30
|
|
|
*/ |
|
31
|
|
|
public function getSql() |
|
32
|
|
|
{ |
|
33
|
|
|
if ($this->constraint instanceof ForeignKey) { |
|
34
|
|
|
return sprintf('ALTER TABLE %s ADD CONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s (%s) MATCH SIMPLE ON UPDATE %s ON DELETE %s;' |
|
35
|
|
|
, $this->constraint->getTable()->getQualifiedName() |
|
36
|
|
|
, $this->constraint->getName() |
|
37
|
|
|
, implode(',', $this->constraint->getColumns()) |
|
38
|
|
|
, $this->constraint->getReferencedTable()->getQualifiedName() |
|
39
|
|
|
, implode(',', $this->constraint->getReferencedColumns()) |
|
40
|
|
|
, $this->constraint->getUpdateAction() |
|
41
|
|
|
, $this->constraint->getDeleteAction() |
|
42
|
|
|
); |
|
43
|
|
|
} |
|
44
|
|
|
if ($this->constraint instanceof Unique) { |
|
45
|
|
|
return sprintf('ALTER TABLE %s ADD CONSTRAINT %s UNIQUE (%s);' |
|
46
|
|
|
, $this->constraint->getTable()->getQualifiedName() |
|
47
|
|
|
, $this->constraint->getName() |
|
48
|
|
|
, implode(',', $this->constraint->getColumns()) |
|
49
|
|
|
); |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|