# TruncateQuery.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
    00015     final class TruncateQuery extends QueryIdentification
    00016     {
    00017         private $targets = array();
    00018
    00019         public function __construct($whom = null)
    00020         {
    00021             if ($whom) {
    00022                 if (is_array($whom))
    00023                     $this->targets = $whom;
    00024                 else
    00025                     $this->targets[] = $whom;
    00026             }
    00027         }
    00028
    00029         public function getId()
    00030         {
    00031             throw new UnsupportedMethodException();
    00032         }
    00033
    00037         public function table($table)
    00038         {
    00039             if ($table instanceof SQLTableName)
    00040                 $this->targets[] = $table->getTable();
    00041             else
    00042                 $this->targets[] = $table;
    00043
    00044             return $this;
    00045         }
    00046
    00047         public function toDialectString(Dialect $dialect)
    00048         {
    00049             Assert::isTrue(
    00050                 ($this->targets !== array()),
    00051                 'do not know who should i truncate'
    00052             );
    00053
    00054             if ($dialect->hasTruncate()) {
    00055                 $head = 'TRUNCATE TABLE ';
    00056             } else {
    00057                 $head = 'DELETE FROM ';
    00058             }
    00059
    00060             if ($dialect->hasMultipleTruncate()) {
    00061                 $query = $head.$this->dumpTargets($dialect, null, ',');
    00062             } else {
    00063                 $query = $this->dumpTargets($dialect, $head, ';');
    00064             }
    00065
    00066             return $query.';';
    00067         }
    00068
    00069         private function dumpTargets(
    00070             Dialect $dialect, $prepend = null, $append = null
    00071         )
    00072         {
    00073             if (count($this->targets) == 1) {
    00074                 return $prepend.$dialect->quoteTable(reset($this->targets));
    00075             } else {
    00076                 $tables = array();
    00077
    00078                 foreach ($this->targets as $target) {
    00079                     if ($target instanceof DialectString)
    00080                         $table =
    00081                             $dialect->quoteTable(
    00082                                 $target->toDialectString($dialect)
    00083                             );
    00084                     else
    00085                         $table = $dialect->quoteTable($target);
    00086
    00087                     $tables[] = $prepend.$table;
    00088                 }
    00089
    00090                 return implode($append.' ', $tables);
    00091             }
    00092
    00093             Assert::isUnreachable();
    00094         }
    00095     }
    00096 ?>