1 | <?php |
||
40 | abstract class PdoAdapter extends AbstractAdapter |
||
41 | { |
||
42 | /** |
||
43 | * @var \PDO|null |
||
44 | */ |
||
45 | protected $connection; |
||
46 | |||
47 | /** |
||
48 | * {@inheritdoc} |
||
49 | */ |
||
50 | public function setOptions(array $options) |
||
51 | 287 | { |
|
52 | parent::setOptions($options); |
||
53 | 287 | ||
54 | if (isset($options['connection'])) { |
||
55 | 287 | $this->setConnection($options['connection']); |
|
56 | 3 | } |
|
57 | 3 | ||
58 | return $this; |
||
59 | 287 | } |
|
60 | |||
61 | /** |
||
62 | * Sets the database connection. |
||
63 | * |
||
64 | * @param \PDO $connection Connection |
||
65 | * @return \Phinx\Db\Adapter\AdapterInterface |
||
66 | */ |
||
67 | public function setConnection(\PDO $connection) |
||
68 | 193 | { |
|
69 | $this->connection = $connection; |
||
70 | 193 | ||
71 | // Create the schema table if it doesn't already exist |
||
72 | if (!$this->hasSchemaTable()) { |
||
73 | 193 | $this->createSchemaTable(); |
|
74 | 191 | } else { |
|
75 | 191 | $table = new Table($this->getSchemaTableName(), [], $this); |
|
76 | 74 | if (!$table->hasColumn('migration_name')) { |
|
77 | 74 | $table |
|
78 | ->addColumn( |
||
79 | 'migration_name', |
||
80 | 'string', |
||
81 | ['limit' => 100, 'after' => 'version', 'default' => null, 'null' => true] |
||
82 | ) |
||
83 | ->save(); |
||
84 | } |
||
85 | if (!$table->hasColumn('breakpoint')) { |
||
86 | 74 | $table |
|
87 | ->addColumn('breakpoint', 'boolean', ['default' => false]) |
||
88 | ->save(); |
||
89 | } |
||
90 | } |
||
91 | |||
92 | return $this; |
||
93 | 193 | } |
|
94 | |||
95 | /** |
||
96 | * Gets the database connection |
||
97 | * |
||
98 | * @return \PDO |
||
99 | */ |
||
100 | public function getConnection() |
||
101 | 191 | { |
|
102 | if ($this->connection === null) { |
||
103 | 191 | $this->connect(); |
|
104 | 189 | } |
|
105 | 189 | ||
106 | 191 | return $this->connection; |
|
107 | } |
||
108 | |||
109 | /** |
||
110 | * {@inheritdoc} |
||
111 | */ |
||
112 | 1 | public function connect() |
|
115 | |||
116 | /** |
||
117 | * {@inheritdoc} |
||
118 | */ |
||
119 | public function disconnect() |
||
122 | |||
123 | /** |
||
124 | * {@inheritdoc} |
||
125 | */ |
||
126 | 218 | public function execute($sql) |
|
136 | |||
137 | /** |
||
138 | * Executes a query and returns PDOStatement. |
||
139 | * |
||
140 | * @param string $sql SQL |
||
141 | * @return \PDOStatement |
||
142 | 220 | */ |
|
143 | public function query($sql) |
||
147 | |||
148 | /** |
||
149 | * {@inheritdoc} |
||
150 | 151 | */ |
|
151 | public function fetchRow($sql) |
||
157 | |||
158 | /** |
||
159 | 213 | * {@inheritdoc} |
|
160 | */ |
||
161 | 213 | public function fetchAll($sql) |
|
171 | |||
172 | 1 | /** |
|
173 | * {@inheritdoc} |
||
174 | 1 | */ |
|
175 | 1 | public function insert(Table $table, $row) |
|
189 | |||
190 | 11 | /** |
|
191 | * {@inheritdoc} |
||
192 | 11 | */ |
|
193 | 11 | public function bulkinsert(Table $table, $rows) |
|
221 | |||
222 | 5 | /** |
|
223 | * {@inheritdoc} |
||
224 | 5 | */ |
|
225 | public function getVersions() |
||
231 | |||
232 | 8 | /** |
|
233 | * {@inheritdoc} |
||
234 | 8 | */ |
|
235 | public function getVersionLog() |
||
236 | 8 | { |
|
237 | 8 | $result = []; |
|
238 | 6 | ||
239 | 6 | switch ($this->options['version_order']) { |
|
240 | 2 | case \Phinx\Config\Config::VERSION_ORDER_CREATION_TIME: |
|
241 | 1 | $orderBy = 'version ASC'; |
|
242 | 1 | break; |
|
243 | 1 | case \Phinx\Config\Config::VERSION_ORDER_EXECUTION_TIME: |
|
244 | 1 | $orderBy = 'start_time ASC, version ASC'; |
|
245 | 8 | break; |
|
246 | default: |
||
247 | 7 | throw new \RuntimeException('Invalid version_order configuration option'); |
|
248 | 7 | } |
|
249 | 7 | ||
250 | 7 | $rows = $this->fetchAll(sprintf('SELECT * FROM %s ORDER BY %s', $this->getSchemaTableName(), $orderBy)); |
|
251 | foreach ($rows as $version) { |
||
252 | 7 | $result[$version['version']] = $version; |
|
253 | } |
||
254 | |||
255 | return $result; |
||
256 | } |
||
257 | |||
258 | 5 | /** |
|
259 | * {@inheritdoc} |
||
260 | 5 | */ |
|
261 | public function migrated(MigrationInterface $migration, $direction, $startTime, $endTime) |
||
295 | |||
296 | 1 | /** |
|
297 | * @inheritDoc |
||
298 | 1 | */ |
|
299 | 1 | public function toggleBreakpoint(MigrationInterface $migration) |
|
300 | 1 | { |
|
301 | 1 | $this->query( |
|
302 | 1 | sprintf( |
|
303 | 1 | 'UPDATE %1$s SET %2$s = CASE %2$s WHEN %3$s THEN %4$s ELSE %3$s END, %7$s = %7$s WHERE %5$s = \'%6$s\';', |
|
304 | 1 | $this->getSchemaTableName(), |
|
305 | 1 | $this->quoteColumnName('breakpoint'), |
|
306 | 1 | $this->castToBool(true), |
|
307 | 1 | $this->castToBool(false), |
|
308 | 1 | $this->quoteColumnName('version'), |
|
309 | 1 | $migration->getVersion(), |
|
310 | $this->quoteColumnName('start_time') |
||
311 | 1 | ) |
|
312 | ); |
||
313 | |||
314 | return $this; |
||
315 | } |
||
316 | |||
317 | 1 | /** |
|
318 | * @inheritDoc |
||
319 | 1 | */ |
|
320 | 1 | public function resetAllBreakpoints() |
|
321 | 1 | { |
|
322 | 1 | return $this->execute( |
|
323 | 1 | sprintf( |
|
324 | 1 | 'UPDATE %1$s SET %2$s = %3$s, %4$s = %4$s WHERE %2$s <> %3$s;', |
|
325 | 1 | $this->getSchemaTableName(), |
|
326 | 1 | $this->quoteColumnName('breakpoint'), |
|
327 | 1 | $this->castToBool(false), |
|
328 | $this->quoteColumnName('start_time') |
||
329 | ) |
||
330 | ); |
||
331 | } |
||
332 | |||
333 | /** |
||
334 | * {@inheritdoc} |
||
335 | */ |
||
336 | public function createSchema($schemaName = 'public') |
||
340 | |||
341 | /** |
||
342 | * {@inheritdoc} |
||
343 | */ |
||
344 | public function dropSchema($name) |
||
348 | |||
349 | 208 | /** |
|
350 | * {@inheritdoc} |
||
351 | */ |
||
352 | 208 | public function getColumnTypes() |
|
378 | |||
379 | 121 | /** |
|
380 | * {@inheritdoc} |
||
381 | 121 | */ |
|
382 | public function castToBool($value) |
||
383 | { |
||
384 | return (bool)$value ? 1 : 0; |
||
385 | } |
||
386 | |||
387 | /** |
||
388 | * Get the defintion for a `DEFAULT` statement. |
||
389 | * |
||
390 | * @param mixed $default Default value |
||
391 | * @return string |
||
392 | */ |
||
393 | protected function getDefaultValueDefinition($default) |
||
403 | } |
||
404 |