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
|
|
|
public $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 Connection object setup in the construct |
35
|
|
|
* |
36
|
|
|
* @return ConnectionInterface |
37
|
|
|
*/ |
38
|
|
|
protected function getConnection() |
39
|
|
|
{ |
40
|
|
|
return $this->connection; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Returns a new SphinxQL instance |
45
|
|
|
* |
46
|
|
|
* @return SphinxQL |
47
|
|
|
*/ |
48
|
|
|
protected function getSphinxQL() |
49
|
|
|
{ |
50
|
|
|
return SphinxQL::create($this->getConnection()); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Prepares a query in SphinxQL (not executed) |
55
|
|
|
* |
56
|
|
|
* @param $sql |
57
|
|
|
* |
58
|
|
|
* @return SphinxQL A SphinxQL object ready to be ->execute(); |
59
|
|
|
*/ |
60
|
|
|
protected function query($sql) |
61
|
|
|
{ |
62
|
|
|
return $this->getSphinxQL()->query($sql); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Converts the columns from queries like SHOW VARIABLES to simpler key-value |
67
|
|
|
* |
68
|
|
|
* @param array $result The result of an executed query |
69
|
|
|
* |
70
|
|
|
* @return array Associative array with Variable_name as key and Value as value |
71
|
|
|
*/ |
72
|
|
|
public static function pairsToAssoc($result) |
73
|
|
|
{ |
74
|
|
|
$ordered = array(); |
75
|
|
|
|
76
|
|
|
foreach ($result as $item) { |
77
|
|
|
$ordered[$item['Variable_name']] = $item['Value']; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $ordered; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Runs query: SHOW META |
85
|
|
|
* |
86
|
|
|
* @return SphinxQL A SphinxQL object ready to be ->execute(); |
87
|
|
|
*/ |
88
|
|
|
public function showMeta() |
89
|
|
|
{ |
90
|
|
|
return $this->query('SHOW META'); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Runs query: SHOW WARNINGS |
95
|
|
|
* |
96
|
|
|
* @return SphinxQL A SphinxQL object ready to be ->execute(); |
97
|
|
|
*/ |
98
|
|
|
public function showWarnings() |
99
|
|
|
{ |
100
|
|
|
return $this->query('SHOW WARNINGS'); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Runs query: SHOW STATUS |
105
|
|
|
* |
106
|
|
|
* @return SphinxQL A SphinxQL object ready to be ->execute(); |
107
|
|
|
*/ |
108
|
|
|
public function showStatus() |
109
|
|
|
{ |
110
|
|
|
return $this->query('SHOW STATUS'); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Runs query: SHOW TABLES |
115
|
|
|
* |
116
|
|
|
* @return SphinxQL A SphinxQL object ready to be ->execute(); |
117
|
|
|
*/ |
118
|
|
|
public function showTables() |
119
|
|
|
{ |
120
|
|
|
return $this->query('SHOW TABLES'); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Runs query: SHOW VARIABLES |
125
|
|
|
* |
126
|
|
|
* @return SphinxQL A SphinxQL object ready to be ->execute(); |
127
|
|
|
*/ |
128
|
|
|
public function showVariables() |
129
|
|
|
{ |
130
|
|
|
return $this->query('SHOW VARIABLES'); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* SET syntax |
135
|
|
|
* |
136
|
|
|
* @param string $name The name of the variable |
137
|
|
|
* @param mixed $value The value of the variable |
138
|
|
|
* @param boolean $global True if the variable should be global, false otherwise |
139
|
|
|
* |
140
|
|
|
* @return SphinxQL A SphinxQL object ready to be ->execute(); |
141
|
|
|
*/ |
142
|
|
|
public function setVariable($name, $value, $global = false) |
143
|
|
|
{ |
144
|
|
|
$query = 'SET '; |
145
|
|
|
|
146
|
|
|
if ($global) { |
147
|
|
|
$query .= 'GLOBAL '; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
$user_var = strpos($name, '@') === 0; |
151
|
|
|
|
152
|
|
|
// if it has an @ it's a user variable and we can't wrap it |
153
|
|
|
if ($user_var) { |
154
|
|
|
$query .= $name.' '; |
155
|
|
|
} else { |
156
|
|
|
$query .= $this->getConnection()->quoteIdentifier($name).' '; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
// user variables must always be processed as arrays |
160
|
|
|
if ($user_var && ! is_array($value)) { |
161
|
|
|
$query .= '= ('.$this->getConnection()->quote($value).')'; |
162
|
|
|
} elseif (is_array($value)) { |
163
|
|
|
$query .= '= ('.implode(', ', $this->getConnection()->quoteArr($value)).')'; |
164
|
|
|
} else { |
165
|
|
|
$query .= '= '.$this->getConnection()->quote($value); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
return $this->query($query); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* CALL SNIPPETS syntax |
173
|
|
|
* |
174
|
|
|
* @param string $data The document text (or documents) to search |
175
|
|
|
* @param string $index |
176
|
|
|
* @param string $query Search query used for highlighting |
177
|
|
|
* @param array $options Associative array of additional options |
178
|
|
|
* |
179
|
|
|
* @return SphinxQL A SphinxQL object ready to be ->execute(); |
180
|
|
|
*/ |
181
|
|
|
public function callSnippets($data, $index, $query, $options = array()) |
182
|
|
|
{ |
183
|
|
|
array_unshift($options, $data, $index, $query); |
184
|
|
|
|
185
|
|
|
$arr = $this->getConnection()->quoteArr($options); |
186
|
|
|
foreach ($arr as $key => &$val) { |
187
|
|
|
if (is_string($key)) { |
188
|
|
|
$val .= ' AS '.$key; |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
return $this->query('CALL SNIPPETS('.implode(', ', $arr).')'); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* CALL KEYWORDS syntax |
197
|
|
|
* |
198
|
|
|
* @param string $text |
199
|
|
|
* @param string $index |
200
|
|
|
* @param null|string $hits |
201
|
|
|
* |
202
|
|
|
* @return SphinxQL A SphinxQL object ready to be ->execute(); |
203
|
|
|
*/ |
204
|
|
|
public function callKeywords($text, $index, $hits = null) |
205
|
|
|
{ |
206
|
|
|
$arr = array($text, $index); |
207
|
|
|
if ($hits !== null) { |
208
|
|
|
$arr[] = $hits; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
return $this->query('CALL KEYWORDS('.implode(', ', $this->getConnection()->quoteArr($arr)).')'); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* DESCRIBE syntax |
216
|
|
|
* |
217
|
|
|
* @param string $index The name of the index |
218
|
|
|
* |
219
|
|
|
* @return SphinxQL A SphinxQL object ready to be ->execute(); |
220
|
|
|
*/ |
221
|
|
|
public function describe($index) |
222
|
|
|
{ |
223
|
|
|
return $this->query('DESCRIBE '.$this->getConnection()->quoteIdentifier($index)); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* CREATE FUNCTION syntax |
228
|
|
|
* |
229
|
|
|
* @param string $udf_name |
230
|
|
|
* @param string $returns Whether INT|BIGINT|FLOAT|STRING |
231
|
|
|
* @param string $so_name |
232
|
|
|
* |
233
|
|
|
* @return SphinxQL A SphinxQL object ready to be ->execute(); |
234
|
|
|
*/ |
235
|
|
|
public function createFunction($udf_name, $returns, $so_name) |
236
|
|
|
{ |
237
|
|
|
return $this->query('CREATE FUNCTION '.$this->getConnection()->quoteIdentifier($udf_name). |
238
|
|
|
' RETURNS '.$returns.' SONAME '.$this->getConnection()->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 '.$this->getConnection()->quoteIdentifier($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 '.$this->getConnection()->quoteIdentifier($disk_index). |
264
|
|
|
' TO RTINDEX '. $this->getConnection()->quoteIdentifier($rt_index)); |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* FLUSH RTINDEX syntax |
269
|
|
|
* |
270
|
|
|
* @param string $index |
271
|
|
|
* |
272
|
|
|
* @return SphinxQL A SphinxQL object ready to be ->execute(); |
273
|
|
|
*/ |
274
|
|
|
public function flushRtIndex($index) |
275
|
|
|
{ |
276
|
|
|
return $this->query('FLUSH RTINDEX '.$this->getConnection()->quoteIdentifier($index)); |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* OPTIMIZE INDEX syntax |
281
|
|
|
* |
282
|
|
|
* @param string $index |
283
|
|
|
* |
284
|
|
|
* @return SphinxQL A SphinxQL object ready to be ->execute(); |
285
|
|
|
*/ |
286
|
|
|
public function optimizeIndex($index) |
287
|
|
|
{ |
288
|
|
|
return $this->query('OPTIMIZE INDEX '.$this->getConnection()->quoteIdentifier($index)); |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* SHOW INDEX STATUS syntax |
293
|
|
|
* |
294
|
|
|
* @param $index |
295
|
|
|
* |
296
|
|
|
* @return SphinxQL A SphinxQL object ready to be ->execute(); |
297
|
|
|
*/ |
298
|
|
|
public function showIndexStatus($index) |
299
|
|
|
{ |
300
|
|
|
return $this->query('SHOW INDEX '.$this->getConnection()->quoteIdentifier($index).' STATUS'); |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* FLUSH RAMCHUNK syntax |
305
|
|
|
* |
306
|
|
|
* @param $index |
307
|
|
|
* |
308
|
|
|
* @return SphinxQL A SphinxQL object ready to be ->execute(); |
309
|
|
|
*/ |
310
|
|
|
public function flushRamchunk($index) |
311
|
|
|
{ |
312
|
|
|
return $this->query('FLUSH RAMCHUNK '.$this->getConnection()->quoteIdentifier($index)); |
313
|
|
|
} |
314
|
|
|
} |
315
|
|
|
|