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