3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Db\Sql\Platform\Oracle
;
12 use Zend\Db\Adapter\AdapterInterface
;
13 use Zend\Db\Adapter\Driver\DriverInterface
;
14 use Zend\Db\Adapter\ParameterContainer
;
15 use Zend\Db\Adapter\Platform\PlatformInterface
;
16 use Zend\Db\Adapter\StatementContainerInterface
;
17 use Zend\Db\Sql\ExpressionInterface
;
18 use Zend\Db\Sql\Platform\PlatformDecoratorInterface
;
19 use Zend\Db\Sql\Select
;
21 class SelectDecorator
extends Select
implements PlatformDecoratorInterface
27 protected $select = null;
30 * @param Select $select
32 public function setSubject($select)
34 $this->select
= $select;
38 * @see \Zend\Db\Sql\Select::renderTable
40 protected function renderTable($table, $alias = null)
42 return $table . ' ' . $alias;
46 * @param AdapterInterface $adapter
47 * @param StatementContainerInterface $statementContainer
49 public function prepareStatement(AdapterInterface
$adapter, StatementContainerInterface
$statementContainer)
52 foreach (get_object_vars($this->select
) as $name => $value) {
53 $this->{$name} = $value;
56 unset($this->specifications
[self
::LIMIT
]);
57 unset($this->specifications
[self
::OFFSET
]);
59 $this->specifications
['LIMITOFFSET'] = null;
60 parent
::prepareStatement($adapter, $statementContainer);
64 * @param PlatformInterface $platform
67 public function getSqlString(PlatformInterface
$platform = null)
70 foreach (get_object_vars($this->select
) as $name => $value) {
71 $this->{$name} = $value;
75 unset($this->specifications
[self
::LIMIT
]);
76 unset($this->specifications
[self
::OFFSET
]);
78 $this->specifications
['LIMITOFFSET'] = null;
79 return parent
::getSqlString($platform);
83 * @param PlatformInterface $platform
84 * @param DriverInterface $driver
85 * @param ParameterContainer $parameterContainer
90 protected function processLimitOffset(PlatformInterface
$platform, DriverInterface
$driver = null, ParameterContainer
$parameterContainer = null, &$sqls, &$parameters)
92 if ($this->limit
=== null && $this->offset
=== null) {
96 $selectParameters = $parameters[self
::SELECT
];
98 $starSuffix = $platform->getIdentifierSeparator() . self
::SQL_STAR
;
99 foreach ($selectParameters[0] as $i => $columnParameters) {
100 if ($columnParameters[0] == self
::SQL_STAR ||
(isset($columnParameters[1]) && $columnParameters[1] == self
::SQL_STAR
) ||
strpos($columnParameters[0], $starSuffix)) {
101 $selectParameters[0] = array(array(self
::SQL_STAR
));
104 if (isset($columnParameters[1])) {
105 array_shift($columnParameters);
106 $selectParameters[0][$i] = $columnParameters;
110 if ($this->offset
=== null) {
114 // first, produce column list without compound names (using the AS portion only)
115 array_unshift($sqls, $this->createSqlFromSpecificationAndParameters(
116 array('SELECT %1$s FROM (SELECT b.%1$s, rownum b_rownum FROM (' => current($this->specifications
[self
::SELECT
])), $selectParameters
119 if ($parameterContainer) {
120 if ($this->limit
=== null) {
121 array_push($sqls, ') b ) WHERE b_rownum > (:offset)');
122 $parameterContainer->offsetSet('offset', $this->offset
, $parameterContainer::TYPE_INTEGER
);
124 // create bottom part of query, with offset and limit using row_number
125 array_push($sqls, ') b WHERE rownum <= (:offset+:limit)) WHERE b_rownum >= (:offset + 1)');
126 $parameterContainer->offsetSet('offset', $this->offset
, $parameterContainer::TYPE_INTEGER
);
127 $parameterContainer->offsetSet('limit', $this->limit
, $parameterContainer::TYPE_INTEGER
);
130 if ($this->limit
=== null) {
131 array_push($sqls, ') b ) WHERE b_rownum > ('. (int) $this->offset
. ')'
134 array_push($sqls, ') b WHERE rownum <= ('
135 . (int) $this->offset
138 . ')) WHERE b_rownum >= ('
139 . (int) $this->offset
145 $sqls[self
::SELECT
] = $this->createSqlFromSpecificationAndParameters(
146 $this->specifications
[self
::SELECT
], $parameters[self
::SELECT
]
151 protected function processJoins(PlatformInterface
$platform, DriverInterface
$driver = null, ParameterContainer
$parameterContainer = null)
158 $joinSpecArgArray = array();
159 foreach ($this->joins
as $j => $join) {
160 $joinSpecArgArray[$j] = array();
162 $joinSpecArgArray[$j][] = strtoupper($join['type']);
164 $joinSpecArgArray[$j][] = (is_array($join['name']))
165 ?
$platform->quoteIdentifier(current($join['name'])) . ' ' . $platform->quoteIdentifier(key($join['name']))
166 : $platform->quoteIdentifier($join['name']);
168 $joinSpecArgArray[$j][] = ($join['on'] instanceof ExpressionInterface
)
169 ?
$this->processExpression($join['on'], $platform, $driver, $this->processInfo
['paramPrefix'] . 'join')
170 : $platform->quoteIdentifierInFragment($join['on'], array('=', 'AND', 'OR', '(', ')', 'BETWEEN')); // on
171 if ($joinSpecArgArray[$j][2] instanceof StatementContainerInterface
) {
172 if ($parameterContainer) {
173 $parameterContainer->merge($joinSpecArgArray[$j][2]->getParameterContainer());
175 $joinSpecArgArray[$j][2] = $joinSpecArgArray[$j][2]->getSql();
179 return array($joinSpecArgArray);