Completed
Push — master ( db80f0...72976a )
by Pavel
02:46
created

NetteDatabaseTableMssqlDataSource::limit()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 19
rs 9.4285
cc 1
eloc 10
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 Ublaboo\DataGrid\Filter;
12
use Ublaboo\DataGrid\Utils\NetteDatabaseSelectionHelper;
13
14
class NetteDatabaseTableMssqlDataSource extends NetteDatabaseTableDataSource implements IDataSource
15
{
16
17
	/**
18
	 * Filter by date
19
	 * @param  Filter\FilterDate $filter
20
	 * @return void
21
	 */
22 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...
23
	{
24
		$conditions = $filter->getCondition();
25
26
		$date = \DateTime::createFromFormat($filter->getPhpFormat(), $conditions[$filter->getColumn()]);
27
28
		$this->data_source->where(
29
			"CONVERT(varchar(10), {$filter->getColumn()}, 112) = ?",
30
			$date->format('Y-m-d')
31
		);
32
	}
33
34
35
	/**
36
	 * Filter by date range
37
	 * @param  Filter\FilterDateRange $filter
38
	 * @return void
39
	 */
40 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...
41
	{
42
		$conditions = $filter->getCondition();
43
44
		$value_from = $conditions[$filter->getColumn()]['from'];
45
		$value_to   = $conditions[$filter->getColumn()]['to'];
46
47
		if ($value_from) {
48
			$date_from = \DateTime::createFromFormat($filter->getPhpFormat(), $value_from);
49
			$date_from->setTime(0, 0, 0);
50
51
			$this->data_source->where(
52
				"CONVERT(varchar(10), {$filter->getColumn()}, 112) >= ?",
53
				$date_from->format('Y-m-d')
54
			);
55
		}
56
57
		if ($value_to) {
58
			$date_to = \DateTime::createFromFormat($filter->getPhpFormat(), $value_to);
59
			$date_to->setTime(23, 59, 59);
60
61
			$this->data_source->where(
62
				"CONVERT(varchar(10), {$filter->getColumn()}, 112) <= ?",
63
				$date_to->format('Y-m-d')
64
			);
65
		}
66
	}
67
68
69
	/**
70
	 * Apply limit and offset on data
71
	 * @param int $offset
72
	 * @param int $limit
73
	 * @return static
74
	 */
75
	public function limit($offset, $limit)
76
	{
77
		$context = NetteDatabaseSelectionHelper::getContext($this->data_source);
78
79
		$sql = (string) $this->data_source->getSql();
80
81
		$params = $this->data_source->getSqlBuilder()->getParameters();
82
83
		$params[] = $offset;
84
		$params[] = $limit;
85
86
		array_unshift($params, "$sql OFFSET ? ROWS FETCH NEXT ? ROWS ONLY");
87
88
		$result = call_user_func_array([$context, 'query'], $params);
89
90
		$this->data = $result->fetchAll();
91
92
		return $this;
93
	}
94
95
}
96