|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace CodexShaper\DBM\Database\Schema; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Schema\Index as DoctrineIndex; |
|
6
|
|
|
|
|
7
|
|
|
class Index |
|
8
|
|
|
{ |
|
9
|
|
|
const PRIMARY = 'PRIMARY'; |
|
10
|
|
|
const UNIQUE = 'UNIQUE'; |
|
11
|
|
|
const INDEX = 'INDEX'; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Create new index. |
|
15
|
|
|
* |
|
16
|
|
|
* @param array $index |
|
17
|
|
|
* |
|
18
|
|
|
* @return \Doctrine\DBAL\Schema\Index |
|
19
|
|
|
*/ |
|
20
|
|
|
public static function create($index) |
|
21
|
|
|
{ |
|
22
|
|
|
$indexColumns = $index['columns']; |
|
23
|
|
|
if (! is_array($indexColumns)) { |
|
24
|
|
|
$indexColumns = [$indexColumns]; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
if (isset($index['type'])) { |
|
28
|
|
|
$type = $index['type']; |
|
29
|
|
|
|
|
30
|
|
|
$isPrimary = ($type == static::PRIMARY); |
|
31
|
|
|
$isUnique = $isPrimary || ($type == static::UNIQUE); |
|
32
|
|
|
} else { |
|
33
|
|
|
$isPrimary = $index['isPrimary']; |
|
34
|
|
|
$isUnique = $index['isUnique']; |
|
35
|
|
|
|
|
36
|
|
|
// Set the type |
|
37
|
|
|
if ($isPrimary) { |
|
38
|
|
|
$type = static::PRIMARY; |
|
39
|
|
|
} elseif ($isUnique) { |
|
40
|
|
|
$type = static::UNIQUE; |
|
41
|
|
|
} else { |
|
42
|
|
|
$type = static::INDEX; |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
// Set the name |
|
47
|
|
|
$indexName = trim($index['name'] ?? ''); |
|
48
|
|
|
if (empty($indexName)) { |
|
49
|
|
|
$table = $index['table'] ?? null; |
|
50
|
|
|
$indexName = static::createName($indexColumns, $type, $table); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
$flags = $index['flags'] ?? []; |
|
54
|
|
|
$options = $index['options'] ?? []; |
|
55
|
|
|
|
|
56
|
|
|
return new DoctrineIndex($indexName, $indexColumns, $isUnique, $isPrimary, $flags, $options); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Get index name. |
|
61
|
|
|
* |
|
62
|
|
|
* @param array $columns |
|
63
|
|
|
* @param string $type |
|
64
|
|
|
* @param string|null $table |
|
65
|
|
|
* |
|
66
|
|
|
* @return string |
|
67
|
|
|
*/ |
|
68
|
|
|
public static function createName($columns, $type, $table = null) |
|
69
|
|
|
{ |
|
70
|
|
|
$table = isset($table) ? trim($table).'_' : ''; |
|
71
|
|
|
$type = trim($type); |
|
72
|
|
|
$name = strtolower($table.implode('_', $columns).'_'.$type); |
|
73
|
|
|
|
|
74
|
|
|
return str_replace(['-', '.'], '_', $name); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Get all indexes as an array. |
|
79
|
|
|
* |
|
80
|
|
|
* @return array |
|
81
|
|
|
*/ |
|
82
|
|
|
public static function toArray(DoctrineIndex $index) |
|
83
|
|
|
{ |
|
84
|
|
|
return [ |
|
85
|
|
|
'name' => $index->getName(), |
|
86
|
|
|
'oldName' => $index->getName(), |
|
87
|
|
|
'columns' => $index->getColumns(), |
|
88
|
|
|
'type' => static::getType($index), |
|
89
|
|
|
'isPrimary' => $index->isPrimary(), |
|
90
|
|
|
'isUnique' => $index->isUnique(), |
|
91
|
|
|
'isComposite' => (count($index->getColumns()) > 1) ? true : false, |
|
92
|
|
|
'flags' => $index->getFlags(), |
|
93
|
|
|
'options' => $index->getOptions(), |
|
94
|
|
|
]; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Get index type. |
|
99
|
|
|
* |
|
100
|
|
|
* @return string |
|
101
|
|
|
*/ |
|
102
|
|
|
public static function getType(DoctrineIndex $index) |
|
103
|
|
|
{ |
|
104
|
|
|
if ($index->isPrimary()) { |
|
105
|
|
|
return static::PRIMARY; |
|
106
|
|
|
} elseif ($index->isUnique() && ! $index->isPrimary()) { |
|
107
|
|
|
return static::UNIQUE; |
|
108
|
|
|
} elseif ($index->isSimpleIndex()) { |
|
109
|
|
|
return static::INDEX; |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|