Completed
Branch feature/pre-split (5afa53)
by Anton
13:51
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 isUnique(): bool
43
    {
44
        return $this->type == self::UNIQUE;
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function getColumns(): array
51
    {
52
        return $this->columns;
53
    }
54
55
    /**
56
     * Declare index type and behaviour to unique/non-unique state.
57
     *
58
     * @param bool $unique
59
     *
60
     * @return self
61
     */
62
    public function unique(bool $unique = true): AbstractIndex
63
    {
64
        $this->type = $unique ? self::UNIQUE : self::NORMAL;
65
66
        return $this;
67
    }
68
69
    /**
70
     * Change set of index forming columns. Method must support both array and string parameters.
71
     *
72
     * Example:
73
     * $index->columns('key');
74
     * $index->columns('key', 'key2');
75
     * $index->columns(['key', 'key2']);
76
     *
77
     * @param string|array $columns Columns array or comma separated list of parameters.
78
     *
79
     * @return self
80
     */
81
    public function columns($columns): AbstractIndex
82
    {
83
        if (!is_array($columns)) {
84
            $columns = func_get_args();
85
        }
86
87
        $this->columns = $columns;
88
89
        return $this;
90
    }
91
92
    /**
93
     * Index sql creation syntax.
94
     *
95
     * @param Driver $driver
96
     * @param bool   $includeTable Include table ON statement (not required for inline index
97
     *                             creation).
98
     *
99
     * @return string
100
     */
101
    public function sqlStatement(Driver $driver, bool $includeTable = true): string
102
    {
103
        $statement = [$this->type == self::UNIQUE ? 'UNIQUE INDEX' : 'INDEX'];
104
105
        $statement[] = $driver->identifier($this->name);
106
107
        if ($includeTable) {
108
            $statement[] = "ON {$driver->identifier($this->table)}";
109
        }
110
111
        //Wrapping column names
112
        $columns = implode(', ', array_map([$driver, 'identifier'], $this->columns));
113
114
        $statement[] = "({$columns})";
115
116
        return implode(' ', $statement);
117
    }
118
119
    /**
120
     * {@inheritdoc}
121
     */
122
    public function compare(IndexInterface $initial): bool
123
    {
124
        return $this == clone $initial;
125
    }
126
}