CreateTableQuery.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 CreateTableQuery extends QueryIdentification
00016     {
00017         private $table = null;
00018         
00019         public function __construct(DBTable $table)
00020         {
00021             $this->table = $table;
00022         }
00023         
00024         public function toDialectString(Dialect $dialect)
00025         {
00026             $name = $this->table->getName();
00027             
00028             $middle = "CREATE TABLE {$dialect->quoteTable($name)} (\n    ";
00029             
00030             $prepend = array();
00031             $columns = array();
00032             $primary = array();
00033             
00034             $order = $this->table->getOrder();
00035             
00036             foreach ($order as $column) {
00037                 
00038                 if ($column->isAutoincrement()) {
00039                     
00040                     if ($pre = $dialect->preAutoincrement($column))
00041                         $prepend[] = $pre;
00042                     
00043                     $columns[] = implode(' ',
00044                         array(
00045                             $column->toDialectString($dialect),
00046                             $dialect->postAutoincrement($column)
00047                         )
00048                     );
00049                 } else
00050                     $columns[] = $column->toDialectString($dialect);
00051 
00052                 $name = $column->getName();
00053                 
00054                 if ($column->isPrimaryKey())
00055                     $primary[] = $dialect->quoteField($name);
00056             }
00057             
00058             $out =
00059                 (
00060                     $prepend
00061                         ? implode("\n", $prepend)."\n"
00062                         : null
00063                 )
00064                 .$middle
00065                 .implode(",\n    ", $columns);
00066             
00067             if ($primary)
00068                 $out .= ",\n    PRIMARY KEY(".implode(', ', $primary).')';
00069                 
00070             if ($uniques = $this->table->getUniques()) {
00071                 $names = array();
00072                 
00073                 foreach ($uniques as $row) {
00074                     foreach ($row as $name) {
00075                         $names[] = $dialect->quoteField($name);
00076                     }
00077                     
00078                     $out .= ",\n    UNIQUE(".implode(', ', $names).')';
00079                 }
00080             }
00081             
00082             return $out."\n);\n";
00083         }
00084     }
00085 ?>