Completed
Branch feature/pre-split (42159e)
by Anton
05:36
created

MySQLHandler   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 0
loc 74
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 6

6 Methods

Rating   Name   Duplication   Size   Complexity  
A alterColumn() 0 7 1
A dropIndex() 0 6 1
A alterIndex() 0 4 1
A dropForeign() 0 4 1
A createStatement() 0 8 2
A assertValid() 0 11 4
1
<?php
2
/**
3
 * Spiral Framework, Core 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\Drivers\MySQLDriverException;
12
use Spiral\Database\Exceptions\SchemaException;
13
use Spiral\Database\Schemas\Prototypes\AbstractColumn;
14
use Spiral\Database\Schemas\Prototypes\AbstractIndex;
15
use Spiral\Database\Schemas\Prototypes\AbstractReference;
16
use Spiral\Database\Schemas\Prototypes\AbstractTable;
17
18
class MySQLHandler extends AbstractHandler
19
{
20
    /**
21
     * {@inheritdoc}
22
     */
23
    public function alterColumn(
24
        AbstractTable $table,
25
        AbstractColumn $initial,
26
        AbstractColumn $column
27
    ) {
28
        $this->run("ALTER TABLE {$this->identify($table)} CHANGE {$this->identify($initial)} {$column->sqlStatement($this->driver)}");
29
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34
    public function dropIndex(AbstractTable $table, AbstractIndex $index)
35
    {
36
        $this->run("DROP INDEX {$this->identify($index)} ON {$this->identify($table)}");
37
38
        return $this;
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function alterIndex(AbstractTable $table, AbstractIndex $initial, AbstractIndex $index)
45
    {
46
        $this->run("ALTER TABLE {$this->identify($table)} DROP INDEX  {$this->identify($index)}, ADD {$index->sqlStatement($this->driver, false)}");
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function dropForeign(AbstractTable $table, AbstractReference $foreign)
53
    {
54
        $this->run("ALTER TABLE {$this->identify($table)} DROP FOREIGN KEY {$this->identify($foreign)}");
55
    }
56
57
    /**
58
     * Get statement needed to create table.
59
     *
60
     * @param AbstractTable $table
61
     *
62
     * @return string
63
     *
64
     * @throws SchemaException
65
     */
66
    protected function createStatement(AbstractTable $table)
67
    {
68
        if (!$table instanceof MySQLTable) {
69
            throw new SchemaException('MySQLHandler can process only MySQL tables');
70
        }
71
72
        return parent::createStatement($table) . " ENGINE {$table->getEngine()}";
73
    }
74
75
    /**
76
     * @param AbstractColumn $column
77
     *
78
     * @throws MySQLDriverException
79
     */
80
    protected function assertValid(AbstractColumn $column)
81
    {
82
        if (
83
            in_array($column->abstractType(), ['text', 'tinyText', 'longText'])
84
            && is_string($column->getDefaultValue()) && $column->getDefaultValue() !== ''
85
        ) {
86
            throw new MySQLDriverException(
87
                "Column {$column} of type text/blob can not have non empty default value"
88
            );
89
        }
90
    }
91
}