Passed
Pull Request — master (#153)
by
unknown
01:48
created

Helper::pairsToAssoc()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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