Completed
Branch feature/pre-split (1fb89d)
by Anton
03:12
created

AbstractIndex::getName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
/**
3
 * components
4
 *
5
 * @author    Wolfy-J
6
 */
7
namespace Spiral\Database\Schemas\Prototypes;
8
9
use Spiral\Database\Entities\Driver;
10
use Spiral\Database\Schemas\IndexInterface;
11
12
/**
13
 * Abstract index schema with read (see IndexInterface) and write abilities. Must be implemented
14
 * by driver to support DBMS specific syntax and creation rules.
15
 */
16
abstract class AbstractIndex extends AbstractElement implements IndexInterface
17
{
18
    /**
19
     * Index types.
20
     */
21
    const NORMAL = 'INDEX';
22
    const UNIQUE = 'UNIQUE';
23
24
    /**
25
     * Index type, by default NORMAL and UNIQUE indexes supported, additional types can be
26
     * implemented on database driver level.
27
     *
28
     * @var string
29
     */
30
    protected $type = self::NORMAL;
31
32
    /**
33
     * Columns used to form index.
34
     *
35
     * @var array
36
     */
37
    protected $columns = [];
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function getName(): string
43
    {
44
        if (empty($this->name)) {
45
            //Let's generate index name on a fly
46
            $this->setName($this->generateName());
47
        }
48
49
        return parent::getName();
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function isUnique(): bool
56
    {
57
        return $this->type == self::UNIQUE;
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function getColumns(): array
64
    {
65
        return $this->columns;
66
    }
67
68
    //--MODIFICATIONS
69
70
    /**
71
     * Index sql creation syntax.
72
     *
73
     * @param Driver $driver
74
     * @param bool   $includeTable Include table ON statement (not required for inline index
75
     *                             creation).
76
     *
77
     * @return string
78
     */
79
    public function sqlStatement(Driver $driver, bool $includeTable = true): string
80
    {
81
        $statement = [$this->type == self::UNIQUE ? 'UNIQUE INDEX' : 'INDEX'];
82
83
        $statement[] = $driver->identifier($this->name);
84
85
        if ($includeTable) {
86
            $statement[] = "ON {$driver->identifier($this->table)}";
87
        }
88
89
        //Wrapping column names
90
        $columns = implode(', ', array_map([$driver, 'identifier'], $this->columns));
91
92
        $statement[] = "({$columns})";
93
94
        return implode(' ', $statement);
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100
    public function compare(IndexInterface $initial): bool
101
    {
102
        return $this == clone $initial;
103
    }
104
105
    /**
106
     * Generate unique index name.
107
     *
108
     * @return string
109
     */
110
    protected function generateName(): string
111
    {
112
        //We can generate name
113
        $name = $this->table . '_index_' . implode('_', $this->columns) . '_' . uniqid();
114
115
        if (strlen($name) > 64) {
116
            //Many dbs has limitations on identifier length
117
            $name = md5($name);
118
        }
119
120
        return $name;
121
    }
122
}