Completed
Push — master ( 7e8e79...1f43e8 )
by Hung
02:20 queued 20s
created

Helper::createFunction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 3
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
    /**
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
     */
102
    public function showTables()
103
    {
104
        return $this->query('SHOW TABLES');
105
    }
106
107
    /**
108
     * Runs query: SHOW VARIABLES
109
     *
110
     * @return SphinxQL A SphinxQL object ready to be ->execute();
111
     */
112
    public function showVariables()
113
    {
114
        return $this->query('SHOW VARIABLES');
115
    }
116
117
    /**
118
     * SET syntax
119
     *
120
     * @param string  $name   The name of the variable
121
     * @param mixed   $value  The value of the variable
122
     * @param boolean $global True if the variable should be global, false otherwise
123
     *
124
     * @return SphinxQL A SphinxQL object ready to be ->execute();
125
     */
126
    public function setVariable($name, $value, $global = false)
127
    {
128
        $query = 'SET ';
129
130
        if ($global) {
131
            $query .= 'GLOBAL ';
132
        }
133
134
        $user_var = strpos($name, '@') === 0;
135
136
        $query .= $name.' ';
137
138
        // user variables must always be processed as arrays
139
        if ($user_var && ! is_array($value)) {
140
            $query .= '= ('.$this->connection->quote($value).')';
141
        } elseif (is_array($value)) {
142
            $query .= '= ('.implode(', ', $this->connection->quoteArr($value)).')';
143
        } else {
144
            $query .= '= '.$this->connection->quote($value);
145
        }
146
147
        return $this->query($query);
148
    }
149
150
    /**
151
     * CALL SNIPPETS syntax
152
     *
153
     * @param string|array $data    The document text (or documents) to search
154
     * @param string       $index
155
     * @param string       $query   Search query used for highlighting
156
     * @param array        $options Associative array of additional options
157
     *
158
     * @return SphinxQL A SphinxQL object ready to be ->execute();
159
     */
160
    public function callSnippets($data, $index, $query, $options = array())
161
    {
162
        array_unshift($options, $data, $index, $query);
163
164
        $arr = $this->connection->quoteArr($options);
165
        foreach ($arr as $key => &$val) {
166
            if (is_string($key)) {
167
                $val .= ' AS '.$key;
168
            }
169
        }
170
171
        return $this->query('CALL SNIPPETS('.implode(', ', $arr).')');
172
    }
173
174
    /**
175
     * CALL KEYWORDS syntax
176
     *
177
     * @param string      $text
178
     * @param string      $index
179
     * @param null|string $hits
180
     *
181
     * @return SphinxQL A SphinxQL object ready to be ->execute();
182
     */
183
    public function callKeywords($text, $index, $hits = null)
184
    {
185
        $arr = array($text, $index);
186
        if ($hits !== null) {
187
            $arr[] = $hits;
188
        }
189
190
        return $this->query('CALL KEYWORDS('.implode(', ', $this->connection->quoteArr($arr)).')');
191
    }
192
193
    /**
194
     * DESCRIBE syntax
195
     *
196
     * @param string $index The name of the index
197
     *
198
     * @return SphinxQL A SphinxQL object ready to be ->execute();
199
     */
200
    public function describe($index)
201
    {
202
        return $this->query('DESCRIBE '.$index);
203
    }
204
205
    /**
206
     * CREATE FUNCTION syntax
207
     *
208
     * @param string $udf_name
209
     * @param string $returns  Whether INT|BIGINT|FLOAT|STRING
210
     * @param string $so_name
211
     *
212
     * @return SphinxQL A SphinxQL object ready to be ->execute();
213
     */
214
    public function createFunction($udf_name, $returns, $so_name)
215
    {
216
        return $this->query('CREATE FUNCTION '.$udf_name.
217
            ' RETURNS '.$returns.' SONAME '.$this->connection->quote($so_name));
218
    }
219
220
    /**
221
     * DROP FUNCTION syntax
222
     *
223
     * @param string $udf_name
224
     *
225
     * @return SphinxQL A SphinxQL object ready to be ->execute();
226
     */
227
    public function dropFunction($udf_name)
228
    {
229
        return $this->query('DROP FUNCTION '.$udf_name);
230
    }
231
232
    /**
233
     * ATTACH INDEX * TO RTINDEX * syntax
234
     *
235
     * @param string $disk_index
236
     * @param string $rt_index
237
     *
238
     * @return SphinxQL A SphinxQL object ready to be ->execute();
239
     */
240
    public function attachIndex($disk_index, $rt_index)
241
    {
242
        return $this->query('ATTACH INDEX '.$disk_index.' TO RTINDEX '.$rt_index);
243
    }
244
245
    /**
246
     * FLUSH RTINDEX syntax
247
     *
248
     * @param string $index
249
     *
250
     * @return SphinxQL A SphinxQL object ready to be ->execute();
251
     */
252
    public function flushRtIndex($index)
253
    {
254
        return $this->query('FLUSH RTINDEX '.$index);
255
    }
256
257
    /**
258
     * TRUNCATE RTINDEX syntax
259
     *
260
     * @param string $index
261
     *
262
     * @return SphinxQL A SphinxQL object ready to be ->execute();
263
     */
264
    public function truncateRtIndex($index)
265
    {
266
        return $this->query('TRUNCATE RTINDEX '.$index);
267
    }
268
269
    /**
270
     * OPTIMIZE INDEX syntax
271
     *
272
     * @param string $index
273
     *
274
     * @return SphinxQL A SphinxQL object ready to be ->execute();
275
     */
276
    public function optimizeIndex($index)
277
    {
278
        return $this->query('OPTIMIZE INDEX '.$index);
279
    }
280
281
    /**
282
     * SHOW INDEX STATUS syntax
283
     *
284
     * @param $index
285
     *
286
     * @return SphinxQL A SphinxQL object ready to be ->execute();
287
     */
288
    public function showIndexStatus($index)
289
    {
290
        return $this->query('SHOW INDEX '.$index.' STATUS');
291
    }
292
293
    /**
294
     * FLUSH RAMCHUNK syntax
295
     *
296
     * @param $index
297
     *
298
     * @return SphinxQL A SphinxQL object ready to be ->execute();
299
     */
300
    public function flushRamchunk($index)
301
    {
302
        return $this->query('FLUSH RAMCHUNK '.$index);
303
    }
304
}
305