1 | <?php |
||
12 | abstract class AbstractDatabaseManipulator |
||
13 | { |
||
14 | |||
15 | const CONVERT_TO_DRIVER = 'driver'; |
||
16 | const CONVERT_TO_YENTU = 'yentu'; |
||
17 | |||
18 | private $schemaDescription; |
||
19 | private $assertor; |
||
20 | private $connection; |
||
21 | private $dumpQuery; |
||
22 | private $disableQuery; |
||
23 | protected $defaultSchema; |
||
24 | private $io; |
||
25 | |||
26 | 33 | public function __construct(Yentu $yentu, DriverFactory $driverFactory, ConsoleIO $io) |
|
|
|||
27 | { |
||
28 | 33 | $this->connection = $driverFactory->createDriver(); |
|
29 | 33 | $this->connection->connect(); |
|
30 | 33 | $this->io = $io; |
|
31 | 33 | } |
|
32 | |||
33 | 33 | public function __get($name) |
|
34 | { |
||
35 | 33 | if ($name === 'description') { |
|
36 | 33 | return $this->getDescription(); |
|
37 | } |
||
38 | } |
||
39 | |||
40 | 10 | public function setDumpQuery($dumpQuery) |
|
41 | { |
||
42 | 10 | $this->dumpQuery = $dumpQuery; |
|
43 | 10 | } |
|
44 | |||
45 | 10 | public function setDisableQuery($disableQuery) |
|
46 | { |
||
47 | 10 | $this->disableQuery = $disableQuery; |
|
48 | 10 | } |
|
49 | |||
50 | 30 | public function __call($name, $arguments) |
|
51 | { |
||
52 | 30 | if (preg_match("/^(add|drop|change|executeQuery|reverseQuery)/", $name)) { |
|
53 | 30 | $details = Parameters::wrap($arguments[0]); |
|
54 | 30 | $this->description->$name($details); |
|
55 | 30 | $name = "_$name"; |
|
56 | 30 | new \ReflectionMethod($this, $name); |
|
57 | 30 | return $this->$name($details); |
|
58 | } else { |
||
59 | throw new \Exception("Failed to execute method '$name'"); |
||
60 | } |
||
61 | } |
||
62 | |||
63 | 30 | public function query($query, $bind = false) |
|
64 | { |
||
65 | try { |
||
66 | 30 | if ($this->dumpQuery) { |
|
67 | echo "$query\n"; |
||
68 | } |
||
69 | |||
70 | 30 | $this->io->output("\n > Running Query [$query]", ConsoleIO::OUTPUT_LEVEL_3); |
|
71 | |||
72 | 30 | if ($this->disableQuery !== true) { |
|
73 | 30 | return $this->connection->query($query, $bind); |
|
74 | } |
||
75 | 1 | } catch (\ntentan\atiaa\exceptions\DatabaseDriverException $e) { |
|
76 | 1 | throw new exceptions\DatabaseManipulatorException($e->getMessage()); |
|
77 | } |
||
78 | 10 | } |
|
79 | |||
80 | 27 | public function disconnect() |
|
81 | { |
||
82 | 27 | $this->connection->disconnect(); |
|
83 | 27 | } |
|
84 | |||
85 | 1 | public function getDefaultSchema() |
|
86 | { |
||
87 | 1 | return $this->connection->getDefaultSchema(); |
|
88 | } |
||
89 | |||
90 | 30 | public function getAssertor() |
|
91 | { |
||
92 | 30 | if (!is_object($this->assertor)) { |
|
93 | 30 | $this->assertor = new DatabaseAssertor($this->description); |
|
94 | } |
||
95 | 30 | return $this->assertor; |
|
96 | } |
||
97 | |||
98 | abstract protected function _addSchema($name); |
||
99 | |||
100 | abstract protected function _dropSchema($name); |
||
101 | |||
102 | abstract protected function _addTable($details); |
||
103 | |||
104 | abstract protected function _dropTable($details); |
||
105 | |||
106 | abstract protected function _changeTableName($details); |
||
107 | |||
108 | abstract protected function _addColumn($details); |
||
109 | |||
110 | abstract protected function _changeColumnNulls($details); |
||
111 | |||
112 | abstract protected function _changeColumnName($details); |
||
113 | |||
114 | abstract protected function _changeColumnDefault($details); |
||
115 | |||
116 | abstract protected function _dropColumn($details); |
||
117 | |||
118 | abstract protected function _addPrimaryKey($details); |
||
119 | |||
120 | abstract protected function _dropPrimaryKey($details); |
||
121 | |||
122 | abstract protected function _addUniqueKey($details); |
||
123 | |||
124 | abstract protected function _dropUniqueKey($details); |
||
125 | |||
126 | abstract protected function _addAutoPrimaryKey($details); |
||
127 | |||
128 | abstract protected function _dropAutoPrimaryKey($details); |
||
129 | |||
130 | abstract protected function _addForeignKey($details); |
||
131 | |||
132 | abstract protected function _dropForeignKey($details); |
||
133 | |||
134 | abstract protected function _addIndex($details); |
||
135 | |||
136 | abstract protected function _dropIndex($details); |
||
137 | |||
138 | abstract protected function _addView($details); |
||
139 | |||
140 | abstract protected function _dropView($details); |
||
141 | |||
142 | abstract protected function _changeViewDefinition($details); |
||
143 | |||
144 | protected function _changeForeignKeyOnDelete($details) |
||
149 | |||
150 | protected function _changeForeignKeyOnUpdate($details) |
||
155 | |||
156 | protected function _executeQuery($details) |
||
160 | |||
161 | protected function _reverseQuery($details) |
||
165 | |||
166 | abstract public function convertTypes($type, $direction, $length); |
||
167 | |||
168 | /** |
||
169 | * |
||
170 | * @return SchemaDescription |
||
171 | */ |
||
172 | 33 | public function getDescription() |
|
173 | { |
||
174 | 33 | if (!is_object($this->schemaDescription)) { |
|
175 | 33 | $this->schemaDescription = SchemaDescription::wrap($this->connection->describe(), $this); |
|
176 | } |
||
177 | 33 | return $this->schemaDescription; |
|
178 | } |
||
179 | |||
180 | 8 | public function setVersion($version) |
|
181 | { |
||
182 | 8 | $this->query('INSERT INTO yentu_history(version) values (?)', array($version)); |
|
183 | 8 | } |
|
184 | |||
185 | public function getVersion() |
||
190 | |||
191 | 6 | public function getLastSession() |
|
192 | { |
||
193 | 6 | $session = $this->query("SELECT session FROM yentu_history ORDER BY id DESC LIMIT 1"); |
|
194 | 6 | return isset($session[0]['session']) ? $session[0]['session'] : null; |
|
195 | } |
||
196 | |||
197 | 3 | public function getSessionVersions($session) |
|
198 | { |
||
199 | 3 | $sessionVersions = array(); |
|
200 | 3 | $versions = $this->query( |
|
201 | 3 | "SELECT DISTINCT version FROM yentu_history WHERE session = ?", array($session) |
|
202 | ); |
||
210 | |||
211 | 27 | public function createHistory() |
|
231 | |||
232 | 10 | public function __clone() |
|
238 | |||
239 | } |
||
240 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.