Completed
Branch feature/pre-split (b5c37f)
by Anton
03:43
created

MySQLHandler::createStatement()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * components
4
 *
5
 * @author    Wolfy-J
6
 */
7
namespace Spiral\Database\Drivers\MySQL;
8
9
use Spiral\Database\Drivers\MySQL\Schemas\MySQLTable;
10
use Spiral\Database\Entities\AbstractHandler;
11
use Spiral\Database\Exceptions\SchemaException;
12
use Spiral\Database\Schemas\Prototypes\AbstractColumn;
13
use Spiral\Database\Schemas\Prototypes\AbstractIndex;
14
use Spiral\Database\Schemas\Prototypes\AbstractReference;
15
use Spiral\Database\Schemas\Prototypes\AbstractTable;
16
17
class MySQLHandler extends AbstractHandler
18
{
19
    /**
20
     * {@inheritdoc}
21
     */
22
    public function alterColumn(
23
        AbstractTable $table,
24
        AbstractColumn $initial,
25
        AbstractColumn $column
26
    ) {
27
        $this->run("ALTER TABLE {$this->identify($table)} CHANGE {$this->identify($column)} {$column->sqlStatement($this->driver)}");
28
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function dropIndex(AbstractTable $table, AbstractIndex $index)
34
    {
35
        $this->run("DROP INDEX {$this->identify($index)} ON {$this->identify($table)}");
36
37
        return $this;
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function alterIndex(AbstractTable $table, AbstractIndex $initial, AbstractIndex $index)
44
    {
45
        $this->run("ALTER TABLE {$this->identify($table)} DROP INDEX  {$this->identify($index)}, ADD {$index->sqlStatement($this->driver, false)}");
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function dropForeign(AbstractTable $table, AbstractReference $foreign)
52
    {
53
        $this->run("ALTER TABLE {$this->identify($table)} DROP FOREIGN KEY {$this->identify($foreign)}");
54
    }
55
56
    /**
57
     * Get statement needed to create table.
58
     *
59
     * @param AbstractTable $table
60
     *
61
     * @return string
62
     *
63
     * @throws SchemaException
64
     */
65
    protected function createStatement(AbstractTable $table)
66
    {
67
        if (!$table instanceof MySQLTable) {
68
            throw new SchemaException('MySQLHandler can process only MySQL tables');
69
        }
70
71
        return parent::createStatement($table) . " ENGINE {$table->getEngine()}";
72
    }
73
}