Completed
Branch feature/pre-split (76ded7)
by Anton
03:22
created

TableSchema   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

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

7 Methods

Rating   Name   Duplication   Size   Complexity  
A initSchema() 0 9 1
A setEngine() 0 10 2
A getEngine() 0 4 1
A fetchColumns() 0 11 2
A fetchIndexes() 0 4 1
A fetchReferences() 0 4 1
A fetchPrimaryKeys() 0 4 1
1
<?php
2
/**
3
 * components
4
 *
5
 * @author    Wolfy-J
6
 */
7
namespace Spiral\Database\Drivers\MySQL\Schemas;
8
9
use Spiral\Database\Exceptions\SchemaException;
10
use Spiral\Database\Schemas\Prototypes\AbstractTable;
11
use Spiral\Database\Schemas\TableState;
12
13
class TableSchema extends AbstractTable
14
{
15
    /**
16
     * List of most common MySQL table engines.
17
     */
18
    const ENGINE_INNODB = 'InnoDB';
19
    const ENGINE_MYISAM = 'MyISAM';
20
    const ENGINE_MEMORY = 'Memory';
21
22
    /**
23
     * MySQL table engine.
24
     *
25
     * @var string
26
     */
27
    private $engine = self::ENGINE_INNODB;
28
29
    /**
30
     * Populate table schema with values from database.
31
     *
32
     * @param TableState $state
33
     */
34
    protected function initSchema(TableState $state)
35
    {
36
        parent::initSchema($state);
37
38
        //Reading table schema
39
        $this->engine = $this->driver->query('SHOW TABLE STATUS WHERE `Name` = ?', [
40
            $state->getName()
41
        ])->fetch()['Engine'];
42
    }
43
44
    /**
45
     * Change table engine. Such operation will be applied only at moment of table creation.
46
     *
47
     * @param string $engine
48
     *
49
     * @return $this
50
     *
51
     * @throws SchemaException
52
     */
53
    public function setEngine($engine)
54
    {
55
        if ($this->exists()) {
56
            throw new SchemaException('Table engine can be set only at moment of creation');
0 ignored issues
show
Unused Code introduced by
The call to SchemaException::__construct() has too many arguments starting with 'Table engine can be set... at moment of creation'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
57
        }
58
59
        $this->engine = $engine;
60
61
        return $this;
62
    }
63
64
    /**
65
     * @return string
66
     */
67
    public function getEngine()
68
    {
69
        return $this->engine;
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    protected function fetchColumns(): array
76
    {
77
        $query = "SHOW FULL COLUMNS FROM {$this->driver->identifier($this->getName())}";
78
79
        $result = [];
80
        foreach ($this->driver->query($query) as $schema) {
81
            $result[] = ColumnSchema::createInstance($this->getName(), $schema);
82
        }
83
84
        return $result;
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90
    protected function fetchIndexes(): array
91
    {
92
        return [];
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98
    protected function fetchReferences(): array
99
    {
100
        return [];
101
    }
102
103
    /**
104
     * Fetching primary keys from table.
105
     *
106
     * @return array
107
     */
108
    protected function fetchPrimaryKeys(): array
109
    {
110
        return [];
111
    }
112
}