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