10 defined(
'JPATH_PLATFORM') or die;
30 protected $name_quotes =
'`';
39 protected $null_date =
'1900-01-01 00:00:00';
51 protected $offset = 0;
60 public function __toString()
67 $query .= (string) $this->select;
68 $query .= (string) $this->from;
70 if ($this instanceof JDatabaseQueryLimitable && ($this->limit > 0 || $this->offset > 0))
74 $query .= (string) $this->order;
77 $query = $this->processLimit($query, $this->limit, $this->offset);
83 foreach ($this->join as $join)
85 $query .= (string) $join;
91 $query .= (string) $this->where;
96 $query .= (string) $this->group;
101 $query .= (string) $this->having;
107 $query .= (string) $this->insert;
112 $query .= (string) $this->
set;
115 elseif ($this->values)
119 $query .= (string) $this->columns;
122 $elements = $this->insert->getElements();
123 $tableName = array_shift($elements);
126 $query .= (string) $this->values;
128 if ($this->autoIncrementField)
130 $query =
'SET IDENTITY_INSERT ' . $tableName .
' ON;' . $query .
'SET IDENTITY_INSERT ' . $tableName .
' OFF;';
135 $query .= (string) $this->where;
142 $query = parent::__toString();
160 public function castAsChar($value)
162 return 'CAST(' . $value .
' as NVARCHAR(10))';
176 public function charLength($field, $operator = null, $condition = null)
178 return 'DATALENGTH(' . $field .
')' . (isset($operator) && isset($condition) ?
' ' . $operator .
' ' . $condition :
'');
191 public function concatenate($values, $separator = null)
195 return '(' . implode(
'+' . $this->quote($separator) .
'+', $values) .
')';
199 return '(' . implode(
'+', $values) .
')';
210 public function currentTimestamp()
224 public function length($value)
226 return 'LEN(' . $value .
')';
245 public function dateAdd($date, $interval, $datePart)
247 return "DATEADD('" . $datePart .
"', '" . $interval .
"', '" . $date .
"'" .
')';
263 public function processLimit($query, $limit, $offset = 0)
265 $start = $offset + 1;
266 $end = $offset + $limit;
268 $orderBy = stristr($query,
'ORDER BY');
270 if (is_null($orderBy) || empty($orderBy))
272 $orderBy =
'ORDER BY (select 0)';
275 $query = str_ireplace($orderBy,
'', $query);
277 $rowNumberText =
', ROW_NUMBER() OVER (' . $orderBy .
') AS RowNumber FROM ';
279 $query = preg_replace(
'/\sFROM\s/i', $rowNumberText, $query, 1);
280 $query =
'SELECT * FROM (' . $query .
') _myResults WHERE RowNumber BETWEEN ' . $start .
' AND ' . $end;
299 public function setLimit($limit = 0, $offset = 0)
301 $this->limit = (int) $limit;
302 $this->offset = (int) $offset;