|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Rentgen\Schema; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
6
|
|
|
|
|
7
|
|
|
use Rentgen\Rentgen; |
|
8
|
|
|
use Rentgen\Database\Column; |
|
9
|
|
|
use Rentgen\Database\Constraint\ConstraintInterface; |
|
10
|
|
|
use Rentgen\Database\DatabaseObjectInterface; |
|
11
|
|
|
use Rentgen\Database\Index; |
|
12
|
|
|
use Rentgen\Database\Schema; |
|
13
|
|
|
use Rentgen\Database\Table; |
|
14
|
|
|
|
|
15
|
|
|
class Manipulation |
|
16
|
|
|
{ |
|
17
|
|
|
private $container; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Constructor. |
|
21
|
|
|
* |
|
22
|
|
|
* @param Symfony\Component\DependencyInjection\ContainerInterface $container |
|
23
|
|
|
*/ |
|
24
|
|
|
public function __construct(ContainerInterface $container) |
|
25
|
|
|
{ |
|
26
|
|
|
$this->container = $container; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Create a new database object. |
|
31
|
|
|
* |
|
32
|
|
|
* @param DatabaseObjectInterface $databaseObject Database object. |
|
33
|
|
|
* |
|
34
|
|
|
* @return integer |
|
35
|
|
|
*/ |
|
36
|
|
|
public function create(DatabaseObjectInterface $databaseObject) |
|
37
|
|
|
{ |
|
38
|
|
|
$command = $this->getCommand($databaseObject); |
|
39
|
|
|
|
|
40
|
|
|
return $command->execute(); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Drop a database object. |
|
45
|
|
|
* |
|
46
|
|
|
* @param DatabaseObjectInterface $databaseObject Database object. |
|
47
|
|
|
* @param bool $cascade Drop databse object cascade. |
|
48
|
|
|
* |
|
49
|
|
|
* @return integer |
|
50
|
|
|
*/ |
|
51
|
|
|
public function drop(DatabaseObjectInterface $databaseObject, $cascade = false) |
|
52
|
|
|
{ |
|
53
|
|
|
$command = $this->getCommand($databaseObject, false); |
|
54
|
|
|
if ($cascade) { |
|
55
|
|
|
$command->cascade(); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
return $command->execute(); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Clear current database. Turn database to default state - empty public schema. |
|
63
|
|
|
* |
|
64
|
|
|
* @return integer |
|
65
|
|
|
*/ |
|
66
|
|
|
public function clearDatabase() |
|
67
|
|
|
{ |
|
68
|
|
|
$clearDatabaseCommand = $this->container->get('rentgen.clear_database'); |
|
69
|
|
|
|
|
70
|
|
|
return $clearDatabaseCommand->execute(); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Execute SQL query. |
|
75
|
|
|
* |
|
76
|
|
|
* @param string $sql Sql query. |
|
77
|
|
|
* |
|
78
|
|
|
* @return integer |
|
79
|
|
|
*/ |
|
80
|
|
|
public function execute($sql) |
|
81
|
|
|
{ |
|
82
|
|
|
return $this->container |
|
83
|
|
|
->get('connection') |
|
84
|
|
|
->execute($sql); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Get a command to execute. |
|
89
|
|
|
* |
|
90
|
|
|
* @param DatabaseObjectInterface $databaseObject Database object. |
|
91
|
|
|
* @param bool $cascade Drop databse object cascade. |
|
|
|
|
|
|
92
|
|
|
* |
|
93
|
|
|
* @return Rentgen\Schema\Command |
|
94
|
|
|
*/ |
|
95
|
|
|
private function getCommand(DatabaseObjectInterface $databaseObject, $isCreate = true) |
|
96
|
|
|
{ |
|
97
|
|
|
if ($databaseObject instanceof Column) { |
|
98
|
|
|
$command = $this->container |
|
99
|
|
|
->get($isCreate ? 'rentgen.add_column' : 'rentgen.drop_column') |
|
100
|
|
|
->setColumn($databaseObject); |
|
101
|
|
|
} elseif ($databaseObject instanceof ConstraintInterface) { |
|
102
|
|
|
$command = $this->container |
|
103
|
|
|
->get($isCreate ? 'rentgen.add_constraint' : 'rentgen.drop_constraint') |
|
104
|
|
|
->setConstraint($databaseObject); |
|
105
|
|
|
} elseif ($databaseObject instanceof Index) { |
|
106
|
|
|
$command = $this->container |
|
107
|
|
|
->get($isCreate ? 'rentgen.create_index' : 'rentgen.drop_index') |
|
108
|
|
|
->setIndex($databaseObject); |
|
109
|
|
|
} elseif ($databaseObject instanceof Schema) { |
|
110
|
|
|
$command = $this->container |
|
111
|
|
|
->get($isCreate ? 'rentgen.create_schema' : 'rentgen.drop_schema') |
|
112
|
|
|
->setSchema($databaseObject); |
|
113
|
|
|
} elseif ($databaseObject instanceof Table) { |
|
114
|
|
|
$command = $this->container |
|
115
|
|
|
->get($isCreate ? 'rentgen.create_table' : 'rentgen.drop_table') |
|
116
|
|
|
->setTable($databaseObject); |
|
117
|
|
|
} else { |
|
118
|
|
|
throw new \Exception(sprintf("Class %s is not supported", get_class($databaseObject))); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
return $command; |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.