/src/libraries/fof/database/query/oracle.php

https://bitbucket.org/ke2083/transfans.co.uk-website · PHP · 205 lines · 87 code · 21 blank · 97 comment · 10 complexity · bedaded5795508bb7bd963d5acdda77b MD5 · raw file

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