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

Generated on Sun Dec 9 21:56:23 2007 for onPHP by  doxygen 1.5.4