Joomla Platform  13.1
Documentation des API du framework Joomla Platform
 Tout Classes Espaces de nommage Fichiers Fonctions Variables Pages
sqlsrv.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  * Query Building Class.
14  *
15  * @package Joomla.Platform
16  * @subpackage Database
17  * @since 11.1
18  */
20 {
21  /**
22  * The character(s) used to quote SQL statement names such as table names or field names,
23  * etc. The child classes should define this as necessary. If a single character string the
24  * same character is used for both sides of the quoted name, else the first character will be
25  * used for the opening quote and the second for the closing quote.
26  *
27  * @var string
28  * @since 11.1
29  */
30  protected $name_quotes = '`';
31 
32  /**
33  * The null or zero representation of a timestamp for the database driver. This should be
34  * defined in child classes to hold the appropriate value for the engine.
35  *
36  * @var string
37  * @since 11.1
38  */
39  protected $null_date = '1900-01-01 00:00:00';
40 
41  /**
42  * @var integer The affected row limit for the current SQL statement.
43  * @since 3.2 CMS
44  */
45  protected $limit = 0;
46 
47  /**
48  * @var integer The affected row offset to apply for the current SQL statement.
49  * @since 3.2 CMS
50  */
51  protected $offset = 0;
52 
53  /**
54  * Magic function to convert the query to a string.
55  *
56  * @return string The completed query.
57  *
58  * @since 11.1
59  */
60  public function __toString()
61  {
62  $query = '';
63 
64  switch ($this->type)
65  {
66  case 'select':
67  $query .= (string) $this->select;
68  $query .= (string) $this->from;
69 
70  if ($this instanceof JDatabaseQueryLimitable && ($this->limit > 0 || $this->offset > 0))
71  {
72  if ($this->order)
73  {
74  $query .= (string) $this->order;
75  }
76 
77  $query = $this->processLimit($query, $this->limit, $this->offset);
78  }
79 
80  if ($this->join)
81  {
82  // Special case for joins
83  foreach ($this->join as $join)
84  {
85  $query .= (string) $join;
86  }
87  }
88 
89  if ($this->where)
90  {
91  $query .= (string) $this->where;
92  }
93 
94  if ($this->group)
95  {
96  $query .= (string) $this->group;
97  }
98 
99  if ($this->having)
100  {
101  $query .= (string) $this->having;
102  }
103 
104  break;
105 
106  case 'insert':
107  $query .= (string) $this->insert;
108 
109  // Set method
110  if ($this->set)
111  {
112  $query .= (string) $this->set;
113  }
114  // Columns-Values method
115  elseif ($this->values)
116  {
117  if ($this->columns)
118  {
119  $query .= (string) $this->columns;
120  }
121 
122  $elements = $this->insert->getElements();
123  $tableName = array_shift($elements);
124 
125  $query .= 'VALUES ';
126  $query .= (string) $this->values;
127 
128  if ($this->autoIncrementField)
129  {
130  $query = 'SET IDENTITY_INSERT ' . $tableName . ' ON;' . $query . 'SET IDENTITY_INSERT ' . $tableName . ' OFF;';
131  }
132 
133  if ($this->where)
134  {
135  $query .= (string) $this->where;
136  }
137  }
138 
139  break;
140 
141  default:
142  $query = parent::__toString();
143  break;
144  }
145 
146  return $query;
147  }
148 
149  /**
150  * Casts a value to a char.
151  *
152  * Ensure that the value is properly quoted before passing to the method.
153  *
154  * @param string $value The value to cast as a char.
155  *
156  * @return string Returns the cast value.
157  *
158  * @since 11.1
159  */
160  public function castAsChar($value)
161  {
162  return 'CAST(' . $value . ' as NVARCHAR(10))';
163  }
164 
165  /**
166  * Gets the function to determine the length of a character string.
167  *
168  * @param string $field A value.
169  * @param string $operator Comparison operator between charLength integer value and $condition
170  * @param string $condition Integer value to compare charLength with.
171  *
172  * @return string The required char length call.
173  *
174  * @since 11.1
175  */
176  public function charLength($field, $operator = null, $condition = null)
177  {
178  return 'DATALENGTH(' . $field . ')' . (isset($operator) && isset($condition) ? ' ' . $operator . ' ' . $condition : '');
179  }
180 
181  /**
182  * Concatenates an array of column names or values.
183  *
184  * @param array $values An array of values to concatenate.
185  * @param string $separator As separator to place between each value.
186  *
187  * @return string The concatenated values.
188  *
189  * @since 11.1
190  */
191  public function concatenate($values, $separator = null)
192  {
193  if ($separator)
194  {
195  return '(' . implode('+' . $this->quote($separator) . '+', $values) . ')';
196  }
197  else
198  {
199  return '(' . implode('+', $values) . ')';
200  }
201  }
202 
203  /**
204  * Gets the current date and time.
205  *
206  * @return string
207  *
208  * @since 11.1
209  */
210  public function currentTimestamp()
211  {
212  return 'GETDATE()';
213  }
214 
215  /**
216  * Get the length of a string in bytes.
217  *
218  * @param string $value The string to measure.
219  *
220  * @return integer
221  *
222  * @since 11.1
223  */
224  public function length($value)
225  {
226  return 'LEN(' . $value . ')';
227  }
228 
229  /**
230  * Add to the current date and time.
231  * Usage:
232  * $query->select($query->dateAdd());
233  * Prefixing the interval with a - (negative sign) will cause subtraction to be used.
234  *
235  * @param datetime $date The date to add to; type may be time or datetime.
236  * @param string $interval The string representation of the appropriate number of units
237  * @param string $datePart The part of the date to perform the addition on
238  *
239  * @return string The string with the appropriate sql for addition of dates
240  *
241  * @since 13.1
242  * @note Not all drivers support all units.
243  * @link http://msdn.microsoft.com/en-us/library/ms186819.aspx for more information
244  */
245  public function dateAdd($date, $interval, $datePart)
246  {
247  return "DATEADD('" . $datePart . "', '" . $interval . "', '" . $date . "'" . ')';
248  }
249 
250  /**
251  * Method to modify a query already in string format with the needed
252  * additions to make the query limited to a particular number of
253  * results, or start at a particular offset.
254  *
255  * @param string $query The query in string format
256  * @param integer $limit The limit for the result set
257  * @param integer $offset The offset for the result set
258  *
259  * @return string
260  *
261  * @since 12.1
262  */
263  public function processLimit($query, $limit, $offset = 0)
264  {
265  $start = $offset + 1;
266  $end = $offset + $limit;
267 
268  $orderBy = stristr($query, 'ORDER BY');
269 
270  if (is_null($orderBy) || empty($orderBy))
271  {
272  $orderBy = 'ORDER BY (select 0)';
273  }
274 
275  $query = str_ireplace($orderBy, '', $query);
276 
277  $rowNumberText = ', ROW_NUMBER() OVER (' . $orderBy . ') AS RowNumber FROM ';
278 
279  $query = preg_replace('/\sFROM\s/i', $rowNumberText, $query, 1);
280  $query = 'SELECT * FROM (' . $query . ') _myResults WHERE RowNumber BETWEEN ' . $start . ' AND ' . $end;
281 
282  return $query;
283  }
284 
285  /**
286  * Sets the offset and limit for the result set, if the database driver supports it.
287  *
288  * Usage:
289  * $query->setLimit(100, 0); (retrieve 100 rows, starting at first record)
290  * $query->setLimit(50, 50); (retrieve 50 rows, starting at 50th record)
291  *
292  * @param integer $limit The limit for the result set
293  * @param integer $offset The offset for the result set
294  *
295  * @return JDatabaseQuery Returns this object to allow chaining.
296  *
297  * @since 12.1
298  */
299  public function setLimit($limit = 0, $offset = 0)
300  {
301  $this->limit = (int) $limit;
302  $this->offset = (int) $offset;
303 
304  return $this;
305  }
306 }