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