1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Foolz\SphinxQL; |
4
|
|
|
|
5
|
|
|
use Foolz\SphinxQL\Drivers\ConnectionInterface; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* SQL queries that don't require "query building" |
9
|
|
|
* These return a valid SphinxQL that can even be enqueued |
10
|
|
|
*/ |
11
|
|
|
class Helper |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var ConnectionInterface |
15
|
|
|
*/ |
16
|
|
|
protected $connection; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @param ConnectionInterface $connection |
20
|
|
|
*/ |
21
|
|
|
public function __construct(ConnectionInterface $connection) |
22
|
|
|
{ |
23
|
|
|
$this->connection = $connection; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Returns a new SphinxQL instance |
28
|
|
|
* |
29
|
|
|
* @return SphinxQL |
30
|
|
|
*/ |
31
|
|
|
protected function getSphinxQL() |
32
|
|
|
{ |
33
|
|
|
return new SphinxQL($this->connection); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Prepares a query in SphinxQL (not executed) |
38
|
|
|
* |
39
|
|
|
* @param $sql |
40
|
|
|
* |
41
|
|
|
* @return SphinxQL A SphinxQL object ready to be ->execute(); |
42
|
|
|
*/ |
43
|
|
|
protected function query($sql) |
44
|
|
|
{ |
45
|
|
|
return $this->getSphinxQL()->query($sql); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Converts the columns from queries like SHOW VARIABLES to simpler key-value |
50
|
|
|
* |
51
|
|
|
* @param array $result The result of an executed query |
52
|
|
|
* |
53
|
|
|
* @return array Associative array with Variable_name as key and Value as value |
54
|
|
|
* @todo make non static |
55
|
|
|
*/ |
56
|
|
|
public static function pairsToAssoc($result) |
57
|
|
|
{ |
58
|
|
|
$ordered = array(); |
59
|
|
|
|
60
|
|
|
foreach ($result as $item) { |
61
|
|
|
$ordered[$item['Variable_name']] = $item['Value']; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return $ordered; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Runs query: SHOW META |
69
|
|
|
* |
70
|
|
|
* @return SphinxQL A SphinxQL object ready to be ->execute(); |
71
|
|
|
*/ |
72
|
|
|
public function showMeta() |
73
|
|
|
{ |
74
|
|
|
return $this->query('SHOW META'); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Runs query: SHOW WARNINGS |
79
|
|
|
* |
80
|
|
|
* @return SphinxQL A SphinxQL object ready to be ->execute(); |
81
|
|
|
*/ |
82
|
|
|
public function showWarnings() |
83
|
|
|
{ |
84
|
|
|
return $this->query('SHOW WARNINGS'); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Runs query: SHOW STATUS |
89
|
|
|
* |
90
|
|
|
* @return SphinxQL A SphinxQL object ready to be ->execute(); |
91
|
|
|
*/ |
92
|
|
|
public function showStatus() |
93
|
|
|
{ |
94
|
|
|
return $this->query('SHOW STATUS'); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Runs query: SHOW TABLES |
99
|
|
|
* |
100
|
|
|
* @return SphinxQL A SphinxQL object ready to be ->execute(); |
101
|
|
|
* @throws Exception\ConnectionException |
102
|
|
|
* @throws Exception\DatabaseException |
103
|
|
|
*/ |
104
|
|
|
public function showTables( $index ) |
105
|
|
|
{ |
106
|
|
|
$queryAppend = ''; |
107
|
|
|
if ( ! empty( $index ) ) { |
108
|
|
|
$queryAppend = ' LIKE ' . $this->connection->quote($index); |
109
|
|
|
} |
110
|
|
|
return $this->query( 'SHOW TABLES' . $queryAppend ); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Runs query: SHOW VARIABLES |
115
|
|
|
* |
116
|
|
|
* @return SphinxQL A SphinxQL object ready to be ->execute(); |
117
|
|
|
*/ |
118
|
|
|
public function showVariables() |
119
|
|
|
{ |
120
|
|
|
return $this->query('SHOW VARIABLES'); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* SET syntax |
125
|
|
|
* |
126
|
|
|
* @param string $name The name of the variable |
127
|
|
|
* @param mixed $value The value of the variable |
128
|
|
|
* @param bool $global True if the variable should be global, false otherwise |
129
|
|
|
* |
130
|
|
|
* @return SphinxQL A SphinxQL object ready to be ->execute(); |
131
|
|
|
* @throws Exception\ConnectionException |
132
|
|
|
* @throws Exception\DatabaseException |
133
|
|
|
*/ |
134
|
|
|
public function setVariable($name, $value, $global = false) |
135
|
|
|
{ |
136
|
|
|
$query = 'SET '; |
137
|
|
|
|
138
|
|
|
if ($global) { |
139
|
|
|
$query .= 'GLOBAL '; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
$user_var = strpos($name, '@') === 0; |
143
|
|
|
|
144
|
|
|
$query .= $name.' '; |
145
|
|
|
|
146
|
|
|
// user variables must always be processed as arrays |
147
|
|
|
if ($user_var && !is_array($value)) { |
148
|
|
|
$query .= '= ('.$this->connection->quote($value).')'; |
149
|
|
|
} elseif (is_array($value)) { |
150
|
|
|
$query .= '= ('.implode(', ', $this->connection->quoteArr($value)).')'; |
151
|
|
|
} else { |
152
|
|
|
$query .= '= '.$this->connection->quote($value); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
return $this->query($query); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* CALL SNIPPETS syntax |
160
|
|
|
* |
161
|
|
|
* @param string|array $data The document text (or documents) to search |
162
|
|
|
* @param string $index |
163
|
|
|
* @param string $query Search query used for highlighting |
164
|
|
|
* @param array $options Associative array of additional options |
165
|
|
|
* |
166
|
|
|
* @return SphinxQL A SphinxQL object ready to be ->execute(); |
167
|
|
|
* @throws Exception\ConnectionException |
168
|
|
|
* @throws Exception\DatabaseException |
169
|
|
|
*/ |
170
|
|
|
public function callSnippets($data, $index, $query, $options = array()) |
171
|
|
|
{ |
172
|
|
|
$documents = array(); |
173
|
|
|
if (is_array($data)) { |
174
|
|
|
$documents[] = '('.implode(', ', $this->connection->quoteArr($data)).')'; |
175
|
|
|
} else { |
176
|
|
|
$documents[] = $this->connection->quote($data); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
array_unshift($options, $index, $query); |
180
|
|
|
|
181
|
|
|
$arr = $this->connection->quoteArr($options); |
182
|
|
|
foreach ($arr as $key => &$val) { |
183
|
|
|
if (is_string($key)) { |
184
|
|
|
$val .= ' AS '.$key; |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
return $this->query('CALL SNIPPETS('.implode(', ', array_merge($documents, $arr)).')'); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* CALL KEYWORDS syntax |
193
|
|
|
* |
194
|
|
|
* @param string $text |
195
|
|
|
* @param string $index |
196
|
|
|
* @param null|string $hits |
197
|
|
|
* |
198
|
|
|
* @return SphinxQL A SphinxQL object ready to be ->execute(); |
199
|
|
|
* @throws Exception\ConnectionException |
200
|
|
|
* @throws Exception\DatabaseException |
201
|
|
|
*/ |
202
|
|
|
public function callKeywords($text, $index, $hits = null) |
203
|
|
|
{ |
204
|
|
|
$arr = array($text, $index); |
205
|
|
|
if ($hits !== null) { |
206
|
|
|
$arr[] = $hits; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
return $this->query('CALL KEYWORDS('.implode(', ', $this->connection->quoteArr($arr)).')'); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* DESCRIBE syntax |
214
|
|
|
* |
215
|
|
|
* @param string $index The name of the index |
216
|
|
|
* |
217
|
|
|
* @return SphinxQL A SphinxQL object ready to be ->execute(); |
218
|
|
|
*/ |
219
|
|
|
public function describe($index) |
220
|
|
|
{ |
221
|
|
|
return $this->query('DESCRIBE '.$index); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* CREATE FUNCTION syntax |
226
|
|
|
* |
227
|
|
|
* @param string $udf_name |
228
|
|
|
* @param string $returns Whether INT|BIGINT|FLOAT|STRING |
229
|
|
|
* @param string $so_name |
230
|
|
|
* |
231
|
|
|
* @return SphinxQL A SphinxQL object ready to be ->execute(); |
232
|
|
|
* @throws Exception\ConnectionException |
233
|
|
|
* @throws Exception\DatabaseException |
234
|
|
|
*/ |
235
|
|
|
public function createFunction($udf_name, $returns, $so_name) |
236
|
|
|
{ |
237
|
|
|
return $this->query('CREATE FUNCTION '.$udf_name. |
238
|
|
|
' RETURNS '.$returns.' SONAME '.$this->connection->quote($so_name)); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* DROP FUNCTION syntax |
243
|
|
|
* |
244
|
|
|
* @param string $udf_name |
245
|
|
|
* |
246
|
|
|
* @return SphinxQL A SphinxQL object ready to be ->execute(); |
247
|
|
|
*/ |
248
|
|
|
public function dropFunction($udf_name) |
249
|
|
|
{ |
250
|
|
|
return $this->query('DROP FUNCTION '.$udf_name); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* ATTACH INDEX * TO RTINDEX * syntax |
255
|
|
|
* |
256
|
|
|
* @param string $disk_index |
257
|
|
|
* @param string $rt_index |
258
|
|
|
* |
259
|
|
|
* @return SphinxQL A SphinxQL object ready to be ->execute(); |
260
|
|
|
*/ |
261
|
|
|
public function attachIndex($disk_index, $rt_index) |
262
|
|
|
{ |
263
|
|
|
return $this->query('ATTACH INDEX '.$disk_index.' TO RTINDEX '.$rt_index); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* FLUSH RTINDEX syntax |
268
|
|
|
* |
269
|
|
|
* @param string $index |
270
|
|
|
* |
271
|
|
|
* @return SphinxQL A SphinxQL object ready to be ->execute(); |
272
|
|
|
*/ |
273
|
|
|
public function flushRtIndex($index) |
274
|
|
|
{ |
275
|
|
|
return $this->query('FLUSH RTINDEX '.$index); |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* TRUNCATE RTINDEX syntax |
280
|
|
|
* |
281
|
|
|
* @param string $index |
282
|
|
|
* |
283
|
|
|
* @return SphinxQL A SphinxQL object ready to be ->execute(); |
284
|
|
|
*/ |
285
|
|
|
public function truncateRtIndex($index) |
286
|
|
|
{ |
287
|
|
|
return $this->query('TRUNCATE RTINDEX '.$index); |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* OPTIMIZE INDEX syntax |
292
|
|
|
* |
293
|
|
|
* @param string $index |
294
|
|
|
* |
295
|
|
|
* @return SphinxQL A SphinxQL object ready to be ->execute(); |
296
|
|
|
*/ |
297
|
|
|
public function optimizeIndex($index) |
298
|
|
|
{ |
299
|
|
|
return $this->query('OPTIMIZE INDEX '.$index); |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* SHOW INDEX STATUS syntax |
304
|
|
|
* |
305
|
|
|
* @param $index |
306
|
|
|
* |
307
|
|
|
* @return SphinxQL A SphinxQL object ready to be ->execute(); |
308
|
|
|
*/ |
309
|
|
|
public function showIndexStatus($index) |
310
|
|
|
{ |
311
|
|
|
return $this->query('SHOW INDEX '.$index.' STATUS'); |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* FLUSH RAMCHUNK syntax |
316
|
|
|
* |
317
|
|
|
* @param $index |
318
|
|
|
* |
319
|
|
|
* @return SphinxQL A SphinxQL object ready to be ->execute(); |
320
|
|
|
*/ |
321
|
|
|
public function flushRamchunk($index) |
322
|
|
|
{ |
323
|
|
|
return $this->query('FLUSH RAMCHUNK '.$index); |
324
|
|
|
} |
325
|
|
|
} |
326
|
|
|
|