11
11
*/
12
12
class SearchController{
13
13
/**
14
- * @var string SQL patterns
15
- */
14
+ * @var string SQL patterns
15
+ */
16
16
public const START_WITH_QUERY = "query% " ;
17
17
public const END_WITH_QUERY = "%query " ;
18
18
public const HAVE_ANY_QUERY = "%query% " ;
@@ -22,62 +22,59 @@ class SearchController{
22
22
public const START_END_WITH_QUERY = "query%query " ;
23
23
24
24
/**
25
- * @var string SearchController algorithms
26
- */
25
+ * @var string SearchController algorithms
26
+ */
27
27
public const OR = "OR " ;
28
28
public const AND = "AND " ;
29
29
public const NAND = "NAND " ;
30
30
public const NOR = "NOR " ;
31
31
32
32
/**
33
- * @var string SQL search keywords
34
- */
33
+ * @var string SQL search keywords
34
+ */
35
35
public const LIKE = "LIKE " ;
36
36
public const NOT_LIKE = "NOT LIKE " ;
37
37
38
38
/**
39
- * @var string SQL Query
40
- */
39
+ * @var string SQL Query
40
+ */
41
41
private $ QueryCondition = "" ;
42
42
43
43
/**
44
- * @var string Search query algorithm that needs to be used
45
- */
44
+ * @var string Search query algorithm that needs to be used
45
+ */
46
46
private $ searchAlgorithm ;
47
47
48
48
/**
49
- * @var string Search request query value
50
- */
49
+ * @var string Search request query value
50
+ */
51
51
private $ searchQuery = null ;
52
52
53
53
/**
54
- * @var array MYSQL database table rows to search from
55
- */
54
+ * @var array MYSQL database table rows to search from
55
+ */
56
56
private $ paramArray = array ();
57
57
58
58
/**
59
- * @var string MYSQL database table row for tag value
60
- */
59
+ * @var string MYSQL database table row for tag value
60
+ */
61
61
private $ paramTags ;
62
62
63
63
/**
64
- * @var string SQL LIKE query operator to be use
65
- */
64
+ * @var string SQL LIKE query operator to be use
65
+ */
66
66
private $ operators ;
67
67
68
68
/**
69
- * @var string SQL query prefix
70
- */
69
+ * @var string SQL query prefix
70
+ */
71
71
private $ queryStart ;
72
72
73
73
/**
74
- * @var string SQL query suffix
75
- */
74
+ * @var string SQL query suffix
75
+ */
76
76
private $ queryEnd ;
77
77
78
- /**
79
- * Constructor
80
- */
81
78
public function __construct ($ algorithm = self ::OR ) {
82
79
$ this ->searchAlgorithm = $ algorithm ;
83
80
$ this ->operators = self ::END_WITH_QUERY ;
@@ -86,67 +83,78 @@ public function __construct($algorithm = self::OR) {
86
83
}
87
84
88
85
/**
89
- * Set database search table columns.
90
- *
91
- * @param array $param columns
92
- */
86
+ * Set database search table columns.
87
+ *
88
+ * @param array $param columns
89
+ */
93
90
public function setParameter ($ param =array ()){
94
91
$ this ->paramArray = $ param ;
92
+ return $ this ;
95
93
}
96
94
97
95
/**
98
- * Set initial SQL queries.
99
- * @param string $query query
100
- */
96
+ * Set initial SQL queries.
97
+ *
98
+ * @param string $query query
99
+ */
101
100
public function setSQLQuery ($ query ){
102
101
$ this ->QueryCondition = $ query ;
103
102
}
104
103
105
104
/**
106
- * Set database search operator pattern.
107
- * @param string $operator name
108
- */
109
- public function setOperators ($ operator ){
110
- $ this ->operators = $ operator ;
105
+ * Set database search operator pattern.
106
+ *
107
+ * @param string $pattern name
108
+ */
109
+ public function setOperators ($ pattern ){
110
+ $ this ->operators = $ pattern ;
111
+ return $ this ;
111
112
}
112
113
113
114
/**
114
- * Set database tag table column name.
115
- * @param string $column name
116
- */
115
+ * Set database tag table column name.
116
+ *
117
+ * @param string $column name
118
+ */
117
119
public function setTags ($ column ){
118
120
$ this ->paramTags = $ column ;
121
+ return $ this ;
119
122
}
120
123
121
124
/**
122
- * Set search query value.
123
- * @param string $query query value
124
- * @return object|SearchController
125
- */
125
+ * Set search query value.
126
+ *
127
+ * @param string $query query value
128
+ * @return object|SearchController
129
+ */
126
130
public function setQuery ($ query ){
127
131
$ this ->searchQuery = htmlspecialchars ($ query , ENT_QUOTES , "UTF-8 " );
128
132
return $ this ;
129
133
}
130
134
131
135
/**
132
- * Set query prefix string.
133
- * @param string $str query prefix
134
- */
136
+ * Set query prefix string.
137
+ *
138
+ * @param string $str query prefix
139
+ */
135
140
public function setStart ($ str ){
136
141
$ this ->queryStart = $ str ;
142
+ return $ this ;
137
143
}
138
144
139
145
/**
140
- * Set query suffix string.
141
- * @param string $str query suffix
142
- */
146
+ * Set query suffix string.
147
+ *
148
+ * @param string $str query suffix
149
+ */
143
150
public function setEnd ($ str ){
144
151
$ this ->queryEnd = $ str ;
152
+ return $ this ;
145
153
}
146
154
147
155
/**
148
- * Split search query value by space.
149
- */
156
+ * Split search query value by space.
157
+ */
150
158
public function split (){
151
159
if (strpos ($ this ->searchQuery , " " ) !== false ) {
152
160
$ this ->searchQuery = explode (" " , $ this ->searchQuery );
@@ -156,10 +164,11 @@ public function split(){
156
164
}
157
165
158
166
/**
159
- * Create SQL query from the specified pattern.
160
- * @param string $value query value
161
- * @return string query
162
- */
167
+ * Create SQL query from the specified pattern.
168
+ *
169
+ * @param string $value query value
170
+ * @return string query
171
+ */
163
172
164
173
private function format ($ value ) {
165
174
$ queryString = "" ;
@@ -170,26 +179,19 @@ private function format($value) {
170
179
return $ queryString ;
171
180
}
172
181
173
- /**
174
- * Builds SQL search query.
175
- * @return string query
176
- */
177
182
private function buildQuery (){
178
183
return rtrim ($ this ->format ($ this ->searchQuery ) , " {$ this ->queryEnd } " );
179
184
}
180
185
181
- /**
182
- * Builds SQL search query array.
183
- * @return array queries
184
- */
185
186
private function buildArrayQuery ($ i = 0 ){
186
187
return rtrim ($ this ->format ($ this ->searchQuery [$ i ]) , " {$ this ->queryEnd } " );;
187
188
}
188
189
189
190
/**
190
- * Determine which search method to use while creating query.
191
- * @return string SQL query
192
- */
191
+ * Determine which search method to use while creating query.
192
+ *
193
+ * @return string SQL query
194
+ */
193
195
private function buildSQL (){
194
196
$ sql = "" ;
195
197
if (!empty ($ this ->paramTags )){
@@ -218,71 +220,57 @@ private function buildSQL(){
218
220
}
219
221
220
222
/**
221
- * Gernerates the search query.
222
- * @param string $checkOpt the search query start operator
223
- * @return string SQL query
224
- */
225
- public function generateQuery ( $ checkOpt = " AUTO " ){
223
+ * Execute search query.
224
+ *
225
+ * @return string SQL query
226
+ */
227
+ public function getQuery ( ){
226
228
if (!empty ($ this ->searchQuery )){
227
229
if (!empty ($ this ->searchQuery )){
228
- if ($ checkOpt != "AUTO " && empty ($ this ->QueryCondition )){
229
- $ this ->QueryCondition .= " {$ checkOpt } ( " ;
230
- }else {
231
- $ this ->QueryCondition .= (!empty ($ this ->QueryCondition ) ? " AND ( " : " WHERE ( " );
232
- }
230
+ if (!empty ($ this ->QueryCondition )){
231
+ $ this ->QueryCondition .= " AND ( " ;
232
+ }else {
233
+ $ this ->QueryCondition .= " WHERE ( " ;
234
+ }
235
+
233
236
switch ($ this ->searchAlgorithm ){
237
+
234
238
case self ::OR :
235
239
$ this ->setStart (self ::LIKE );
236
240
$ this ->setEnd (self ::OR );
241
+ $ this ->QueryCondition .= $ this ->buildSQL ();
242
+ $ this ->QueryCondition .= " ) " ;
237
243
break ;
238
-
244
+
239
245
case self ::AND :
240
246
$ this ->setStart (self ::LIKE );
241
247
$ this ->setEnd (self ::AND );
248
+ $ this ->QueryCondition .= $ this ->buildSQL ();
249
+ $ this ->QueryCondition .= " ) " ;
242
250
break ;
243
-
251
+
244
252
case self ::NAND :
245
253
$ this ->setStart (self ::NOT_LIKE );
246
254
$ this ->setEnd (self ::AND );
255
+ $ this ->QueryCondition .= $ this ->buildSQL ();
256
+ $ this ->QueryCondition .= " ) " ;
247
257
break ;
248
-
258
+
249
259
case self ::NOR :
250
260
$ this ->setStart (self ::NOT_LIKE );
251
261
$ this ->setEnd (self ::OR );
262
+ $ this ->QueryCondition .= $ this ->buildSQL ();
263
+ $ this ->QueryCondition .= " ) " ;
252
264
break ;
253
265
default :
254
266
$ this ->setStart (self ::LIKE );
255
267
$ this ->setEnd (self ::OR );
268
+ $ this ->QueryCondition .= $ this ->buildSQL ();
269
+ $ this ->QueryCondition .= " ) " ;
256
270
break ;
257
271
}
258
- $ this ->QueryCondition .= $ this ->buildSQL ();
259
- $ this ->QueryCondition .= " ) " ;
260
272
}
261
273
}
262
274
return $ this ->QueryCondition ;
263
275
}
264
-
265
- /**
266
- * Gets sql search query by apending AND clause.
267
- * @return string SQL query
268
- */
269
- public function getAndQuery (){
270
- return $ this ->generateQuery ("AND " );
271
- }
272
-
273
- /**
274
- * Gets sql search query by apending WHERE clause.
275
- * @return string SQL query
276
- */
277
- public function getWhereQuery (){
278
- return $ this ->generateQuery ("WHERE " );
279
- }
280
-
281
- /**
282
- * Gets sql search query by automatically detecting and apending start clause.
283
- * @return string SQL query
284
- */
285
- public function getQuery (){
286
- return $ this ->generateQuery ();
287
- }
288
276
}
0 commit comments