|
1
|
|
|
<?php namespace Xethron\MigrationsGenerator\Generators; |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
class IndexGenerator { |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* @var array |
|
8
|
|
|
*/ |
|
9
|
|
|
protected $indexes; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @var array |
|
13
|
|
|
*/ |
|
14
|
|
|
protected $multiFieldIndexes; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var bool |
|
18
|
|
|
*/ |
|
19
|
|
|
private $ignoreIndexNames; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @param string $table Table Name |
|
23
|
|
|
* @param \Doctrine\DBAL\Schema\AbstractSchemaManager $schema |
|
24
|
|
|
* @param bool $ignoreIndexNames |
|
25
|
|
|
*/ |
|
26
|
|
|
public function __construct($table, $schema, $ignoreIndexNames) |
|
27
|
|
|
{ |
|
28
|
|
|
$this->indexes = array(); |
|
29
|
|
|
$this->multiFieldIndexes = array(); |
|
30
|
|
|
$this->ignoreIndexNames = $ignoreIndexNames; |
|
31
|
|
|
|
|
32
|
|
|
$indexes = $schema->listTableIndexes( $table ); |
|
33
|
|
|
|
|
34
|
|
|
foreach ( $indexes as $index ) { |
|
35
|
|
|
$indexArray = $this->indexToArray($table, $index); |
|
36
|
|
|
if ( count( $indexArray['columns'] ) == 1 ) { |
|
37
|
|
|
$columnName = $indexArray['columns'][0]; |
|
38
|
|
|
$this->indexes[ $columnName ] = (object) $indexArray; |
|
39
|
|
|
} else { |
|
40
|
|
|
$this->multiFieldIndexes[] = (object) $indexArray; |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param string $table |
|
48
|
|
|
* @param \Doctrine\DBAL\Schema\Index $index |
|
49
|
|
|
* @return array |
|
50
|
|
|
*/ |
|
51
|
|
|
protected function indexToArray($table, $index) |
|
52
|
|
|
{ |
|
53
|
|
|
if ( $index->isPrimary() ) { |
|
54
|
|
|
$type = 'primary'; |
|
55
|
|
|
} elseif ( $index->isUnique() ) { |
|
56
|
|
|
$type = 'unique'; |
|
57
|
|
|
} else { |
|
58
|
|
|
$type = 'index'; |
|
59
|
|
|
} |
|
60
|
|
|
$array = ['type' => $type, 'name' => null, 'columns' => $index->getColumns()]; |
|
61
|
|
|
|
|
62
|
|
|
if ( ! $this->ignoreIndexNames and ! $this->isDefaultIndexName($table, $index->getName(), $type, $index->getColumns())) { |
|
63
|
|
|
// Sent Index name to exclude spaces |
|
64
|
|
|
$array['name'] = str_replace(' ', '', $index->getName()); |
|
65
|
|
|
} |
|
66
|
|
|
return $array; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param string $table Table Name |
|
71
|
|
|
* @param string $type Index Type |
|
72
|
|
|
* @param string|array $columns Column Names |
|
73
|
|
|
* @return string |
|
74
|
|
|
*/ |
|
75
|
|
|
protected function getDefaultIndexName( $table, $type, $columns ) |
|
76
|
|
|
{ |
|
77
|
|
|
if ($type=='primary') { |
|
78
|
|
|
return 'PRIMARY'; |
|
79
|
|
|
} |
|
80
|
|
|
if ( is_array( $columns ) ) { |
|
81
|
|
|
$columns = implode( '_', $columns ); |
|
82
|
|
|
} |
|
83
|
|
|
return $table .'_'. $columns .'_'. $type; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @param string $table Table Name |
|
88
|
|
|
* @param string $name Current Name |
|
89
|
|
|
* @param string $type Index Type |
|
90
|
|
|
* @param string|array $columns Column Names |
|
91
|
|
|
* @return bool |
|
92
|
|
|
*/ |
|
93
|
|
|
protected function isDefaultIndexName( $table, $name, $type, $columns ) |
|
94
|
|
|
{ |
|
95
|
|
|
return $name == $this->getDefaultIndexName( $table, $type, $columns ); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @param string $name |
|
101
|
|
|
* @return null|object |
|
102
|
|
|
*/ |
|
103
|
|
|
public function getIndex($name) |
|
104
|
|
|
{ |
|
105
|
|
|
if ( isset( $this->indexes[ $name ] ) ) { |
|
106
|
|
|
return (object) $this->indexes[ $name ]; |
|
107
|
|
|
} |
|
108
|
|
|
return null; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @return null|object |
|
113
|
|
|
*/ |
|
114
|
|
|
public function getMultiFieldIndexes() |
|
115
|
|
|
{ |
|
116
|
|
|
return $this->multiFieldIndexes; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
} |
|
120
|
|
|
|