|
@@ -1,387 +1,410 @@
|
1
|
|
-<?
|
2
|
|
-class SphinxqlQuery {
|
3
|
|
- private $Sphinxql;
|
|
1
|
+<?php
|
|
2
|
+class SphinxqlQuery
|
|
3
|
+{
|
|
4
|
+ private $Sphinxql;
|
4
|
5
|
|
5
|
|
- private $Errors;
|
6
|
|
- private $Expressions;
|
7
|
|
- private $Filters;
|
8
|
|
- private $GroupBy;
|
9
|
|
- private $Indexes;
|
10
|
|
- private $Limits;
|
11
|
|
- private $Options;
|
12
|
|
- private $QueryString;
|
13
|
|
- private $Select;
|
14
|
|
- private $SortBy;
|
15
|
|
- private $SortGroupBy;
|
|
6
|
+ private $Errors;
|
|
7
|
+ private $Expressions;
|
|
8
|
+ private $Filters;
|
|
9
|
+ private $GroupBy;
|
|
10
|
+ private $Indexes;
|
|
11
|
+ private $Limits;
|
|
12
|
+ private $Options;
|
|
13
|
+ private $QueryString;
|
|
14
|
+ private $Select;
|
|
15
|
+ private $SortBy;
|
|
16
|
+ private $SortGroupBy;
|
16
|
17
|
|
17
|
|
- /**
|
18
|
|
- * Initialize Sphinxql object
|
19
|
|
- *
|
20
|
|
- * @param string $Server server address or hostname
|
21
|
|
- * @param int $Port listening port
|
22
|
|
- * @param string $Socket Unix socket address, overrides $Server:$Port
|
23
|
|
- */
|
24
|
|
- public function __construct($Server = SPHINXQL_HOST, $Port = SPHINXQL_PORT, $Socket = SPHINXQL_SOCK) {
|
25
|
|
- $this->Sphinxql = Sphinxql::init_connection($Server, $Port, $Socket);
|
26
|
|
- $this->reset();
|
27
|
|
- }
|
28
|
|
-
|
29
|
|
- /**
|
30
|
|
- * Specify what data the Sphinx query is supposed to return
|
31
|
|
- *
|
32
|
|
- * @param string $Fields Attributes and expressions
|
33
|
|
- * @return current Sphinxql query object
|
34
|
|
- */
|
35
|
|
- public function select($Fields) {
|
36
|
|
- $this->Select = $Fields;
|
37
|
|
- return $this;
|
38
|
|
- }
|
|
18
|
+ /**
|
|
19
|
+ * Initialize Sphinxql object
|
|
20
|
+ *
|
|
21
|
+ * @param string $Server server address or hostname
|
|
22
|
+ * @param int $Port listening port
|
|
23
|
+ * @param string $Socket Unix socket address, overrides $Server:$Port
|
|
24
|
+ */
|
|
25
|
+ public function __construct($Server = SPHINXQL_HOST, $Port = SPHINXQL_PORT, $Socket = SPHINXQL_SOCK)
|
|
26
|
+ {
|
|
27
|
+ $this->Sphinxql = Sphinxql::init_connection($Server, $Port, $Socket);
|
|
28
|
+ $this->reset();
|
|
29
|
+ }
|
39
|
30
|
|
40
|
|
- /**
|
41
|
|
- * Specify the indexes to use in the search
|
42
|
|
- *
|
43
|
|
- * @param string $Indexes comma separated list of indexes
|
44
|
|
- * @return current Sphinxql query object
|
45
|
|
- */
|
46
|
|
- public function from($Indexes) {
|
47
|
|
- $this->Indexes = $Indexes;
|
48
|
|
- return $this;
|
49
|
|
- }
|
|
31
|
+ /**
|
|
32
|
+ * Specify what data the Sphinx query is supposed to return
|
|
33
|
+ *
|
|
34
|
+ * @param string $Fields Attributes and expressions
|
|
35
|
+ * @return current Sphinxql query object
|
|
36
|
+ */
|
|
37
|
+ public function select($Fields)
|
|
38
|
+ {
|
|
39
|
+ $this->Select = $Fields;
|
|
40
|
+ return $this;
|
|
41
|
+ }
|
50
|
42
|
|
51
|
|
- /**
|
52
|
|
- * Add attribute filter. Calling multiple filter functions results in boolean AND between each condition.
|
53
|
|
- *
|
54
|
|
- * @param string $Attribute attribute which the filter will apply to
|
55
|
|
- * @param mixed $Values scalar or array of numerical values. Array uses boolean OR in query condition
|
56
|
|
- * @param bool $Exclude whether to exclude or include matching documents. Default mode is to include matches
|
57
|
|
- * @return current Sphinxql query object
|
58
|
|
- */
|
59
|
|
- public function where($Attribute, $Values, $Exclude = false) {
|
60
|
|
- if (empty($Attribute) || !isset($Values)) {
|
61
|
|
- $this->error("Attribute name and filter value are required.");
|
62
|
|
- return $this;
|
|
43
|
+ /**
|
|
44
|
+ * Specify the indexes to use in the search
|
|
45
|
+ *
|
|
46
|
+ * @param string $Indexes comma separated list of indexes
|
|
47
|
+ * @return current Sphinxql query object
|
|
48
|
+ */
|
|
49
|
+ public function from($Indexes)
|
|
50
|
+ {
|
|
51
|
+ $this->Indexes = $Indexes;
|
|
52
|
+ return $this;
|
63
|
53
|
}
|
64
|
|
- $Filters = [];
|
65
|
|
- if (is_array($Values)) {
|
66
|
|
- foreach ($Values as $Value) {
|
67
|
|
- if (!is_number($Value)) {
|
68
|
|
- $this->error("Filters only support numeric values.");
|
69
|
|
- return $this;
|
|
54
|
+
|
|
55
|
+ /**
|
|
56
|
+ * Add attribute filter. Calling multiple filter functions results in boolean AND between each condition.
|
|
57
|
+ *
|
|
58
|
+ * @param string $Attribute attribute which the filter will apply to
|
|
59
|
+ * @param mixed $Values scalar or array of numerical values. Array uses boolean OR in query condition
|
|
60
|
+ * @param bool $Exclude whether to exclude or include matching documents. Default mode is to include matches
|
|
61
|
+ * @return current Sphinxql query object
|
|
62
|
+ */
|
|
63
|
+ public function where($Attribute, $Values, $Exclude = false)
|
|
64
|
+ {
|
|
65
|
+ if (empty($Attribute) || !isset($Values)) {
|
|
66
|
+ $this->error("Attribute name and filter value are required.");
|
|
67
|
+ return $this;
|
|
68
|
+ }
|
|
69
|
+ $Filters = [];
|
|
70
|
+ if (is_array($Values)) {
|
|
71
|
+ foreach ($Values as $Value) {
|
|
72
|
+ if (!is_number($Value)) {
|
|
73
|
+ $this->error("Filters only support numeric values.");
|
|
74
|
+ return $this;
|
|
75
|
+ }
|
|
76
|
+ }
|
|
77
|
+ if ($Exclude) {
|
|
78
|
+ $Filters[] = "$Attribute NOT IN (".implode(",", $Values).")";
|
|
79
|
+ } else {
|
|
80
|
+ $Filters[] = "$Attribute IN (".implode(",", $Values).")";
|
|
81
|
+ }
|
|
82
|
+ } else {
|
|
83
|
+ if (!is_number($Values)) {
|
|
84
|
+ $this->error("Filters only support numeric values.");
|
|
85
|
+ return $this;
|
|
86
|
+ }
|
|
87
|
+ if ($Exclude) {
|
|
88
|
+ $Filters[] = "$Attribute != $Values";
|
|
89
|
+ } else {
|
|
90
|
+ $Filters[] = "$Attribute = $Values";
|
|
91
|
+ }
|
70
|
92
|
}
|
71
|
|
- }
|
72
|
|
- if ($Exclude) {
|
73
|
|
- $Filters[] = "$Attribute NOT IN (".implode(",", $Values).")";
|
74
|
|
- } else {
|
75
|
|
- $Filters[] = "$Attribute IN (".implode(",", $Values).")";
|
76
|
|
- }
|
77
|
|
- } else {
|
78
|
|
- if (!is_number($Values)) {
|
79
|
|
- $this->error("Filters only support numeric values.");
|
|
93
|
+ $this->Filters[] = implode(" AND ", $Filters);
|
80
|
94
|
return $this;
|
81
|
|
- }
|
82
|
|
- if ($Exclude) {
|
83
|
|
- $Filters[] = "$Attribute != $Values";
|
84
|
|
- } else {
|
85
|
|
- $Filters[] = "$Attribute = $Values";
|
86
|
|
- }
|
87
|
95
|
}
|
88
|
|
- $this->Filters[] = implode(" AND ", $Filters);
|
89
|
|
- return $this;
|
90
|
|
- }
|
91
|
96
|
|
92
|
|
- /**
|
93
|
|
- * Add attribute less-than filter. Calling multiple filter functions results in boolean AND between each condition.
|
94
|
|
- *
|
95
|
|
- * @param string $Attribute attribute which the filter will apply to
|
96
|
|
- * @param array $Value upper limit for matches
|
97
|
|
- * @param bool $Inclusive whether to use <= or <
|
98
|
|
- * @return current Sphinxql query object
|
99
|
|
- */
|
100
|
|
- public function where_lt($Attribute, $Value, $Inclusive = false) {
|
101
|
|
- if (empty($Attribute) || !isset($Value) || !is_number($Value)) {
|
102
|
|
- $this->error("Attribute name is required and only numeric filters are supported.");
|
103
|
|
- return $this;
|
|
97
|
+ /**
|
|
98
|
+ * Add attribute less-than filter. Calling multiple filter functions results in boolean AND between each condition.
|
|
99
|
+ *
|
|
100
|
+ * @param string $Attribute attribute which the filter will apply to
|
|
101
|
+ * @param array $Value upper limit for matches
|
|
102
|
+ * @param bool $Inclusive whether to use <= or <
|
|
103
|
+ * @return current Sphinxql query object
|
|
104
|
+ */
|
|
105
|
+ public function where_lt($Attribute, $Value, $Inclusive = false)
|
|
106
|
+ {
|
|
107
|
+ if (empty($Attribute) || !isset($Value) || !is_number($Value)) {
|
|
108
|
+ $this->error("Attribute name is required and only numeric filters are supported.");
|
|
109
|
+ return $this;
|
|
110
|
+ }
|
|
111
|
+ $this->Filters[] = $Inclusive ? "$Attribute <= $Value" : "$Attribute < $Value";
|
|
112
|
+ return $this;
|
104
|
113
|
}
|
105
|
|
- $this->Filters[] = $Inclusive ? "$Attribute <= $Value" : "$Attribute < $Value";
|
106
|
|
- return $this;
|
107
|
|
- }
|
108
|
114
|
|
109
|
|
- /**
|
110
|
|
- * Add attribute greater-than filter. Calling multiple filter functions results in boolean AND between each condition.
|
111
|
|
- *
|
112
|
|
- * @param string $Attribute attribute which the filter will apply to
|
113
|
|
- * @param array $Value lower limit for matches
|
114
|
|
- * @param bool $Inclusive whether to use >= or >
|
115
|
|
- * @return current Sphinxql query object
|
116
|
|
- */
|
117
|
|
- public function where_gt($Attribute, $Value, $Inclusive = false) {
|
118
|
|
- if (empty($Attribute) || !isset($Value) || !is_number($Value)) {
|
119
|
|
- $this->error("Attribute name is required and only numeric filters are supported.");
|
120
|
|
- return $this;
|
|
115
|
+ /**
|
|
116
|
+ * Add attribute greater-than filter. Calling multiple filter functions results in boolean AND between each condition.
|
|
117
|
+ *
|
|
118
|
+ * @param string $Attribute attribute which the filter will apply to
|
|
119
|
+ * @param array $Value lower limit for matches
|
|
120
|
+ * @param bool $Inclusive whether to use >= or >
|
|
121
|
+ * @return current Sphinxql query object
|
|
122
|
+ */
|
|
123
|
+ public function where_gt($Attribute, $Value, $Inclusive = false)
|
|
124
|
+ {
|
|
125
|
+ if (empty($Attribute) || !isset($Value) || !is_number($Value)) {
|
|
126
|
+ $this->error("Attribute name is required and only numeric filters are supported.");
|
|
127
|
+ return $this;
|
|
128
|
+ }
|
|
129
|
+ $this->Filters[] = $Inclusive ? "$Attribute >= $Value" : "$Attribute > $Value";
|
|
130
|
+ return $this;
|
121
|
131
|
}
|
122
|
|
- $this->Filters[] = $Inclusive ? "$Attribute >= $Value" : "$Attribute > $Value";
|
123
|
|
- return $this;
|
124
|
|
- }
|
125
|
132
|
|
126
|
|
- /**
|
127
|
|
- * Add attribute range filter. Calling multiple filter functions results in boolean AND between each condition.
|
128
|
|
- *
|
129
|
|
- * @param string $Attribute attribute which the filter will apply to
|
130
|
|
- * @param array $Values pair of numerical values that defines the filter range
|
131
|
|
- * @return current Sphinxql query object
|
132
|
|
- */
|
133
|
|
- public function where_between($Attribute, $Values) {
|
134
|
|
- if (empty($Attribute) || empty($Values) || count($Values) != 2 || !is_number($Values[0]) || !is_number($Values[1])) {
|
135
|
|
- $this->error("Filter range requires array of two numerical boundaries as values.");
|
136
|
|
- return $this;
|
|
133
|
+ /**
|
|
134
|
+ * Add attribute range filter. Calling multiple filter functions results in boolean AND between each condition.
|
|
135
|
+ *
|
|
136
|
+ * @param string $Attribute attribute which the filter will apply to
|
|
137
|
+ * @param array $Values pair of numerical values that defines the filter range
|
|
138
|
+ * @return current Sphinxql query object
|
|
139
|
+ */
|
|
140
|
+ public function where_between($Attribute, $Values)
|
|
141
|
+ {
|
|
142
|
+ if (empty($Attribute) || empty($Values) || count($Values) != 2 || !is_number($Values[0]) || !is_number($Values[1])) {
|
|
143
|
+ $this->error("Filter range requires array of two numerical boundaries as values.");
|
|
144
|
+ return $this;
|
|
145
|
+ }
|
|
146
|
+ $this->Filters[] = "$Attribute BETWEEN $Values[0] AND $Values[1]";
|
|
147
|
+ return $this;
|
137
|
148
|
}
|
138
|
|
- $this->Filters[] = "$Attribute BETWEEN $Values[0] AND $Values[1]";
|
139
|
|
- return $this;
|
140
|
|
- }
|
141
|
149
|
|
142
|
|
- /**
|
143
|
|
- * Add fulltext query expression. Calling multiple filter functions results in boolean AND between each condition.
|
144
|
|
- * Query expression is escaped automatically
|
145
|
|
- *
|
146
|
|
- * @param string $Expr query expression
|
147
|
|
- * @param string $Field field to match $Expr against. Default is *, which means all available fields
|
148
|
|
- * @return current Sphinxql query object
|
149
|
|
- */
|
150
|
|
- public function where_match($Expr, $Field = '*', $Escape = true) {
|
151
|
|
- if (empty($Expr)) {
|
152
|
|
- return $this;
|
153
|
|
- }
|
154
|
|
- if ($Field !== false) {
|
155
|
|
- $Field = "@$Field ";
|
156
|
|
- }
|
157
|
|
- if ($Escape === true) {
|
158
|
|
- $this->Expressions[] = "$Field".Sphinxql::sph_escape_string($Expr);
|
159
|
|
- } else {
|
160
|
|
- $this->Expressions[] = $Field.$Expr;
|
|
150
|
+ /**
|
|
151
|
+ * Add fulltext query expression. Calling multiple filter functions results in boolean AND between each condition.
|
|
152
|
+ * Query expression is escaped automatically
|
|
153
|
+ *
|
|
154
|
+ * @param string $Expr query expression
|
|
155
|
+ * @param string $Field field to match $Expr against. Default is *, which means all available fields
|
|
156
|
+ * @return current Sphinxql query object
|
|
157
|
+ */
|
|
158
|
+ public function where_match($Expr, $Field = '*', $Escape = true)
|
|
159
|
+ {
|
|
160
|
+ if (empty($Expr)) {
|
|
161
|
+ return $this;
|
|
162
|
+ }
|
|
163
|
+ if ($Field !== false) {
|
|
164
|
+ $Field = "@$Field ";
|
|
165
|
+ }
|
|
166
|
+ if ($Escape === true) {
|
|
167
|
+ $this->Expressions[] = "$Field".Sphinxql::sph_escape_string($Expr);
|
|
168
|
+ } else {
|
|
169
|
+ $this->Expressions[] = $Field.$Expr;
|
|
170
|
+ }
|
|
171
|
+ return $this;
|
161
|
172
|
}
|
162
|
|
- return $this;
|
163
|
|
- }
|
164
|
173
|
|
165
|
|
- /**
|
166
|
|
- * Specify the order of the matches. Calling this function multiple times sets secondary priorities
|
167
|
|
- *
|
168
|
|
- * @param string $Attribute attribute to use for sorting.
|
169
|
|
- * Passing an empty attribute value will clear the current sort settings
|
170
|
|
- * @param string $Mode sort method to apply to the selected attribute
|
171
|
|
- * @return current Sphinxql query object
|
172
|
|
- */
|
173
|
|
- public function order_by($Attribute = false, $Mode = false) {
|
174
|
|
- if (empty($Attribute)) {
|
175
|
|
- $this->SortBy = [];
|
176
|
|
- } else {
|
177
|
|
- $this->SortBy[] = "$Attribute $Mode";
|
|
174
|
+ /**
|
|
175
|
+ * Specify the order of the matches. Calling this function multiple times sets secondary priorities
|
|
176
|
+ *
|
|
177
|
+ * @param string $Attribute attribute to use for sorting.
|
|
178
|
+ * Passing an empty attribute value will clear the current sort settings
|
|
179
|
+ * @param string $Mode sort method to apply to the selected attribute
|
|
180
|
+ * @return current Sphinxql query object
|
|
181
|
+ */
|
|
182
|
+ public function order_by($Attribute = false, $Mode = false)
|
|
183
|
+ {
|
|
184
|
+ if (empty($Attribute)) {
|
|
185
|
+ $this->SortBy = [];
|
|
186
|
+ } else {
|
|
187
|
+ $this->SortBy[] = "$Attribute $Mode";
|
|
188
|
+ }
|
|
189
|
+ return $this;
|
178
|
190
|
}
|
179
|
|
- return $this;
|
180
|
|
- }
|
181
|
191
|
|
182
|
|
- /**
|
183
|
|
- * Specify how the results are grouped
|
184
|
|
- *
|
185
|
|
- * @param string $Attribute group matches with the same $Attribute value.
|
186
|
|
- * Passing an empty attribute value will clear the current group settings
|
187
|
|
- * @return current Sphinxql query object
|
188
|
|
- */
|
189
|
|
- public function group_by($Attribute = false) {
|
190
|
|
- if (empty($Attribute)) {
|
191
|
|
- $this->GroupBy = '';
|
192
|
|
- } else {
|
193
|
|
- $this->GroupBy = $Attribute;
|
|
192
|
+ /**
|
|
193
|
+ * Specify how the results are grouped
|
|
194
|
+ *
|
|
195
|
+ * @param string $Attribute group matches with the same $Attribute value.
|
|
196
|
+ * Passing an empty attribute value will clear the current group settings
|
|
197
|
+ * @return current Sphinxql query object
|
|
198
|
+ */
|
|
199
|
+ public function group_by($Attribute = false)
|
|
200
|
+ {
|
|
201
|
+ if (empty($Attribute)) {
|
|
202
|
+ $this->GroupBy = '';
|
|
203
|
+ } else {
|
|
204
|
+ $this->GroupBy = $Attribute;
|
|
205
|
+ }
|
|
206
|
+ return $this;
|
194
|
207
|
}
|
195
|
|
- return $this;
|
196
|
|
- }
|
197
|
208
|
|
198
|
|
- /**
|
199
|
|
- * Specify the order of the results within groups
|
200
|
|
- *
|
201
|
|
- * @param string $Attribute attribute to use for sorting.
|
202
|
|
- * Passing an empty attribute will clear the current group sort settings
|
203
|
|
- * @param string $Mode sort method to apply to the selected attribute
|
204
|
|
- * @return current Sphinxql query object
|
205
|
|
- */
|
206
|
|
- public function order_group_by($Attribute = false, $Mode = false) {
|
207
|
|
- if (empty($Attribute)) {
|
208
|
|
- $this->SortGroupBy = '';
|
209
|
|
- } else {
|
210
|
|
- $this->SortGroupBy = "$Attribute $Mode";
|
|
209
|
+ /**
|
|
210
|
+ * Specify the order of the results within groups
|
|
211
|
+ *
|
|
212
|
+ * @param string $Attribute attribute to use for sorting.
|
|
213
|
+ * Passing an empty attribute will clear the current group sort settings
|
|
214
|
+ * @param string $Mode sort method to apply to the selected attribute
|
|
215
|
+ * @return current Sphinxql query object
|
|
216
|
+ */
|
|
217
|
+ public function order_group_by($Attribute = false, $Mode = false)
|
|
218
|
+ {
|
|
219
|
+ if (empty($Attribute)) {
|
|
220
|
+ $this->SortGroupBy = '';
|
|
221
|
+ } else {
|
|
222
|
+ $this->SortGroupBy = "$Attribute $Mode";
|
|
223
|
+ }
|
|
224
|
+ return $this;
|
211
|
225
|
}
|
212
|
|
- return $this;
|
213
|
|
- }
|
214
|
|
-
|
215
|
|
- /**
|
216
|
|
- * Specify the offset and amount of matches to return
|
217
|
|
- *
|
218
|
|
- * @param int $Offset number of matches to discard
|
219
|
|
- * @param int $Limit number of matches to return
|
220
|
|
- * @param int $MaxMatches number of results to store in the Sphinx server's memory. Must be >= ($Offset+$Limit)
|
221
|
|
- * @return current Sphinxql query object
|
222
|
|
- */
|
223
|
|
- public function limit($Offset, $Limit, $MaxMatches = SPHINX_MAX_MATCHES) {
|
224
|
|
- $this->Limits = "$Offset, $Limit";
|
225
|
|
- $this->set('max_matches', $MaxMatches);
|
226
|
|
- return $this;
|
227
|
|
- }
|
228
|
|
-
|
229
|
|
- /**
|
230
|
|
- * Tweak the settings to use for the query. Sanity checking shouldn't be needed as Sphinx already does it
|
231
|
|
- *
|
232
|
|
- * @param string $Name setting name
|
233
|
|
- * @param mixed $Value value
|
234
|
|
- * @return current Sphinxql query object
|
235
|
|
- */
|
236
|
|
- public function set($Name, $Value) {
|
237
|
|
- $this->Options[$Name] = $Value;
|
238
|
|
- return $this;
|
239
|
|
- }
|
240
|
226
|
|
241
|
|
- /**
|
242
|
|
- * Combine the query options into a valid Sphinx query segment
|
243
|
|
- *
|
244
|
|
- * @return string of options
|
245
|
|
- */
|
246
|
|
- private function build_options() {
|
247
|
|
- $Options = [];
|
248
|
|
- foreach ($this->Options as $Option => $Value) {
|
249
|
|
- $Options[] = "$Option = $Value";
|
|
227
|
+ /**
|
|
228
|
+ * Specify the offset and amount of matches to return
|
|
229
|
+ *
|
|
230
|
+ * @param int $Offset number of matches to discard
|
|
231
|
+ * @param int $Limit number of matches to return
|
|
232
|
+ * @param int $MaxMatches number of results to store in the Sphinx server's memory. Must be >= ($Offset+$Limit)
|
|
233
|
+ * @return current Sphinxql query object
|
|
234
|
+ */
|
|
235
|
+ public function limit($Offset, $Limit, $MaxMatches = SPHINX_MAX_MATCHES)
|
|
236
|
+ {
|
|
237
|
+ $this->Limits = "$Offset, $Limit";
|
|
238
|
+ $this->set('max_matches', $MaxMatches);
|
|
239
|
+ return $this;
|
250
|
240
|
}
|
251
|
|
- return implode(', ', $Options);
|
252
|
|
- }
|
253
|
241
|
|
254
|
|
- /**
|
255
|
|
- * Combine the query conditions into a valid Sphinx query segment
|
256
|
|
- */
|
257
|
|
- private function build_query() {
|
258
|
|
- if (!$this->Indexes) {
|
259
|
|
- $this->error('Index name is required.');
|
260
|
|
- return false;
|
261
|
|
- }
|
262
|
|
- $this->QueryString = "SELECT $this->Select\nFROM $this->Indexes";
|
263
|
|
- if (!empty($this->Expressions)) {
|
264
|
|
- $this->Filters['expr'] = "MATCH('".implode(' ', $this->Expressions)."')";
|
265
|
|
- }
|
266
|
|
- if (!empty($this->Filters)) {
|
267
|
|
- $this->QueryString .= "\nWHERE ".implode("\n\tAND ", $this->Filters);
|
268
|
|
- }
|
269
|
|
- if (!empty($this->GroupBy)) {
|
270
|
|
- $this->QueryString .= "\nGROUP BY $this->GroupBy";
|
271
|
|
- }
|
272
|
|
- if (!empty($this->SortGroupBy)) {
|
273
|
|
- $this->QueryString .= "\nWITHIN GROUP ORDER BY $this->SortGroupBy";
|
274
|
|
- }
|
275
|
|
- if (!empty($this->SortBy)) {
|
276
|
|
- $this->QueryString .= "\nORDER BY ".implode(", ", $this->SortBy);
|
277
|
|
- }
|
278
|
|
- if (!empty($this->Limits)) {
|
279
|
|
- $this->QueryString .= "\nLIMIT $this->Limits";
|
|
242
|
+ /**
|
|
243
|
+ * Tweak the settings to use for the query. Sanity checking shouldn't be needed as Sphinx already does it
|
|
244
|
+ *
|
|
245
|
+ * @param string $Name setting name
|
|
246
|
+ * @param mixed $Value value
|
|
247
|
+ * @return current Sphinxql query object
|
|
248
|
+ */
|
|
249
|
+ public function set($Name, $Value)
|
|
250
|
+ {
|
|
251
|
+ $this->Options[$Name] = $Value;
|
|
252
|
+ return $this;
|
280
|
253
|
}
|
281
|
|
- if (!empty($this->Options)) {
|
282
|
|
- $Options = $this->build_options();
|
283
|
|
- $this->QueryString .= "\nOPTION $Options";
|
|
254
|
+
|
|
255
|
+ /**
|
|
256
|
+ * Combine the query options into a valid Sphinx query segment
|
|
257
|
+ *
|
|
258
|
+ * @return string of options
|
|
259
|
+ */
|
|
260
|
+ private function build_options()
|
|
261
|
+ {
|
|
262
|
+ $Options = [];
|
|
263
|
+ foreach ($this->Options as $Option => $Value) {
|
|
264
|
+ $Options[] = "$Option = $Value";
|
|
265
|
+ }
|
|
266
|
+ return implode(', ', $Options);
|
284
|
267
|
}
|
285
|
|
- }
|
286
|
268
|
|
287
|
|
- /**
|
288
|
|
- * Construct and send the query. Register the query in the global Sphinxql object
|
289
|
|
- *
|
290
|
|
- * @param bool GetMeta whether to fetch meta data for the executed query. Default is yes
|
291
|
|
- * @return Sphinxql result object
|
292
|
|
- */
|
293
|
|
- public function query($GetMeta = true) {
|
294
|
|
- $QueryStartTime = microtime(true);
|
295
|
|
- $this->build_query();
|
296
|
|
- if (count($this->Errors) > 0) {
|
297
|
|
- $ErrorMsg = implode("\n", $this->Errors);
|
298
|
|
- $this->Sphinxql->error("Query builder found errors:\n$ErrorMsg");
|
299
|
|
- return new SphinxqlResult(null, null, 1, $ErrorMsg);
|
|
269
|
+ /**
|
|
270
|
+ * Combine the query conditions into a valid Sphinx query segment
|
|
271
|
+ */
|
|
272
|
+ private function build_query()
|
|
273
|
+ {
|
|
274
|
+ if (!$this->Indexes) {
|
|
275
|
+ $this->error('Index name is required.');
|
|
276
|
+ return false;
|
|
277
|
+ }
|
|
278
|
+ $this->QueryString = "SELECT $this->Select\nFROM $this->Indexes";
|
|
279
|
+ if (!empty($this->Expressions)) {
|
|
280
|
+ $this->Filters['expr'] = "MATCH('".implode(' ', $this->Expressions)."')";
|
|
281
|
+ }
|
|
282
|
+ if (!empty($this->Filters)) {
|
|
283
|
+ $this->QueryString .= "\nWHERE ".implode("\n\tAND ", $this->Filters);
|
|
284
|
+ }
|
|
285
|
+ if (!empty($this->GroupBy)) {
|
|
286
|
+ $this->QueryString .= "\nGROUP BY $this->GroupBy";
|
|
287
|
+ }
|
|
288
|
+ if (!empty($this->SortGroupBy)) {
|
|
289
|
+ $this->QueryString .= "\nWITHIN GROUP ORDER BY $this->SortGroupBy";
|
|
290
|
+ }
|
|
291
|
+ if (!empty($this->SortBy)) {
|
|
292
|
+ $this->QueryString .= "\nORDER BY ".implode(", ", $this->SortBy);
|
|
293
|
+ }
|
|
294
|
+ if (!empty($this->Limits)) {
|
|
295
|
+ $this->QueryString .= "\nLIMIT $this->Limits";
|
|
296
|
+ }
|
|
297
|
+ if (!empty($this->Options)) {
|
|
298
|
+ $Options = $this->build_options();
|
|
299
|
+ $this->QueryString .= "\nOPTION $Options";
|
|
300
|
+ }
|
300
|
301
|
}
|
301
|
|
- $QueryString = $this->QueryString;
|
302
|
|
- $Result = $this->send_query($GetMeta);
|
303
|
|
- $QueryProcessTime = (microtime(true) - $QueryStartTime)*1000;
|
304
|
|
- Sphinxql::register_query($QueryString, $QueryProcessTime);
|
305
|
|
- return $Result;
|
306
|
|
- }
|
307
|
302
|
|
308
|
|
- /**
|
309
|
|
- * Run a manually constructed query
|
310
|
|
- *
|
311
|
|
- * @param string Query query expression
|
312
|
|
- * @param bool GetMeta whether to fetch meta data for the executed query. Default is yes
|
313
|
|
- * @return Sphinxql result object
|
314
|
|
- */
|
315
|
|
- public function raw_query($Query, $GetMeta = true) {
|
316
|
|
- $this->QueryString = $Query;
|
317
|
|
- return $this->send_query($GetMeta);
|
318
|
|
- }
|
|
303
|
+ /**
|
|
304
|
+ * Construct and send the query. Register the query in the global Sphinxql object
|
|
305
|
+ *
|
|
306
|
+ * @param bool GetMeta whether to fetch meta data for the executed query. Default is yes
|
|
307
|
+ * @return Sphinxql result object
|
|
308
|
+ */
|
|
309
|
+ public function query($GetMeta = true)
|
|
310
|
+ {
|
|
311
|
+ $QueryStartTime = microtime(true);
|
|
312
|
+ $this->build_query();
|
|
313
|
+ if (count($this->Errors) > 0) {
|
|
314
|
+ $ErrorMsg = implode("\n", $this->Errors);
|
|
315
|
+ $this->Sphinxql->error("Query builder found errors:\n$ErrorMsg");
|
|
316
|
+ return new SphinxqlResult(null, null, 1, $ErrorMsg);
|
|
317
|
+ }
|
|
318
|
+ $QueryString = $this->QueryString;
|
|
319
|
+ $Result = $this->send_query($GetMeta);
|
|
320
|
+ $QueryProcessTime = (microtime(true) - $QueryStartTime)*1000;
|
|
321
|
+ Sphinxql::register_query($QueryString, $QueryProcessTime);
|
|
322
|
+ return $Result;
|
|
323
|
+ }
|
319
|
324
|
|
320
|
|
- /**
|
321
|
|
- * Run a pre-processed query. Only used internally
|
322
|
|
- *
|
323
|
|
- * @param bool GetMeta whether to fetch meta data for the executed query
|
324
|
|
- * @return Sphinxql result object
|
325
|
|
- */
|
326
|
|
- private function send_query($GetMeta) {
|
327
|
|
- if (!$this->QueryString) {
|
328
|
|
- return false;
|
|
325
|
+ /**
|
|
326
|
+ * Run a manually constructed query
|
|
327
|
+ *
|
|
328
|
+ * @param string Query query expression
|
|
329
|
+ * @param bool GetMeta whether to fetch meta data for the executed query. Default is yes
|
|
330
|
+ * @return Sphinxql result object
|
|
331
|
+ */
|
|
332
|
+ public function raw_query($Query, $GetMeta = true)
|
|
333
|
+ {
|
|
334
|
+ $this->QueryString = $Query;
|
|
335
|
+ return $this->send_query($GetMeta);
|
329
|
336
|
}
|
330
|
|
- $this->Sphinxql->sph_connect();
|
331
|
|
- $Result = $this->Sphinxql->query($this->QueryString);
|
332
|
|
- if ($Result === false) {
|
333
|
|
- $Errno = $this->Sphinxql->errno;
|
334
|
|
- $Error = $this->Sphinxql->error;
|
335
|
|
- $this->Sphinxql->error("Query returned error $Errno ($Error).\n$this->QueryString");
|
336
|
|
- $Meta = null;
|
337
|
|
- } else {
|
338
|
|
- $Errno = 0;
|
339
|
|
- $Error = '';
|
340
|
|
- $Meta = $GetMeta ? $this->get_meta() : null;
|
|
337
|
+
|
|
338
|
+ /**
|
|
339
|
+ * Run a pre-processed query. Only used internally
|
|
340
|
+ *
|
|
341
|
+ * @param bool GetMeta whether to fetch meta data for the executed query
|
|
342
|
+ * @return Sphinxql result object
|
|
343
|
+ */
|
|
344
|
+ private function send_query($GetMeta)
|
|
345
|
+ {
|
|
346
|
+ if (!$this->QueryString) {
|
|
347
|
+ return false;
|
|
348
|
+ }
|
|
349
|
+ $this->Sphinxql->sph_connect();
|
|
350
|
+ $Result = $this->Sphinxql->query($this->QueryString);
|
|
351
|
+ if ($Result === false) {
|
|
352
|
+ $Errno = $this->Sphinxql->errno;
|
|
353
|
+ $Error = $this->Sphinxql->error;
|
|
354
|
+ $this->Sphinxql->error("Query returned error $Errno ($Error).\n$this->QueryString");
|
|
355
|
+ $Meta = null;
|
|
356
|
+ } else {
|
|
357
|
+ $Errno = 0;
|
|
358
|
+ $Error = '';
|
|
359
|
+ $Meta = $GetMeta ? $this->get_meta() : null;
|
|
360
|
+ }
|
|
361
|
+ return new SphinxqlResult($Result, $Meta, $Errno, $Error);
|
341
|
362
|
}
|
342
|
|
- return new SphinxqlResult($Result, $Meta, $Errno, $Error);
|
343
|
|
- }
|
344
|
363
|
|
345
|
|
- /**
|
346
|
|
- * Reset all query options and conditions
|
347
|
|
- */
|
348
|
|
- public function reset() {
|
349
|
|
- $this->Errors = [];
|
350
|
|
- $this->Expressions = [];
|
351
|
|
- $this->Filters = [];
|
352
|
|
- $this->GroupBy = '';
|
353
|
|
- $this->Indexes = '';
|
354
|
|
- $this->Limits = [];
|
355
|
|
- $this->Options = array('ranker' => 'none');
|
356
|
|
- $this->QueryString = '';
|
357
|
|
- $this->Select = '*';
|
358
|
|
- $this->SortBy = [];
|
359
|
|
- $this->SortGroupBy = '';
|
360
|
|
- }
|
|
364
|
+ /**
|
|
365
|
+ * Reset all query options and conditions
|
|
366
|
+ */
|
|
367
|
+ public function reset()
|
|
368
|
+ {
|
|
369
|
+ $this->Errors = [];
|
|
370
|
+ $this->Expressions = [];
|
|
371
|
+ $this->Filters = [];
|
|
372
|
+ $this->GroupBy = '';
|
|
373
|
+ $this->Indexes = '';
|
|
374
|
+ $this->Limits = [];
|
|
375
|
+ $this->Options = array('ranker' => 'none');
|
|
376
|
+ $this->QueryString = '';
|
|
377
|
+ $this->Select = '*';
|
|
378
|
+ $this->SortBy = [];
|
|
379
|
+ $this->SortGroupBy = '';
|
|
380
|
+ }
|
361
|
381
|
|
362
|
|
- /**
|
363
|
|
- * Fetch and store meta data for the last executed query
|
364
|
|
- *
|
365
|
|
- * @return meta data
|
366
|
|
- */
|
367
|
|
- private function get_meta() {
|
368
|
|
- return $this->raw_query("SHOW META", false)->to_pair(0, 1);
|
369
|
|
- }
|
|
382
|
+ /**
|
|
383
|
+ * Fetch and store meta data for the last executed query
|
|
384
|
+ *
|
|
385
|
+ * @return meta data
|
|
386
|
+ */
|
|
387
|
+ private function get_meta()
|
|
388
|
+ {
|
|
389
|
+ return $this->raw_query("SHOW META", false)->to_pair(0, 1);
|
|
390
|
+ }
|
370
|
391
|
|
371
|
|
- /**
|
372
|
|
- * Copy attribute filters from another SphinxqlQuery object
|
373
|
|
- *
|
374
|
|
- * @param SphinxqlQuery $SphQLSource object to copy the filters from
|
375
|
|
- * @return current SphinxqlQuery object
|
376
|
|
- */
|
377
|
|
- public function copy_attributes_from($SphQLSource) {
|
378
|
|
- $this->Filters = $SphQLSource->Filters;
|
379
|
|
- }
|
|
392
|
+ /**
|
|
393
|
+ * Copy attribute filters from another SphinxqlQuery object
|
|
394
|
+ *
|
|
395
|
+ * @param SphinxqlQuery $SphQLSource object to copy the filters from
|
|
396
|
+ * @return current SphinxqlQuery object
|
|
397
|
+ */
|
|
398
|
+ public function copy_attributes_from($SphQLSource)
|
|
399
|
+ {
|
|
400
|
+ $this->Filters = $SphQLSource->Filters;
|
|
401
|
+ }
|
380
|
402
|
|
381
|
|
- /**
|
382
|
|
- * Store error messages
|
383
|
|
- */
|
384
|
|
- private function error($Msg) {
|
385
|
|
- $this->Errors[] = $Msg;
|
386
|
|
- }
|
|
403
|
+ /**
|
|
404
|
+ * Store error messages
|
|
405
|
+ */
|
|
406
|
+ private function error($Msg)
|
|
407
|
+ {
|
|
408
|
+ $this->Errors[] = $Msg;
|
|
409
|
+ }
|
387
|
410
|
}
|