# SQLChain.class.php

Go to the documentation of this file.

    00001 <?php
    00002 /****************************************************************************
    00003  *   Copyright (C) 2004-2007 by Konstantin V. Arkhipov, 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     abstract class SQLChain implements LogicalObject, MappableObject
    00016     {
    00017         protected $chain = array();
    00018         protected $logic = array();
    00019
    00023         protected function exp(DialectString $exp, $logic)
    00024         {
    00025             $this->chain[] = $exp;
    00026             $this->logic[] = $logic;
    00027
    00028             return $this;
    00029         }
    00030
    00031         public function getSize()
    00032         {
    00033             return count($this->chain);
    00034         }
    00035
    00036         public function getChain()
    00037         {
    00038             return $this->chain;
    00039         }
    00040
    00041         public function getLogic()
    00042         {
    00043             return $this->logic;
    00044         }
    00045
    00049         public function toMapped(ProtoDAO $dao, JoinCapableQuery $query)
    00050         {
    00051             $size = count($this->chain);
    00052
    00053             Assert::isTrue($size > 0, 'empty chain');
    00054
    00055             $chain = new $this;
    00056
    00057             for ($i = 0; $i < $size; ++$i) {
    00058                 $chain->exp(
    00059                     $dao->guessAtom($this->chain[$i], $query),
    00060                     $this->logic[$i]
    00061                 );
    00062             }
    00063
    00064             return $chain;
    00065         }
    00066
    00067         public function toDialectString(Dialect $dialect)
    00068         {
    00069             if ($this->chain) {
    00070                 $out = $this->chain[0]->toDialectString($dialect).' ';
    00071                 for ($i = 1, $size = count($this->chain); $i < $size; ++$i) {
    00072                     $out .=
    00073                         $this->logic[$i]
    00074                         .' '
    00075                         .$this->chain[$i]->toDialectString($dialect)
    00076                         .' ';
    00077                 }
    00078
    00079                 if ($size > 1)
    00080                     $out = rtrim($out); // trailing space
    00081
    00082                 if ($size === 1)
    00083                     return $out;
    00084
    00085                 return '('.$out.')';
    00086             }
    00087
    00088             return null;
    00089         }
    00090     }
    00091 ?>