DataType.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: DataType.class.php 4687 2007-12-09 18:57:18Z voxus $ */
00012 
00018     final class DataType extends Enumeration implements DialectString
00019     {
00020         const SMALLINT          = 0x001001;
00021         const INTEGER           = 0x001002;
00022         const BIGINT            = 0x001003;
00023         const NUMERIC           = 0x001704;
00024         
00025         const REAL              = 0x001005;
00026         const DOUBLE            = 0x001006;
00027         
00028         const BOOLEAN           = 0x000007;
00029         
00030         const CHAR              = 0x000108;
00031         const VARCHAR           = 0x000109;
00032         const TEXT              = 0x00000A;
00033         
00034         const DATE              = 0x00000B;
00035         const TIME              = 0x000A0C;
00036         const TIMESTAMP         = 0x000A0D;
00037         
00038         const HAVE_SIZE         = 0x000100;
00039         const HAVE_PRECISION    = 0x000200;
00040         const HAVE_SCALE        = 0x000400;
00041         const HAVE_TIMEZONE     = 0x000800;
00042         const CAN_BE_UNSIGNED   = 0x001000;
00043         
00044         private $size       = null;
00045         private $precision  = null;
00046         private $scale      = null;
00047         
00048         private $null       = true;
00049         private $timezone   = false;
00050         private $unsigned   = false;
00051         
00052         protected $names = array(
00053             self::SMALLINT      => 'SMALLINT',
00054             self::INTEGER       => 'INTEGER',
00055             self::BIGINT        => 'BIGINT',
00056             self::NUMERIC       => 'NUMERIC',
00057             
00058             self::REAL          => 'REAL',
00059             self::DOUBLE        => 'DOUBLE PRECISION',
00060             
00061             self::BOOLEAN       => 'BOOLEAN',
00062             
00063             self::CHAR          => 'CHARACTER',
00064             self::VARCHAR       => 'CHARACTER VARYING',
00065             self::TEXT          => 'TEXT',
00066             
00067             self::DATE          => 'DATE',
00068             self::TIME          => 'TIME',
00069             self::TIMESTAMP     => 'TIMESTAMP'
00070         );
00071         
00075         public static function create($id)
00076         {
00077             return new self($id);
00078         }
00079         
00080         public static function getAnyId()
00081         {
00082             return self::BOOLEAN;
00083         }
00084         
00085         public function getSize()
00086         {
00087             return $this->size;
00088         }
00089         
00094         public function setSize($size)
00095         {
00096             Assert::isInteger($size);
00097             Assert::isTrue($this->hasSize());
00098             
00099             $this->size = $size;
00100             
00101             return $this;
00102         }
00103         
00104         public function hasSize()
00105         {
00106             return (bool) ($this->id & self::HAVE_SIZE);
00107         }
00108         
00109         public function getPrecision()
00110         {
00111             return $this->precision;
00112         }
00113         
00118         public function setPrecision($precision)
00119         {
00120             Assert::isInteger($precision);
00121             Assert::isTrue(($this->id & self::HAVE_PRECISION) > 0);
00122             
00123             $this->precision = $precision;
00124             
00125             return $this;
00126         }
00127         
00128         public function hasPrecision()
00129         {
00130             return (bool) ($this->id & self::HAVE_PRECISION);
00131         }
00132         
00133         public function getScale()
00134         {
00135             return $this->scale;
00136         }
00137         
00142         public function setScale($scale)
00143         {
00144             Assert::isInteger($scale);
00145             Assert::isTrue(($this->id & self::HAVE_SCALE) > 0);
00146             
00147             $this->scale = $scale;
00148             
00149             return $this;
00150         }
00151         
00156         public function setTimezoned($zoned = false)
00157         {
00158             Assert::isTrue(($this->id & self::HAVE_TIMEZONE) > 0);
00159             
00160             $this->timezone = (true === $zoned);
00161             
00162             return $this;
00163         }
00164         
00165         public function isTimezoned()
00166         {
00167             return $this->timezone;
00168         }
00169         
00173         public function setNull($isNull = false)
00174         {
00175             $this->null = ($isNull === true);
00176             
00177             return $this;
00178         }
00179         
00180         public function isNull()
00181         {
00182             return $this->null;
00183         }
00184         
00189         public function setUnsigned($unsigned = false)
00190         {
00191             Assert::isTrue(($this->id && self::CAN_BE_UNSIGNED) > 0);
00192             
00193             $this->unsigned = ($unsigned === true);
00194             
00195             return $this;
00196         }
00197         
00198         public function isUnsigned()
00199         {
00200             return $this->unsigned;
00201         }
00202         
00203         public function typeToString(Dialect $dialect)
00204         {
00205             if (
00206                 $this->id == self::BIGINT
00207                 && $dialect instanceof LiteDialect
00208             ) {
00209                 return $this->names[self::INTEGER];
00210             }
00211             
00212             return $this->name;
00213         }
00214         
00215         public function toDialectString(Dialect $dialect)
00216         {
00217             $out = $this->typeToString($dialect);
00218             
00219             if ($this->unsigned) {
00220                 $out .= ' UNSIGNED';
00221             }
00222             
00223             if ($this->id & self::HAVE_PRECISION) {
00224                 if ($this->precision) {
00225                     
00226                     switch ($this->id) {
00227                         
00228                         case self::TIME:
00229                         case self::TIMESTAMP:
00230                             
00231                             $out .= "({$this->precision})";
00232                             break;
00233                         
00234                         case self::NUMERIC:
00235                             
00236                             $out .=
00237                                 $this->precision
00238                                     ? "({$this->size}, {$this->precision})"
00239                                     : "({$this->size})";
00240                             break;
00241                         
00242                         default:
00243                             
00244                             throw new WrongStateException();
00245                     }
00246                 }
00247             } elseif ($this->hasSize()) {
00248                 if (!$this->size)
00249                     throw new WrongStateException(
00250                         "type '{$this->name}' must have size"
00251                     );
00252                 
00253                 $out .= "({$this->size})";
00254             }
00255             
00256             if ($this->id & self::HAVE_TIMEZONE)
00257                 $out .= $dialect->timeZone($this->timezone);
00258             
00259             $out .=
00260                 $this->null
00261                     ? ' NULL'
00262                     : ' NOT NULL';
00263             
00264             return $out;
00265         }
00266     }
00267 ?>

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