UpdateQuery.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *   Copyright (C) 2004-2007 by Anton E. Lebedevich                        *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU Lesser General Public License as        *
00007  *   published by the Free Software Foundation; either version 3 of the    *
00008  *   License, or (at your option) any later version.                       *
00009  *                                                                         *
00010  ***************************************************************************/
00011 
00015     final class UpdateQuery
00016         extends InsertOrUpdateQuery
00017         implements JoinCapableQuery
00018     {
00019         private $joiner = null;
00020         
00021         public function __construct($table = null)
00022         {
00023             $this->table = $table;
00024             $this->joiner = new Joiner();
00025         }
00026         
00027         public function __clone()
00028         {
00029             $this->joiner = clone $this->joiner;
00030         }
00031         
00035         public function from($table, $alias = null)
00036         {
00037             $this->joiner->from(new FromTable($table, $alias));
00038             
00039             return $this;
00040         }
00041         
00042         public function hasJoinedTable($table)
00043         {
00044             return $this->joiner->hasJoinedTable($table);
00045         }
00046         
00050         public function join($table, LogicalObject $logic, $alias = null)
00051         {
00052             $this->joiner->join(new SQLJoin($table, $logic, $alias));
00053             return $this;
00054         }
00055         
00059         public function leftJoin($table, LogicalObject $logic, $alias = null)
00060         {
00061             $this->joiner->leftJoin(new SQLLeftJoin($table, $logic, $alias));
00062             return $this;
00063         }
00064         
00068         public function rightJoin($table, LogicalObject $logic, $alias = null)
00069         {
00070             $this->joiner->rightJoin(new SQLRightJoin($table, $logic, $alias));
00071             return $this;
00072         }
00073         
00077         public function setTable($table)
00078         {
00079             $this->table = $table;
00080             
00081             return $this;
00082         }
00083         
00084         public function toDialectString(Dialect $dialect)
00085         {
00086             $query = 'UPDATE '.$dialect->quoteTable($this->table).' SET ';
00087             
00088             $sets = array();
00089             
00090             foreach ($this->fields as $var => $val) {
00091                 if ($val instanceof DialectString)
00092                     $sets[] =
00093                         $dialect->quoteField($var)
00094                         .' = ('
00095                         .$val->toDialectString($dialect)
00096                         .')';
00097                 elseif ($val === null)
00098                     $sets[] = $dialect->quoteField($var).' = NULL';
00099                 elseif (true === $val)
00100                     $sets[] = $dialect->quoteField($var).' = TRUE';
00101                 elseif (false === $val)
00102                     $sets[] = $dialect->quoteField($var).' = FALSE';
00103                 else
00104                     $sets[] =
00105                         $dialect->quoteField($var)
00106                         .' = '
00107                         .$dialect->quoteValue($val);
00108             }
00109             
00110             return
00111                 $query
00112                 .implode(', ', $sets)
00113                 .$this->joiner->toDialectString($dialect)
00114                 .parent::toDialectString($dialect);
00115         }
00116     }
00117 ?>