forked from zendframework/zend-db
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SelectDecorator.php
203 lines (172 loc) · 6.23 KB
/
SelectDecorator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Db\Sql\Platform\IbmDb2;
use Zend\Db\Adapter\Driver\DriverInterface;
use Zend\Db\Adapter\ParameterContainer;
use Zend\Db\Adapter\Platform\PlatformInterface;
use Zend\Db\Sql\Platform\PlatformDecoratorInterface;
use Zend\Db\Sql\Select;
class SelectDecorator extends Select implements PlatformDecoratorInterface
{
/**
* @var bool
*/
protected $isSelectContainDistinct= false;
/**
* @var Select
*/
protected $subject = null;
/**
* @var bool
*/
protected $supportsLimitOffset = false;
/**
* @return bool
*/
public function getIsSelectContainDistinct()
{
return $this->isSelectContainDistinct;
}
/**
* @param boolean $isSelectContainDistinct
*/
public function setIsSelectContainDistinct($isSelectContainDistinct)
{
$this->isSelectContainDistinct = $isSelectContainDistinct;
}
/**
* @param Select $select
*/
public function setSubject($select)
{
$this->subject = $select;
}
/**
* @return bool
*/
public function getSupportsLimitOffset()
{
return $this->supportsLimitOffset;
}
/**
* @param bool $supportsLimitOffset
*/
public function setSupportsLimitOffset($supportsLimitOffset)
{
$this->supportsLimitOffset = $supportsLimitOffset;
}
/**
* @see Select::renderTable
*/
protected function renderTable($table, $alias = null)
{
return $table . ' ' . $alias;
}
protected function localizeVariables()
{
parent::localizeVariables();
// set specifications
unset($this->specifications[self::LIMIT]);
unset($this->specifications[self::OFFSET]);
$this->specifications['LIMITOFFSET'] = null;
}
/**
* @param PlatformInterface $platform
* @param DriverInterface $driver
* @param ParameterContainer $parameterContainer
* @param array $sqls
* @param array $parameters
*/
protected function processLimitOffset(PlatformInterface $platform, DriverInterface $driver = null, ParameterContainer $parameterContainer = null, &$sqls, &$parameters)
{
if ($this->limit === null && $this->offset === null) {
return;
}
if ($this->supportsLimitOffset) {
// Note: db2_prepare/db2_execute fails with positional parameters, for LIMIT & OFFSET
$limit = (int)$this->limit;
if (! $limit) {
return;
}
$offset = (int)$this->offset;
if ($offset) {
array_push($sqls, sprintf("LIMIT %s OFFSET %s", $limit, $offset));
return;
}
array_push($sqls, sprintf("LIMIT %s", $limit));
return;
}
$selectParameters = $parameters[self::SELECT];
$starSuffix = $platform->getIdentifierSeparator() . self::SQL_STAR;
foreach ($selectParameters[0] as $i => $columnParameters) {
if ($columnParameters[0] == self::SQL_STAR
|| (isset($columnParameters[1]) && $columnParameters[1] == self::SQL_STAR)
|| strpos($columnParameters[0], $starSuffix)
) {
$selectParameters[0] = [[self::SQL_STAR]];
break;
}
if (isset($columnParameters[1])) {
array_shift($columnParameters);
$selectParameters[0][$i] = $columnParameters;
}
}
// first, produce column list without compound names (using the AS portion only)
array_unshift($sqls, $this->createSqlFromSpecificationAndParameters(
['SELECT %1$s FROM (' => current($this->specifications[self::SELECT])],
$selectParameters
));
if (preg_match('/DISTINCT/i', $sqls[0])) {
$this->setIsSelectContainDistinct(true);
}
if ($parameterContainer) {
// create bottom part of query, with offset and limit using row_number
$limitParamName = $driver->formatParameterName('limit');
$offsetParamName = $driver->formatParameterName('offset');
array_push($sqls, sprintf(
") AS ZEND_IBMDB2_SERVER_LIMIT_OFFSET_EMULATION WHERE ZEND_IBMDB2_SERVER_LIMIT_OFFSET_EMULATION.ZEND_DB_ROWNUM BETWEEN %s AND %s",
$offsetParamName,
$limitParamName
));
if ((int) $this->offset > 0) {
$parameterContainer->offsetSet('offset', (int) $this->offset + 1);
} else {
$parameterContainer->offsetSet('offset', (int) $this->offset);
}
$parameterContainer->offsetSet('limit', (int) $this->limit + (int) $this->offset);
} else {
if ((int) $this->offset > 0) {
$offset = (int) $this->offset + 1;
} else {
$offset = (int) $this->offset;
}
array_push($sqls, sprintf(
") AS ZEND_IBMDB2_SERVER_LIMIT_OFFSET_EMULATION WHERE ZEND_IBMDB2_SERVER_LIMIT_OFFSET_EMULATION.ZEND_DB_ROWNUM BETWEEN %d AND %d",
$offset,
(int) $this->limit + (int) $this->offset
));
}
if (isset($sqls[self::ORDER])) {
$orderBy = $sqls[self::ORDER];
unset($sqls[self::ORDER]);
} else {
$orderBy = '';
}
// add a column for row_number() using the order specification //dense_rank()
if ($this->getIsSelectContainDistinct()) {
$parameters[self::SELECT][0][] = ['DENSE_RANK() OVER (' . $orderBy . ')', 'ZEND_DB_ROWNUM'];
} else {
$parameters[self::SELECT][0][] = ['ROW_NUMBER() OVER (' . $orderBy . ')', 'ZEND_DB_ROWNUM'];
}
$sqls[self::SELECT] = $this->createSqlFromSpecificationAndParameters(
$this->specifications[self::SELECT],
$parameters[self::SELECT]
);
}
}