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

https://github.com/dextercowley/joomla-cms · PHP · 263 lines · 101 code · 24 blank · 138 comment · 14 complexity · 4f4e7d0106223791dfcd128006b6152d MD5 · raw file

  1. <?php
  2. /**
  3. * @package Joomla.Platform
  4. * @subpackage Database
  5. *
  6. * @copyright Copyright (C) 2005 - 2014 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 The offset for the result set.
  21. * @since 12.1
  22. */
  23. protected $offset;
  24. /**
  25. * @var integer The limit for the result set.
  26. * @since 12.1
  27. */
  28. protected $limit;
  29. /**
  30. * @var array Bounded object array
  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 JDatabaseQuerySqlite
  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. * Gets the number of characters in a string.
  102. *
  103. * Note, use 'length' to find the number of bytes in a string.
  104. *
  105. * Usage:
  106. * $query->select($query->charLength('a'));
  107. *
  108. * @param string $field A value.
  109. * @param string $operator Comparison operator between charLength integer value and $condition
  110. * @param string $condition Integer value to compare charLength with.
  111. *
  112. * @return string The required char length call.
  113. *
  114. * @since 13.1
  115. */
  116. public function charLength($field, $operator = null, $condition = null)
  117. {
  118. return 'length(' . $field . ')' . (isset($operator) && isset($condition) ? ' ' . $operator . ' ' . $condition : '');
  119. }
  120. /**
  121. * Clear data from the query or a specific clause of the query.
  122. *
  123. * @param string $clause Optionally, the name of the clause to clear, or nothing to clear the whole query.
  124. *
  125. * @return JDatabaseQuerySqlite Returns this object to allow chaining.
  126. *
  127. * @since 12.1
  128. */
  129. public function clear($clause = null)
  130. {
  131. switch ($clause)
  132. {
  133. case null:
  134. $this->bounded = array();
  135. break;
  136. }
  137. parent::clear($clause);
  138. return $this;
  139. }
  140. /**
  141. * Concatenates an array of column names or values.
  142. *
  143. * Usage:
  144. * $query->select($query->concatenate(array('a', 'b')));
  145. *
  146. * @param array $values An array of values to concatenate.
  147. * @param string $separator As separator to place between each value.
  148. *
  149. * @return string The concatenated values.
  150. *
  151. * @since 11.1
  152. */
  153. public function concatenate($values, $separator = null)
  154. {
  155. if ($separator)
  156. {
  157. return implode(' || ' . $this->quote($separator) . ' || ', $values);
  158. }
  159. else
  160. {
  161. return implode(' || ', $values);
  162. }
  163. }
  164. /**
  165. * Method to modify a query already in string format with the needed
  166. * additions to make the query limited to a particular number of
  167. * results, or start at a particular offset. This method is used
  168. * automatically by the __toString() method if it detects that the
  169. * query implements the JDatabaseQueryLimitable interface.
  170. *
  171. * @param string $query The query in string format
  172. * @param integer $limit The limit for the result set
  173. * @param integer $offset The offset for the result set
  174. *
  175. * @return string
  176. *
  177. * @since 12.1
  178. */
  179. public function processLimit($query, $limit, $offset = 0)
  180. {
  181. if ($limit > 0 || $offset > 0)
  182. {
  183. $query .= ' LIMIT ' . $offset . ', ' . $limit;
  184. }
  185. return $query;
  186. }
  187. /**
  188. * Sets the offset and limit for the result set, if the database driver supports it.
  189. *
  190. * Usage:
  191. * $query->setLimit(100, 0); (retrieve 100 rows, starting at first record)
  192. * $query->setLimit(50, 50); (retrieve 50 rows, starting at 50th record)
  193. *
  194. * @param integer $limit The limit for the result set
  195. * @param integer $offset The offset for the result set
  196. *
  197. * @return JDatabaseQuerySqlite Returns this object to allow chaining.
  198. *
  199. * @since 12.1
  200. */
  201. public function setLimit($limit = 0, $offset = 0)
  202. {
  203. $this->limit = (int) $limit;
  204. $this->offset = (int) $offset;
  205. return $this;
  206. }
  207. /**
  208. * Add to the current date and time.
  209. * Usage:
  210. * $query->select($query->dateAdd());
  211. * Prefixing the interval with a - (negative sign) will cause subtraction to be used.
  212. *
  213. * @param datetime $date The date or datetime to add to
  214. * @param string $interval The string representation of the appropriate number of units
  215. * @param string $datePart The part of the date to perform the addition on
  216. *
  217. * @return string The string with the appropriate sql for addition of dates
  218. *
  219. * @since 13.1
  220. * @link http://www.sqlite.org/lang_datefunc.html
  221. */
  222. public function dateAdd($date, $interval, $datePart)
  223. {
  224. // SQLite does not support microseconds as a separate unit. Convert the interval to seconds
  225. if (strcasecmp($datePart, 'microseconds') == 0)
  226. {
  227. $interval = .001 * $interval;
  228. $datePart = 'seconds';
  229. }
  230. if (substr($interval, 0, 1) != '-')
  231. {
  232. return "datetime('" . $date . "', '+" . $interval . " " . $datePart . "')";
  233. }
  234. else
  235. {
  236. return "datetime('" . $date . "', '" . $interval . " " . $datePart . "')";
  237. }
  238. }
  239. }