Completed
Push — master ( c4daf0...94b85e )
by Hung
9s
created

Helper   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 310
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 31
c 0
b 0
f 0
lcom 1
cbo 2
dl 0
loc 310
rs 9.8

23 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A create() 0 4 1
A getConnection() 0 4 1
A getSphinxQL() 0 4 1
A query() 0 4 1
A pairsToAssoc() 0 10 2
A showMeta() 0 4 1
A showWarnings() 0 4 1
A showStatus() 0 4 1
A showTables() 0 4 1
A showVariables() 0 4 1
B setVariable() 0 23 5
A callSnippets() 0 13 3
A callKeywords() 0 9 2
A describe() 0 4 1
A createFunction() 0 5 1
A dropFunction() 0 4 1
A attachIndex() 0 4 1
A flushRtIndex() 0 4 1
A truncateRtIndex() 0 4 1
A optimizeIndex() 0 4 1
A showIndexStatus() 0 4 1
A flushRamchunk() 0 4 1
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
        $query .= $name.' ';
153
154
        // user variables must always be processed as arrays
155
        if ($user_var && ! is_array($value)) {
156
            $query .= '= ('.$this->getConnection()->quote($value).')';
157
        } elseif (is_array($value)) {
158
            $query .= '= ('.implode(', ', $this->getConnection()->quoteArr($value)).')';
159
        } else {
160
            $query .= '= '.$this->getConnection()->quote($value);
161
        }
162
163
        return $this->query($query);
164
    }
165
166
    /**
167
     * CALL SNIPPETS syntax
168
     *
169
     * @param string $data    The document text (or documents) to search
170
     * @param string $index
171
     * @param string $query   Search query used for highlighting
172
     * @param array  $options Associative array of additional options
173
     *
174
     * @return SphinxQL A SphinxQL object ready to be ->execute();
175
     */
176
    public function callSnippets($data, $index, $query, $options = array())
177
    {
178
        array_unshift($options, $data, $index, $query);
179
180
        $arr = $this->getConnection()->quoteArr($options);
181
        foreach ($arr as $key => &$val) {
182
            if (is_string($key)) {
183
                $val .= ' AS '.$key;
184
            }
185
        }
186
187
        return $this->query('CALL SNIPPETS('.implode(', ', $arr).')');
188
    }
189
190
    /**
191
     * CALL KEYWORDS syntax
192
     *
193
     * @param string      $text
194
     * @param string      $index
195
     * @param null|string $hits
196
     *
197
     * @return SphinxQL A SphinxQL object ready to be ->execute();
198
     */
199
    public function callKeywords($text, $index, $hits = null)
200
    {
201
        $arr = array($text, $index);
202
        if ($hits !== null) {
203
            $arr[] = $hits;
204
        }
205
206
        return $this->query('CALL KEYWORDS('.implode(', ', $this->getConnection()->quoteArr($arr)).')');
207
    }
208
209
    /**
210
     * DESCRIBE syntax
211
     *
212
     * @param string $index The name of the index
213
     *
214
     * @return SphinxQL A SphinxQL object ready to be ->execute();
215
     */
216
    public function describe($index)
217
    {
218
        return $this->query('DESCRIBE '.$index);
219
    }
220
221
    /**
222
     * CREATE FUNCTION syntax
223
     *
224
     * @param string $udf_name
225
     * @param string $returns  Whether INT|BIGINT|FLOAT|STRING
226
     * @param string $so_name
227
     *
228
     * @return SphinxQL A SphinxQL object ready to be ->execute();
229
     */
230
    public function createFunction($udf_name, $returns, $so_name)
231
    {
232
        return $this->query('CREATE FUNCTION '.$udf_name.
233
            ' RETURNS '.$returns.' SONAME '.$this->getConnection()->quote($so_name));
234
    }
235
236
    /**
237
     * DROP FUNCTION syntax
238
     *
239
     * @param string $udf_name
240
     *
241
     * @return SphinxQL A SphinxQL object ready to be ->execute();
242
     */
243
    public function dropFunction($udf_name)
244
    {
245
        return $this->query('DROP FUNCTION '.$udf_name);
246
    }
247
248
    /**
249
     * ATTACH INDEX * TO RTINDEX * syntax
250
     *
251
     * @param string $disk_index
252
     * @param string $rt_index
253
     *
254
     * @return SphinxQL A SphinxQL object ready to be ->execute();
255
     */
256
    public function attachIndex($disk_index, $rt_index)
257
    {
258
        return $this->query('ATTACH INDEX '.$disk_index.' TO RTINDEX '.$rt_index);
259
    }
260
261
    /**
262
     * FLUSH RTINDEX syntax
263
     *
264
     * @param string $index
265
     *
266
     * @return SphinxQL A SphinxQL object ready to be ->execute();
267
     */
268
    public function flushRtIndex($index)
269
    {
270
        return $this->query('FLUSH RTINDEX '.$index);
271
    }
272
273
    /**
274
     * TRUNCATE RTINDEX syntax
275
     *
276
     * @param string $index
277
     *
278
     * @return SphinxQL A SphinxQL object ready to be ->execute();
279
     */
280
    public function truncateRtIndex($index)
281
    {
282
        return $this->query('TRUNCATE RTINDEX '.$index);
283
    }
284
285
    /**
286
     * OPTIMIZE INDEX syntax
287
     *
288
     * @param string $index
289
     *
290
     * @return SphinxQL A SphinxQL object ready to be ->execute();
291
     */
292
    public function optimizeIndex($index)
293
    {
294
        return $this->query('OPTIMIZE INDEX '.$index);
295
    }
296
297
    /**
298
     * SHOW INDEX STATUS syntax
299
     *
300
     * @param $index
301
     *
302
     * @return SphinxQL A SphinxQL object ready to be ->execute();
303
     */
304
    public function showIndexStatus($index)
305
    {
306
        return $this->query('SHOW INDEX '.$index.' STATUS');
307
    }
308
309
    /**
310
     * FLUSH RAMCHUNK syntax
311
     *
312
     * @param $index
313
     *
314
     * @return SphinxQL A SphinxQL object ready to be ->execute();
315
     */
316
    public function flushRamchunk($index)
317
    {
318
        return $this->query('FLUSH RAMCHUNK '.$index);
319
    }
320
}
321