Passed
Pull Request — master (#195)
by
unknown
02:23
created

Helper::callQsuggest()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 6
c 1
b 0
f 1
nc 3
nop 3
dl 0
loc 13
rs 10
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
     * CALL SUGGEST syntax
214
     *
215
     * @param string      $text
216
     * @param string      $index
217
     * @param array       $options Associative array of additional options
218
     *
219
     * @return SphinxQL A SphinxQL object ready to be ->execute();
220
     * @throws Exception\ConnectionException
221
     * @throws Exception\DatabaseException
222
     */
223
    public function callSuggest($text, $index, $options = array())
224
    {
225
        $arr = array($text, $index);
226
227
        foreach ($options as $key => &$val) {
228
            if (is_string($key)) {
229
                $val .= ' AS '.$key;
230
            }
231
        }
232
        
233
        $arr = array_merge($this->connection->quoteArr($arr), $options);
234
235
        return $this->query('CALL SUGGEST('.implode(', ', $arr).')');
236
    }
237
238
    /**
239
     * CALL QSUGGEST syntax
240
     *
241
     * @param string      $text
242
     * @param string      $index
243
     * @param array       $options Associative array of additional options
244
     *
245
     * @return SphinxQL A SphinxQL object ready to be ->execute();
246
     * @throws Exception\ConnectionException
247
     * @throws Exception\DatabaseException
248
     */
249
    public function callQsuggest($text, $index, $options = array())
250
    {
251
        $arr = array($text, $index);
252
253
        foreach ($options as $key => &$val) {
254
            if (is_string($key)) {
255
                $val .= ' AS '.$key;
256
            }
257
        }
258
        
259
        $arr = array_merge($this->connection->quoteArr($arr), $options);
260
261
        return $this->query('CALL QSUGGEST('.implode(', ', $arr).')');
262
    }
263
264
    /**
265
     * DESCRIBE syntax
266
     *
267
     * @param string $index The name of the index
268
     *
269
     * @return SphinxQL A SphinxQL object ready to be ->execute();
270
     */
271
    public function describe($index)
272
    {
273
        return $this->query('DESCRIBE '.$index);
274
    }
275
276
    /**
277
     * CREATE FUNCTION syntax
278
     *
279
     * @param string $udf_name
280
     * @param string $returns  Whether INT|BIGINT|FLOAT|STRING
281
     * @param string $so_name
282
     *
283
     * @return SphinxQL A SphinxQL object ready to be ->execute();
284
     * @throws Exception\ConnectionException
285
     * @throws Exception\DatabaseException
286
     */
287
    public function createFunction($udf_name, $returns, $so_name)
288
    {
289
        return $this->query('CREATE FUNCTION '.$udf_name.
290
            ' RETURNS '.$returns.' SONAME '.$this->connection->quote($so_name));
291
    }
292
293
    /**
294
     * DROP FUNCTION syntax
295
     *
296
     * @param string $udf_name
297
     *
298
     * @return SphinxQL A SphinxQL object ready to be ->execute();
299
     */
300
    public function dropFunction($udf_name)
301
    {
302
        return $this->query('DROP FUNCTION '.$udf_name);
303
    }
304
305
    /**
306
     * ATTACH INDEX * TO RTINDEX * syntax
307
     *
308
     * @param string $disk_index
309
     * @param string $rt_index
310
     *
311
     * @return SphinxQL A SphinxQL object ready to be ->execute();
312
     */
313
    public function attachIndex($disk_index, $rt_index)
314
    {
315
        return $this->query('ATTACH INDEX '.$disk_index.' TO RTINDEX '.$rt_index);
316
    }
317
318
    /**
319
     * FLUSH RTINDEX syntax
320
     *
321
     * @param string $index
322
     *
323
     * @return SphinxQL A SphinxQL object ready to be ->execute();
324
     */
325
    public function flushRtIndex($index)
326
    {
327
        return $this->query('FLUSH RTINDEX '.$index);
328
    }
329
330
    /**
331
     * TRUNCATE RTINDEX syntax
332
     *
333
     * @param string $index
334
     *
335
     * @return SphinxQL A SphinxQL object ready to be ->execute();
336
     */
337
    public function truncateRtIndex($index)
338
    {
339
        return $this->query('TRUNCATE RTINDEX '.$index);
340
    }
341
342
    /**
343
     * OPTIMIZE INDEX syntax
344
     *
345
     * @param string $index
346
     *
347
     * @return SphinxQL A SphinxQL object ready to be ->execute();
348
     */
349
    public function optimizeIndex($index)
350
    {
351
        return $this->query('OPTIMIZE INDEX '.$index);
352
    }
353
354
    /**
355
     * SHOW INDEX STATUS syntax
356
     *
357
     * @param $index
358
     *
359
     * @return SphinxQL A SphinxQL object ready to be ->execute();
360
     */
361
    public function showIndexStatus($index)
362
    {
363
        return $this->query('SHOW INDEX '.$index.' STATUS');
364
    }
365
366
    /**
367
     * FLUSH RAMCHUNK syntax
368
     *
369
     * @param $index
370
     *
371
     * @return SphinxQL A SphinxQL object ready to be ->execute();
372
     */
373
    public function flushRamchunk($index)
374
    {
375
        return $this->query('FLUSH RAMCHUNK '.$index);
376
    }
377
}
378