/libraries/joomla/database/query/sqlite.php

https://bitbucket.org/eternaware/joomus · PHP · 182 lines · 70 code · 19 blank · 93 comment · 8 complexity · be5e5f7ca99a02ba17ea68015b66a88b MD5 · raw file

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