DBColumn.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *   Copyright (C) 2006-2008 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 DBColumn implements SQLTableName
00016     {
00017         private $type       = null;
00018         private $name       = null;
00019         
00020         private $table      = null;
00021         private $default    = null;
00022         
00023         private $reference  = null;
00024         private $onUpdate   = null;
00025         private $onDelete   = null;
00026         
00027         private $primary    = null;
00028         
00029         private $sequenced  = null;
00030         
00034         public static function create(DataType $type, $name)
00035         {
00036             return new self($type, $name);
00037         }
00038         
00039         public function __construct(DataType $type, $name)
00040         {
00041             $this->type = $type;
00042             $this->name = $name;
00043         }
00044         
00048         public function getType()
00049         {
00050             return $this->type;
00051         }
00052         
00056         public function setTable(DBTable $table)
00057         {
00058             $this->table = $table;
00059             
00060             return $this;
00061         }
00062         
00063         public function getName()
00064         {
00065             return $this->name;
00066         }
00067         
00071         public function getTable()
00072         {
00073             return $this->table;
00074         }
00075         
00076         public function isPrimaryKey()
00077         {
00078             return $this->primary;
00079         }
00080         
00084         public function setPrimaryKey($primary = false)
00085         {
00086             $this->primary = true === $primary;
00087             
00088             return $this;
00089         }
00090         
00094         public function setDefault($default)
00095         {
00096             $this->default = $default;
00097             
00098             return $this;
00099         }
00100         
00101         public function getDefault()
00102         {
00103             return $this->default;
00104         }
00105         
00110         public function setReference(
00111             DBColumn $column,
00112             /* ForeignChangeAction */ $onDelete = null,
00113             /* ForeignChangeAction */ $onUpdate = null
00114         )
00115         {
00116             Assert::isTrue(
00117                 (
00118                     (null === $onDelete)
00119                     || $onDelete instanceof ForeignChangeAction
00120                 )
00121                 && (
00122                     (null === $onUpdate)
00123                     || $onUpdate instanceof ForeignChangeAction
00124                 )
00125             );
00126             
00127             $this->reference    = $column;
00128             $this->onDelete     = $onDelete;
00129             $this->onUpdate     = $onUpdate;
00130             
00131             return $this;
00132         }
00133         
00137         public function dropReference()
00138         {
00139             $this->reference    = null;
00140             $this->onDelete     = null;
00141             $this->onUpdate     = null;
00142             
00143             return $this;
00144         }
00145         
00146         public function hasReference()
00147         {
00148             return ($this->reference !== null);
00149         }
00150         
00154         public function setAutoincrement($auto = false)
00155         {
00156             $this->sequenced = (true === $auto);
00157             
00158             return $this;
00159         }
00160         
00161         public function isAutoincrement()
00162         {
00163             return $this->sequenced;
00164         }
00165         
00166         public function toDialectString(Dialect $dialect)
00167         {
00168             $out =
00169                 $dialect->quoteField($this->name).' '
00170                 .$this->type->toDialectString($dialect);
00171             
00172             if (null !== $this->default) {
00173                 
00174                 if ($this->type->getId() == DataType::BOOLEAN)
00175                     $default = $this->default ? 'true' : 'false';
00176                 else
00177                     $default = $this->default;
00178                 
00179                 $out .=
00180                     ' DEFAULT '
00181                     .(
00182                         $this->default instanceof DialectString
00183                             ? $this->default->toDialectString($dialect)
00184                             : $dialect->valueToString($default)
00185                     );
00186             }
00187             
00188             if ($this->reference) {
00189                 
00190                 $table  = $this->reference->getTable()->getName();
00191                 $column = $this->reference->getName();
00192                 
00193                 $out .=
00194                     " REFERENCES {$dialect->quoteTable($table)}"
00195                     ."({$dialect->quoteField($column)})";
00196                 
00197                 if ($this->onDelete)
00198                     $out .= ' ON DELETE '.$this->onDelete->toString();
00199                 
00200                 if ($this->onUpdate)
00201                     $out .= ' ON UPDATE '.$this->onUpdate->toString();
00202             }
00203             
00204             return $out;
00205         }
00206     }
00207 ?>