1 <?php
2 /**
3 * @package Joomla.Platform
4 * @subpackage Database
5 *
6 * @copyright Copyright (C) 2005 - 2017 Open Source Matters, Inc. All rights reserved.
7 * @license GNU General Public License version 2 or later; see LICENSE
8 */
9
10 defined('JPATH_PLATFORM') or die;
11
12 /**
13 * Oracle Query Building Class.
14 *
15 * @since 12.1
16 */
17 class JDatabaseQueryOracle extends JDatabaseQueryPdo implements JDatabaseQueryPreparable, JDatabaseQueryLimitable
18 {
19 /**
20 * @var integer The offset for the result set.
21 * @since 12.1
22 */
23 protected $offset;
24
25 /**
26 * @var integer The limit for the result set.
27 * @since 12.1
28 */
29 protected $limit;
30
31 /**
32 * @var array Bounded object array
33 * @since 12.1
34 */
35 protected $bounded = array();
36
37 /**
38 * Method to add a variable to an internal array that will be bound to a prepared SQL statement before query execution. Also
39 * removes a variable that has been bounded from the internal bounded array when the passed in value is null.
40 *
41 * @param string|integer $key The key that will be used in your SQL query to reference the value. Usually of
42 * the form ':key', but can also be an integer.
43 * @param mixed &$value The value that will be bound. The value is passed by reference to support output
44 * parameters such as those possible with stored procedures.
45 * @param integer $dataType Constant corresponding to a SQL datatype.
46 * @param integer $length The length of the variable. Usually required for OUTPUT parameters.
47 * @param array $driverOptions Optional driver options to be used.
48 *
49 * @return JDatabaseQueryOracle
50 *
51 * @since 12.1
52 */
53 public function bind($key = null, &$value = null, $dataType = PDO::PARAM_STR, $length = 0, $driverOptions = array())
54 {
55 // Case 1: Empty Key (reset $bounded array)
56 if (empty($key))
57 {
58 $this->bounded = array();
59
60 return $this;
61 }
62
63 // Case 2: Key Provided, null value (unset key from $bounded array)
64 if (is_null($value))
65 {
66 if (isset($this->bounded[$key]))
67 {
68 unset($this->bounded[$key]);
69 }
70
71 return $this;
72 }
73
74 $obj = new stdClass;
75
76 $obj->value = &$value;
77 $obj->dataType = $dataType;
78 $obj->length = $length;
79 $obj->driverOptions = $driverOptions;
80
81 // Case 3: Simply add the Key/Value into the bounded array
82 $this->bounded[$key] = $obj;
83
84 return $this;
85 }
86
87 /**
88 * Retrieves the bound parameters array when key is null and returns it by reference. If a key is provided then that item is
89 * returned.
90 *
91 * @param mixed $key The bounded variable key to retrieve.
92 *
93 * @return mixed
94 *
95 * @since 12.1
96 */
97 public function &getBounded($key = null)
98 {
99 if (empty($key))
100 {
101 return $this->bounded;
102 }
103 else
104 {
105 if (isset($this->bounded[$key]))
106 {
107 return $this->bounded[$key];
108 }
109 }
110 }
111
112 /**
113 * Clear data from the query or a specific clause of the query.
114 *
115 * @param string $clause Optionally, the name of the clause to clear, or nothing to clear the whole query.
116 *
117 * @return JDatabaseQueryOracle Returns this object to allow chaining.
118 *
119 * @since 12.1
120 */
121 public function clear($clause = null)
122 {
123 switch ($clause)
124 {
125 case null:
126 $this->bounded = array();
127 break;
128 }
129
130 parent::clear($clause);
131
132 return $this;
133 }
134
135 /**
136 * Method to modify a query already in string format with the needed
137 * additions to make the query limited to a particular number of
138 * results, or start at a particular offset. This method is used
139 * automatically by the __toString() method if it detects that the
140 * query implements the JDatabaseQueryLimitable interface.
141 *
142 * @param string $query The query in string format
143 * @param integer $limit The limit for the result set
144 * @param integer $offset The offset for the result set
145 *
146 * @return string
147 *
148 * @since 12.1
149 */
150 public function processLimit($query, $limit, $offset = 0)
151 {
152 // Check if we need to mangle the query.
153 if ($limit || $offset)
154 {
155 $query = 'SELECT joomla2.*
156 FROM (
157 SELECT joomla1.*, ROWNUM AS joomla_db_rownum
158 FROM (
159 ' . $query . '
160 ) joomla1
161 ) joomla2';
162
163 // Check if the limit value is greater than zero.
164 if ($limit > 0)
165 {
166 $query .= ' WHERE joomla2.joomla_db_rownum BETWEEN ' . ($offset + 1) . ' AND ' . ($offset + $limit);
167 }
168 else
169 {
170 // Check if there is an offset and then use this.
171 if ($offset)
172 {
173 $query .= ' WHERE joomla2.joomla_db_rownum > ' . ($offset + 1);
174 }
175 }
176 }
177
178 return $query;
179 }
180
181 /**
182 * Sets the offset and limit for the result set, if the database driver supports it.
183 *
184 * Usage:
185 * $query->setLimit(100, 0); (retrieve 100 rows, starting at first record)
186 * $query->setLimit(50, 50); (retrieve 50 rows, starting at 50th record)
187 *
188 * @param integer $limit The limit for the result set
189 * @param integer $offset The offset for the result set
190 *
191 * @return JDatabaseQueryOracle Returns this object to allow chaining.
192 *
193 * @since 12.1
194 */
195 public function setLimit($limit = 0, $offset = 0)
196 {
197 $this->limit = (int) $limit;
198 $this->offset = (int) $offset;
199
200 return $this;
201 }
202 }
203