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