00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00018 final class SQLFunction
00019 extends Castable
00020 implements DialectString, MappableObject, Aliased
00021 {
00022 const AGGREGATE_ALL = 1;
00023 const AGGREGATE_DISTINCT = 2;
00024
00025 private $name = null;
00026 private $alias = null;
00027 private $aggregate = null;
00028
00029 private $args = array();
00030
00034 public static function create($name )
00035 {
00036 if (func_num_args() > 1) {
00037 $args = func_get_args();
00038 array_shift($args);
00039 return new SQLFunction($name, $args);
00040 } else
00041 return new SQLFunction($name);
00042 }
00043
00044 public function __construct($name )
00045 {
00046 $this->name = $name;
00047
00048 if (func_num_args() > 1) {
00049 $args = func_get_args();
00050
00051 if (is_array($args[1]))
00052 $this->args = $args[1];
00053 else {
00054 array_shift($args);
00055 $this->args = $args;
00056 }
00057 }
00058 }
00059
00060 public function getAlias()
00061 {
00062 return $this->alias;
00063 }
00064
00065 public function getName()
00066 {
00067 return $this->name;
00068 }
00069
00073 public function setAlias($alias)
00074 {
00075 $this->alias = $alias;
00076
00077 return $this;
00078 }
00079
00083 public function setAggregateAll()
00084 {
00085 $this->aggregate = self::AGGREGATE_ALL;
00086
00087 return $this;
00088 }
00089
00093 public function setAggregateDistinct()
00094 {
00095 $this->aggregate = self::AGGREGATE_DISTINCT;
00096
00097 return $this;
00098 }
00099
00103 public function toMapped(StorableDAO $dao, JoinCapableQuery $query)
00104 {
00105 $mapped = array();
00106
00107 $mapped[] = $this->name;
00108
00109 foreach ($this->args as $arg) {
00110 if ($arg instanceof MappableObject)
00111 $mapped[] = $arg->toMapped($dao, $query);
00112 else
00113 $mapped[] = $dao->guessAtom($arg, $query);
00114 }
00115
00116 $sqlFunction = call_user_func_array(array('self', 'create'), $mapped);
00117
00118 $sqlFunction->aggregate = $this->aggregate;
00119
00120 return $sqlFunction;
00121 }
00122
00123 public function toDialectString(Dialect $dialect)
00124 {
00125 $args = array();
00126
00127 if ($this->args) {
00128 foreach ($this->args as $arg)
00129 if ($arg instanceof DBValue)
00130 $args[] = $arg->toDialectString($dialect);
00131
00132 elseif ($arg === '*') {
00133 Assert::isTrue(
00134 (strtolower($this->name) === 'count')
00135 || defined('__I_HATE_MY_KARMA__'),
00136
00137 'do not want to use "*" with '.$this->args[0]
00138 );
00139
00140 $args[] = $dialect->quoteValue($arg);
00141 } elseif ($arg instanceof SelectQuery)
00142 $args[] = '('.$dialect->fieldToString($arg).')';
00143 else
00144 $args[] = $dialect->fieldToString($arg);
00145 }
00146
00147 $out = $this->name.'(';
00148
00149 if ($this->aggregate == self::AGGREGATE_ALL) {
00150 $out .= 'ALL ';
00151 } elseif ($this->aggregate == self::AGGREGATE_DISTINCT) {
00152 $out .= 'DISTINCT ';
00153 }
00154
00155 $out .= ($args == array() ? null : implode(', ', $args)).')';
00156
00157 $out =
00158 $this->cast
00159 ? $dialect->toCasted($out, $this->cast)
00160 : $out;
00161
00162 return
00163 $this->alias
00164 ? $out.' AS '.$dialect->quoteTable($this->alias)
00165 : $out;
00166 }
00167 }
00168 ?>