CDbColumnSchema.php 3.9 KB

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