10 defined(
'JPATH_PLATFORM') or die;
29 protected $name = null;
35 protected $elements = null;
41 protected $glue = null;
52 public function __construct($name, $elements, $glue =
',')
54 $this->elements = array();
58 $this->append($elements);
68 public function __toString()
70 if (substr($this->name, -2) ==
'()')
72 return PHP_EOL . substr($this->name, 0, -2) .
'(' . implode($this->glue, $this->elements) .
')';
76 return PHP_EOL . $this->name .
' ' . implode($this->glue, $this->elements);
89 public function append($elements)
91 if (is_array($elements))
93 $this->elements = array_merge($this->elements, $elements);
97 $this->elements = array_merge($this->elements, array($elements));
108 public function getElements()
110 return $this->elements;
121 public function __clone()
123 foreach ($this as $k => $v)
125 if (is_object($v) || is_array($v))
127 $this->{$k} = unserialize(serialize($v));
302 return $this->
quote($args[0], isset($args[1]) ? $args[1] :
true);
306 return $this->
quoteName($args[0], isset($args[1]) ? $args[1] : null);
310 return $this->
escape($args[0], isset($args[1]) ? $args[1] :
false);
346 $query .= (string) $this->element;
350 $query .= (string) $this->
select;
351 $query .= (string) $this->
from;
358 $query .= (string) $join;
364 $query .= (string) $this->
where;
369 $query .= (string) $this->
group;
374 $query .= (string) $this->
having;
379 $query .= (string) $this->
order;
385 $query .= (string) $this->
union;
393 $query .= (string) $this->
delete;
394 $query .= (string) $this->
from;
401 $query .= (string) $join;
407 $query .= (string) $this->
where;
413 $query .= (string) $this->
update;
420 $query .= (string) $join;
424 $query .= (string) $this->
set;
428 $query .= (string) $this->
where;
434 $query .= (string) $this->
insert;
439 $query .= (string) $this->
set;
446 $query .= (string) $this->
columns;
449 $elements = $this->
values->getElements();
451 if (!($elements[0] instanceof $this))
453 $query .=
' VALUES ';
456 $query .= (string) $this->
values;
462 $query .= (string) $this->
call;
466 $query .= (string) $this->
exec;
472 $query = $this->processLimit($query, $this->limit, $this->offset);
489 return isset($this->$name) ? $this->$name : null;
510 $this->type =
'call';
512 if (is_null($this->
call))
559 public function charLength($field, $operator = null, $condition = null)
561 return 'CHAR_LENGTH(' . $field .
')' . (isset($operator) && isset($condition) ?
' ' . $operator .
' ' . $condition :
'');
573 public function clear($clause = null)
585 $this->
delete = null;
597 $this->autoIncrementField = null;
662 $this->
delete = null;
674 $this->autoIncrementField = null;
727 return 'CONCATENATE(' . implode(
' || ' . $this->
quote($separator) .
' || ',
$values) .
')';
731 return 'CONCATENATE(' . implode(
' || ',
$values) .
')';
747 return 'CURRENT_TIMESTAMP()';
764 throw new RuntimeException(
'JLIB_DATABASE_ERROR_INVALID_DB_OBJECT');
767 return $this->db->getDateFormat();
782 return '<pre class="jdatabasequery">' . str_replace(
'#__', $this->db->getPrefix(), $this) .
'</pre>';
799 public function delete($table = null)
801 $this->type =
'delete';
828 public function escape($text, $extra =
false)
832 throw new RuntimeException(
'JLIB_DATABASE_ERROR_INVALID_DB_OBJECT');
835 return $this->db->escape($text, $extra);
856 $this->type =
'exec';
858 if (is_null($this->
exec))
889 public function from($tables, $subQueryAlias = null)
891 if (is_null($this->
from))
893 if ($tables instanceof $this)
895 if (is_null($subQueryAlias))
897 throw new RuntimeException(
'JLIB_DATABASE_ERROR_NULL_SUBQUERY_ALIAS');
900 $tables =
'( ' . (string) $tables .
' ) AS ' . $this->
quoteName($subQueryAlias);
907 $this->
from->append($tables);
927 return 'YEAR(' . $date .
')';
944 return 'MONTH(' . $date .
')';
959 public function day($date)
961 return 'DAY(' . $date .
')';
978 return 'HOUR(' . $date .
')';
995 return 'MINUTE(' . $date .
')';
1012 return 'SECOND(' . $date .
')';
1029 if (is_null($this->
group))
1054 public function having($conditions, $glue =
'AND')
1056 if (is_null($this->
having))
1058 $glue = strtoupper($glue);
1063 $this->
having->append($conditions);
1083 $this->
join(
'INNER', $condition);
1105 public function insert($table, $incrementField=
false)
1107 $this->type =
'insert';
1109 $this->autoIncrementField = $incrementField;
1129 if (is_null($this->
join))
1131 $this->
join = array();
1152 $this->
join(
'LEFT', $condition);
1173 return 'LENGTH(' . $value .
')';
1195 throw new RuntimeException(
'JLIB_DATABASE_ERROR_INVALID_DB_OBJECT');
1198 $result = $this->db->getNullDate($quoted);
1202 return $this->db->quote($result);
1223 if (is_null($this->
order))
1249 $this->
join(
'OUTER', $condition);
1275 public function quote($text, $escape =
true)
1279 throw new RuntimeException(
'JLIB_DATABASE_ERROR_INVALID_DB_OBJECT');
1282 return $this->db->quote($text, $escape);
1312 throw new RuntimeException(
'JLIB_DATABASE_ERROR_INVALID_DB_OBJECT');
1315 return $this->db->quoteName($name, $as);
1332 $this->
join(
'RIGHT', $condition);
1355 $this->type =
'select';
1357 if (is_null($this->
select))
1384 public function set($conditions, $glue =
',')
1386 if (is_null($this->
set))
1388 $glue = strtoupper($glue);
1393 $this->
set->append($conditions);
1436 $this->type =
'update';
1457 if (is_null($this->
values))
1484 public function where($conditions, $glue =
'AND')
1486 if (is_null($this->
where))
1488 $glue = strtoupper($glue);
1493 $this->
where->append($conditions);
1509 foreach ($this as $k => $v)
1516 if (is_object($v) || is_array($v))
1518 $this->{$k} = unserialize(serialize($v));
1540 public function union($query, $distinct =
false, $glue =
'')
1544 if (!is_null($this->
order))
1546 $this->
clear(
'order');
1552 $name =
'UNION DISTINCT ()';
1553 $glue =
')' . PHP_EOL .
'UNION DISTINCT (';
1557 $glue =
')' . PHP_EOL .
'UNION (';
1563 if (is_null($this->
union))
1565 $this->
union = new JDatabaseQueryElement($name, $query, "$glue");
1570 $this->
union->append($query);
1594 return $this->
union($query, $distinct, $glue);
1647 $args = array_slice(func_get_args(), 1);
1648 array_unshift($args, null);
1651 $func =
function ($match) use ($query, $args, &$i)
1653 if (isset($match[6]) && $match[6] ==
'%')
1662 return $query->currentTimestamp();
1666 return $query->nullDate(
false);
1670 return $query->nullDate(
true);
1675 $index = is_numeric($match[4]) ? (int) $match[4] : $i++;
1677 if (!$index || !isset($args[$index]))
1684 $replacement = $args[$index];
1690 return 0 + $replacement;
1694 return $query->escape($replacement);
1698 return $query->escape($replacement,
true);
1702 return $query->quoteName($replacement);
1706 return $query->quote($replacement);
1710 return $query->quote($replacement,
false);
1714 return $replacement;
1719 return $query->year($query->quote($replacement));
1723 return $query->year($query->quoteName($replacement));
1727 return $query->month($query->quote($replacement));
1731 return $query->month($query->quoteName($replacement));
1735 return $query->day($query->quote($replacement));
1739 return $query->day($query->quoteName($replacement));
1743 return $query->hour($query->quote($replacement));
1747 return $query->hour($query->quoteName($replacement));
1751 return $query->minute($query->quote($replacement));
1755 return $query->minute($query->quoteName($replacement));
1759 return $query->second($query->quote($replacement));
1763 return $query->second($query->quoteName($replacement));
1781 return preg_replace_callback(
'#%(((([\d]+)\$)?([aeEnqQryYmMdDhHiIsStzZ]))|(%))#', $func, $format);
1800 public function dateAdd($date, $interval, $datePart)
1802 return trim(
"DATE_ADD('" . $date .
"', INTERVAL " . $interval .
' ' . $datePart .
')');
1822 public function unionAll($query, $distinct =
false, $glue =
'')
1824 $glue =
')' . PHP_EOL .
'UNION ALL (';
1825 $name =
'UNION ALL ()';