Advertisement
Guest User

unzend.com_331

a guest
Oct 7th, 2015
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.95 KB | None | 0 0
  1. <?php
  2. // Ioncube Decoder Unzend.Com Email unzend@gmail.com
  3. // http://www.unzend.com
  4. /**
  5.  * CDbColumnSchema class file.
  6.  *
  7.  * @author Qiang Xue <qiang.xue@gmail.com>
  8.  * @link http://www.yiiframework.com/
  9.  * @copyright 2008-2013 Yii Software LLC
  10.  * @license http://www.yiiframework.com/license/
  11.  */
  12.  
  13. /**
  14.  * CDbColumnSchema class describes the column meta data of a database table.
  15.  *
  16.  * @author Qiang Xue <qiang.xue@gmail.com>
  17.  * @package system.db.schema
  18.  * @since 1.0
  19.  */
  20. class CDbColumnSchema extends CComponent
  21. {
  22.     /**
  23.      * @var string name of this column (without quotes).
  24.      */
  25.     public $name;
  26.     /**
  27.      * @var string raw name of this column. This is the quoted name that can be used in SQL queries.
  28.      */
  29.     public $rawName;
  30.     /**
  31.      * @var boolean whether this column can be null.
  32.      */
  33.     public $allowNull;
  34.     /**
  35.      * @var string the DB type of this column.
  36.      */
  37.     public $dbType;
  38.     /**
  39.      * @var string the PHP type of this column.
  40.      */
  41.     public $type;
  42.     /**
  43.      * @var mixed default value of this column
  44.      */
  45.     public $defaultValue;
  46.     /**
  47.      * @var integer size of the column.
  48.      */
  49.     public $size;
  50.     /**
  51.      * @var integer precision of the column data, if it is numeric.
  52.      */
  53.     public $precision;
  54.     /**
  55.      * @var integer scale of the column data, if it is numeric.
  56.      */
  57.     public $scale;
  58.     /**
  59.      * @var boolean whether this column is a primary key
  60.      */
  61.     public $isPrimaryKey;
  62.     /**
  63.      * @var boolean whether this column is a foreign key
  64.      */
  65.     public $isForeignKey;
  66.     /**
  67.      * @var boolean whether this column is auto-incremental
  68.      * @since 1.1.7
  69.      */
  70.     public $autoIncrement=false;
  71.     /**
  72.      * @var string comment of this column. Default value is empty string which means that no comment
  73.      * has been set for the column. Null value means that RDBMS does not support column comments
  74.      * at all (SQLite) or comment retrieval for the active RDBMS is not yet supported by the framework.
  75.      * @since 1.1.13
  76.      */
  77.     public $comment='';
  78.  
  79.     /**
  80.      * Initializes the column with its DB type and default value.
  81.      * This sets up the column's PHP type, size, precision, scale as well as default value.
  82.      * @param string $dbType the column's DB type
  83.      * @param mixed $defaultValue the default value
  84.      */
  85.     public function init($dbType, $defaultValue)
  86.     {
  87.         $this->dbType=$dbType;
  88.         $this->extractType($dbType);
  89.         $this->extractLimit($dbType);
  90.         if($defaultValue!==null)
  91.             $this->extractDefault($defaultValue);
  92.     }
  93.  
  94.     /**
  95.      * Extracts the PHP type from DB type.
  96.      * @param string $dbType DB type
  97.      */
  98.     protected function extractType($dbType)
  99.     {
  100.         if(stripos($dbType,'int')!==false && stripos($dbType,'unsigned int')===false)
  101.             $this->type='integer';
  102.         elseif(stripos($dbType,'bool')!==false)
  103.             $this->type='boolean';
  104.         elseif(preg_match('/(real|floa|doub)/i',$dbType))
  105.             $this->type='double';
  106.         else
  107.             $this->type='string';
  108.     }
  109.  
  110.     /**
  111.      * Extracts size, precision and scale information from column's DB type.
  112.      * @param string $dbType the column's DB type
  113.      */
  114.     protected function extractLimit($dbType)
  115.     {
  116.         if(strpos($dbType,'(') && preg_match('/\((.*)\)/',$dbType,$matches))
  117.         {
  118.             $values=explode(',',$matches[1]);
  119.             $this->size=$this->precision=(int)$values[0];
  120.             if(isset($values[1]))
  121.                 $this->scale=(int)$values[1];
  122.         }
  123.     }
  124.  
  125.     /**
  126.      * Extracts the default value for the column.
  127.      * The value is typecasted to correct PHP type.
  128.      * @param mixed $defaultValue the default value obtained from metadata
  129.      */
  130.     protected function extractDefault($defaultValue)
  131.     {
  132.         $this->defaultValue=$this->typecast($defaultValue);
  133.     }
  134.  
  135.     /**
  136.      * Converts the input value to the type that this column is of.
  137.      * @param mixed $value input value
  138.      * @return mixed converted value
  139.      */
  140.     public function typecast($value)
  141.     {
  142.         if(gettype($value)===$this->type || $value===null || $value instanceof CDbExpression)
  143.             return $value;
  144.         if($value==='' && $this->allowNull)
  145.             return $this->type==='string' ? '' : null;
  146.         switch($this->type)
  147.         {
  148.             case 'string': return (string)$value;
  149.             case 'integer': return (integer)$value;
  150.             case 'boolean': return (boolean)$value;
  151.             case 'double':
  152.             default: return $value;
  153.         }
  154.     }
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement