CMysqlColumnSchema.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. /**
  3. * CMysqlColumnSchema 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. * CMysqlColumnSchema class describes the column meta data of a MySQL table.
  12. *
  13. * @author Qiang Xue <qiang.xue@gmail.com>
  14. * @package system.db.schema.mysql
  15. * @since 1.0
  16. */
  17. class CMysqlColumnSchema extends CDbColumnSchema
  18. {
  19. /**
  20. * Extracts the PHP type from DB type.
  21. * @param string $dbType DB type
  22. */
  23. protected function extractType($dbType)
  24. {
  25. if(strncmp($dbType,'enum',4)===0)
  26. $this->type='string';
  27. elseif(strpos($dbType,'float')!==false || strpos($dbType,'double')!==false)
  28. $this->type='double';
  29. elseif(strpos($dbType,'bool')!==false)
  30. $this->type='boolean';
  31. elseif(strpos($dbType,'int')===0 && strpos($dbType,'unsigned')===false || preg_match('/(bit|tinyint|smallint|mediumint)/',$dbType))
  32. $this->type='integer';
  33. else
  34. $this->type='string';
  35. }
  36. /**
  37. * Extracts the default value for the column.
  38. * The value is typecasted to correct PHP type.
  39. * @param mixed $defaultValue the default value obtained from metadata
  40. */
  41. protected function extractDefault($defaultValue)
  42. {
  43. if(strncmp($this->dbType,'bit',3)===0)
  44. $this->defaultValue=bindec(trim($defaultValue,'b\''));
  45. elseif($this->dbType==='timestamp' && $defaultValue==='CURRENT_TIMESTAMP')
  46. $this->defaultValue=null;
  47. else
  48. parent::extractDefault($defaultValue);
  49. }
  50. /**
  51. * Extracts size, precision and scale information from column's DB type.
  52. * @param string $dbType the column's DB type
  53. */
  54. protected function extractLimit($dbType)
  55. {
  56. if (strncmp($dbType, 'enum', 4)===0 && preg_match('/\(([\'"])(.*)\\1\)/',$dbType,$matches))
  57. {
  58. // explode by (single or double) quote and comma (ENUM values may contain commas)
  59. $values = explode($matches[1].','.$matches[1], $matches[2]);
  60. $size = 0;
  61. foreach($values as $value)
  62. {
  63. if(($n=strlen($value)) > $size)
  64. $size=$n;
  65. }
  66. $this->size = $this->precision = $size;
  67. }
  68. else
  69. parent::extractLimit($dbType);
  70. }
  71. }