DibiFluentMssqlDataSource::limit()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 1
eloc 6
nc 1
nop 2
1
<?php
2
3
/**
4
 * @copyright   Copyright (c) 2015 ublaboo <[email protected]>
5
 * @author      Pavel Janda <[email protected]>
6
 * @package     Ublaboo
7
 */
8
9
namespace Ublaboo\DataGrid\DataSource;
10
11
use Dibi\Fluent;
12
use Dibi\Helpers;
13
use Ublaboo\DataGrid\Filter;
14
use Ublaboo\DataGrid\Utils\DateTimeHelper;
15
16
class DibiFluentMssqlDataSource extends DibiFluentDataSource
17
{
18
19
	/**
20
	 * @var Fluent
21
	 */
22
	protected $data_source;
23
24
	/**
25
	 * @var array
26
	 */
27
	protected $data = [];
28
29
	/**
30
	 * @var string
31
	 */
32
	protected $primary_key;
33
34
35
	/**
36
	 * @param Fluent $data_source
37
	 * @param string $primary_key
38
	 */
39
	public function __construct(Fluent $data_source, $primary_key)
40
	{
41
		$this->data_source = $data_source;
42
		$this->primary_key = $primary_key;
43
	}
44
45
46
	/********************************************************************************
47
	 *                          IDataSource implementation                          *
48
	 ********************************************************************************/
49
50
51
	/**
52
	 * Get count of data
53
	 * @return int
54
	 */
55
	public function getCount()
56
	{
57
		$clone = clone $this->data_source;
58
		$clone->removeClause('ORDER BY');
59
60
		return $clone->count();
61
	}
62
63
64
	/**
65
	 * Get the data
66
	 * @param array $condition
67
	 * @return static
68
	 */
69
	public function filterOne(array $condition)
70
	{
71
		$this->data_source->where($condition);
72
73
		return $this;
74
	}
75
76
77
	/**
78
	 * Filter by date
79
	 * @param  Filter\FilterDate $filter
80
	 * @return void
81
	 */
82 View Code Duplication
	public function applyFilterDate(Filter\FilterDate $filter)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
83
	{
84
		$conditions = $filter->getCondition();
85
86
		$date = DateTimeHelper::tryConvertToDateTime($conditions[$filter->getColumn()], [$filter->getPhpFormat()]);
87
88
		$this->data_source->where('CONVERT(varchar(10), %n, 112) = ?', $filter->getColumn(), $date->format('Ymd'));
89
	}
90
91
92
	/**
93
	 * Filter by date range
94
	 * @param  Filter\FilterDateRange $filter
95
	 * @return void
96
	 */
97 View Code Duplication
	public function applyFilterDateRange(Filter\FilterDateRange $filter)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
98
	{
99
		$conditions = $filter->getCondition();
100
101
		$value_from = $conditions[$filter->getColumn()]['from'];
102
		$value_to = $conditions[$filter->getColumn()]['to'];
103
104
		if ($value_from) {
105
			$this->data_source->where('CONVERT(varchar(10), %n, 112) >= ?', $filter->getColumn(), $value_from);
106
		}
107
108
		if ($value_to) {
109
			$this->data_source->where('CONVERT(varchar(10), %n, 112) <= ?', $filter->getColumn(), $value_to);
110
		}
111
	}
112
113
114
	/**
115
	 * Filter by date
116
	 * @param  Filter\FilterText $filter
117
	 * @return void
118
	 */
119
	public function applyFilterText(Filter\FilterText $filter)
120
	{
121
		$condition = $filter->getCondition();
122
		$driver = $this->data_source->getConnection()->getDriver();
123
		$or = [];
124
125
		foreach ($condition as $column => $value) {
126 View Code Duplication
			if (class_exists(Helpers::class) === true) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
127
				$column = Helpers::escape(
128
					$driver,
129
					$column,
130
					\dibi::IDENTIFIER
131
				);
132
			} else {
133
				$column = $driver->escape(
134
					$column,
135
					\dibi::IDENTIFIER
136
				);
137
			}
138
139
			if ($filter->isExactSearch()) {
140
				$this->data_source->where("$column = %s", $value);
141
				continue;
142
			}
143
144
			$or[] = "$column LIKE \"%$value%\"";
145
		}
146
147 View Code Duplication
		if (sizeof($or) > 1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
148
			$this->data_source->where('(%or)', $or);
149
		} else {
150
			$this->data_source->where($or);
151
		}
152
	}
153
154
155
	/**
156
	 * Apply limit and offset on data
157
	 * @param int $offset
158
	 * @param int $limit
159
	 * @return static
160
	 */
161
	public function limit($offset, $limit)
162
	{
163
		$sql = (string) $this->data_source;
164
165
		$result = $this->data_source->getConnection()
166
			->query('%sql OFFSET ? ROWS FETCH NEXT ? ROWS ONLY', $sql, $offset, $limit);
167
168
		$this->data = $result->fetchAll();
169
170
		return $this;
171
	}
172
}
173