1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LaravelFreelancerNL\Aranguent\Schema\Concerns; |
4
|
|
|
|
5
|
|
|
use ArangoDBClient\Exception; |
6
|
|
|
use Illuminate\Support\Fluent; |
7
|
|
|
|
8
|
|
|
trait Indexes |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Add a new index command to the blueprint. |
12
|
|
|
* |
13
|
|
|
* @param string $type |
14
|
|
|
* @param string|array|null $columns |
15
|
|
|
* @param $name |
16
|
|
|
* @param array $indexOptions |
17
|
|
|
* @return Fluent |
18
|
|
|
*/ |
19
|
|
|
protected function indexCommand($type = '', $columns = null, $name = null, $indexOptions = []) |
20
|
|
|
{ |
21
|
|
|
if ($type == '') { |
22
|
|
|
$type = $this->mapIndexType('persistent'); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
if ($columns === null) { |
26
|
|
|
$columns = end($this->columns); |
|
|
|
|
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
if (is_string($columns)) { |
30
|
|
|
$columns = [$columns]; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
$indexOptions['name'] = $name ?: $this->createIndexName($type, $columns); |
34
|
|
|
|
35
|
|
|
return $this->addCommand('index', compact('type', 'columns', 'indexOptions')); |
|
|
|
|
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Specify an index for the table. |
42
|
|
|
* |
43
|
|
|
* @param string|array $columns |
44
|
|
|
* @param string $name |
45
|
|
|
* @param string|null $algorithm |
46
|
|
|
* @return Fluent |
47
|
|
|
*/ |
48
|
|
|
public function index($columns = null, $name = null, $algorithm = null) |
|
|
|
|
49
|
|
|
{ |
50
|
|
|
$type = $this->mapIndexType($algorithm); |
51
|
|
|
|
52
|
|
|
return $this->indexCommand($type, $columns); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Create a hash index for fast exact matching. |
57
|
|
|
* |
58
|
|
|
* @param null $columns |
59
|
|
|
* @param array $indexOptions |
60
|
|
|
* @return Fluent |
61
|
|
|
*/ |
62
|
|
|
public function hashIndex($columns = null, $indexOptions = []) |
63
|
|
|
{ |
64
|
|
|
return $this->indexCommand('hash', $columns, $indexOptions); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param null|string $column |
69
|
|
|
* @param $name |
70
|
|
|
* @param array $indexOptions |
71
|
|
|
* @return Fluent |
72
|
|
|
*/ |
73
|
|
|
public function fulltextIndex($column = null, $name = null, $indexOptions = []) |
74
|
|
|
{ |
75
|
|
|
return $this->indexCommand('fulltext', $column, $name, $indexOptions); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Specify a spatial index for the table. |
80
|
|
|
* |
81
|
|
|
* @param $columns |
82
|
|
|
* @param null $name |
83
|
|
|
* @param array $indexOptions |
84
|
|
|
* @return Fluent |
85
|
|
|
*/ |
86
|
|
|
public function geoIndex($columns, $name = null, $indexOptions = []) |
87
|
|
|
{ |
88
|
|
|
return $this->indexCommand('geo', $columns, $name, $indexOptions); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Specify a spatial index for the table. |
93
|
|
|
* @param string|array $columns |
94
|
|
|
* @param string $name |
95
|
|
|
* @return Fluent |
96
|
|
|
*/ |
97
|
|
|
public function spatialIndex($columns, $name = null) |
98
|
|
|
{ |
99
|
|
|
return $this->geoIndex($columns, $name); |
|
|
|
|
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param $columns |
104
|
|
|
* @param string|null $name |
105
|
|
|
* @param array $indexOptions |
106
|
|
|
* @return Fluent |
107
|
|
|
*/ |
108
|
|
|
public function skiplistIndex($columns, $name = null, $indexOptions = []) |
109
|
|
|
{ |
110
|
|
|
return $this->indexCommand('skiplist', $columns, $name, $indexOptions); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function persistentIndex($columns, $name = null, $indexOptions = []) |
114
|
|
|
{ |
115
|
|
|
return $this->indexCommand('persistent', $columns, $name, $indexOptions); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Create a TTL index for the table. |
120
|
|
|
* |
121
|
|
|
* @param $columns |
122
|
|
|
* @param null $name |
123
|
|
|
* @param array $indexOptions |
124
|
|
|
* @return Fluent |
125
|
|
|
*/ |
126
|
|
|
public function ttlIndex($columns, $name = null, $indexOptions = []) |
127
|
|
|
{ |
128
|
|
|
return $this->indexCommand('ttl', $columns, $name, $indexOptions); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Specify a unique index for the table. |
133
|
|
|
* |
134
|
|
|
* @param string|array $columns |
135
|
|
|
* @param string $name |
136
|
|
|
* @param string|null $algorithm |
137
|
|
|
* @return Fluent |
138
|
|
|
*/ |
139
|
|
|
public function unique($columns = null, $name = null, $algorithm = null) |
140
|
|
|
{ |
141
|
|
|
$type = $this->mapIndexType($algorithm); |
142
|
|
|
|
143
|
|
|
$indexOptions['unique'] = true; |
|
|
|
|
144
|
|
|
|
145
|
|
|
return $this->indexCommand($type, $columns, $name, $indexOptions); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param $command |
150
|
|
|
* @throws Exception |
151
|
|
|
*/ |
152
|
|
|
public function executeIndexCommand($command) |
153
|
|
|
{ |
154
|
|
|
if ($this->connection->pretending()) { |
155
|
|
|
$this->connection->logQuery('/* '.$command->explanation." */\n", []); |
|
|
|
|
156
|
|
|
|
157
|
|
|
return; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
$options = [ |
161
|
|
|
'type' => $command->type, |
162
|
|
|
'fields' => $command->columns, |
163
|
|
|
'unique' => $command->unique, |
164
|
|
|
'options' => $command->indexOptions |
165
|
|
|
]; |
166
|
|
|
|
167
|
|
|
if (isset($command->indexOptions) && is_array($command->indexOptions)) { |
168
|
|
|
$options = array_merge($options, $command->indexOptions); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
$this->collectionHandler->createIndex($this->table, $options); |
|
|
|
|
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Indicate that the given index should be dropped. |
176
|
|
|
* |
177
|
|
|
* @param $name |
178
|
|
|
* @return Fluent |
179
|
|
|
*/ |
180
|
|
|
public function dropIndex($name) |
181
|
|
|
{ |
182
|
|
|
$parameters['name'] = 'dropIndex'; |
|
|
|
|
183
|
|
|
$parameters['index'] = $name; |
184
|
|
|
$parameters['explanation'] = "Drop the '".$name."' index on the {$this->table} table."; |
185
|
|
|
$parameters['handler'] = 'collection'; |
186
|
|
|
|
187
|
|
|
return $this->addCommand('dropIndex', $parameters); |
|
|
|
|
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Drop the index by first getting all the indexes on the table; then selecting the matching one |
192
|
|
|
* by type and columns. |
193
|
|
|
* @param $command |
194
|
|
|
*/ |
195
|
|
|
public function executeDropIndexCommand($command) |
196
|
|
|
{ |
197
|
|
|
if ($this->connection->pretending()) { |
198
|
|
|
$this->connection->logQuery('/* '.$command->explanation." */\n", []); |
199
|
|
|
return; |
200
|
|
|
} |
201
|
|
|
$this->collectionHandler->dropIndex($this->table, $command->index); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* @param string|null $algorithm |
206
|
|
|
* @return mixed|string |
207
|
|
|
*/ |
208
|
|
|
protected function mapIndexType($algorithm) |
209
|
|
|
{ |
210
|
|
|
$typeConversion = [ |
211
|
|
|
'HASH' => 'hash', |
212
|
|
|
'BTREE' => 'persistent', |
213
|
|
|
'RTREE' => 'geo', |
214
|
|
|
'TTL' => 'ttl', |
215
|
|
|
]; |
216
|
|
|
$algorithm = strtoupper($algorithm); |
217
|
|
|
|
218
|
|
|
return (isset($typeConversion[$algorithm])) ? $typeConversion[$algorithm] : 'persistent'; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Create a default index name for the table. |
223
|
|
|
* |
224
|
|
|
* @param string $type |
225
|
|
|
* @param array $columns |
226
|
|
|
* @return string |
227
|
|
|
*/ |
228
|
|
|
public function createIndexName($type, array $columns) |
229
|
|
|
{ |
230
|
|
|
$index = strtolower($this->prefix.$this->table.'_'.implode('_', $columns).'_'.$type); |
|
|
|
|
231
|
|
|
|
232
|
|
|
return str_replace(['-', '.'], '_', $index); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
|
236
|
|
|
} |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: