Joomla Platform  13.1
Documentation des API du framework Joomla Platform
 Tout Classes Espaces de nommage Fichiers Fonctions Variables Pages
sqlite.php
Aller à la documentation de ce fichier.
1 <?php
2 /**
3  * @package Joomla.Platform
4  * @subpackage Database
5  *
6  * @copyright Copyright (C) 2005 - 2013 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  * SQLite Query Building Class.
14  *
15  * @package Joomla.Platform
16  * @subpackage Database
17  * @since 12.1
18  */
20 {
21  /**
22  * @var integer The offset for the result set.
23  * @since 12.1
24  */
25  protected $offset;
26 
27  /**
28  * @var integer The limit for the result set.
29  * @since 12.1
30  */
31  protected $limit;
32 
33  /**
34  * @var array Bounded object array
35  * @since 12.1
36  */
37  protected $bounded = array();
38 
39  /**
40  * Method to add a variable to an internal array that will be bound to a prepared SQL statement before query execution. Also
41  * removes a variable that has been bounded from the internal bounded array when the passed in value is null.
42  *
43  * @param string|integer $key The key that will be used in your SQL query to reference the value. Usually of
44  * the form ':key', but can also be an integer.
45  * @param mixed &$value The value that will be bound. The value is passed by reference to support output
46  * parameters such as those possible with stored procedures.
47  * @param integer $dataType Constant corresponding to a SQL datatype.
48  * @param integer $length The length of the variable. Usually required for OUTPUT parameters.
49  * @param array $driverOptions Optional driver options to be used.
50  *
51  * @return JDatabaseQuerySqlite
52  *
53  * @since 12.1
54  */
55  public function bind($key = null, &$value = null, $dataType = PDO::PARAM_STR, $length = 0, $driverOptions = array())
56  {
57  // Case 1: Empty Key (reset $bounded array)
58  if (empty($key))
59  {
60  $this->bounded = array();
61 
62  return $this;
63  }
64 
65  // Case 2: Key Provided, null value (unset key from $bounded array)
66  if (is_null($value))
67  {
68  if (isset($this->bounded[$key]))
69  {
70  unset($this->bounded[$key]);
71  }
72 
73  return $this;
74  }
75 
76  $obj = new stdClass;
77 
78  $obj->value = &$value;
79  $obj->dataType = $dataType;
80  $obj->length = $length;
81  $obj->driverOptions = $driverOptions;
82 
83  // Case 3: Simply add the Key/Value into the bounded array
84  $this->bounded[$key] = $obj;
85 
86  return $this;
87  }
88 
89  /**
90  * Retrieves the bound parameters array when key is null and returns it by reference. If a key is provided then that item is
91  * returned.
92  *
93  * @param mixed $key The bounded variable key to retrieve.
94  *
95  * @return mixed
96  *
97  * @since 12.1
98  */
99  public function &getBounded($key = null)
100  {
101  if (empty($key))
102  {
103  return $this->bounded;
104  }
105  else
106  {
107  if (isset($this->bounded[$key]))
108  {
109  return $this->bounded[$key];
110  }
111  }
112  }
113 
114  /**
115  * Clear data from the query or a specific clause of the query.
116  *
117  * @param string $clause Optionally, the name of the clause to clear, or nothing to clear the whole query.
118  *
119  * @return JDatabaseQuerySqlite Returns this object to allow chaining.
120  *
121  * @since 12.1
122  */
123  public function clear($clause = null)
124  {
125  switch ($clause)
126  {
127  case null:
128  $this->bounded = array();
129  break;
130  }
131 
132  parent::clear($clause);
133 
134  return $this;
135  }
136 
137  /**
138  * Method to modify a query already in string format with the needed
139  * additions to make the query limited to a particular number of
140  * results, or start at a particular offset. This method is used
141  * automatically by the __toString() method if it detects that the
142  * query implements the JDatabaseQueryLimitable interface.
143  *
144  * @param string $query The query in string format
145  * @param integer $limit The limit for the result set
146  * @param integer $offset The offset for the result set
147  *
148  * @return string
149  *
150  * @since 12.1
151  */
152  public function processLimit($query, $limit, $offset = 0)
153  {
154  if ($limit > 0 || $offset > 0)
155  {
156  $query .= ' LIMIT ' . $offset . ', ' . $limit;
157  }
158 
159  return $query;
160  }
161 
162  /**
163  * Sets the offset and limit for the result set, if the database driver supports it.
164  *
165  * Usage:
166  * $query->setLimit(100, 0); (retrieve 100 rows, starting at first record)
167  * $query->setLimit(50, 50); (retrieve 50 rows, starting at 50th record)
168  *
169  * @param integer $limit The limit for the result set
170  * @param integer $offset The offset for the result set
171  *
172  * @return JDatabaseQuerySqlite Returns this object to allow chaining.
173  *
174  * @since 12.1
175  */
176  public function setLimit($limit = 0, $offset = 0)
177  {
178  $this->limit = (int) $limit;
179  $this->offset = (int) $offset;
180 
181  return $this;
182  }
183 
184  /**
185  * Add to the current date and time.
186  * Usage:
187  * $query->select($query->dateAdd());
188  * Prefixing the interval with a - (negative sign) will cause subtraction to be used.
189  *
190  * @param datetime $date The date or datetime to add to
191  * @param string $interval The string representation of the appropriate number of units
192  * @param string $datePart The part of the date to perform the addition on
193  *
194  * @return string The string with the appropriate sql for addition of dates
195  *
196  * @since 13.1
197  * @link http://www.sqlite.org/lang_datefunc.html
198  */
199  public function dateAdd($date, $interval, $datePart)
200  {
201  // SQLite does not support microseconds as a separate unit. Convert the interval to seconds
202  if (strcasecmp($datePart, 'microseconds') == 0)
203  {
204  $interval = .001 * $interval;
205  $datePart = 'seconds';
206  }
207 
208  if (substr($interval, 0, 1) != '-')
209  {
210  return "datetime('" . $date . "', '+" . $interval . " " . $datePart . "')";
211  }
212  else
213  {
214  return "datetime('" . $date . "', '" . $interval . " " . $datePart . "')";
215  }
216  }
217 }