1 | <?php |
||
13 | abstract class AbstractDatabaseManipulator |
||
14 | { |
||
15 | |||
16 | const CONVERT_TO_DRIVER = 'driver'; |
||
17 | const CONVERT_TO_YENTU = 'yentu'; |
||
18 | |||
19 | private $schemaDescription; |
||
20 | private $assertor; |
||
21 | private $connection; |
||
22 | private $dumpQuery; |
||
23 | private $disableQuery; |
||
24 | protected $defaultSchema; |
||
25 | private $io; |
||
26 | |||
27 | /** |
||
28 | * AbstractDatabaseManipulator constructor. |
||
29 | * @param DriverFactory $driverFactory |
||
30 | * @param Io $io |
||
31 | * @throws \ntentan\atiaa\exceptions\ConnectionException |
||
32 | */ |
||
33 | public function __construct(DriverFactory $driverFactory, Io $io) |
||
34 | { |
||
35 | $this->connection = $driverFactory->createDriver(); |
||
36 | $this->connection->connect(); |
||
37 | $this->io = $io; |
||
38 | } |
||
39 | |||
40 | public function __get($name) |
||
41 | { |
||
42 | if ($name === 'description') { |
||
43 | return $this->getDescription(); |
||
44 | } |
||
45 | } |
||
46 | |||
47 | public function setDumpQuery($dumpQuery) |
||
48 | { |
||
49 | $this->dumpQuery = $dumpQuery; |
||
50 | } |
||
51 | |||
52 | public function setDisableQuery($disableQuery) |
||
53 | { |
||
54 | $this->disableQuery = $disableQuery; |
||
55 | } |
||
56 | |||
57 | public function __call($name, $arguments) |
||
|
|||
58 | { |
||
59 | if (preg_match("/^(add|drop|change|executeQuery|reverseQuery)/", $name)) { |
||
60 | $details = Parameters::wrap($arguments[0]); |
||
61 | $this->description->$name($details); |
||
62 | $name = "_$name"; |
||
63 | new \ReflectionMethod($this, $name); |
||
64 | return $this->$name($details); |
||
65 | } else { |
||
66 | throw new \Exception("Failed to execute method '$name'"); |
||
67 | } |
||
68 | } |
||
69 | |||
70 | public function query($query, $bind = false) |
||
71 | { |
||
72 | try { |
||
73 | if ($this->dumpQuery) { |
||
74 | echo "$query\n"; |
||
75 | } |
||
76 | |||
77 | $this->io->output("\n > Running Query [$query]", Io::OUTPUT_LEVEL_3); |
||
78 | |||
79 | if ($this->disableQuery !== true) { |
||
80 | return $this->connection->query($query, $bind); |
||
81 | } |
||
82 | } catch (\ntentan\atiaa\exceptions\DatabaseDriverException $e) { |
||
83 | throw new DatabaseManipulatorException($e->getMessage()); |
||
84 | } |
||
85 | } |
||
86 | |||
87 | public function disconnect() |
||
88 | { |
||
89 | $this->connection->disconnect(); |
||
90 | } |
||
91 | |||
92 | public function getDefaultSchema() |
||
96 | |||
97 | public function getAssertor() |
||
98 | { |
||
99 | if (!is_object($this->assertor)) { |
||
100 | $this->assertor = new DatabaseAssertor($this->description); |
||
101 | } |
||
102 | return $this->assertor; |
||
103 | } |
||
104 | |||
105 | abstract protected function _addSchema($name); |
||
106 | |||
107 | abstract protected function _dropSchema($name); |
||
108 | |||
109 | abstract protected function _addTable($details); |
||
110 | |||
111 | abstract protected function _dropTable($details); |
||
112 | |||
113 | abstract protected function _changeTableName($details); |
||
114 | |||
115 | abstract protected function _addColumn($details); |
||
116 | |||
117 | abstract protected function _changeColumnNulls($details); |
||
118 | |||
119 | abstract protected function _changeColumnName($details); |
||
120 | |||
121 | abstract protected function _changeColumnDefault($details); |
||
122 | |||
123 | abstract protected function _dropColumn($details); |
||
124 | |||
125 | abstract protected function _addPrimaryKey($details); |
||
126 | |||
127 | abstract protected function _dropPrimaryKey($details); |
||
128 | |||
129 | abstract protected function _addUniqueKey($details); |
||
130 | |||
131 | abstract protected function _dropUniqueKey($details); |
||
132 | |||
133 | abstract protected function _addAutoPrimaryKey($details); |
||
134 | |||
135 | abstract protected function _dropAutoPrimaryKey($details); |
||
136 | |||
137 | abstract protected function _addForeignKey($details); |
||
138 | |||
139 | abstract protected function _dropForeignKey($details); |
||
140 | |||
141 | abstract protected function _addIndex($details); |
||
142 | |||
143 | abstract protected function _dropIndex($details); |
||
144 | |||
145 | abstract protected function _addView($details); |
||
146 | |||
147 | abstract protected function _dropView($details); |
||
148 | |||
149 | abstract protected function _changeViewDefinition($details); |
||
150 | |||
151 | protected function _changeForeignKeyOnDelete($details) |
||
156 | |||
157 | protected function _changeForeignKeyOnUpdate($details) |
||
162 | |||
163 | protected function _executeQuery($details) |
||
167 | |||
168 | protected function _reverseQuery($details) |
||
172 | |||
173 | abstract public function convertTypes($type, $direction, $length); |
||
174 | |||
175 | /** |
||
176 | * |
||
177 | * @return SchemaDescription |
||
178 | */ |
||
179 | public function getDescription() |
||
180 | { |
||
181 | if (!is_object($this->schemaDescription)) { |
||
182 | $this->schemaDescription = SchemaDescription::wrap($this->connection->describe(), $this); |
||
183 | } |
||
184 | return $this->schemaDescription; |
||
185 | } |
||
186 | |||
187 | public function setVersion($version) |
||
188 | { |
||
189 | $this->query('INSERT INTO yentu_history(version) values (?)', array($version)); |
||
190 | } |
||
191 | |||
192 | public function getVersion() |
||
197 | |||
198 | public function getLastSession() |
||
199 | { |
||
200 | $session = $this->query("SELECT session FROM yentu_history ORDER BY id DESC LIMIT 1"); |
||
201 | return isset($session[0]['session']) ? $session[0]['session'] : null; |
||
202 | } |
||
203 | |||
204 | public function getSessionVersions($session) |
||
205 | { |
||
206 | $sessionVersions = array(); |
||
207 | $versions = $this->query( |
||
208 | "SELECT DISTINCT version FROM yentu_history WHERE session = ?", array($session) |
||
217 | |||
218 | public function createHistory() |
||
238 | |||
239 | public function __clone() |
||
245 | |||
246 | } |
||
247 |
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@return
annotation as described here.