DataType.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 
00017     final class DataType extends Enumeration implements DialectString
00018     {
00019         const SMALLINT          = 0x001001;
00020         const INTEGER           = 0x001002;
00021         const BIGINT            = 0x001003;
00022         const NUMERIC           = 0x001704;
00023         
00024         const REAL              = 0x001105;
00025         const DOUBLE            = 0x001106;
00026         
00027         const BOOLEAN           = 0x000007;
00028         
00029         const CHAR              = 0x000108;
00030         const VARCHAR           = 0x000109;
00031         const TEXT              = 0x00000A;
00032         
00033         const DATE              = 0x00000B;
00034         const TIME              = 0x000A0C;
00035         const TIMESTAMP         = 0x000A0D;
00036         const INTERVAL          = 0x00000F;
00037         
00038         const BINARY            = 0x00000E;
00039         
00040         const HAVE_SIZE         = 0x000100;
00041         const HAVE_PRECISION    = 0x000200;
00042         const HAVE_SCALE        = 0x000400;
00043         const HAVE_TIMEZONE     = 0x000800;
00044         const CAN_BE_UNSIGNED   = 0x001000;
00045         
00046         private $size       = null;
00047         private $precision  = null;
00048         private $scale      = null;
00049         
00050         private $null       = true;
00051         private $timezone   = false;
00052         private $unsigned   = false;
00053         
00054         protected $names = array(
00055             self::SMALLINT      => 'SMALLINT',
00056             self::INTEGER       => 'INTEGER',
00057             self::BIGINT        => 'BIGINT',
00058             self::NUMERIC       => 'NUMERIC',
00059             
00060             self::REAL          => 'FLOAT',
00061             self::DOUBLE        => 'DOUBLE PRECISION',
00062             
00063             self::BOOLEAN       => 'BOOLEAN',
00064             
00065             self::CHAR          => 'CHARACTER',
00066             self::VARCHAR       => 'CHARACTER VARYING',
00067             self::TEXT          => 'TEXT',
00068             
00069             self::DATE          => 'DATE',
00070             self::TIME          => 'TIME',
00071             self::TIMESTAMP     => 'TIMESTAMP',
00072             self::INTERVAL      => 'INTERVAL',
00073             
00074             self::BINARY        => 'BINARY',
00075         );
00076         
00080         public static function create($id)
00081         {
00082             return new self($id);
00083         }
00084         
00085         public static function getAnyId()
00086         {
00087             return self::BOOLEAN;
00088         }
00089         
00090         public function getSize()
00091         {
00092             return $this->size;
00093         }
00094         
00099         public function setSize($size)
00100         {
00101             Assert::isInteger($size);
00102             Assert::isTrue($this->hasSize());
00103             
00104             $this->size = $size;
00105             
00106             return $this;
00107         }
00108         
00109         public function hasSize()
00110         {
00111             return (bool) ($this->id & self::HAVE_SIZE);
00112         }
00113         
00114         public function getPrecision()
00115         {
00116             return $this->precision;
00117         }
00118         
00123         public function setPrecision($precision)
00124         {
00125             Assert::isInteger($precision);
00126             Assert::isTrue(($this->id & self::HAVE_PRECISION) > 0);
00127             
00128             $this->precision = $precision;
00129             
00130             return $this;
00131         }
00132         
00133         public function hasPrecision()
00134         {
00135             return (bool) ($this->id & self::HAVE_PRECISION);
00136         }
00137         
00138         public function getScale()
00139         {
00140             return $this->scale;
00141         }
00142         
00147         public function setScale($scale)
00148         {
00149             Assert::isInteger($scale);
00150             Assert::isTrue(($this->id & self::HAVE_SCALE) > 0);
00151             
00152             $this->scale = $scale;
00153             
00154             return $this;
00155         }
00156         
00161         public function setTimezoned($zoned = false)
00162         {
00163             Assert::isTrue(($this->id & self::HAVE_TIMEZONE) > 0);
00164             
00165             $this->timezone = (true === $zoned);
00166             
00167             return $this;
00168         }
00169         
00170         public function isTimezoned()
00171         {
00172             return $this->timezone;
00173         }
00174         
00178         public function setNull($isNull = false)
00179         {
00180             $this->null = ($isNull === true);
00181             
00182             return $this;
00183         }
00184         
00185         public function isNull()
00186         {
00187             return $this->null;
00188         }
00189         
00194         public function setUnsigned($unsigned = false)
00195         {
00196             Assert::isTrue(($this->id && self::CAN_BE_UNSIGNED) > 0);
00197             
00198             $this->unsigned = ($unsigned === true);
00199             
00200             return $this;
00201         }
00202         
00203         public function isUnsigned()
00204         {
00205             return $this->unsigned;
00206         }
00207         
00208         public function toDialectString(Dialect $dialect)
00209         {
00210             $out = $dialect->typeToString($this);
00211             
00212             if ($this->unsigned) {
00213                 $out .= ' UNSIGNED';
00214             }
00215             
00216             if ($this->id & self::HAVE_PRECISION) {
00217                 if ($this->precision) {
00218                     
00219                     switch ($this->id) {
00220                         
00221                         case self::TIME:
00222                         case self::TIMESTAMP:
00223                             
00224                             $out .= "({$this->precision})";
00225                             break;
00226                         
00227                         case self::NUMERIC:
00228                             
00229                             $out .=
00230                                 $this->precision
00231                                     ? "({$this->size}, {$this->precision})"
00232                                     : "({$this->size})";
00233                             break;
00234                         
00235                         default:
00236                             
00237                             throw new WrongStateException();
00238                     }
00239                 }
00240             } elseif ($this->hasSize()) {
00241                 if (!$this->size)
00242                     throw new WrongStateException(
00243                         "type '{$this->name}' must have size"
00244                     );
00245                 
00246                 $out .= "({$this->size})";
00247             }
00248             
00249             if ($this->id & self::HAVE_TIMEZONE)
00250                 $out .= $dialect->timeZone($this->timezone);
00251             
00252             $out .=
00253                 $this->null
00254                     ? ' NULL'
00255                     : ' NOT NULL';
00256             
00257             return $out;
00258         }
00259     }
00260 ?>