Joiner.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *   Copyright (C) 2006-2007 by Konstantin V. Arkhipov                     *
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 
00016     final class Joiner implements DialectString
00017     {
00018         private $from   = array();
00019         private $tables = array();
00020         
00024         public function from(FromTable $from)
00025         {
00026             $this->from[] = $from;
00027             
00028             return $this;
00029         }
00030         
00031         public function hasJoinedTable($table)
00032         {
00033             return isset($this->tables[$table]);
00034         }
00035         
00036         public function getTablesCount()
00037         {
00038             return count($this->from);
00039         }
00040         
00044         public function join(SQLJoin $join)
00045         {
00046             $this->from[] = $join;
00047             $this->tables[$join->getTable()] = true;
00048             
00049             return $this;
00050         }
00051         
00055         public function leftJoin(SQLLeftJoin $join)
00056         {
00057             $this->from[] = $join;
00058             $this->tables[$join->getTable()] = true;
00059             
00060             return $this;
00061         }
00062         
00066         public function rightJoin(SQLRightJoin $join)
00067         {
00068             $this->from[] = $join;
00069             $this->tables[$join->getTable()] = true;
00070             
00071             return $this;
00072         }
00073         
00074         public function getFirstTable()
00075         {
00076             if ($this->from)
00077                 return $this->from[0]->getTable();
00078             
00079             return null;
00080         }
00081         
00082         public function getLastTable()
00083         {
00084             if ($this->from)
00085                 return $this->from[count($this->from) - 1]->getTable();
00086             
00087             return null;
00088         }
00089         
00090         public function toDialectString(Dialect $dialect)
00091         {
00092             $fromString = null;
00093             
00094             for ($i = 0, $size = count($this->from); $i < $size; ++$i) {
00095                 if ($i == 0)
00096                     $separator = null;
00097                 elseif (
00098                     $this->from[$i] instanceof FromTable &&
00099                     !$this->from[$i]->getTable() instanceof SelectQuery
00100                 )
00101                     $separator = ', ';
00102                 else
00103                     $separator = ' ';
00104                 
00105                 $fromString .=
00106                     $separator
00107                     .$this->from[$i]->toDialectString($dialect);
00108             }
00109             
00110             if ($fromString)
00111                 return ' FROM '.$fromString;
00112             
00113             return null;
00114         }
00115     }
00116 ?>