Completed
Push — master ( 4aa15f...354c04 )
by Arjay
02:26
created

QueryBuilderEngine::totalCount()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 4
rs 10
nc 1
cc 1
eloc 2
nop 0
1
<?php
2
3
namespace Yajra\Datatables\Engines;
4
5
/**
6
 * Laravel Datatables Query Builder Engine
7
 *
8
 * @package  Laravel
9
 * @category Package
10
 * @author   Arjay Angeles <[email protected]>
11
 */
12
13
use Closure;
14
use Illuminate\Database\Query\Builder;
15
use Illuminate\Support\Str;
16
use Yajra\Datatables\Contracts\DataTableEngineContract;
17
use Yajra\Datatables\Helper;
18
use Yajra\Datatables\Request;
19
20
class QueryBuilderEngine extends BaseEngine implements DataTableEngineContract
21
{
22
    /**
23
     * @param \Illuminate\Database\Query\Builder $builder
24
     * @param \Yajra\Datatables\Request $request
25
     */
26
    public function __construct(Builder $builder, Request $request)
27
    {
28
        $this->query = $builder;
29
        $this->init($request, $builder);
30
    }
31
32
    /**
33
     * Initialize attributes.
34
     *
35
     * @param  \Yajra\Datatables\Request $request
36
     * @param  \Illuminate\Database\Query\Builder $builder
37
     * @param  string $type
38
     */
39
    protected function init($request, $builder, $type = 'builder')
0 ignored issues
show
Bug introduced by
You have injected the Request via parameter $request. This is generally not recommended as there might be multiple instances during a request cycle (f.e. when using sub-requests). Instead, it is recommended to inject the RequestStack and retrieve the current request each time you need it via getCurrentRequest().
Loading history...
40
    {
41
        $this->request    = $request;
42
        $this->query_type = $type;
43
        $this->columns    = $builder->columns;
44
        $this->connection = $builder->getConnection();
45
        $this->prefix     = $this->connection->getTablePrefix();
46
        $this->database   = $this->connection->getDriverName();
47
        if ($this->isDebugging()) {
48
            $this->connection->enableQueryLog();
49
        }
50
    }
51
52
    /**
53
     * @inheritdoc
54
     */
55
    public function filter(Closure $callback)
56
    {
57
        $this->overrideGlobalSearch($callback, $this->query);
58
59
        return $this;
60
    }
61
62
    /**
63
     * @inheritdoc
64
     */
65
    public function make($mDataSupport = false, $orderFirst = false)
66
    {
67
        return parent::make($mDataSupport, $orderFirst);
68
    }
69
70
    /**
71
     * @inheritdoc
72
     */
73
    public function totalCount()
74
    {
75
        return $this->count();
76
    }
77
78
    /**
79
     * Counts current query.
80
     *
81
     * @return int
82
     */
83
    public function count()
84
    {
85
        $myQuery = clone $this->query;
86
        // if its a normal query ( no union, having and distinct word )
87
        // replace the select with static text to improve performance
88
        if (! Str::contains(Str::lower($myQuery->toSql()), ['union', 'having', 'distinct'])) {
89
            $row_count = $this->connection->getQueryGrammar()->wrap('row_count');
90
            $myQuery->select($this->connection->raw("'1' as {$row_count}"));
91
        }
92
93
        return $this->connection->table($this->connection->raw('(' . $myQuery->toSql() . ') count_row_table'))
94
                                ->setBindings($myQuery->getBindings())->count();
95
    }
96
97
    /**
98
     * @inheritdoc
99
     */
100
    public function filtering()
101
    {
102
        $this->query->where(
103
            function ($query) {
104
                $keyword = $this->setupKeyword($this->request->keyword());
105
                foreach ($this->request->searchableColumnIndex() as $index) {
106
                    $columnName = $this->setupColumnName($index);
107
108
                    if (isset($this->columnDef['filter'][$columnName])) {
109
                        $method     = Helper::getOrMethod($this->columnDef['filter'][$columnName]['method']);
110
                        $parameters = $this->columnDef['filter'][$columnName]['parameters'];
111
                        $this->compileColumnQuery(
112
                            $this->getQueryBuilder($query), $method, $parameters, $columnName, $keyword
113
                        );
114
                    } else {
115
                        $this->compileGlobalSearch($this->getQueryBuilder($query), $columnName, $keyword);
116
                    }
117
118
                    $this->isFilterApplied = true;
119
                }
120
            }
121
        );
122
    }
123
124
    /**
125
     * Perform filter column on selected field.
126
     *
127
     * @param mixed $query
128
     * @param string $method
129
     * @param mixed $parameters
130
     * @param string $column
131
     * @param string $keyword
132
     */
133
    protected function compileColumnQuery($query, $method, $parameters, $column, $keyword)
134
    {
135
        if (method_exists($query, $method)
136
            && count($parameters) <= with(new \ReflectionMethod($query, $method))->getNumberOfParameters()
137
        ) {
138
            if (Str::contains(Str::lower($method), 'raw')
139
                || Str::contains(Str::lower($method), 'exists')
140
            ) {
141
                call_user_func_array(
142
                    [$query, $method],
143
                    $this->parameterize($parameters, $keyword)
144
                );
145
            } else {
146
                call_user_func_array(
147
                    [$query, $method],
148
                    $this->parameterize($column, $parameters, $keyword)
149
                );
150
            }
151
        }
152
    }
153
154
    /**
155
     * Build Query Builder Parameters.
156
     *
157
     * @return array
158
     */
159
    protected function parameterize()
160
    {
161
        $args       = func_get_args();
162
        $keyword    = count($args) > 2 ? $args[2] : $args[1];
163
        $parameters = Helper::buildParameters($args);
164
        $parameters = Helper::replacePatternWithKeyword($parameters, $keyword, '$1');
165
166
        return $parameters;
167
    }
168
169
    /**
170
     * Add a query on global search.
171
     *
172
     * @param mixed $query
173
     * @param string $column
174
     * @param string $keyword
175
     */
176
    protected function compileGlobalSearch($query, $column, $keyword)
177
    {
178
        $column = $this->castColumn($column);
179
        $sql    = $column . ' LIKE ?';
180
        if ($this->isCaseInsensitive()) {
181
            $sql     = 'LOWER(' . $column . ') LIKE ?';
182
            $keyword = Str::lower($keyword);
183
        }
184
185
        $query->orWhereRaw($sql, [$keyword]);
186
    }
187
188
    /**
189
     * Wrap a column and cast in pgsql
190
     *
191
     * @param  string $column
192
     * @return string
193
     */
194
    public function castColumn($column)
195
    {
196
        $column = $this->connection->getQueryGrammar()->wrap($column);
197
        if ($this->database === 'pgsql') {
198
            $column = 'CAST(' . $column . ' as TEXT)';
199
        }
200
201
        return $column;
202
    }
203
204
    /**
205
     * @inheritdoc
206
     */
207
    public function columnSearch()
208
    {
209
        $columns = $this->request->get('columns');
210
        for ($i = 0, $c = count($columns); $i < $c; $i++) {
211
            if ($this->request->isColumnSearchable($i)) {
212
                $column  = $this->setupColumnName($i);
213
                $keyword = $this->getSearchKeyword($i);
214
215
                if (isset($this->columnDef['filter'][$column])) {
216
                    $method     = $this->columnDef['filter'][$column]['method'];
217
                    $parameters = $this->columnDef['filter'][$column]['parameters'];
218
                    $this->compileColumnQuery($this->getQueryBuilder(), $method, $parameters, $column, $keyword);
219
                } else {
220
                    $column = $this->castColumn($column);
221
                    if ($this->isCaseInsensitive()) {
222
                        if ($this->request->isRegex($i)) {
223
                            $this->query->whereRaw('LOWER(' . $column . ') REGEXP ?', [Str::lower($keyword)]);
224
                        } else {
225
                            $this->query->whereRaw('LOWER(' . $column . ') LIKE ?', [Str::lower($keyword)]);
226
                        }
227
                    } else {
228
                        $col = strstr($column, '(') ? $this->connection->raw($column) : $column;
229
                        if ($this->request->isRegex($i)) {
230
                            $this->query->whereRaw($col . ' REGEXP ?', [$keyword]);
231
                        } else {
232
                            $this->query->whereRaw($col . ' LIKE ?', [$keyword]);
233
                        }
234
                    }
235
                }
236
237
                $this->isFilterApplied = true;
238
            }
239
        }
240
    }
241
242
    /**
243
     * Get proper keyword to use for search.
244
     *
245
     * @param int $i
246
     * @return string
247
     */
248
    private function getSearchKeyword($i)
249
    {
250
        if ($this->request->isRegex($i)) {
251
            return $this->request->columnKeyword($i);
252
        }
253
254
        return $this->setupKeyword($this->request->columnKeyword($i));
255
    }
256
257
    /**
258
     * @inheritdoc
259
     */
260
    public function ordering()
261
    {
262
        foreach ($this->request->orderableColumns() as $orderable) {
263
            $column = $this->setupOrderColumn($orderable);
264
            if (isset($this->columnDef['order'][$column])) {
265
                $method     = $this->columnDef['order'][$column]['method'];
266
                $parameters = $this->columnDef['order'][$column]['parameters'];
267
                $this->compileColumnQuery(
268
                    $this->getQueryBuilder(), $method, $parameters, $column, $orderable['direction']
269
                );
270
            } else {
271
                /**
272
                 * If we perform a select("*"), the ORDER BY clause will look like this:
273
                 * ORDER BY * ASC
274
                 * which causes a query exception
275
                 * The temporary fix is modify `*` column to `id` column
276
                 */
277
                if ($column === '*') {
278
                    $column = 'id';
279
                }
280
                $this->getQueryBuilder()->orderBy($column, $orderable['direction']);
281
            }
282
        }
283
    }
284
285
    /**
286
     * Get order by column name.
287
     *
288
     * @param array $orderable
289
     * @return string
290
     */
291
    private function setupOrderColumn(array $orderable)
292
    {
293
        $r_column = $this->request->input('columns')[$orderable['column']];
294
        $column   = isset($r_column['name']) ? $r_column['name'] : $r_column['data'];
295
        if ($column >= 0) {
296
            $column = $this->setupColumnName($orderable['column'], true);
297
298
            return $column;
299
        }
300
301
        return $column;
302
    }
303
304
    /**
305
     * @inheritdoc
306
     */
307
    public function paging()
308
    {
309
        $this->query->skip($this->request['start'])
310
                    ->take((int) $this->request['length'] > 0 ? $this->request['length'] : 10);
311
    }
312
313
    /**
314
     * Get results
315
     *
316
     * @return array|static[]
317
     */
318
    public function results()
319
    {
320
        return $this->query->get();
321
    }
322
}
323